All files / coherent.js/packages/runtime/src/utils runtime-detector.js

62.89% Statements 295/469
60.21% Branches 56/93
75.75% Functions 25/33
62.89% Lines 295/469

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 5161x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 23x 23x 23x   1x 1x 1x 1x 1x   1x 1x 20x       20x 20x 20x   1x 20x 20x 20x 20x   20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 18x   1x 20x   1x 1x 20x 20x 20x 20x   1x 15x 15x 15x 15x 15x   1x 20x 20x 20x   1x 20x 20x 20x 20x 20x   1x 20x 20x 20x 20x   1x 20x 20x 20x   1x 20x 5x 20x 20x 20x 20x   1x 20x 20x 20x 20x 20x 20x   1x 20x 20x 20x 20x 20x 20x   1x 20x 20x 20x 20x 20x   1x 20x 20x 20x 20x 20x   1x 1x 4x 4x 4x       4x 4x 4x     4x 4x 4x   1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   4x                                             4x                                             4x                                             4x                                                   4x                                                   4x 1x 4x 4x   1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   1x 1x 4x 4x   1x 4x 4x 2x 4x 4x   4x 4x     4x 4x 1x 4x 4x 1x 4x 4x   1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   1x 4x 3x 3x 1x 4x   1x 4x 4x 4x 4x 4x 4x 4x 4x 4x                                           4x   1x 1x                       1x                         1x 1x 16x 16x   1x       1x       1x     1x   1x 1x 16x 16x   1x       1x       1x       1x 1x
/**
 * Runtime Detector - Detects and analyzes JavaScript runtime environments
 * Provides detailed capability analysis for universal applications
 */
 
// Runtime environment constants
export const RuntimeEnvironment = {
  BROWSER: 'browser',
  NODE: 'node',
  DENO: 'deno',
  BUN: 'bun',
  EDGE: 'edge',
  CLOUDFLARE: 'cloudflare',
  ELECTRON: 'electron',
  TAURI: 'tauri',
  WEBVIEW: 'webview',
  STATIC: 'static',
  UNKNOWN: 'unknown'
};
 
export class RuntimeDetector {
  constructor() {
    this.detectionResult = null;
    this.capabilities = null;
  }
 
  // Reset detection cache (useful for testing)
  reset() {
    this.detectionResult = null;
    this.capabilities = null;
  }
 
  // Main detection method
  detect() {
    if (this.detectionResult) {
      return this.detectionResult;
    }
 
    this.detectionResult = this.performDetection();
    return this.detectionResult;
  }
 
  performDetection() {
    // Desktop app frameworks (check first as they may have browser-like globals)
    if (this.isTauri()) return RuntimeEnvironment.TAURI;
    if (this.isElectron()) return RuntimeEnvironment.ELECTRON;
    if (this.isWebView()) return RuntimeEnvironment.WEBVIEW;
 
    // JavaScript runtimes
    if (this.isDeno()) return RuntimeEnvironment.DENO;
    if (this.isBun()) return RuntimeEnvironment.BUN;
    
    // Edge environments
    if (this.isCloudflareWorkers()) return RuntimeEnvironment.CLOUDFLARE;
    if (this.isEdgeRuntime()) return RuntimeEnvironment.EDGE;
    
    // Browser and Node.js
    if (this.isBrowser()) return RuntimeEnvironment.BROWSER;
    if (this.isNodeJS()) return RuntimeEnvironment.NODE;
 
    return RuntimeEnvironment.UNKNOWN;
  }
 
  // Environment-specific detection methods
  isBrowser() {
    return typeof window !== 'undefined' && 
           typeof document !== 'undefined' &&
           typeof navigator !== 'undefined';
  }
 
  isNodeJS() {
    return typeof process !== 'undefined' && 
           process.versions && 
           process.versions.node &&
           typeof require !== 'undefined';
  }
 
  isDeno() {
    return typeof Deno !== 'undefined' && 
           typeof Deno.version !== 'undefined';
  }
 
  isBun() {
    return typeof Bun !== 'undefined' || 
           (typeof process !== 'undefined' && 
            process.versions && 
            process.versions.bun);
  }
 
  isElectron() {
    return typeof window !== 'undefined' && 
           typeof window.require !== 'undefined' &&
           typeof window.require('electron') !== 'undefined';
  }
 
  isTauri() {
    return typeof window !== 'undefined' && 
           typeof window.__TAURI__ !== 'undefined';
  }
 
  isWebView() {
    return typeof window !== 'undefined' && 
           (typeof window.webkit !== 'undefined' || 
            typeof window.external !== 'undefined') &&
           !this.isElectron() && 
           !this.isTauri();
  }
 
  isCloudflareWorkers() {
    return typeof caches !== 'undefined' && 
           typeof fetch !== 'undefined' &&
           typeof Response !== 'undefined' &&
           typeof addEventListener !== 'undefined' &&
           typeof window === 'undefined';
  }
 
  isEdgeRuntime() {
    return typeof EdgeRuntime !== 'undefined' ||
           (typeof globalThis !== 'undefined' && 
            typeof globalThis.EdgeRuntime !== 'undefined') ||
           this.isVercelEdge() ||
           this.isNetlifyEdge();
  }
 
  isVercelEdge() {
    return typeof process !== 'undefined' && 
           process.env && 
           process.env.VERCEL === '1' &&
           typeof EdgeRuntime !== 'undefined';
  }
 
  isNetlifyEdge() {
    return typeof Netlify !== 'undefined' || 
           (typeof process !== 'undefined' && 
            process.env && 
            process.env.NETLIFY === 'true');
  }
 
  // Detailed capability analysis
  getCapabilities(environment = null) {
    const env = environment || this.detect();
    
    if (this.capabilities && !environment) {
      return this.capabilities;
    }
 
    const capabilities = this.analyzeCapabilities(env);
    
    if (!environment) {
      this.capabilities = capabilities;
    }
    
    return capabilities;
  }
 
  analyzeCapabilities(environment) {
    const base = {
      environment,
      dom: false,
      ssr: false,
      filesystem: false,
      fetch: false,
      websockets: false,
      workers: false,
      storage: false,
      crypto: false,
      streams: false,
      modules: {
        esm: false,
        commonjs: false,
        dynamicImport: false
      },
      apis: {
        console: false,
        timers: false,
        events: false,
        url: false,
        base64: false
      }
    };
 
    switch (environment) {
      case RuntimeEnvironment.BROWSER:
        return {
          ...base,
          dom: true,
          fetch: typeof fetch !== 'undefined',
          websockets: typeof WebSocket !== 'undefined',
          workers: typeof Worker !== 'undefined',
          storage: typeof localStorage !== 'undefined',
          crypto: typeof crypto !== 'undefined',
          streams: typeof ReadableStream !== 'undefined',
          modules: {
            esm: true,
            commonjs: false,
            dynamicImport: (() => { try { return typeof Function('return import("")') === 'function'; } catch { return false; } })()
          },
          apis: {
            console: typeof console !== 'undefined',
            timers: typeof setTimeout !== 'undefined',
            events: typeof EventTarget !== 'undefined',
            url: typeof URL !== 'undefined',
            base64: typeof btoa !== 'undefined'
          }
        };
 
      case RuntimeEnvironment.NODE:
        return {
          ...base,
          ssr: true,
          filesystem: true,
          fetch: typeof fetch !== 'undefined',
          websockets: true,
          crypto: true,
          streams: true,
          modules: {
            esm: true,
            commonjs: true,
            dynamicImport: (() => { try { return typeof Function('return import("")') === 'function'; } catch { return false; } })()
          },
          apis: {
            console: true,
            timers: true,
            events: true,
            url: typeof URL !== 'undefined',
            base64: typeof Buffer !== 'undefined'
          }
        };
 
      case RuntimeEnvironment.DENO:
        return {
          ...base,
          ssr: true,
          filesystem: typeof Deno.readTextFile !== 'undefined',
          fetch: typeof fetch !== 'undefined',
          websockets: typeof WebSocket !== 'undefined',
          crypto: typeof crypto !== 'undefined',
          streams: typeof ReadableStream !== 'undefined',
          modules: {
            esm: true,
            commonjs: false,
            dynamicImport: true
          },
          apis: {
            console: true,
            timers: true,
            events: true,
            url: typeof URL !== 'undefined',
            base64: typeof btoa !== 'undefined'
          }
        };
 
      case RuntimeEnvironment.BUN:
        return {
          ...base,
          ssr: true,
          filesystem: true,
          fetch: typeof fetch !== 'undefined',
          websockets: typeof WebSocket !== 'undefined',
          crypto: typeof crypto !== 'undefined',
          streams: typeof ReadableStream !== 'undefined',
          modules: {
            esm: true,
            commonjs: true,
            dynamicImport: true
          },
          apis: {
            console: true,
            timers: true,
            events: true,
            url: typeof URL !== 'undefined',
            base64: typeof btoa !== 'undefined'
          }
        };
 
      case RuntimeEnvironment.CLOUDFLARE:
        return {
          ...base,
          ssr: true,
          fetch: typeof fetch !== 'undefined',
          websockets: typeof WebSocket !== 'undefined',
          storage: typeof caches !== 'undefined',
          crypto: typeof crypto !== 'undefined',
          streams: typeof ReadableStream !== 'undefined',
          modules: {
            esm: true,
            commonjs: false,
            dynamicImport: true
          },
          apis: {
            console: true,
            timers: true,
            events: true,
            url: typeof URL !== 'undefined',
            base64: typeof btoa !== 'undefined'
          }
        };
 
      case RuntimeEnvironment.ELECTRON:
        return {
          ...base,
          dom: true,
          ssr: true,
          filesystem: true,
          fetch: typeof fetch !== 'undefined',
          websockets: typeof WebSocket !== 'undefined',
          workers: typeof Worker !== 'undefined',
          storage: typeof localStorage !== 'undefined',
          crypto: typeof crypto !== 'undefined',
          streams: typeof ReadableStream !== 'undefined',
          modules: {
            esm: true,
            commonjs: true,
            dynamicImport: true
          },
          apis: {
            console: true,
            timers: true,
            events: true,
            url: typeof URL !== 'undefined',
            base64: typeof btoa !== 'undefined'
          }
        };
 
      case RuntimeEnvironment.TAURI:
        return {
          ...base,
          dom: true,
          ssr: true,
          filesystem: typeof window !== 'undefined' && 
                    typeof window.__TAURI__ !== 'undefined',
          fetch: typeof fetch !== 'undefined',
          websockets: typeof WebSocket !== 'undefined',
          storage: typeof localStorage !== 'undefined',
          crypto: typeof crypto !== 'undefined',
          streams: typeof ReadableStream !== 'undefined',
          modules: {
            esm: true,
            commonjs: false,
            dynamicImport: true
          },
          apis: {
            console: true,
            timers: true,
            events: true,
            url: typeof URL !== 'undefined',
            base64: typeof btoa !== 'undefined'
          }
        };
 
      default:
        return base;
    }
  }
 
  // Get detailed runtime information
  getRuntimeInfo() {
    const environment = this.detect();
    const capabilities = this.getCapabilities(environment);
    
    return {
      environment,
      capabilities,
      version: this.getRuntimeVersion(environment),
      features: this.getAvailableFeatures(),
      userAgent: this.getUserAgent(),
      platform: this.getPlatformInfo()
    };
  }
 
  // Alias method for backward compatibility
  getEnvironmentInfo() {
    return this.getRuntimeInfo();
  }
 
  getRuntimeVersion(environment) {
    switch (environment) {
      case RuntimeEnvironment.NODE:
        return typeof process !== 'undefined' ? process.version : null;
        
      case RuntimeEnvironment.DENO:
        return typeof Deno !== 'undefined' ? Deno.version.deno : null;
        
      case RuntimeEnvironment.BUN:
        return typeof Bun !== 'undefined' ? Bun.version : 
               (typeof process !== 'undefined' ? process.versions.bun : null);
        
      case RuntimeEnvironment.BROWSER:
        return typeof navigator !== 'undefined' ? navigator.userAgent : null;
        
      default:
        return null;
    }
  }
 
  getAvailableFeatures() {
    const features = [];
    
    if (typeof fetch !== 'undefined') features.push('fetch');
    if (typeof WebSocket !== 'undefined') features.push('websockets');
    if (typeof Worker !== 'undefined') features.push('workers');
    if (typeof crypto !== 'undefined') features.push('crypto');
    if (typeof localStorage !== 'undefined') features.push('localStorage');
    if (typeof sessionStorage !== 'undefined') features.push('sessionStorage');
    if (typeof IndexedDB !== 'undefined') features.push('indexedDB');
    if (typeof WebAssembly !== 'undefined') features.push('webassembly');
    if (typeof SharedArrayBuffer !== 'undefined') features.push('sharedArrayBuffer');
    if (typeof BigInt !== 'undefined') features.push('bigint');
    if ((() => { try { return typeof Function('return import("")') === 'function'; } catch { return false; } })()) features.push('dynamicImport');
    
    return features;
  }
 
  getUserAgent() {
    if (typeof navigator !== 'undefined' && navigator.userAgent) {
      return navigator.userAgent;
    }
    return null;
  }
 
  getPlatformInfo() {
    // Browser platform info
    if (typeof navigator !== 'undefined') {
      return {
        platform: navigator.platform,
        language: navigator.language,
        cookieEnabled: navigator.cookieEnabled,
        onLine: navigator.onLine
      };
    }
    
    // Node.js platform info
    if (typeof process !== 'undefined') {
      return {
        platform: process.platform,
        arch: process.arch,
        version: process.version,
        pid: process.pid
      };
    }
    
    // Deno platform info
    if (typeof Deno !== 'undefined') {
      return {
        os: Deno.build.os,
        arch: Deno.build.arch,
        version: Deno.version
      };
    }
    
    return null;
  }
 
  // Compatibility checking
  isCompatible(requirements) {
    const capabilities = this.getCapabilities();
    
    for (const [feature, required] of Object.entries(requirements)) {
      if (required && !capabilities[feature]) {
        return false;
      }
    }
    
    return true;
  }
 
  getIncompatibilities(requirements) {
    const capabilities = this.getCapabilities();
    const incompatibilities = [];
    
    for (const [feature, required] of Object.entries(requirements)) {
      if (required && !capabilities[feature]) {
        incompatibilities.push(feature);
      }
    }
    
    return incompatibilities;
  }
 
  // Static methods for quick access
  static detect() {
    return new RuntimeDetector().detect();
  }
 
  static getCapabilities(environment = null) {
    return new RuntimeDetector().getCapabilities(environment);
  }
 
  static getRuntimeInfo() {
    return new RuntimeDetector().getRuntimeInfo();
  }
 
  static isCompatible(requirements) {
    return new RuntimeDetector().isCompatible(requirements);
  }
}
 
// Convenience functions
export function detectRuntime() {
  return RuntimeDetector.detect();
}
 
export function getRuntimeCapabilities(environment = null) {
  return RuntimeDetector.getCapabilities(environment);
}
 
export function getRuntimeInfo() {
  return RuntimeDetector.getRuntimeInfo();
}
 
export function isRuntimeCompatible(requirements) {
  return RuntimeDetector.isCompatible(requirements);
}
 
// Default detector instance
export const runtimeDetector = new RuntimeDetector();