/** * @file esm-module-loader.ts * @description Core engine for dynamically loading npm packages. * * Supports two runtime environments: * - **Node.js**: fetch → cache to disk → import() from file:// URL * - **Browser**: fetch → in-memory cache → evaluate via Function constructor */ import type { FrontMcpLogger } from '../common'; import type { EsmRegistryAuth } from './esm-auth.types'; import type { EsmCacheManager } from './esm-cache'; import type { ParsedPackageSpecifier } from './package-specifier'; import type { FrontMcpPackageManifest } from './esm-manifest'; /** * Result of loading an ESM package. */ export interface EsmLoadResult { /** Normalized package manifest with primitives */ manifest: FrontMcpPackageManifest; /** Concrete version that was loaded */ resolvedVersion: string; /** Whether the bundle came from cache or network */ source: 'cache' | 'network'; /** Timestamp when the load completed */ loadedAt: number; /** The raw module export (for direct access to classes/functions) */ rawModule: unknown; } /** * Options for the ESM module loader. */ export interface EsmModuleLoaderOptions { /** Cache manager for storing ESM bundles */ cache: EsmCacheManager; /** Authentication for private registries */ registryAuth?: EsmRegistryAuth; /** Request timeout in milliseconds (default: 30000) */ timeout?: number; /** Logger instance */ logger?: FrontMcpLogger; /** Custom ESM CDN base URL */ esmBaseUrl?: string; } /** * Core ESM module loader. * * Loads npm packages dynamically at runtime: * 1. Resolves semver range to concrete version via npm registry * 2. Checks cache for the resolved version (in-memory + disk) * 3. On cache miss, fetches the bundle from esm.sh (or custom CDN) * 4. Caches the bundle (disk in Node.js, in-memory in browser) * 5. Evaluates the module (import() in Node.js, Function constructor in browser) * 6. Normalizes the module export into a FrontMcpPackageManifest */ export declare class EsmModuleLoader { private readonly cache; private readonly versionResolver; private readonly timeout; private readonly logger?; private readonly esmBaseUrl?; constructor(options: EsmModuleLoaderOptions); /** * Load an npm package and return its normalized manifest. * * @param specifier - Parsed package specifier * @returns Load result with manifest and metadata */ load(specifier: ParsedPackageSpecifier): Promise; /** * Resolve a package specifier's range to a concrete version. */ resolveVersion(specifier: ParsedPackageSpecifier): Promise; /** * Load a module from a cached bundle. */ private loadFromCache; /** * Fetch ESM bundle from esm.sh, cache it, and load it. */ private fetchAndCache; /** * Import a bundle from a local file path (Node.js only). * Uses dynamic import with file:// URL for cross-platform compatibility. */ private importFromPath; /** * Import a bundle from its source text. * Detects ESM vs CJS and uses the appropriate evaluation strategy. */ private importBundle; /** * Heuristic: content uses ESM export/import syntax at line boundaries. */ private looksLikeEsm; /** * Import ESM content via Blob URL (browser) or temp file (Node.js). */ private importEsmBundle; } //# sourceMappingURL=esm-module-loader.d.ts.map