/
external-executor.ts
670 lines (578 loc) · 21.1 KB
/
external-executor.ts
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/* eslint-disable antfu/no-cjs-exports */
import vm from 'node:vm'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { dirname } from 'node:path'
import { Module as _Module, createRequire } from 'node:module'
import { readFileSync, statSync } from 'node:fs'
import { basename, extname, join, normalize } from 'pathe'
import { getCachedData, isNodeBuiltin, isPrimitive, setCacheData } from 'vite-node/utils'
import { CSS_LANGS_RE, KNOWN_ASSET_RE } from 'vite-node/constants'
import { getColors } from '@vitest/utils'
import type { ExecuteOptions } from './execute'
// need to copy paste types for vm
// because they require latest @types/node which we don't bundle
interface ModuleEvaluateOptions {
timeout?: vm.RunningScriptOptions['timeout'] | undefined
breakOnSigint?: vm.RunningScriptOptions['breakOnSigint'] | undefined
}
type ModuleLinker = (specifier: string, referencingModule: VMModule, extra: { assert: Object }) => VMModule | Promise<VMModule>
type ModuleStatus = 'unlinked' | 'linking' | 'linked' | 'evaluating' | 'evaluated' | 'errored'
declare class VMModule {
dependencySpecifiers: readonly string[]
error: any
identifier: string
context: vm.Context
namespace: Object
status: ModuleStatus
evaluate(options?: ModuleEvaluateOptions): Promise<void>
link(linker: ModuleLinker): Promise<void>
}
interface SyntheticModuleOptions {
/**
* String used in stack traces.
* @default 'vm:module(i)' where i is a context-specific ascending index.
*/
identifier?: string | undefined
/**
* The contextified object as returned by the `vm.createContext()` method, to compile and evaluate this module in.
*/
context?: vm.Context | undefined
}
declare class VMSyntheticModule extends VMModule {
/**
* Creates a new `SyntheticModule` instance.
* @param exportNames Array of names that will be exported from the module.
* @param evaluateCallback Called when the module is evaluated.
*/
constructor(exportNames: string[], evaluateCallback: (this: VMSyntheticModule) => void, options?: SyntheticModuleOptions)
/**
* This method is used after the module is linked to set the values of exports.
* If it is called before the module is linked, an `ERR_VM_MODULE_STATUS` error will be thrown.
* @param name
* @param value
*/
setExport(name: string, value: any): void
}
declare interface ImportModuleDynamically {
(specifier: string, script: VMModule, importAssertions: Object): VMModule | Promise<VMModule>
}
interface SourceTextModuleOptions {
identifier?: string | undefined
cachedData?: vm.ScriptOptions['cachedData'] | undefined
context?: vm.Context | undefined
lineOffset?: vm.BaseOptions['lineOffset'] | undefined
columnOffset?: vm.BaseOptions['columnOffset'] | undefined
/**
* Called during evaluation of this module to initialize the `import.meta`.
*/
initializeImportMeta?: ((meta: ImportMeta, module: VMSourceTextModule) => void) | undefined
importModuleDynamically?: ImportModuleDynamically
}
declare class VMSourceTextModule extends VMModule {
/**
* Creates a new `SourceTextModule` instance.
* @param code JavaScript Module code to parse
*/
constructor(code: string, options?: SourceTextModuleOptions)
}
const SyntheticModule: typeof VMSyntheticModule = (vm as any).SyntheticModule
const SourceTextModule: typeof VMSourceTextModule = (vm as any).SourceTextModule
interface PrivateNodeModule extends NodeModule {
_compile(code: string, filename: string): void
}
const _require = createRequire(import.meta.url)
const nativeResolve = import.meta.resolve!
const dataURIRegex
= /^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/
export interface ExternalModulesExecutorOptions extends ExecuteOptions {
context: vm.Context
packageCache: Map<string, any>
}
// TODO: improve Node.js strict mode support in #2854
export class ExternalModulesExecutor {
private requireCache: Record<string, NodeModule> = Object.create(null)
private builtinCache: Record<string, NodeModule> = Object.create(null)
private moduleCache = new Map<string, VMModule | Promise<VMModule>>()
private extensions: Record<string, (m: NodeModule, filename: string) => unknown> = Object.create(null)
private esmLinkMap = new WeakMap<VMModule, Promise<void>>()
private context: vm.Context
private fsCache = new Map<string, string>()
private fsBufferCache = new Map<string, Buffer>()
private Module: typeof _Module
private primitives: {
Object: typeof Object
Array: typeof Array
Error: typeof Error
}
constructor(private options: ExternalModulesExecutorOptions) {
this.context = options.context
const primitives = vm.runInContext('({ Object, Array, Error })', this.context) as {
Object: typeof Object
Array: typeof Array
Error: typeof Error
}
this.primitives = primitives
// eslint-disable-next-line @typescript-eslint/no-this-alias
const executor = this
// primitive implementation, some fields are not filled yet, like "paths" - #2854
this.Module = class Module {
exports: any
isPreloading = false
require: NodeRequire
id: string
filename: string
loaded: boolean
parent: null | Module | undefined
children: Module[] = []
path: string
paths: string[] = []
constructor(id: string, parent?: Module) {
this.exports = primitives.Object.create(Object.prototype)
this.require = Module.createRequire(id)
// in our case the path should always be resolved already
this.path = dirname(id)
this.id = id
this.filename = id
this.loaded = false
this.parent = parent
}
_compile(code: string, filename: string) {
const cjsModule = Module.wrap(code)
const script = new vm.Script(cjsModule, {
filename,
importModuleDynamically: executor.importModuleDynamically,
} as any)
// @ts-expect-error mark script with current identifier
script.identifier = filename
const fn = script.runInContext(executor.context)
const __dirname = dirname(filename)
executor.requireCache[filename] = this
try {
fn(this.exports, this.require, this, filename, __dirname)
return this.exports
}
finally {
this.loaded = true
}
}
// exposed for external use, Node.js does the opposite
static _load = (request: string, parent: Module | undefined, _isMain: boolean) => {
const require = Module.createRequire(parent?.filename ?? request)
return require(request)
}
static wrap = (script: string) => {
return Module.wrapper[0] + script + Module.wrapper[1]
}
static wrapper = new primitives.Array(
'(function (exports, require, module, __filename, __dirname) { ',
'\n});',
)
static builtinModules = _Module.builtinModules
static findSourceMap = _Module.findSourceMap
static SourceMap = _Module.SourceMap
static syncBuiltinESMExports = _Module.syncBuiltinESMExports
static _cache = executor.moduleCache
static _extensions = executor.extensions
static createRequire = (filename: string) => {
return executor.createRequire(filename)
}
static runMain = () => {
throw new primitives.Error('[vitest] "runMain" is not implemented.')
}
// @ts-expect-error not typed
static _resolveFilename = _Module._resolveFilename
// @ts-expect-error not typed
static _findPath = _Module._findPath
// @ts-expect-error not typed
static _initPaths = _Module._initPaths
// @ts-expect-error not typed
static _preloadModules = _Module._preloadModules
// @ts-expect-error not typed
static _resolveLookupPaths = _Module._resolveLookupPaths
// @ts-expect-error not typed
static globalPaths = _Module.globalPaths
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore not typed in lower versions
static isBuiltin = _Module.isBuiltin
static Module = Module
}
this.extensions['.js'] = this.requireJs
this.extensions['.json'] = this.requireJson
}
private requireJs = (m: NodeModule, filename: string) => {
const content = this.readFile(filename)
;(m as PrivateNodeModule)._compile(content, filename)
}
private requireJson = (m: NodeModule, filename: string) => {
const code = this.readFile(filename)
m.exports = JSON.parse(code)
}
public importModuleDynamically = async (specifier: string, referencer: VMModule) => {
const module = await this.resolveModule(specifier, referencer.identifier)
return this.evaluateModule(module)
}
private resolveModule = async (specifier: string, referencer: string) => {
const identifier = await this.resolveAsync(specifier, referencer)
return await this.createModule(identifier)
}
private async resolveAsync(specifier: string, parent: string) {
return nativeResolve(specifier, parent)
}
private readFile(path: string) {
const cached = this.fsCache.get(path)
if (cached)
return cached
const source = readFileSync(path, 'utf-8')
this.fsCache.set(path, source)
return source
}
private readBuffer(path: string) {
const cached = this.fsBufferCache.get(path)
if (cached)
return cached
const buffer = readFileSync(path)
this.fsBufferCache.set(path, buffer)
return buffer
}
private findNearestPackageData(basedir: string): { type?: 'module' | 'commonjs' } {
const originalBasedir = basedir
const packageCache = this.options.packageCache
while (basedir) {
const cached = getCachedData(packageCache, basedir, originalBasedir)
if (cached)
return cached
const pkgPath = join(basedir, 'package.json')
try {
if (statSync(pkgPath, { throwIfNoEntry: false })?.isFile()) {
const pkgData = JSON.parse(this.readFile(pkgPath))
if (packageCache)
setCacheData(packageCache, pkgData, basedir, originalBasedir)
return pkgData
}
}
catch {}
const nextBasedir = dirname(basedir)
if (nextBasedir === basedir)
break
basedir = nextBasedir
}
return {}
}
private wrapCoreSynteticModule(identifier: string, exports: Record<string, unknown>) {
const moduleKeys = Object.keys(exports)
const m: any = new SyntheticModule(
[...moduleKeys, 'default'],
() => {
for (const key of moduleKeys)
m.setExport(key, exports[key])
m.setExport('default', exports)
},
{
context: this.context,
identifier,
},
)
return m
}
private interopCommonJsModule(mod: any) {
if (isPrimitive(mod) || Array.isArray(mod) || mod instanceof Promise) {
return {
keys: [],
moduleExports: {},
defaultExport: mod,
}
}
if (this.options.interopDefault !== false && '__esModule' in mod && !isPrimitive(mod.default)) {
return {
keys: Array.from(new Set(Object.keys(mod.default).concat(Object.keys(mod)).filter(key => key !== 'default'))),
moduleExports: new Proxy(mod, {
get(mod, prop) {
return mod[prop] ?? mod.default?.[prop]
},
}),
defaultExport: mod,
}
}
return {
keys: Object.keys(mod).filter(key => key !== 'default'),
moduleExports: mod,
defaultExport: mod,
}
}
private wrapCommonJsSynteticModule(identifier: string, exports: Record<string, unknown>) {
// TODO: technically module should be parsed to find static exports, implement for strict mode in #2854
const { keys, moduleExports, defaultExport } = this.interopCommonJsModule(exports)
const m: any = new SyntheticModule(
[...keys, 'default'],
() => {
for (const key of keys)
m.setExport(key, moduleExports[key])
m.setExport('default', defaultExport)
},
{
context: this.context,
identifier,
},
)
return m
}
private async evaluateModule<T extends VMModule>(m: T): Promise<T> {
if (m.status === 'unlinked') {
this.esmLinkMap.set(
m,
m.link((identifier, referencer) =>
this.resolveModule(identifier, referencer.identifier),
),
)
}
await this.esmLinkMap.get(m)
if (m.status === 'linked')
await m.evaluate()
return m
}
private findLongestRegisteredExtension(filename: string) {
const name = basename(filename)
let currentExtension: string
let index: number
let startIndex = 0
// eslint-disable-next-line no-cond-assign
while ((index = name.indexOf('.', startIndex)) !== -1) {
startIndex = index + 1
if (index === 0)
continue // Skip dotfiles like .gitignore
currentExtension = (name.slice(index))
if (this.extensions[currentExtension])
return currentExtension
}
return '.js'
}
public createRequire = (filename: string) => {
const _require = createRequire(filename)
const require = ((id: string) => {
const resolved = _require.resolve(id)
const ext = extname(resolved)
if (ext === '.node' || isNodeBuiltin(resolved))
return this.requireCoreModule(resolved)
const module = this.createCommonJSNodeModule(resolved)
return this.loadCommonJSModule(module, resolved)
}) as NodeRequire
require.resolve = _require.resolve
Object.defineProperty(require, 'extensions', {
get: () => this.extensions,
set: () => {},
configurable: true,
})
require.main = _require.main
require.cache = this.requireCache
return require
}
private createCommonJSNodeModule(filename: string) {
return new this.Module(filename)
}
// very naive implementation for Node.js require
private loadCommonJSModule(module: NodeModule, filename: string): Record<string, unknown> {
const cached = this.requireCache[filename]
if (cached)
return cached.exports
const extension = this.findLongestRegisteredExtension(filename)
const loader = this.extensions[extension] || this.extensions['.js']
loader(module, filename)
return module.exports
}
private async createEsmModule(fileUrl: string, code: string) {
const cached = this.moduleCache.get(fileUrl)
if (cached)
return cached
const [urlPath] = fileUrl.split('?')
if (CSS_LANGS_RE.test(urlPath) || KNOWN_ASSET_RE.test(urlPath)) {
const path = normalize(urlPath)
let name = path.split('/node_modules/').pop() || ''
if (name?.startsWith('@'))
name = name.split('/').slice(0, 2).join('/')
else
name = name.split('/')[0]
const ext = extname(path)
let error = `[vitest] Cannot import ${fileUrl}. At the moment, importing ${ext} files inside external dependencies is not allowed. `
if (name) {
const c = getColors()
error += 'As a temporary workaround you can try to inline the package by updating your config:'
+ `\n\n${
c.gray(c.dim('// vitest.config.js'))
}\n${
c.green(`export default {
test: {
deps: {
optimizer: {
web: {
include: [
${c.yellow(c.bold(`"${name}"`))}
]
}
}
}
}
}\n`)}`
}
throw new this.primitives.Error(error)
}
// TODO: should not be allowed in strict mode, implement in #2854
if (fileUrl.endsWith('.json')) {
const m = new SyntheticModule(
['default'],
() => {
const result = JSON.parse(code)
m.setExport('default', result)
},
)
this.moduleCache.set(fileUrl, m)
return m
}
const m = new SourceTextModule(
code,
{
identifier: fileUrl,
context: this.context,
importModuleDynamically: this.importModuleDynamically,
initializeImportMeta: (meta, mod) => {
meta.url = mod.identifier
meta.resolve = (specifier: string, importer?: string) => {
return nativeResolve(specifier, importer ?? mod.identifier)
}
},
},
)
this.moduleCache.set(fileUrl, m)
return m
}
private requireCoreModule(identifier: string) {
const normalized = identifier.replace(/^node:/, '')
if (this.builtinCache[normalized])
return this.builtinCache[normalized].exports
const moduleExports = _require(identifier)
if (identifier === 'node:module' || identifier === 'module') {
const module = new this.Module('/module.js') // path should not matter
module.exports = this.Module
this.builtinCache[normalized] = module
return module.exports
}
this.builtinCache[normalized] = _require.cache[normalized]!
return moduleExports
}
private async loadWebAssemblyModule(source: Buffer, identifier: string) {
const cached = this.moduleCache.get(identifier)
if (cached)
return cached
const wasmModule = await WebAssembly.compile(source)
const exports = WebAssembly.Module.exports(wasmModule)
const imports = WebAssembly.Module.imports(wasmModule)
const moduleLookup: Record<string, VMModule> = {}
for (const { module } of imports) {
if (moduleLookup[module] === undefined) {
const resolvedModule = await this.resolveModule(
module,
identifier,
)
moduleLookup[module] = await this.evaluateModule(resolvedModule)
}
}
const syntheticModule = new SyntheticModule(
exports.map(({ name }) => name),
() => {
const importsObject: WebAssembly.Imports = {}
for (const { module, name } of imports) {
if (!importsObject[module])
importsObject[module] = {}
importsObject[module][name] = (moduleLookup[module].namespace as any)[name]
}
const wasmInstance = new WebAssembly.Instance(
wasmModule,
importsObject,
)
for (const { name } of exports)
syntheticModule.setExport(name, wasmInstance.exports[name])
},
{ context: this.context, identifier },
)
return syntheticModule
}
private async createDataModule(identifier: string): Promise<VMModule> {
const cached = this.moduleCache.get(identifier)
if (cached)
return cached
const Error = this.primitives.Error
const match = identifier.match(dataURIRegex)
if (!match || !match.groups)
throw new Error('Invalid data URI')
const mime = match.groups.mime
const encoding = match.groups.encoding
if (mime === 'application/wasm') {
if (!encoding)
throw new Error('Missing data URI encoding')
if (encoding !== 'base64')
throw new Error(`Invalid data URI encoding: ${encoding}`)
const module = await this.loadWebAssemblyModule(
Buffer.from(match.groups.code, 'base64'),
identifier,
)
this.moduleCache.set(identifier, module)
return module
}
let code = match.groups.code
if (!encoding || encoding === 'charset=utf-8')
code = decodeURIComponent(code)
else if (encoding === 'base64')
code = Buffer.from(code, 'base64').toString()
else
throw new Error(`Invalid data URI encoding: ${encoding}`)
if (mime === 'application/json') {
const module = new SyntheticModule(
['default'],
() => {
const obj = JSON.parse(code)
module.setExport('default', obj)
},
{ context: this.context, identifier },
)
this.moduleCache.set(identifier, module)
return module
}
return this.createEsmModule(identifier, code)
}
private async createModule(identifier: string): Promise<VMModule> {
if (identifier.startsWith('data:'))
return this.createDataModule(identifier)
const extension = extname(identifier)
if (extension === '.node' || isNodeBuiltin(identifier)) {
const exports = this.requireCoreModule(identifier)
return this.wrapCoreSynteticModule(identifier, exports)
}
const isFileUrl = identifier.startsWith('file://')
const fileUrl = isFileUrl ? identifier : pathToFileURL(identifier).toString()
const pathUrl = isFileUrl ? fileURLToPath(identifier.split('?')[0]) : identifier
// TODO: support wasm in the future
// if (extension === '.wasm') {
// const source = this.readBuffer(pathUrl)
// const wasm = this.loadWebAssemblyModule(source, fileUrl)
// this.moduleCache.set(fileUrl, wasm)
// return wasm
// }
if (extension === '.cjs') {
const module = this.createCommonJSNodeModule(pathUrl)
const exports = this.loadCommonJSModule(module, pathUrl)
return this.wrapCommonJsSynteticModule(fileUrl, exports)
}
if (extension === '.mjs')
return await this.createEsmModule(fileUrl, this.readFile(pathUrl))
const pkgData = this.findNearestPackageData(normalize(pathUrl))
if (pkgData.type === 'module')
return await this.createEsmModule(fileUrl, this.readFile(pathUrl))
const module = this.createCommonJSNodeModule(pathUrl)
const exports = this.loadCommonJSModule(module, pathUrl)
return this.wrapCommonJsSynteticModule(fileUrl, exports)
}
async import(identifier: string) {
const module = await this.createModule(identifier)
await this.evaluateModule(module)
return module.namespace
}
}