@seanhogg/builderforce-webcontainers-core 2026.10.8 → 2026.10.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build.d.ts +6 -0
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +9 -1
- package/dist/build.js.map +1 -1
- package/dist/buildHost.d.ts +3 -0
- package/dist/buildHost.d.ts.map +1 -1
- package/dist/buildHost.js +25 -12
- package/dist/buildHost.js.map +1 -1
- package/dist/compileComponent.d.ts.map +1 -1
- package/dist/compileComponent.js +4 -1
- package/dist/compileComponent.js.map +1 -1
- package/dist/compileScript.d.ts +12 -4
- package/dist/compileScript.d.ts.map +1 -1
- package/dist/compileScript.js +26 -13
- package/dist/compileScript.js.map +1 -1
- package/dist/components.d.ts +5 -0
- package/dist/components.d.ts.map +1 -1
- package/dist/components.js.map +1 -1
- package/dist/detectProject.d.ts +2 -0
- package/dist/detectProject.d.ts.map +1 -1
- package/dist/detectProject.js +13 -7
- package/dist/detectProject.js.map +1 -1
- package/dist/devServer.d.ts +38 -4
- package/dist/devServer.d.ts.map +1 -1
- package/dist/devServer.js +140 -24
- package/dist/devServer.js.map +1 -1
- package/dist/devStyles.d.ts +27 -0
- package/dist/devStyles.d.ts.map +1 -0
- package/dist/devStyles.js +52 -0
- package/dist/devStyles.js.map +1 -0
- package/dist/hot/client.d.ts +33 -0
- package/dist/hot/client.d.ts.map +1 -0
- package/dist/hot/client.js +46 -0
- package/dist/hot/client.js.map +1 -0
- package/dist/hot/moduleGraph.d.ts +39 -0
- package/dist/hot/moduleGraph.d.ts.map +1 -0
- package/dist/hot/moduleGraph.js +69 -0
- package/dist/hot/moduleGraph.js.map +1 -0
- package/dist/hot/planUpdate.d.ts +33 -0
- package/dist/hot/planUpdate.d.ts.map +1 -0
- package/dist/hot/planUpdate.js +43 -0
- package/dist/hot/planUpdate.js.map +1 -0
- package/dist/hot/reactRefresh.d.ts +45 -0
- package/dist/hot/reactRefresh.d.ts.map +1 -0
- package/dist/hot/reactRefresh.js +86 -0
- package/dist/hot/reactRefresh.js.map +1 -0
- package/dist/html.d.ts +2 -8
- package/dist/html.d.ts.map +1 -1
- package/dist/html.js +11 -17
- package/dist/html.js.map +1 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/installer/tree.d.ts.map +1 -1
- package/dist/installer/tree.js +30 -5
- package/dist/installer/tree.js.map +1 -1
- package/dist/installer/wasmBuilds.d.ts +34 -0
- package/dist/installer/wasmBuilds.d.ts.map +1 -0
- package/dist/installer/wasmBuilds.js +41 -0
- package/dist/installer/wasmBuilds.js.map +1 -0
- package/dist/node/builtins.d.ts +6 -1
- package/dist/node/builtins.d.ts.map +1 -1
- package/dist/node/builtins.js +17 -10
- package/dist/node/builtins.js.map +1 -1
- package/dist/node/childProcess.d.ts +3 -3
- package/dist/node/childProcess.d.ts.map +1 -1
- package/dist/node/childProcess.js +68 -16
- package/dist/node/childProcess.js.map +1 -1
- package/dist/node/crypto.d.ts.map +1 -1
- package/dist/node/crypto.js +5 -4
- package/dist/node/crypto.js.map +1 -1
- package/dist/node/esm.d.ts +7 -0
- package/dist/node/esm.d.ts.map +1 -1
- package/dist/node/esm.js +28 -2
- package/dist/node/esm.js.map +1 -1
- package/dist/node/fs/index.d.ts +3 -0
- package/dist/node/fs/index.d.ts.map +1 -1
- package/dist/node/fs/index.js +22 -106
- package/dist/node/fs/index.js.map +1 -1
- package/dist/node/fs/stdinFd.d.ts +10 -0
- package/dist/node/fs/stdinFd.d.ts.map +1 -0
- package/dist/node/fs/stdinFd.js +40 -0
- package/dist/node/fs/stdinFd.js.map +1 -0
- package/dist/node/fs/streams.d.ts +24 -0
- package/dist/node/fs/streams.d.ts.map +1 -0
- package/dist/node/fs/streams.js +144 -0
- package/dist/node/fs/streams.js.map +1 -0
- package/dist/node/fs/sync.d.ts +2 -0
- package/dist/node/fs/sync.d.ts.map +1 -1
- package/dist/node/fs/sync.js +17 -0
- package/dist/node/fs/sync.js.map +1 -1
- package/dist/node/globalObject.d.ts +25 -0
- package/dist/node/globalObject.d.ts.map +1 -0
- package/dist/node/globalObject.js +83 -0
- package/dist/node/globalObject.js.map +1 -0
- package/dist/node/http/index.d.ts.map +1 -1
- package/dist/node/http/index.js +4 -3
- package/dist/node/http/index.js.map +1 -1
- package/dist/node/http/server.d.ts.map +1 -1
- package/dist/node/http/server.js +8 -5
- package/dist/node/http/server.js.map +1 -1
- package/dist/node/http/serverResponse.d.ts +3 -0
- package/dist/node/http/serverResponse.d.ts.map +1 -1
- package/dist/node/http/serverResponse.js +7 -0
- package/dist/node/http/serverResponse.js.map +1 -1
- package/dist/node/ipc.d.ts +26 -0
- package/dist/node/ipc.d.ts.map +1 -0
- package/dist/node/ipc.js +76 -0
- package/dist/node/ipc.js.map +1 -0
- package/dist/node/loader.d.ts +2 -0
- package/dist/node/loader.d.ts.map +1 -1
- package/dist/node/loader.js +79 -14
- package/dist/node/loader.js.map +1 -1
- package/dist/node/misc.d.ts.map +1 -1
- package/dist/node/misc.js +20 -6
- package/dist/node/misc.js.map +1 -1
- package/dist/node/process.d.ts +2 -5
- package/dist/node/process.d.ts.map +1 -1
- package/dist/node/process.js +16 -11
- package/dist/node/process.js.map +1 -1
- package/dist/node/processExit.d.ts +13 -0
- package/dist/node/processExit.d.ts.map +1 -0
- package/dist/node/processExit.js +25 -0
- package/dist/node/processExit.js.map +1 -0
- package/dist/node/program.d.ts.map +1 -1
- package/dist/node/program.js +6 -5
- package/dist/node/program.js.map +1 -1
- package/dist/node/runtime.d.ts +6 -1
- package/dist/node/runtime.d.ts.map +1 -1
- package/dist/node/runtime.js +23 -15
- package/dist/node/runtime.js.map +1 -1
- package/dist/node/stream/readable.d.ts +2 -1
- package/dist/node/stream/readable.d.ts.map +1 -1
- package/dist/node/stream/readable.js +2 -1
- package/dist/node/stream/readable.js.map +1 -1
- package/dist/node/urlqs.d.ts +1 -1
- package/dist/node/urlqs.d.ts.map +1 -1
- package/dist/node/urlqs.js +11 -2
- package/dist/node/urlqs.js.map +1 -1
- package/dist/node/wasi/abi.d.ts +82 -0
- package/dist/node/wasi/abi.d.ts.map +1 -0
- package/dist/node/wasi/abi.js +144 -0
- package/dist/node/wasi/abi.js.map +1 -0
- package/dist/node/wasi/files.d.ts +67 -0
- package/dist/node/wasi/files.d.ts.map +1 -0
- package/dist/node/wasi/files.js +319 -0
- package/dist/node/wasi/files.js.map +1 -0
- package/dist/node/wasi/index.d.ts +17 -0
- package/dist/node/wasi/index.d.ts.map +1 -0
- package/dist/node/wasi/index.js +129 -0
- package/dist/node/wasi/index.js.map +1 -0
- package/dist/node/workerThreads.d.ts +31 -0
- package/dist/node/workerThreads.d.ts.map +1 -0
- package/dist/node/workerThreads.js +165 -0
- package/dist/node/workerThreads.js.map +1 -0
- package/dist/node/zlib.d.ts.map +1 -1
- package/dist/node/zlib.js +23 -2
- package/dist/node/zlib.js.map +1 -1
- package/dist/shell/context.d.ts.map +1 -1
- package/dist/shell/context.js +2 -2
- package/dist/shell/context.js.map +1 -1
- package/dist/shell/devHandoff.d.ts +10 -4
- package/dist/shell/devHandoff.d.ts.map +1 -1
- package/dist/shell/devHandoff.js +7 -5
- package/dist/shell/devHandoff.js.map +1 -1
- package/dist/shell/exec.d.ts.map +1 -1
- package/dist/shell/exec.js +6 -5
- package/dist/shell/exec.js.map +1 -1
- package/dist/shell/npm.js +2 -2
- package/dist/shell/npm.js.map +1 -1
- package/dist/shell/program.js +5 -5
- package/dist/shell/program.js.map +1 -1
- package/dist/styleModules.d.ts +2 -5
- package/dist/styleModules.d.ts.map +1 -1
- package/dist/styleModules.js +23 -14
- package/dist/styleModules.js.map +1 -1
- package/dist/styles/compile.d.ts +18 -0
- package/dist/styles/compile.d.ts.map +1 -0
- package/dist/styles/compile.js +34 -0
- package/dist/styles/compile.js.map +1 -0
- package/dist/styles/compilers.d.ts +75 -0
- package/dist/styles/compilers.d.ts.map +1 -0
- package/dist/styles/compilers.js +77 -0
- package/dist/styles/compilers.js.map +1 -0
- package/dist/styles/cssModules.d.ts +40 -0
- package/dist/styles/cssModules.d.ts.map +1 -0
- package/dist/styles/cssModules.js +239 -0
- package/dist/styles/cssModules.js.map +1 -0
- package/dist/styles/imports.d.ts +37 -0
- package/dist/styles/imports.d.ts.map +1 -0
- package/dist/styles/imports.js +88 -0
- package/dist/styles/imports.js.map +1 -0
- package/dist/styles/languages.d.ts +13 -0
- package/dist/styles/languages.d.ts.map +1 -0
- package/dist/styles/languages.js +20 -0
- package/dist/styles/languages.js.map +1 -0
- package/dist/styles/packageStyles.d.ts +19 -0
- package/dist/styles/packageStyles.d.ts.map +1 -0
- package/dist/styles/packageStyles.js +32 -0
- package/dist/styles/packageStyles.js.map +1 -0
- package/dist/svelteCompiler.d.ts.map +1 -1
- package/dist/svelteCompiler.js +19 -2
- package/dist/svelteCompiler.js.map +1 -1
- package/dist/system/commands.d.ts +2 -0
- package/dist/system/commands.d.ts.map +1 -1
- package/dist/system/commands.js +18 -0
- package/dist/system/commands.js.map +1 -1
- package/dist/system/index.d.ts +3 -1
- package/dist/system/index.d.ts.map +1 -1
- package/dist/system/index.js +1 -0
- package/dist/system/index.js.map +1 -1
- package/dist/system/ipc.d.ts +33 -0
- package/dist/system/ipc.d.ts.map +1 -0
- package/dist/system/ipc.js +65 -0
- package/dist/system/ipc.js.map +1 -0
- package/dist/system/kernel.d.ts.map +1 -1
- package/dist/system/kernel.js +6 -3
- package/dist/system/kernel.js.map +1 -1
- package/dist/system/process.d.ts +3 -0
- package/dist/system/process.d.ts.map +1 -1
- package/dist/system/process.js +50 -26
- package/dist/system/process.js.map +1 -1
- package/dist/system/terminal.d.ts +10 -4
- package/dist/system/terminal.d.ts.map +1 -1
- package/dist/system/terminal.js +21 -13
- package/dist/system/terminal.js.map +1 -1
- package/dist/system/types.d.ts +29 -7
- package/dist/system/types.d.ts.map +1 -1
- package/dist/vueCompiler.d.ts.map +1 -1
- package/dist/vueCompiler.js +11 -8
- package/dist/vueCompiler.js.map +1 -1
- package/package.json +4 -1
- package/src/build.ts +13 -1
- package/src/buildHost.ts +23 -10
- package/src/compileComponent.ts +4 -1
- package/src/compileScript.ts +30 -11
- package/src/components.ts +5 -0
- package/src/detectProject.ts +13 -6
- package/src/devServer.ts +148 -23
- package/src/devStyles.ts +70 -0
- package/src/hot/client.ts +70 -0
- package/src/hot/moduleGraph.ts +90 -0
- package/src/hot/planUpdate.ts +66 -0
- package/src/hot/reactRefresh.ts +93 -0
- package/src/html.ts +13 -19
- package/src/index.ts +19 -1
- package/src/installer/tree.ts +32 -5
- package/src/installer/wasmBuilds.ts +60 -0
- package/src/node/builtins.ts +23 -12
- package/src/node/childProcess.ts +73 -20
- package/src/node/crypto.ts +5 -4
- package/src/node/esm.ts +26 -2
- package/src/node/fs/index.ts +26 -103
- package/src/node/fs/stdinFd.ts +44 -0
- package/src/node/fs/streams.ts +177 -0
- package/src/node/fs/sync.ts +15 -0
- package/src/node/globalObject.ts +82 -0
- package/src/node/http/index.ts +4 -3
- package/src/node/http/server.ts +8 -5
- package/src/node/http/serverResponse.ts +7 -0
- package/src/node/ipc.ts +91 -0
- package/src/node/loader.ts +78 -14
- package/src/node/misc.ts +19 -6
- package/src/node/process.ts +15 -8
- package/src/node/processExit.ts +21 -0
- package/src/node/program.ts +12 -9
- package/src/node/runtime.ts +27 -15
- package/src/node/stream/readable.ts +2 -1
- package/src/node/urlqs.ts +10 -2
- package/src/node/wasi/abi.ts +151 -0
- package/src/node/wasi/files.ts +350 -0
- package/src/node/wasi/index.ts +149 -0
- package/src/node/workerThreads.ts +198 -0
- package/src/node/zlib.ts +24 -2
- package/src/shell/context.ts +2 -2
- package/src/shell/devHandoff.ts +16 -9
- package/src/shell/exec.ts +6 -5
- package/src/shell/npm.ts +2 -2
- package/src/shell/program.ts +5 -5
- package/src/styleModules.ts +30 -14
- package/src/styles/compile.ts +43 -0
- package/src/styles/compilers.ts +143 -0
- package/src/styles/cssModules.ts +254 -0
- package/src/styles/imports.ts +125 -0
- package/src/styles/languages.ts +29 -0
- package/src/styles/packageStyles.ts +35 -0
- package/src/svelteCompiler.ts +20 -3
- package/src/system/commands.ts +17 -0
- package/src/system/index.ts +4 -0
- package/src/system/ipc.ts +89 -0
- package/src/system/kernel.ts +6 -3
- package/src/system/process.ts +57 -30
- package/src/system/terminal.ts +34 -12
- package/src/system/types.ts +31 -7
- package/src/vueCompiler.ts +11 -8
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `fs.ReadStream` / `fs.WriteStream`. Both are callable without `new`, as Node's are:
|
|
3
|
+
* graceful-fs (under webpack's cache and many CLIs) subclasses them the pre-class way,
|
|
4
|
+
* `fs$WriteStream.apply(this, arguments)` on an object it made, and replaces `open`.
|
|
5
|
+
*/
|
|
6
|
+
import { Buffer } from '../buffer.js';
|
|
7
|
+
import { callable } from '../callable.js';
|
|
8
|
+
import { initReadable, Readable } from '../stream/readable.js';
|
|
9
|
+
import { initWritable, Writable } from '../stream/writable.js';
|
|
10
|
+
import { queueCallback } from '../processExit.js';
|
|
11
|
+
import type { FsContext, SyncFs } from './sync.js';
|
|
12
|
+
|
|
13
|
+
type Fn = (...args: any[]) => any;
|
|
14
|
+
type ReadOptions = { start?: number; end?: number; encoding?: string; highWaterMark?: number; fd?: number; flags?: string; mode?: number };
|
|
15
|
+
type WriteOptions = { flags?: string; encoding?: string; mode?: number };
|
|
16
|
+
|
|
17
|
+
const CHUNK = 64 * 1024;
|
|
18
|
+
|
|
19
|
+
const optionsOf = <T extends { encoding?: string }>(options: T | string | undefined): T => (typeof options === 'string' ? ({ encoding: options } as T) : options ?? ({} as T));
|
|
20
|
+
|
|
21
|
+
export interface FsStreams {
|
|
22
|
+
ReadStream: new (path: string | URL, options?: ReadOptions | string) => Readable;
|
|
23
|
+
WriteStream: new (path: string | URL, options?: WriteOptions | string) => Writable;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createFsStreams(sync: SyncFs, ctx: FsContext): FsStreams {
|
|
27
|
+
class ReadStream extends Readable {
|
|
28
|
+
declare bytesRead: number;
|
|
29
|
+
declare path: string;
|
|
30
|
+
declare fd: number | null;
|
|
31
|
+
declare flags: string;
|
|
32
|
+
declare mode: number;
|
|
33
|
+
declare pending: boolean;
|
|
34
|
+
declare data?: Uint8Array;
|
|
35
|
+
declare offset: number;
|
|
36
|
+
declare endAt: number;
|
|
37
|
+
|
|
38
|
+
constructor(path: string | URL, options?: ReadOptions | string) {
|
|
39
|
+
const opts = optionsOf(options);
|
|
40
|
+
super({ highWaterMark: opts.highWaterMark ?? CHUNK, encoding: opts.encoding });
|
|
41
|
+
startRead(this, path, opts);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Node's legacy hook; a subclass that replaces it opens the file itself. */
|
|
45
|
+
open(): void {}
|
|
46
|
+
|
|
47
|
+
override _read(): void {
|
|
48
|
+
if (!this.data) {
|
|
49
|
+
this._readableState.reading = false; // retried once the file is loaded
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const end = Math.min(this.data.length, this.endAt + 1);
|
|
53
|
+
if (this.offset >= end) {
|
|
54
|
+
this.push(null);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const chunk = this.data.subarray(this.offset, Math.min(end, this.offset + this._readableState.highWaterMark));
|
|
58
|
+
this.offset += chunk.length;
|
|
59
|
+
this.bytesRead += chunk.length;
|
|
60
|
+
this.push(Buffer.from(chunk));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
close(callback?: Fn): void {
|
|
64
|
+
this.destroy();
|
|
65
|
+
if (callback) this.once('close', callback);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function startRead(self: ReadStream, path: string | URL, opts: ReadOptions): void {
|
|
70
|
+
self.bytesRead = 0;
|
|
71
|
+
self.fd = null;
|
|
72
|
+
self.pending = true;
|
|
73
|
+
self.flags = opts.flags ?? 'r';
|
|
74
|
+
self.mode = opts.mode ?? 0o666;
|
|
75
|
+
self.path = opts.fd !== undefined ? '' : sync._resolve(path);
|
|
76
|
+
self.offset = opts.start ?? 0;
|
|
77
|
+
self.endAt = opts.end ?? Infinity;
|
|
78
|
+
queueCallback(() => {
|
|
79
|
+
try {
|
|
80
|
+
self.data = sync.readFileSync(self.path) as Buffer;
|
|
81
|
+
self.pending = false;
|
|
82
|
+
if (self.open !== ReadStream.prototype.open) self.open();
|
|
83
|
+
else {
|
|
84
|
+
self.fd = -1;
|
|
85
|
+
self.emit('open', self.fd);
|
|
86
|
+
self.emit('ready');
|
|
87
|
+
}
|
|
88
|
+
self._pull();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
self.destroy(error as Error);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
class WriteStream extends Writable {
|
|
96
|
+
declare bytesWritten: number;
|
|
97
|
+
declare path: string;
|
|
98
|
+
declare fd: number | null;
|
|
99
|
+
declare flags: string;
|
|
100
|
+
declare mode: number;
|
|
101
|
+
declare pending: boolean;
|
|
102
|
+
declare started: boolean;
|
|
103
|
+
|
|
104
|
+
constructor(path: string | URL, options?: WriteOptions | string) {
|
|
105
|
+
super({ decodeStrings: true });
|
|
106
|
+
startWrite(this, path, optionsOf(options));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Node's legacy hook; a subclass that replaces it opens the file itself. */
|
|
110
|
+
open(): void {}
|
|
111
|
+
|
|
112
|
+
/** Create or truncate the file once, as opening it with `flags` would. */
|
|
113
|
+
ensureFile(): void {
|
|
114
|
+
if (this.started) return;
|
|
115
|
+
this.started = true;
|
|
116
|
+
if (!this.flags.startsWith('a') || !ctx.vfs.isFile(this.path)) sync.writeFileSync(this.path, '', { flag: this.flags.includes('x') ? 'wx' : 'w' });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
override _write(chunk: Buffer, encoding: string, callback: (error?: Error | null) => void): void {
|
|
120
|
+
// A replaced `open` opens (and truncates) later: writes wait for it, as Node's do.
|
|
121
|
+
if (this.fd === null && this.open !== WriteStream.prototype.open) {
|
|
122
|
+
this.once('open', () => this._write(chunk, encoding, callback));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
this.ensureFile();
|
|
127
|
+
sync.appendFileSync(this.path, chunk);
|
|
128
|
+
this.bytesWritten += chunk.length;
|
|
129
|
+
callback();
|
|
130
|
+
} catch (error) {
|
|
131
|
+
callback(error as Error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
close(callback?: Fn): void {
|
|
136
|
+
this.end();
|
|
137
|
+
if (callback) this.once('close', callback);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function startWrite(self: WriteStream, path: string | URL, opts: WriteOptions): void {
|
|
142
|
+
self.bytesWritten = 0;
|
|
143
|
+
self.fd = null;
|
|
144
|
+
self.pending = false;
|
|
145
|
+
self.started = false;
|
|
146
|
+
self.flags = opts.flags ?? 'w';
|
|
147
|
+
self.mode = opts.mode ?? 0o666;
|
|
148
|
+
self.path = sync._resolve(path);
|
|
149
|
+
queueCallback(() => {
|
|
150
|
+
try {
|
|
151
|
+
if (self.open !== WriteStream.prototype.open) {
|
|
152
|
+
self.started = true; // the replacement opens (and truncates) the file
|
|
153
|
+
self.open();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
self.ensureFile();
|
|
157
|
+
self.fd = -1;
|
|
158
|
+
self.emit('open', self.fd);
|
|
159
|
+
self.emit('ready');
|
|
160
|
+
} catch (error) {
|
|
161
|
+
self.destroy(error as Error);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
ReadStream: callable(ReadStream, (self: ReadStream, path: string | URL, options?: ReadOptions | string) => {
|
|
168
|
+
const opts = optionsOf(options);
|
|
169
|
+
initReadable(self, { highWaterMark: opts.highWaterMark ?? CHUNK, encoding: opts.encoding });
|
|
170
|
+
startRead(self, path, opts);
|
|
171
|
+
}),
|
|
172
|
+
WriteStream: callable(WriteStream, (self: WriteStream, path: string | URL, options?: WriteOptions | string) => {
|
|
173
|
+
initWritable(self, { decodeStrings: true });
|
|
174
|
+
startWrite(self, path, optionsOf(options));
|
|
175
|
+
}),
|
|
176
|
+
};
|
|
177
|
+
}
|
package/src/node/fs/sync.ts
CHANGED
|
@@ -311,6 +311,21 @@ export function createSyncFs(ctx: FsContext) {
|
|
|
311
311
|
return chunk.length;
|
|
312
312
|
},
|
|
313
313
|
writeSync: writeFd,
|
|
314
|
+
// Vectored IO. Without `fs.writev`, tar/fs-minipass fall back to process.binding('fs').
|
|
315
|
+
writevSync(fd: number, buffers: readonly ArrayBufferView[], position?: number | null): number {
|
|
316
|
+
let total = 0;
|
|
317
|
+
for (const buffer of buffers) total += writeFd(fd, buffer, 0, buffer.byteLength, position == null ? null : position + total);
|
|
318
|
+
return total;
|
|
319
|
+
},
|
|
320
|
+
readvSync(fd: number, buffers: readonly ArrayBufferView[], position?: number | null): number {
|
|
321
|
+
let total = 0;
|
|
322
|
+
for (const buffer of buffers) {
|
|
323
|
+
const read = api.readSync(fd, buffer, 0, buffer.byteLength, position == null ? null : position + total);
|
|
324
|
+
total += read;
|
|
325
|
+
if (read < buffer.byteLength) break;
|
|
326
|
+
}
|
|
327
|
+
return total;
|
|
328
|
+
},
|
|
314
329
|
opendirSync(p: PathLike) {
|
|
315
330
|
const path = resolve(p);
|
|
316
331
|
const entries = api.readdirSync(path, { withFileTypes: true }) as Dirent[];
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A process's global object, as its code sees it (`global`, `globalThis`).
|
|
3
|
+
*
|
|
4
|
+
* - In a dedicated worker the real global object is the process's own: Node's
|
|
5
|
+
* names are installed on it.
|
|
6
|
+
* - Where several processes share one realm (tests, the in-process kernel), each
|
|
7
|
+
* gets a view: its own names first, everything else read from the real global
|
|
8
|
+
* WITH the real one as receiver, because host getters (`crypto`, `navigator`)
|
|
9
|
+
* check what `this` is. Code the process compiles with `new Function` sees the
|
|
10
|
+
* same view (esbuild-wasm's launcher compiles Go's loader that way and the two
|
|
11
|
+
* hand `globalThis.Go` and `globalThis.crypto` to each other).
|
|
12
|
+
*
|
|
13
|
+
* Either way, assigning a global works as in Node. A browser's `crypto` (and a
|
|
14
|
+
* host Node's) is a getter with no setter, so `globalThis.crypto = …` (Go's
|
|
15
|
+
* WebAssembly loader, which esbuild-wasm runs) would throw in strict code; Node's
|
|
16
|
+
* own globals let it replace the value.
|
|
17
|
+
*
|
|
18
|
+
* Adds `global` and `globalThis` to `globals` (and, for a view, `Function`), so
|
|
19
|
+
* module code finds them in its outer scope. For a view it returns the `with`
|
|
20
|
+
* scope module code is compiled in, so a bare name another module put on
|
|
21
|
+
* `globalThis` (Go's loader sets `globalThis.fs`, then calls `fs.writeSync`)
|
|
22
|
+
* resolves as it would on a real global.
|
|
23
|
+
*/
|
|
24
|
+
export function installGlobalObject(globals: Record<string, unknown>, installOnRealGlobal: boolean): object | undefined {
|
|
25
|
+
makeAssignable(globalThis, ['crypto', 'performance', 'navigator']);
|
|
26
|
+
if (installOnRealGlobal) {
|
|
27
|
+
Object.assign(globalThis, globals, { global: globalThis });
|
|
28
|
+
globals.global = globals.globalThis = globalThis;
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
const own = Object.assign(Object.create(globalThis), globals) as Record<PropertyKey, unknown>;
|
|
32
|
+
const view: Record<PropertyKey, unknown> = new Proxy(own, {
|
|
33
|
+
get: (target, key) => (Object.hasOwn(target, key) ? target[key as string] : Reflect.get(globalThis, key)),
|
|
34
|
+
// An assignment lands on this process's view: never on the shared real global, and
|
|
35
|
+
// never refused by a getter-only accessor inherited from it.
|
|
36
|
+
set: (target, key, value) => Reflect.defineProperty(target, key, { value, writable: true, enumerable: true, configurable: true }),
|
|
37
|
+
// Redefining an inherited global without a value (`defineProperty(globalThis, 'crypto',
|
|
38
|
+
// { writable: true })`) keeps the value it had, as it would on the real global.
|
|
39
|
+
defineProperty: (target, key, descriptor) =>
|
|
40
|
+
Reflect.defineProperty(target, key, Object.hasOwn(target, key) || 'value' in descriptor || descriptor.get || descriptor.set ? descriptor : { value: Reflect.get(globalThis, key), ...descriptor }),
|
|
41
|
+
});
|
|
42
|
+
// For `with`: claims only the view's own names, so everything else falls through to the real global.
|
|
43
|
+
const scope = new Proxy(view, {
|
|
44
|
+
has: (_target, key) => typeof key === 'string' && Object.hasOwn(own, key),
|
|
45
|
+
get: (target, key) => (key === Symbol.unscopables ? undefined : Reflect.get(target, key)),
|
|
46
|
+
});
|
|
47
|
+
const scopedFunction = createScopedFunction(scope);
|
|
48
|
+
own.global = own.globalThis = view;
|
|
49
|
+
own.Function = scopedFunction;
|
|
50
|
+
globals.global = globals.globalThis = view;
|
|
51
|
+
globals.Function = scopedFunction;
|
|
52
|
+
return scope;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `Function` whose compiled code resolves free names through `scope` (the view's
|
|
57
|
+
* own names, then the real global), as code compiled in a process's own realm would.
|
|
58
|
+
*/
|
|
59
|
+
function createScopedFunction(scope: object): FunctionConstructor {
|
|
60
|
+
const compile = (args: unknown[]) => {
|
|
61
|
+
const params = args.slice(0, -1).map(String).join(', ');
|
|
62
|
+
const body = args.length ? String(args[args.length - 1]) : '';
|
|
63
|
+
// Sloppy outer function: `with` routes free names through the view; the body may still be strict.
|
|
64
|
+
return new Function('__bfwc_scope', `with (__bfwc_scope) { return function anonymous(${params}\n) {\n${body}\n}; }`)(scope);
|
|
65
|
+
};
|
|
66
|
+
return new Proxy(Function, { apply: (_target, _this, args) => compile(args), construct: (_target, args) => compile(args) });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Give each getter-only global (own or inherited) a writable own value, as Node's are. */
|
|
70
|
+
function makeAssignable(target: object, names: readonly string[]): void {
|
|
71
|
+
for (const name of names) {
|
|
72
|
+
let holder: object | null = target;
|
|
73
|
+
let descriptor: PropertyDescriptor | undefined;
|
|
74
|
+
while (holder && !(descriptor = Object.getOwnPropertyDescriptor(holder, name))) holder = Object.getPrototypeOf(holder);
|
|
75
|
+
if (!descriptor || descriptor.set || 'value' in descriptor) continue;
|
|
76
|
+
try {
|
|
77
|
+
Object.defineProperty(target, name, { value: Reflect.get(target, name), writable: true, enumerable: true, configurable: true });
|
|
78
|
+
} catch {
|
|
79
|
+
// a non-configurable own accessor: leave it as the host made it
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
package/src/node/http/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { ClientRequest, createFetch, type ClientDeps } from './client.js';
|
|
|
9
9
|
import { FakeSocket, IncomingMessage, OutgoingHeaders, Server, ServerResponse } from './server.js';
|
|
10
10
|
import { METHODS, STATUS_CODES } from './status.js';
|
|
11
11
|
import { textResponse } from '../../system/httpStream.js';
|
|
12
|
+
import { queueCallback } from '../processExit.js';
|
|
12
13
|
|
|
13
14
|
export { createFetch };
|
|
14
15
|
|
|
@@ -77,12 +78,12 @@ export function createNetModule(deps: ClientDeps): Record<string, unknown> {
|
|
|
77
78
|
try {
|
|
78
79
|
this.reservation = deps.system.listen(port, async () => textResponse(502, 'Not an HTTP server'));
|
|
79
80
|
} catch (error) {
|
|
80
|
-
|
|
81
|
+
queueCallback(() => this.emit('error', error));
|
|
81
82
|
return this;
|
|
82
83
|
}
|
|
83
84
|
this.listening = true;
|
|
84
85
|
this.release = deps.loop.hold();
|
|
85
|
-
|
|
86
|
+
queueCallback(() => this.emit('listening'));
|
|
86
87
|
return this;
|
|
87
88
|
}
|
|
88
89
|
address() {
|
|
@@ -94,7 +95,7 @@ export function createNetModule(deps: ClientDeps): Record<string, unknown> {
|
|
|
94
95
|
this.listening = false;
|
|
95
96
|
this.release?.();
|
|
96
97
|
if (callback) this.once('close', callback);
|
|
97
|
-
|
|
98
|
+
queueCallback(() => this.emit('close'));
|
|
98
99
|
return this;
|
|
99
100
|
}
|
|
100
101
|
ref(): this { return this; }
|
package/src/node/http/server.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { pushStream } from '../../system/httpStream.js';
|
|
|
14
14
|
import { FakeSocket, IncomingMessage } from './messages.js';
|
|
15
15
|
import { ServerResponse } from './serverResponse.js';
|
|
16
16
|
import { UpgradeSocket } from './upgradeSocket.js';
|
|
17
|
+
import { queueCallback } from '../processExit.js';
|
|
17
18
|
|
|
18
19
|
export { FakeSocket, IncomingMessage, OutgoingHeaders } from './messages.js';
|
|
19
20
|
export { ServerResponse } from './serverResponse.js';
|
|
@@ -48,12 +49,12 @@ export class Server extends EventEmitterBase {
|
|
|
48
49
|
try {
|
|
49
50
|
this.registration = this.deps.system.listen(port, (request) => this.handle(request), (request, socket) => this.upgrade(request, socket));
|
|
50
51
|
} catch (error) {
|
|
51
|
-
|
|
52
|
+
queueCallback(() => this.emit('error', error));
|
|
52
53
|
return this;
|
|
53
54
|
}
|
|
54
55
|
this.listening = true;
|
|
55
56
|
this.release = this.deps.loop.hold();
|
|
56
|
-
|
|
57
|
+
queueCallback(() => this.emit('listening'));
|
|
57
58
|
return this;
|
|
58
59
|
}
|
|
59
60
|
|
|
@@ -63,7 +64,7 @@ export class Server extends EventEmitterBase {
|
|
|
63
64
|
|
|
64
65
|
close(callback?: (error?: Error) => void): this {
|
|
65
66
|
if (!this.registration) {
|
|
66
|
-
if (callback)
|
|
67
|
+
if (callback) queueCallback(() => callback(Object.assign(new Error('Server is not running.'), { code: 'ERR_SERVER_NOT_RUNNING' })));
|
|
67
68
|
return this;
|
|
68
69
|
}
|
|
69
70
|
this.registration.close();
|
|
@@ -72,7 +73,7 @@ export class Server extends EventEmitterBase {
|
|
|
72
73
|
this.release?.();
|
|
73
74
|
this.release = undefined;
|
|
74
75
|
if (callback) this.once('close', callback);
|
|
75
|
-
|
|
76
|
+
queueCallback(() => this.emit('close'));
|
|
76
77
|
return this;
|
|
77
78
|
}
|
|
78
79
|
|
|
@@ -100,7 +101,9 @@ export class Server extends EventEmitterBase {
|
|
|
100
101
|
const req = new IncomingMessage(socket);
|
|
101
102
|
req.method = request.method.toUpperCase();
|
|
102
103
|
req.url = request.url;
|
|
103
|
-
|
|
104
|
+
// HTTP/1.1 always names the host; servers check it (Vite's `allowedHosts` answers 403 without it).
|
|
105
|
+
const named = Object.keys(headers).some((k) => k.toLowerCase() === 'host');
|
|
106
|
+
req._fill(named ? headers : { ...headers, host: `localhost:${socket.localPort}` }, body);
|
|
104
107
|
return req;
|
|
105
108
|
}
|
|
106
109
|
|
|
@@ -55,6 +55,13 @@ export class ServerResponse extends OutgoingHeaders {
|
|
|
55
55
|
override flushHeaders(): void {
|
|
56
56
|
this.sendHead();
|
|
57
57
|
}
|
|
58
|
+
/** Node internals that middleware (compression, on-headers) patch around: send the head now; is it out yet? */
|
|
59
|
+
_implicitHeader(): void {
|
|
60
|
+
this.sendHead();
|
|
61
|
+
}
|
|
62
|
+
get _header(): boolean {
|
|
63
|
+
return this.headersSent;
|
|
64
|
+
}
|
|
58
65
|
writeContinue(): void {}
|
|
59
66
|
writeProcessing(): void {}
|
|
60
67
|
writeEarlyHints(): void {}
|
package/src/node/ipc.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Node face of a process's message channel (system/ipc.ts): `process.send`,
|
|
3
|
+
* `process.on('message')`, `process.connected` / `disconnect()` in a forked
|
|
4
|
+
* child, and the keep-alive rule both it and `worker_threads`' `parentPort` follow.
|
|
5
|
+
*/
|
|
6
|
+
import type { EventEmitterBase } from './events.js';
|
|
7
|
+
import type { EventLoop } from './loop.js';
|
|
8
|
+
import type { NodeProcess } from './process.js';
|
|
9
|
+
import type { IpcEndpoint } from '../system/ipc.js';
|
|
10
|
+
|
|
11
|
+
export interface ChannelKeepAlive {
|
|
12
|
+
/** Re-check whether the channel should hold the process open. */
|
|
13
|
+
update(): void;
|
|
14
|
+
ref(): void;
|
|
15
|
+
unref(): void;
|
|
16
|
+
hasRef(): boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Node keeps a process alive for an open channel only while something listens to
|
|
21
|
+
* it (`events`) and nobody `unref()`ed it. `onFirstListener` runs once, when the
|
|
22
|
+
* first such listener arrives — the moment to start receiving, so messages sent
|
|
23
|
+
* before then wait in the channel instead of being emitted to no one.
|
|
24
|
+
*/
|
|
25
|
+
export function keepAliveWhileListened(emitter: EventEmitterBase, events: readonly string[], loop: EventLoop, open: () => boolean, onFirstListener: () => void): ChannelKeepAlive {
|
|
26
|
+
let release: (() => void) | undefined;
|
|
27
|
+
let referenced = true;
|
|
28
|
+
let started = false;
|
|
29
|
+
const listened = () => events.some((event) => emitter.listenerCount(event) > 0);
|
|
30
|
+
const update = () => {
|
|
31
|
+
if (!started && listened()) {
|
|
32
|
+
started = true;
|
|
33
|
+
onFirstListener();
|
|
34
|
+
}
|
|
35
|
+
const want = referenced && open() && listened();
|
|
36
|
+
if (want && !release) release = loop.hold();
|
|
37
|
+
else if (!want && release) {
|
|
38
|
+
release();
|
|
39
|
+
release = undefined;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
// `newListener` fires before the listener is added: look once it is in place.
|
|
43
|
+
const changed = (event: string | symbol) => {
|
|
44
|
+
if (typeof event === 'string' && events.includes(event)) queueMicrotask(update);
|
|
45
|
+
};
|
|
46
|
+
emitter.on('newListener', changed);
|
|
47
|
+
emitter.on('removeListener', changed);
|
|
48
|
+
return {
|
|
49
|
+
update,
|
|
50
|
+
ref() {
|
|
51
|
+
referenced = true;
|
|
52
|
+
update();
|
|
53
|
+
},
|
|
54
|
+
unref() {
|
|
55
|
+
referenced = false;
|
|
56
|
+
update();
|
|
57
|
+
},
|
|
58
|
+
hasRef: () => referenced,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type Callback = (error: Error | null) => void;
|
|
63
|
+
|
|
64
|
+
/** Give a forked child's `process` its channel to the parent. */
|
|
65
|
+
export function attachProcessChannel(proc: NodeProcess, ipc: IpcEndpoint, loop: EventLoop): void {
|
|
66
|
+
const keepAlive = keepAliveWhileListened(proc, ['message', 'disconnect'], loop, () => ipc.connected, () => {
|
|
67
|
+
ipc.onMessage((message) => loop._run(() => proc.emit('message', message, undefined)));
|
|
68
|
+
});
|
|
69
|
+
ipc.onDisconnect(() => {
|
|
70
|
+
proc.connected = false;
|
|
71
|
+
loop._run(() => proc.emit('disconnect'));
|
|
72
|
+
keepAlive.update();
|
|
73
|
+
});
|
|
74
|
+
Object.assign(proc, {
|
|
75
|
+
connected: true,
|
|
76
|
+
channel: { ref: keepAlive.ref, unref: keepAlive.unref, hasRef: keepAlive.hasRef },
|
|
77
|
+
send(message: unknown, ...rest: unknown[]): boolean {
|
|
78
|
+
const callback = rest.find((arg): arg is Callback => typeof arg === 'function');
|
|
79
|
+
try {
|
|
80
|
+
ipc.send(message);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (!callback) throw error;
|
|
83
|
+
queueMicrotask(() => loop._run(() => callback(error as Error)));
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (callback) queueMicrotask(() => loop._run(() => callback(null)));
|
|
87
|
+
return true;
|
|
88
|
+
},
|
|
89
|
+
disconnect: () => ipc.disconnect(),
|
|
90
|
+
});
|
|
91
|
+
}
|
package/src/node/loader.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import type { VirtualFileSystem } from '../vfs.js';
|
|
13
13
|
import { dirname, extname } from '../paths.js';
|
|
14
|
-
import { esmToCjs, ESM_MARK, hasModuleSyntax, mayBeEsm } from './esm.js';
|
|
14
|
+
import { esmToCjs, ESM_MARK, hasModuleSyntax, mayBeEsm, rewriteDynamicImports } from './esm.js';
|
|
15
15
|
import { findPackageScope, IMPORT_CONDITIONS, nodeModulePaths, REQUIRE_CONDITIONS, resolveNodeModule, type Resolution } from './resolve.js';
|
|
16
16
|
|
|
17
17
|
export interface LoaderOptions {
|
|
@@ -21,6 +21,8 @@ export interface LoaderOptions {
|
|
|
21
21
|
builtin(name: string): unknown;
|
|
22
22
|
/** Injected into every module's wrapper by name. */
|
|
23
23
|
globals: Record<string, unknown>;
|
|
24
|
+
/** Where processes share a realm: a `with` scope for module code, so names set on the process's `globalThis` resolve bare. */
|
|
25
|
+
scope?: object;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
export interface NodeModule {
|
|
@@ -37,8 +39,6 @@ export interface NodeModule {
|
|
|
37
39
|
__tla?: Promise<unknown>;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
const AsyncFunction = Object.getPrototypeOf(async () => undefined).constructor as FunctionConstructor;
|
|
41
|
-
|
|
42
42
|
function stripShebang(source: string): string {
|
|
43
43
|
const text = source.charCodeAt(0) === 0xfeff ? source.slice(1) : source;
|
|
44
44
|
return text.startsWith('#!') ? '//' + text.slice(2) : text;
|
|
@@ -160,31 +160,71 @@ export function createModuleSystem(options: LoaderOptions) {
|
|
|
160
160
|
_compile(content: string, filename: string, esm = false): unknown {
|
|
161
161
|
const require = makeRequire(this);
|
|
162
162
|
const dir = dirname(filename);
|
|
163
|
-
const
|
|
164
|
-
const
|
|
163
|
+
const cjsNames = ['exports', 'require', 'module', '__filename', '__dirname'];
|
|
164
|
+
const cjsValues: unknown[] = [this.exports, require, this, filename, dir];
|
|
165
|
+
let source = content;
|
|
166
|
+
let helperNames: string[] = [];
|
|
167
|
+
let helperValues: unknown[] = [];
|
|
165
168
|
if (esm) {
|
|
166
|
-
|
|
167
|
-
|
|
169
|
+
helperNames = ['__bfwc_exports', '__bfwc_import', '__bfwc_dynamic', '__bfwc_meta', '__bfwc_esm', '__bfwc_star', '__bfwc_get'];
|
|
170
|
+
helperValues = [this.exports, ...esmHelpers(this, filename)];
|
|
171
|
+
} else if ((source = rewriteDynamicImports(content)) !== content) {
|
|
172
|
+
helperNames = ['__bfwc_dynamic'];
|
|
173
|
+
helperValues = [esmHelpers(this, filename)[1]];
|
|
168
174
|
}
|
|
169
|
-
const code = `${stripShebang(
|
|
175
|
+
const code = `${stripShebang(source)}\n//# sourceURL=${encodeURI('file://' + filename)}`;
|
|
176
|
+
// The process's globals are an OUTER scope, as real globals are, so module code may
|
|
177
|
+
// shadow them (`const process = require('process')`, `import { Buffer } from 'node:buffer'`).
|
|
178
|
+
// An ES module may also declare the CommonJS names itself (`const require =
|
|
179
|
+
// createRequire(import.meta.url)`, `const __filename = fileURLToPath(…)`), so for it
|
|
180
|
+
// those sit in the outer scope too and only the rewrite's own helpers are parameters.
|
|
181
|
+
// The module's body starts on the wrapper's line, so stack line numbers hold.
|
|
182
|
+
const [outerNames, outerValues, innerNames, innerValues] = esm
|
|
183
|
+
? [[...globalNames, ...cjsNames], [...globalValues, ...cjsValues], helperNames, helperValues]
|
|
184
|
+
: [globalNames, globalValues, [...cjsNames, ...helperNames], [...cjsValues, ...helperValues]];
|
|
185
|
+
const wrapped = (async: boolean) => {
|
|
186
|
+
const body = `return ${async ? 'async ' : ''}function (${innerNames.join(', ')}) {${code}\n}`;
|
|
187
|
+
// The `with` sits outside the globals' parameters, so those (and the module's own
|
|
188
|
+
// declarations) still win; it only answers names set on `globalThis` since.
|
|
189
|
+
const fn = options.scope
|
|
190
|
+
? new Function('__bfwc_scope', `with (__bfwc_scope) { return function (${outerNames.join(', ')}) { ${body} }; }`)(options.scope)(...outerValues)
|
|
191
|
+
: new Function(...outerNames, body)(...outerValues);
|
|
192
|
+
return fn as (...args: unknown[]) => unknown;
|
|
193
|
+
};
|
|
170
194
|
let fn: (...args: unknown[]) => unknown;
|
|
171
195
|
try {
|
|
172
|
-
fn =
|
|
196
|
+
fn = wrapped(false);
|
|
173
197
|
} catch (error) {
|
|
174
|
-
|
|
175
|
-
|
|
198
|
+
// Top-level await reads as a syntax error in a plain function, and V8 names the
|
|
199
|
+
// token after `await` (`Unexpected identifier '__bfwc_dynamic'`), not `await` itself.
|
|
200
|
+
let retried: ((...args: unknown[]) => unknown) | undefined;
|
|
201
|
+
if (esm && error instanceof SyntaxError && /\bawait\b/.test(source)) {
|
|
202
|
+
try {
|
|
203
|
+
retried = wrapped(true);
|
|
204
|
+
} catch {
|
|
205
|
+
// not top-level await after all: report the first error
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!retried) {
|
|
176
209
|
if (error instanceof Error) error.message = `${filename}: ${error.message}`;
|
|
177
210
|
throw error;
|
|
178
211
|
}
|
|
212
|
+
fn = retried;
|
|
179
213
|
}
|
|
180
|
-
|
|
214
|
+
// An ES module's static imports evaluate first; one still in top-level await holds
|
|
215
|
+
// this module's body (and so, through its own `__tla`, whoever imports it).
|
|
216
|
+
const pending = esm ? importsAwaiting(this, source) : [];
|
|
217
|
+
const result = pending.length ? Promise.all(pending).then(() => fn.apply(this.exports, innerValues)) : fn.apply(this.exports, innerValues);
|
|
181
218
|
if (result instanceof Promise) this.__tla = result;
|
|
182
219
|
return result;
|
|
183
220
|
}
|
|
184
221
|
}
|
|
185
222
|
|
|
186
|
-
|
|
187
|
-
|
|
223
|
+
/** Load what an ES module imports, as `import` and `import()` do. */
|
|
224
|
+
function importLoader(module: Module) {
|
|
225
|
+
return (rawSpecifier: string): { exports: any; __tla?: Promise<unknown> } => {
|
|
226
|
+
// `import()` takes file URLs (`import(pathToFileURL(configPath).href)`); `require` does not.
|
|
227
|
+
const specifier = rawSpecifier.startsWith('file:') ? decodeURIComponent(new URL(rawSpecifier).pathname) : rawSpecifier;
|
|
188
228
|
let target: string;
|
|
189
229
|
try {
|
|
190
230
|
target = Module._resolveFilename(specifier, module, false, { conditions: REQUIRE_CONDITIONS });
|
|
@@ -196,6 +236,30 @@ export function createModuleSystem(options: LoaderOptions) {
|
|
|
196
236
|
}
|
|
197
237
|
return loadResolved(target, module, false);
|
|
198
238
|
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Evaluate a rewritten module's static imports (`__bfwc_import("…")`, in order) and
|
|
243
|
+
* return the top-level awaits still running among them. One that fails to load is
|
|
244
|
+
* left for the module's own import statement to report.
|
|
245
|
+
*/
|
|
246
|
+
function importsAwaiting(module: Module, rewritten: string): Promise<unknown>[] {
|
|
247
|
+
const load = importLoader(module);
|
|
248
|
+
const pending: Promise<unknown>[] = [];
|
|
249
|
+
for (const [, literal] of rewritten.matchAll(/__bfwc_import\(("(?:[^"\\]|\\.)*")\)/g)) {
|
|
250
|
+
if (!literal) continue;
|
|
251
|
+
try {
|
|
252
|
+
const loaded = load(JSON.parse(literal) as string);
|
|
253
|
+
if (loaded.__tla) pending.push(loaded.__tla);
|
|
254
|
+
} catch {
|
|
255
|
+
// reported where the module imports it
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return pending;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function esmHelpers(module: Module, filename: string): unknown[] {
|
|
262
|
+
const load = importLoader(module);
|
|
199
263
|
const importSync = (specifier: string) => namespaceOf(load(specifier).exports);
|
|
200
264
|
const meta = {
|
|
201
265
|
url: 'file://' + filename,
|