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 | 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 147x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 44x 44x 44x 44x 44x 44x 44x 44x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 90x 44x 57x 57x 57x 57x 57x 57x 57x 57x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 1x 17x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 56x 56x 57x 5x 5x 51x 51x 51x 57x 2x 2x 2x 2x 2x 2x 2x 49x 49x 2x 2x 49x 49x 49x 3x 3x 3x 3x 3x 3x 3x 3x 3x 49x 49x 49x 49x 24x 24x 24x 24x 24x 24x 24x 24x 49x 49x 49x 49x 57x 44x 37x 37x 37x 37x 37x 37x 37x 37x 3x 3x 34x 34x 37x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 37x 30x 29x 29x 37x 44x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 30x 34x 44x | import fs from 'node:fs';
import { createRequire, isBuiltin } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import vm from 'node:vm';
import { FileReadError } from './error';
import { createImportMapResolver } from './import-map-resolve';
import type { ImportMap } from './types';
const requireSync = createRequire(import.meta.url);
export interface VmImportOptions {
readFileSync?: (path: string) => string;
}
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 = {},
options?: VmImportOptions
) {
const readFileSync =
options?.readFileSync ?? ((path) => fs.readFileSync(path, 'utf-8'));
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 {
main: false,
filename,
dirname: path.dirname(filename),
url,
resolve: (specifier: string, parent: string | URL = url) => {
return import.meta.resolve(specifier, parent);
}
};
};
function syncLinker(
specifier: string,
parent: string,
context: vm.Context,
cache: Map<string, vm.SourceTextModule>,
linkStatus: Map<string, Promise<void>>,
moduleIds: string[]
): vm.SourceTextModule | vm.SyntheticModule {
if (isBuiltin(specifier)) {
const nodeModule = requireSync(specifier);
const keys = Object.keys(nodeModule);
const hasDefault = keys.includes('default');
if (!hasDefault) {
keys.push('default');
}
const module = new vm.SyntheticModule(
keys,
function evaluateCallback() {
keys.forEach((key) => {
this.setExport(
key,
key === 'default' && !hasDefault
? nodeModule
: nodeModule[key]
);
});
},
{
identifier: specifier,
context: context
}
);
const linkResult = module.link(() => {
throw new TypeError(`Native modules should not be linked`);
});
const linkPromise =
linkResult && typeof linkResult.then === 'function'
? linkResult.then(() => module.evaluate())
: module.evaluate();
linkStatus.set(specifier, linkPromise);
return module;
}
const meta = buildMeta(specifier, parent);
const cachedModule = cache.get(meta.url);
if (cachedModule) {
return cachedModule;
}
let text: string;
try {
text = readFileSync(meta.filename);
} 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,
linkStatus,
[...moduleIds, meta.filename]
);
}
});
cache.set(meta.url, module);
const linkPromise = module
.link((specifier: string, referrer) => {
return syncLinker(
specifier,
meta.url,
referrer.context,
cache,
linkStatus,
[...moduleIds, meta.filename]
);
})
.then(() => module.evaluate());
linkStatus.set(meta.url, linkPromise);
return module;
}
async function moduleLinker(
specifier: string,
parent: string,
context: vm.Context,
cache: Map<string, vm.SourceTextModule>,
linkStatus: Map<string, Promise<void>>,
moduleIds: string[]
) {
if (isBuiltin(specifier)) {
return importBuiltinModule(specifier, context);
}
const meta = buildMeta(specifier, parent);
const cachedModule = cache.get(meta.url);
if (cachedModule) {
const status = linkStatus.get(meta.url);
if (status) {
await status;
}
return cachedModule;
}
const module = syncLinker(
specifier,
parent,
context,
cache,
linkStatus,
moduleIds
);
const status = linkStatus.get(meta.url);
if (status) {
await status;
}
return module;
}
return async (
specifier: string,
parent: string,
sandbox?: vm.Context,
options?: vm.CreateContextOptions
) => {
const context = vm.createContext(sandbox, options);
const cache = new Map<string, vm.SourceTextModule>();
const linkStatus = new Map<string, Promise<void>>();
const module = await moduleLinker(
specifier,
parent,
context,
cache,
linkStatus,
[]
);
return module.namespace as Record<string, any>;
};
}
|