All files / coherent.js/packages/runtime/src/loaders universal-loader.js

22.74% Statements 53/233
33.33% Branches 2/6
9.09% Functions 2/22
22.74% Lines 53/233

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 2631x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x     1x   1x 1x                                                             1x                                           1x                               1x                         1x                       1x                       1x         1x         1x                                     1x                                       1x               1x           1x         1x 1x               1x 1x                         1x 1x       1x             1x 1x       1x                 1x   1x 1x       1x 1x  
/**
 * Universal Module Loader - Loads modules across different JavaScript environments
 * Provides dynamic module loading for browser, Node.js, Deno, Bun, and edge environments
 */
 
export class UniversalLoader {
  constructor(options = {}) {
    this.options = {
      baseUrl: '',
      moduleMap: {},
      fallbackUrls: [],
      cache: true,
      ...options
    };
    
    this.moduleCache = new Map();
    this.loadingPromises = new Map();
    this.environment = this.detectEnvironment();
  }
 
  detectEnvironment() {
    if (typeof Deno !== 'undefined') return 'deno';
    if (typeof Bun !== 'undefined') return 'bun'; 
    if (typeof window !== 'undefined') return 'browser';
    if (typeof global !== 'undefined' && typeof require !== 'undefined') return 'node';
    if (typeof WorkerGlobalScope !== 'undefined') return 'worker';
    return 'unknown';
  }
 
  // Universal module loading
  async load(modulePath, options = {}) {
    const resolvedPath = this.resolvePath(modulePath, options);
    const cacheKey = resolvedPath + JSON.stringify(options);
    
    // Return cached module if available
    if (this.options.cache && this.moduleCache.has(cacheKey)) {
      return this.moduleCache.get(cacheKey);
    }
 
    // Return existing loading promise to avoid duplicate loads
    if (this.loadingPromises.has(cacheKey)) {
      return this.loadingPromises.get(cacheKey);
    }
 
    // Create loading promise
    const loadingPromise = this.loadModule(resolvedPath, options);
    this.loadingPromises.set(cacheKey, loadingPromise);
 
    try {
      const module = await loadingPromise;
      
      if (this.options.cache) {
        this.moduleCache.set(cacheKey, module);
      }
      
      return module;
    } finally {
      this.loadingPromises.delete(cacheKey);
    }
  }
 
  async loadModule(modulePath, options = {}) {
    switch (this.environment) {
      case 'browser':
        return await this.loadBrowserModule(modulePath, options);
      
      case 'node':
        return await this.loadNodeModule(modulePath, options);
        
      case 'deno':
        return await this.loadDenoModule(modulePath, options);
        
      case 'bun':
        return await this.loadBunModule(modulePath, options);
        
      case 'worker':
        return await this.loadWorkerModule(modulePath, options);
        
      default:
        return await this.loadGenericModule(modulePath, options);
    }
  }
 
  async loadBrowserModule(modulePath, options = {}) {
    if (modulePath.startsWith('http') || modulePath.startsWith('/')) {
      // ES module import
      try {
        return await import(modulePath);
      } catch {
        // Fallback to script tag loading
        return await this.loadScript(modulePath, options);
      }
    } else {
      // Try CDN loading for npm packages
      const cdnUrl = this.toCdnUrl(modulePath);
      return await import(cdnUrl);
    }
  }
 
  async loadNodeModule(modulePath) {
    if (typeof require !== 'undefined') {
      try {
        return require(modulePath);
      } catch {
        // Try dynamic import for ES modules
        return await import(modulePath);
      }
    } else {
      return await import(modulePath);
    }
  }
 
  async loadDenoModule(modulePath) {
    // Deno supports direct URLs and npm: specifier
    if (modulePath.startsWith('npm:')) {
      return await import(modulePath);
    } else if (modulePath.startsWith('http')) {
      return await import(modulePath);
    } else {
      // Convert to npm: specifier
      return await import(`npm:${modulePath}`);
    }
  }
 
  async loadBunModule(modulePath) {
    // Bun supports both require and import
    try {
      return await import(modulePath);
    } catch (error) {
      if (typeof require !== 'undefined') {
        return require(modulePath);
      }
      throw error;
    }
  }
 
  async loadWorkerModule(modulePath) {
    // Worker environments typically support import
    return await import(modulePath);
  }
 
  async loadGenericModule(modulePath) {
    // Generic fallback using dynamic import
    return await import(modulePath);
  }
 
  async loadScript(src, options = {}) {
    return new Promise((resolve, reject) => {
      const script = document.createElement('script');
      script.type = 'module';
      script.src = src;
      
      script.onload = () => {
        // For script tags, we need to get the module from window or a registry
        const moduleName = options.globalName || this.getModuleNameFromPath(src);
        const module = window[moduleName] || {};
        resolve(module);
      };
      
      script.onerror = () => reject(new Error(`Failed to load script: ${src}`));
      
      document.head.appendChild(script);
    });
  }
 
  resolvePath(modulePath, options = {}) {
    // Handle absolute URLs
    if (modulePath.startsWith('http://') || modulePath.startsWith('https://')) {
      return modulePath;
    }
 
    // Handle relative paths
    if (modulePath.startsWith('./') || modulePath.startsWith('../')) {
      return this.resolveRelativePath(modulePath, options.baseUrl || this.options.baseUrl);
    }
 
    // Check module map
    if (this.options.moduleMap[modulePath]) {
      return this.options.moduleMap[modulePath];
    }
 
    // Default resolution
    return this.options.baseUrl + modulePath;
  }
 
  resolveRelativePath(relativePath, baseUrl) {
    if (!baseUrl) return relativePath;
    
    const base = new URL(baseUrl, location?.href || 'file:///');
    const resolved = new URL(relativePath, base);
    return resolved.href;
  }
 
  toCdnUrl(packageName, version = 'latest') {
    // Convert npm package to CDN URL
    const _cleanName = packageName.replace(/^@/, '').replace('/', '-');
    return `https://unpkg.com/${packageName}@${version}/dist/index.js`;
  }
 
  getModuleNameFromPath(path) {
    const filename = path.split('/').pop().split('.')[0];
    return filename.replace(/[-_]/g, '');
  }
 
  // Batch loading
  async loadAll(modulePaths, options = {}) {
    const loadPromises = modulePaths.map(path => 
      this.load(path, options).catch(error => ({ error, path }))
    );
    
    return await Promise.all(loadPromises);
  }
 
  // Preloading
  async preload(modulePaths, options = {}) {
    const preloadPromises = modulePaths.map(async path => {
      try {
        await this.load(path, { ...options, preload: true });
        return { success: true, path };
      } catch (error) {
        return { success: false, path, error };
      }
    });
    
    return await Promise.all(preloadPromises);
  }
 
  // Cache management
  clearCache() {
    this.moduleCache.clear();
  }
 
  getCacheStats() {
    return {
      size: this.moduleCache.size,
      modules: Array.from(this.moduleCache.keys())
    };
  }
 
  // Environment detection helpers
  static isSupported() {
    return (() => { try { return typeof Function('return import("")') === 'function'; } catch { return false; } })() || typeof require !== 'undefined';
  }
 
  static getCapabilities() {
    return {
      dynamicImport: (() => { try { return typeof Function('return import("")') === 'function'; } catch { return false; } })(),
      require: typeof require !== 'undefined',
      fetch: typeof fetch !== 'undefined',
      worker: typeof Worker !== 'undefined',
      environment: new UniversalLoader().environment
    };
  }
}
 
// Factory function
export function createLoader(options = {}) {
  return new UniversalLoader(options);
}
 
// Default instance
export const universalLoader = new UniversalLoader();