All files / runtime/src runtime-factory.js

71.91% Statements 64/89
58.65% Branches 61/104
100% Functions 7/7
80% Lines 60/75

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        2x                                 22x   5x   1x       4x 1x     3x       17x   12x 12x         5x 1x       4x       1x       3x 1x       2x 1x       1x             7x   7x       2x 2x                         3x 3x       1x 1x       1x               7x   7x                       7x   1x                       1x                                             3x                       1x                                                               1x                             2x 2x   2x                     2x   2x   2x                             2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x       2x 2x                                                      
/**
 * Runtime Factory - Detects environment and creates appropriate runtime
 */
 
export const RuntimeEnvironment = {
  BROWSER: 'browser',
  NODE: 'node', 
  EDGE: 'edge',
  CLOUDFLARE: 'cloudflare',
  DENO: 'deno',
  BUN: 'bun',
  ELECTRON: 'electron',
  TAURI: 'tauri',
  STATIC: 'static'
};
 
/**
 * Detect the current runtime environment
 */
export function detectRuntime() {
  // Browser environments
  if (typeof window !== 'undefined' && typeof document !== 'undefined') {
    // Electron detection
    if (typeof window.require !== 'undefined' || 
        typeof window.process !== 'undefined' && window.process.versions?.electron) {
      return RuntimeEnvironment.ELECTRON;
    }
    
    // Tauri detection
    if (typeof window.__TAURI__ !== 'undefined') {
      return RuntimeEnvironment.TAURI;
    }
    
    return RuntimeEnvironment.BROWSER;
  }
  
  // Server-side environments
  if (typeof process !== 'undefined') {
    // Node.js detection
    Eif (process.versions?.node) {
      return RuntimeEnvironment.NODE;
    }
  }
  
  // Edge runtime detection
  if (typeof EdgeRuntime !== 'undefined') {
    return RuntimeEnvironment.EDGE;
  }
  
  // Cloudflare Workers
  if (typeof caches !== 'undefined' && 
      typeof Request !== 'undefined' && 
      typeof Response !== 'undefined' &&
      typeof addEventListener !== 'undefined') {
    return RuntimeEnvironment.CLOUDFLARE;
  }
  
  // Deno detection
  if (typeof Deno !== 'undefined') {
    return RuntimeEnvironment.DENO;
  }
  
  // Bun detection
  if (typeof Bun !== 'undefined') {
    return RuntimeEnvironment.BUN;
  }
  
  // Fallback to browser
  return RuntimeEnvironment.BROWSER;
}
 
/**
 * Create runtime instance based on environment
 */
export async function createRuntime(options = {}) {
  const environment = options.environment || detectRuntime();
  
  switch (environment) {
    case RuntimeEnvironment.BROWSER:
    case RuntimeEnvironment.ELECTRON:
    case RuntimeEnvironment.TAURI: {
      const { BrowserRuntime } = await import('./runtimes/browser.js');
      return new BrowserRuntime(options);
    }
    
    case RuntimeEnvironment.EDGE:
    case RuntimeEnvironment.CLOUDFLARE:
    case RuntimeEnvironment.DENO:
    case RuntimeEnvironment.BUN: {
      const { EdgeRuntime } = await import('./runtimes/edge.js');
      return new EdgeRuntime(options);
    }
    
    case RuntimeEnvironment.NODE: {
      // Use Node.js runtime for Node.js environments
      const { NodeRuntime } = await import('./runtimes/node.js');
      return new NodeRuntime(options);
    }
    
    case RuntimeEnvironment.STATIC: {
      const { StaticRuntime } = await import('./runtimes/static.js');
      return new StaticRuntime(options);
    }
    
    default:
      throw new Error(`Unsupported runtime environment: ${environment}`);
  }
}
 
/**
 * Runtime capabilities detection
 */
export function getRuntimeCapabilities(environment = null) {
  const env = environment || detectRuntime();
  
  const capabilities = {
    dom: false,
    ssr: false,
    filesystem: false,
    fetch: false,
    websockets: false,
    workers: false,
    storage: false,
    crypto: false,
    streams: false
  };
  
  switch (env) {
    case RuntimeEnvironment.BROWSER:
      return {
        ...capabilities,
        dom: true,
        fetch: true,
        websockets: true,
        workers: true,
        storage: true,
        crypto: true,
        streams: true
      };
      
    case RuntimeEnvironment.ELECTRON:
      return {
        ...capabilities,
        dom: true,
        filesystem: true,
        fetch: true,
        websockets: true,
        storage: true,
        crypto: true,
        streams: true
      };
      
    case RuntimeEnvironment.TAURI:
      return {
        ...capabilities,
        dom: true,
        filesystem: true,
        fetch: true,
        websockets: true,
        storage: true,
        crypto: true
      };
      
    case RuntimeEnvironment.NODE:
      return {
        ...capabilities,
        ssr: true,
        filesystem: true,
        fetch: true,
        websockets: true,
        crypto: true,
        streams: true
      };
      
    case RuntimeEnvironment.EDGE:
    case RuntimeEnvironment.CLOUDFLARE:
      return {
        ...capabilities,
        ssr: true,
        fetch: true,
        crypto: true,
        streams: true,
        storage: true
      };
      
    case RuntimeEnvironment.DENO:
      return {
        ...capabilities,
        ssr: true,
        filesystem: true,
        fetch: true,
        websockets: true,
        crypto: true,
        streams: true
      };
      
    case RuntimeEnvironment.BUN:
      return {
        ...capabilities,
        ssr: true,
        filesystem: true,
        fetch: true,
        websockets: true,
        crypto: true,
        streams: true
      };
      
    case RuntimeEnvironment.STATIC:
      return {
        ...capabilities,
        ssr: true,
        filesystem: false
      };
      
    default:
      return capabilities;
  }
}
 
/**
 * Runtime information
 */
export function getRuntimeInfo() {
  const environment = detectRuntime();
  const capabilities = getRuntimeCapabilities(environment);
  
  return {
    environment,
    capabilities,
    version: getEnvironmentVersion(),
    features: getAvailableFeatures(),
    userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : null,
    platform: getPlatform()
  };
}
 
function getEnvironmentVersion() {
  const env = detectRuntime();
  
  switch (env) {
    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 : null;
    case RuntimeEnvironment.BROWSER:
    case RuntimeEnvironment.ELECTRON:
    case RuntimeEnvironment.TAURI:
      return typeof navigator !== 'undefined' ? navigator.userAgent : null;
    default:
      return null;
  }
}
 
function getAvailableFeatures() {
  const features = [];
  
  // Check for various Web APIs and features
  Eif (typeof fetch !== 'undefined') features.push('fetch');
  Eif (typeof WebSocket !== 'undefined') features.push('websockets');
  Iif (typeof Worker !== 'undefined') features.push('workers');
  Iif (typeof localStorage !== 'undefined') features.push('localStorage');
  Iif (typeof sessionStorage !== 'undefined') features.push('sessionStorage');
  Iif (typeof indexedDB !== 'undefined') features.push('indexedDB');
  Eif (typeof crypto !== 'undefined') features.push('crypto');
  Eif (typeof ReadableStream !== 'undefined') features.push('streams');
  Iif (typeof customElements !== 'undefined') features.push('customElements');
  Iif (typeof ShadowRoot !== 'undefined') features.push('shadowDOM');
  Iif (typeof IntersectionObserver !== 'undefined') features.push('intersectionObserver');
  Iif (typeof MutationObserver !== 'undefined') features.push('mutationObserver');
  Iif (typeof requestAnimationFrame !== 'undefined') features.push('animationFrame');
  Iif (typeof requestIdleCallback !== 'undefined') features.push('idleCallback');
  
  return features;
}
 
function getPlatform() {
  Eif (typeof navigator !== 'undefined') {
    return {
      platform: navigator.platform,
      userAgent: navigator.userAgent,
      language: navigator.language,
      cookieEnabled: navigator.cookieEnabled,
      onLine: navigator.onLine
    };
  }
  
  if (typeof process !== 'undefined') {
    return {
      platform: process.platform,
      arch: process.arch,
      version: process.version,
      versions: process.versions
    };
  }
  
  if (typeof Deno !== 'undefined') {
    return {
      platform: Deno.build.os,
      arch: Deno.build.arch,
      version: Deno.version.deno
    };
  }
  
  return null;
}