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 | import fs from 'node:fs'; import { isBuiltin } from 'node:module'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import vm from 'node:vm'; import { CircularDependencyError, FileReadError } from './error'; import { createImportMapResolver } from './import-map-resolve'; import type { ImportMap } from './types'; async function importBuiltinModule(specifier: string, context: vm.Context) { const nodeModule = await import(specifier); const keys = Object.keys(nodeModule); const module = new vm.SyntheticModule( keys, function evaluateCallback() { keys.forEach((key) => { this.setExport(key, nodeModule[key]); }); }, { identifier: specifier, context: context } ); await module.link(() => { throw new TypeError(`Native modules should not be linked`); }); await module.evaluate(); return module; } export function createVmImport(baseURL: URL, importMap: ImportMap = {}) { const importMapResolver = createImportMapResolver(baseURL.href, importMap); const buildMeta = (specifier: string, parent: string): ImportMeta => { const result = importMapResolver(specifier, parent); const url: string = result ?? import.meta.resolve(specifier, parent); const filename = fileURLToPath(url); return { filename, dirname: path.dirname(filename), url, resolve: (specifier: string, parent: string | URL = url) => { return import.meta.resolve(specifier, parent); } }; }; async function moduleLinker( specifier: string, parent: string, context: vm.Context, cache: Map<string, Promise<vm.SourceTextModule>>, moduleIds: string[] ) { if (isBuiltin(specifier)) { return importBuiltinModule(specifier, context); } const meta = buildMeta(specifier, parent); if (moduleIds.includes(meta.url)) { throw new CircularDependencyError( 'Circular dependency detected', moduleIds, meta.filename ); } const module = cache.get(meta.url); if (module) { return module; } const modulePromise = new Promise<vm.SourceTextModule>((resolve) => { process.nextTick(() => { moduleBuild().then(resolve); }); }); cache.set(meta.url, modulePromise); return modulePromise; async function moduleBuild(): Promise<vm.SourceTextModule> { let text: string; try { text = fs.readFileSync(meta.filename, 'utf-8'); } catch (error) { throw new FileReadError( `Failed to read module: ${meta.filename}`, moduleIds, meta.filename, error as Error ); } const module = new vm.SourceTextModule(text, { initializeImportMeta: (importMeta) => { Object.assign(importMeta, meta); }, identifier: specifier, context: context, importModuleDynamically: (specifier, referrer) => { return moduleLinker( specifier, meta.url, referrer.context, cache, [...moduleIds, meta.filename] ); } }); await module.link((specifier: string, referrer) => { return moduleLinker( specifier, meta.url, referrer.context, cache, [...moduleIds, meta.filename] ); }); await module.evaluate(); return module; } } return async ( specifier: string, parent: string, sandbox?: vm.Context, options?: vm.CreateContextOptions ) => { const context = vm.createContext(sandbox, options); const module = await moduleLinker( specifier, parent, context, new Map(), [] ); return module.namespace; }; } |