/** * In-browser execution of Emscripten's Node.js build tools * (`tools/compiler.mjs`, `tools/preprocessor.mjs`, * `tools/acorn-optimizer.mjs`, ...). * * Python's emcc driver invokes these tools via `/usr/bin/node args` * with the tool's stdin (e.g. the settings JSON) piped in. This module runs * the real tool sources inside the Worker: * * 1. Tool sources (and their `/usr/lib/emscripten` dependency tree) are * read from the kernel VFS. * 2. Imports are rewritten so the ESM graph is preserved via blob URLs: * relative/bare specifiers become VFS paths, then VFS paths and `node:*` * specifiers are mapped to blob URLs (builtins → shim modules). * 3. Node builtins (`node:fs`, `node:path`, `node:vm`, ...) are shimmed, * with fs backed by the VFS (write cache for sync reads). * 4. `process.stdout` / `console` writes are captured and forwarded to the * caller so the waiting Python process receives the generated JS glue + * `//FORWARDED_DATA:` payload. */ /** Minimal VFS surface needed by the loader (subset of VFSManager). */ export interface NodeToolVfs { fetchFile(path: string): Promise; readFileSync(path: string): Uint8Array | null; writeFileSync(path: string, data: Uint8Array): void; existsSync(path: string): boolean; readdirSync(path: string): string[]; statSync(path: string): { type: 'file' | 'dir'; size: number; } | null; deleteFileSync(path: string): boolean; mkdirSync(path: string): void; } export interface NodeToolRunOptions { /** Working directory of the emulated process (affects path resolution). */ cwd: string; /** argv, including argv[0] (the node binary path) and the script path. */ argv: string[]; /** Environment variables for the emulated process. */ env?: Record; /** Stdin content (settings JSON piped by Python's run_js_tool). */ stdin?: string; /** Stdout listener (called with each write chunk, not lines). */ onStdout?: (text: string) => void; /** Stderr listener. */ onStderr?: (text: string) => void; } export interface NodeToolResult { exitCode: number; stdout: string; stderr: string; } /** Normalize a VFS path: resolve `.`, `..`, collapse duplicate slashes. */ export declare function normalizeVfsPath(p: string): string; export declare const posixPath: { isAbsolute(p: string): boolean; join(...parts: string[]): string; resolve(...parts: string[]): string; dirname(p: string): string; basename(p: string): string; extname(p: string): string; relative(from: string, to: string): string; }; export declare function rewriteModuleImports(source: string, modulePath: string, rootDir: string): string; /** Node builtin modules the loader can shim. */ export declare const SHIMMED_NODE_BUILTINS: ReadonlySet; /** * Resolve an import specifier against the importing module. * Relative and bare (npm) specifiers become absolute VFS paths; `node:*` and * already-absolute specifiers pass through. */ export declare function resolveSpecifier(spec: string, modulePath: string, rootDir: string): string; /** Minimal util.parseArgs: boolean/string options + positionals. */ export declare function parseArgsShim(argv: string[], config: { options?: Record; allowPositionals?: boolean; }): { values: Record; positionals: string[]; }; /** * Collect the names of the script's own top-level `var` declarations. These * must land on the vm context object (node vm semantics) after evaluation; * function-local declarations are nested and therefore not harvestable from * the wrapper scope, which makes the name scan safe to over-match. */ export declare function collectTopLevelVarNames(code: string): string[]; /** Extract the import specifiers present in (already rewritten) source. */ export declare function extractSpecifiers(source: string): string[]; /** Replace all exact occurrences of a quoted import specifier. */ export declare function replaceImportSpecifier(source: string, from: string, to: string): string; /** * Patch `new URL('', import.meta.url)` expressions to file:// URLs * of the module's VFS directory. Blob URLs are opaque, so relative resolution * against import.meta.url would not work; the url shim understands file://. */ export declare function patchImportMetaUrls(source: string, modulePath: string): string; /** Detect a UMD/CommonJS script with no ESM syntax (e.g. terser.js, acorn UMD). */ export declare function isClassicScriptModule(source: string): boolean; /** Wrap a UMD/CommonJS script so it becomes an ES module with a default export. */ export declare function wrapUmdModule(source: string): string; /** Remove `import ... from '';` statements for spec (already absolute). */ export declare function stripImportOf(source: string, spec: string): string; /** * Build the blob module graph for a VFS entry: fetch source, rewrite * relative/bare imports to VFS paths, map `node:*` imports to shim blob URLs * and absolute VFS-path imports to dependency blob URLs (recursively), then * return the entry blob URL. Identical VFS paths map to identical blob URLs, * so shared state across modules is preserved. */ export declare function buildModuleGraph(vfsPath: string, rootDir: string, resolveShimUrl: (name: string) => string, cache: Map): Promise; /** * Run a Node.js tool module from the VFS in the current Worker. The entry * module executes with top-level await; imports resolve through blob URLs * with node builtins shimmed. */ export declare function runNodeTool(vfs: NodeToolVfs, options: NodeToolRunOptions): Promise; //# sourceMappingURL=node-tool.d.ts.map