@seanhogg/builderforce-webcontainers-core 2026.10.8 → 2026.10.9
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 +22 -4
- package/dist/installer/tree.js.map +1 -1
- package/dist/installer/wasmBuilds.d.ts +27 -0
- package/dist/installer/wasmBuilds.d.ts.map +1 -0
- package/dist/installer/wasmBuilds.js +32 -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 +15 -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 -5
- 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/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 +22 -4
- package/src/installer/wasmBuilds.ts +45 -0
- package/src/node/builtins.ts +21 -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 -5
- 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/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
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,
|
package/src/node/misc.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The small builtins: os, tty, timers (+promises), string_decoder, perf_hooks,
|
|
3
|
-
* async_hooks,
|
|
3
|
+
* async_hooks, vm, dns, v8, diagnostics_channel and stubs for
|
|
4
4
|
* the ones with nothing to offer in a browser (cluster, inspector). Each exists
|
|
5
5
|
* so `require` succeeds and the common calls behave plausibly.
|
|
6
6
|
*/
|
|
@@ -8,6 +8,7 @@ import { EventEmitterBase } from './events.js';
|
|
|
8
8
|
import type { EventLoop } from './loop.js';
|
|
9
9
|
import { decode } from './buffer.js';
|
|
10
10
|
import { callable } from './callable.js';
|
|
11
|
+
import { queueCallback } from './processExit.js';
|
|
11
12
|
|
|
12
13
|
export function createOsModule(env: () => Record<string, string | undefined>): Record<string, unknown> {
|
|
13
14
|
const cpus = Array.from({ length: Math.max(1, Math.min(16, globalThis.navigator?.hardwareConcurrency ?? 4)) }, () => ({
|
|
@@ -171,10 +172,24 @@ export function createAsyncHooksModule(): Record<string, unknown> {
|
|
|
171
172
|
};
|
|
172
173
|
}
|
|
173
174
|
|
|
175
|
+
const VM_CODE = '__bfwc_vm_code';
|
|
176
|
+
|
|
174
177
|
export function createVmModule(): Record<string, unknown> {
|
|
178
|
+
// The context IS the code's global object: free names read it first (then the real
|
|
179
|
+
// builtins), assignments and `globalThis.x = …` land on it. Next.js reads its client
|
|
180
|
+
// manifests back off the context this way.
|
|
175
181
|
const run = (code: string, context: Record<string, unknown> = {}) => {
|
|
176
|
-
const
|
|
177
|
-
|
|
182
|
+
const sandbox: Record<PropertyKey, unknown> = new Proxy(context, {
|
|
183
|
+
has: (_target, key) => key !== VM_CODE,
|
|
184
|
+
get: (target, key) => {
|
|
185
|
+
if (key === Symbol.unscopables) return undefined;
|
|
186
|
+
if (key === 'globalThis') return sandbox;
|
|
187
|
+
return key in target ? target[key as string] : Reflect.get(globalThis, key);
|
|
188
|
+
},
|
|
189
|
+
set: (target, key, value) => Reflect.set(target, key, value),
|
|
190
|
+
});
|
|
191
|
+
// Sloppy-mode function: `with` routes every free name through the sandbox.
|
|
192
|
+
return new Function(VM_CODE, `with (this) { return eval(${VM_CODE}); }`).call(sandbox, code);
|
|
178
193
|
};
|
|
179
194
|
class Script {
|
|
180
195
|
constructor(private readonly code: string) {}
|
|
@@ -231,8 +246,7 @@ const unavailable = (name: string) => () => {
|
|
|
231
246
|
export function createStubModules(): Record<string, Record<string, unknown>> {
|
|
232
247
|
return {
|
|
233
248
|
perf_hooks: { performance: globalThis.performance, PerformanceObserver: class { observe() {} disconnect() {} }, monitorEventLoopDelay: () => ({ enable() {}, disable() {}, percentile: () => 0, mean: 0, max: 0, min: 0 }), constants: {} },
|
|
234
|
-
|
|
235
|
-
dns: { lookup: (host: string, options: unknown, cb?: (e: Error | null, address: string, family: number) => void) => queueMicrotask(() => ((typeof options === 'function' ? options : cb) as NonNullable<typeof cb>)(null, host === 'localhost' ? '127.0.0.1' : host, 4)), resolve: unavailable('dns.resolve'), promises: { lookup: async (host: string) => ({ address: host === 'localhost' ? '127.0.0.1' : host, family: 4 }) }, setDefaultResultOrder: () => undefined },
|
|
249
|
+
dns: { lookup: (host: string, options: unknown, cb?: (e: Error | null, address: string, family: number) => void) => queueCallback(() => ((typeof options === 'function' ? options : cb) as NonNullable<typeof cb>)(null, host === 'localhost' ? '127.0.0.1' : host, 4)), resolve: unavailable('dns.resolve'), promises: { lookup: async (host: string) => ({ address: host === 'localhost' ? '127.0.0.1' : host, family: 4 }) }, setDefaultResultOrder: () => undefined },
|
|
236
250
|
v8: { getHeapStatistics: () => ({ total_heap_size: 30e6, used_heap_size: 20e6, heap_size_limit: 2e9 }), getHeapSpaceStatistics: () => [], serialize: (v: unknown) => new TextEncoder().encode(JSON.stringify(v)), deserialize: (b: Uint8Array) => JSON.parse(new TextDecoder().decode(b)), setFlagsFromString: () => undefined, cachedDataVersionTag: () => 0 },
|
|
237
251
|
cluster: { isMaster: true, isPrimary: true, isWorker: false, workers: {}, fork: unavailable('cluster.fork'), on: () => undefined, settings: {} },
|
|
238
252
|
inspector: { open: () => undefined, close: () => undefined, url: () => undefined, Session: class { connect() {} post() {} disconnect() {} }, console: globalThis.console },
|
package/src/node/process.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* `process.platform`, `process.versions.node` and `process.stdout.isTTY`.
|
|
6
6
|
*/
|
|
7
7
|
import { EventEmitterBase } from './events.js';
|
|
8
|
+
import { Buffer } from './buffer.js';
|
|
8
9
|
import { Readable } from './stream/readable.js';
|
|
9
10
|
import { Writable } from './stream/writable.js';
|
|
10
11
|
import type { EventLoop } from './loop.js';
|
|
@@ -13,13 +14,11 @@ import type { VirtualFileSystem } from '../vfs.js';
|
|
|
13
14
|
import { join, normalizePath } from '../paths.js';
|
|
14
15
|
import { fsError } from './fs/stats.js';
|
|
15
16
|
import { format } from './inspect.js';
|
|
17
|
+
import { ProcessExit, queueCallback } from './processExit.js';
|
|
16
18
|
|
|
17
19
|
export const NODE_VERSION = '20.18.0';
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
export class ProcessExit {
|
|
21
|
-
constructor(readonly code: number) {}
|
|
22
|
-
}
|
|
21
|
+
export { ProcessExit };
|
|
23
22
|
|
|
24
23
|
let nextPid = 100;
|
|
25
24
|
|
|
@@ -64,7 +63,7 @@ class TtyWriteStream extends Writable {
|
|
|
64
63
|
override write(chunk: unknown, encoding?: any, callback?: any): boolean {
|
|
65
64
|
if (typeof encoding === 'function') callback = encoding;
|
|
66
65
|
this.sink(typeof chunk === 'string' || chunk instanceof Uint8Array ? chunk : String(chunk));
|
|
67
|
-
if (typeof callback === 'function')
|
|
66
|
+
if (typeof callback === 'function') queueCallback(callback);
|
|
68
67
|
return true;
|
|
69
68
|
}
|
|
70
69
|
|
|
@@ -106,8 +105,9 @@ class StdinStream extends Readable {
|
|
|
106
105
|
private subscription?: () => void;
|
|
107
106
|
private release?: () => void;
|
|
108
107
|
|
|
108
|
+
// Buffers, as in Node, unless the program calls setEncoding: a binary protocol on stdin must arrive intact.
|
|
109
109
|
constructor(private readonly io: ProgramIO, private readonly loop: EventLoop) {
|
|
110
|
-
super(
|
|
110
|
+
super();
|
|
111
111
|
this.isTTY = !!io.terminal;
|
|
112
112
|
}
|
|
113
113
|
|
|
@@ -149,7 +149,7 @@ class StdinStream extends Readable {
|
|
|
149
149
|
if (chunk === null) {
|
|
150
150
|
this.stopListening();
|
|
151
151
|
this.push(null);
|
|
152
|
-
} else this.push(chunk);
|
|
152
|
+
} else this.push(Buffer.from(typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk));
|
|
153
153
|
});
|
|
154
154
|
}
|
|
155
155
|
|
|
@@ -167,11 +167,15 @@ export function createProcess(options: ProcessOptions): NodeProcess {
|
|
|
167
167
|
const { io, loop, fs } = options;
|
|
168
168
|
let cwd = normalizePath(options.cwd);
|
|
169
169
|
let exited = false;
|
|
170
|
+
let gone = false;
|
|
170
171
|
const started = performance.now();
|
|
171
172
|
const proc = new EventEmitterBase() as NodeProcess;
|
|
172
173
|
const pid = nextPid++;
|
|
173
174
|
|
|
174
175
|
const exit = (code?: number) => {
|
|
176
|
+
// Already gone: a straggling callback (a host promise settling late) has no stack
|
|
177
|
+
// left to unwind, and throwing would surface as the HOST's unhandled rejection.
|
|
178
|
+
if (gone) return undefined as never;
|
|
175
179
|
if (code !== undefined) proc.exitCode = code;
|
|
176
180
|
if (!exited) {
|
|
177
181
|
exited = true;
|
|
@@ -181,6 +185,7 @@ export function createProcess(options: ProcessOptions): NodeProcess {
|
|
|
181
185
|
} catch {
|
|
182
186
|
// an 'exit' listener that throws must not stop the exit
|
|
183
187
|
}
|
|
188
|
+
gone = true;
|
|
184
189
|
options.onExit(Number(proc.exitCode ?? final) || 0);
|
|
185
190
|
}
|
|
186
191
|
throw new ProcessExit(Number(proc.exitCode ?? 0) || 0);
|
|
@@ -208,7 +213,9 @@ export function createProcess(options: ProcessOptions): NodeProcess {
|
|
|
208
213
|
Object.assign(proc, {
|
|
209
214
|
title: 'node',
|
|
210
215
|
version: `v${NODE_VERSION}`,
|
|
211
|
-
|
|
216
|
+
// `webcontainer` is the ecosystem's in-browser-runtime marker: tools that check it
|
|
217
|
+
// pick their WebAssembly builds over native binaries (Next.js loads @next/swc-wasm-nodejs).
|
|
218
|
+
versions: { node: NODE_VERSION, v8: '11.3.244.8-node.23', uv: '1.48.0', zlib: '1.3.0.1', modules: '115', napi: '9', unicode: '15.1', webcontainer: NODE_VERSION },
|
|
212
219
|
release: { name: 'node', lts: 'Iron' },
|
|
213
220
|
platform: 'linux',
|
|
214
221
|
arch: 'x64',
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `process.exit()` unwinds user code by throwing {@link ProcessExit}. Callbacks a
|
|
3
|
+
* builtin queues into user code go through {@link queueCallback}, so an exit inside
|
|
4
|
+
* one ends there instead of escaping to whatever host runs the microtask.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Thrown by `process.exit()` to unwind user code; the runtime catches it. */
|
|
8
|
+
export class ProcessExit {
|
|
9
|
+
constructor(readonly code: number) {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** `queueMicrotask` for a builtin's callback into user code (a stream tick, an fs callback). */
|
|
13
|
+
export function queueCallback(fn: () => void): void {
|
|
14
|
+
queueMicrotask(() => {
|
|
15
|
+
try {
|
|
16
|
+
fn();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (!(error instanceof ProcessExit)) throw error;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
package/src/node/program.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { startNode, type NodeInstance } from './runtime.js';
|
|
|
9
9
|
import { NODE_VERSION } from './process.js';
|
|
10
10
|
import { inspect } from './inspect.js';
|
|
11
11
|
import { parseDotenv } from '../projectConfig.js';
|
|
12
|
+
import { textChunks } from '../system/terminal.js';
|
|
12
13
|
|
|
13
14
|
interface NodeCli {
|
|
14
15
|
entry?: string;
|
|
@@ -66,12 +67,14 @@ Options: -v, -r <module>, --env-file=<file>. Running in BuilderForce WebContaine
|
|
|
66
67
|
async function readAllStdin(ctx: ProgramContext): Promise<string> {
|
|
67
68
|
let text = '';
|
|
68
69
|
await new Promise<void>((resolve) => {
|
|
69
|
-
const stop = ctx.io.onStdin(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
const stop = ctx.io.onStdin(
|
|
71
|
+
textChunks((chunk) => {
|
|
72
|
+
if (chunk === null) {
|
|
73
|
+
stop();
|
|
74
|
+
resolve();
|
|
75
|
+
} else text += chunk;
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
75
78
|
});
|
|
76
79
|
return text;
|
|
77
80
|
}
|
|
@@ -88,7 +91,7 @@ function startRepl(instance: NodeInstance, ctx: ProgramContext): void {
|
|
|
88
91
|
release();
|
|
89
92
|
instance.run(() => instance.process.exit(0));
|
|
90
93
|
};
|
|
91
|
-
const stop = io.onStdin((chunk) => {
|
|
94
|
+
const stop = io.onStdin(textChunks((chunk) => {
|
|
92
95
|
if (chunk === null) return leave();
|
|
93
96
|
for (const raw of chunk.split(/\r?\n/)) {
|
|
94
97
|
const line = raw.trim();
|
|
@@ -99,7 +102,7 @@ function startRepl(instance: NodeInstance, ctx: ProgramContext): void {
|
|
|
99
102
|
else io.stdout(inspect(result.value, { colors: false }) + '\n');
|
|
100
103
|
}
|
|
101
104
|
io.stdout('> ');
|
|
102
|
-
});
|
|
105
|
+
}));
|
|
103
106
|
}
|
|
104
107
|
|
|
105
108
|
/** Evaluate `expression` inside the CommonJS wrapper, so it sees require, process, timers… of this process. */
|
|
@@ -136,7 +139,7 @@ export const nodeProgram: Program = async (ctx) => {
|
|
|
136
139
|
}
|
|
137
140
|
const source = cli.evaluate ?? (cli.entry === '-' || (!entry && !ctx.io.terminal) ? await readAllStdin(ctx) : undefined);
|
|
138
141
|
const scriptPath = entry ?? (source !== undefined ? '[eval]' : undefined);
|
|
139
|
-
const instance = await startNode({ system: ctx.system, io: ctx.io, cwd: ctx.cwd, env, installGlobals: ctx.system.ownsRealm, argv: scriptPath && scriptPath !== '[eval]' ? [scriptPath, ...cli.args] : cli.args });
|
|
142
|
+
const instance = await startNode({ system: ctx.system, io: ctx.io, cwd: ctx.cwd, env, installGlobals: ctx.system.ownsRealm, ipc: ctx.ipc, thread: ctx.thread, argv: scriptPath && scriptPath !== '[eval]' ? [scriptPath, ...cli.args] : cli.args });
|
|
140
143
|
const onAbort = () => instance.kill(typeof ctx.signal.reason === 'string' ? ctx.signal.reason : 'SIGTERM');
|
|
141
144
|
if (ctx.signal.aborted) onAbort();
|
|
142
145
|
ctx.signal.addEventListener('abort', onAbort);
|
package/src/node/runtime.ts
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
* entry and resolves `exit` with the code once the process exits — by
|
|
5
5
|
* `process.exit()`, an uncaught exception, being killed, or the loop going idle.
|
|
6
6
|
*/
|
|
7
|
-
import type { ProgramIO, System } from '../system/types.js';
|
|
7
|
+
import type { ProgramIO, System, ThreadInfo } from '../system/types.js';
|
|
8
|
+
import type { IpcEndpoint } from '../system/ipc.js';
|
|
9
|
+
import { attachProcessChannel } from './ipc.js';
|
|
10
|
+
import { threadErrorEnvelope } from './workerThreads.js';
|
|
8
11
|
import { Buffer } from './buffer.js';
|
|
9
12
|
import { createBuiltins } from './builtins.js';
|
|
10
13
|
import { createModuleSystem, type ModuleSystem } from './loader.js';
|
|
@@ -12,6 +15,7 @@ import { EventLoop } from './loop.js';
|
|
|
12
15
|
import { createConsole, createProcess, ProcessExit, type NodeProcess } from './process.js';
|
|
13
16
|
import { esmReady } from './esm.js';
|
|
14
17
|
import { inspect } from './inspect.js';
|
|
18
|
+
import { installGlobalObject } from './globalObject.js';
|
|
15
19
|
|
|
16
20
|
export interface NodeOptions {
|
|
17
21
|
system: System;
|
|
@@ -27,6 +31,10 @@ export interface NodeOptions {
|
|
|
27
31
|
* share one realm.
|
|
28
32
|
*/
|
|
29
33
|
installGlobals?: boolean;
|
|
34
|
+
/** The channel to the parent: `process.send` in a forked child, `parentPort` in a worker thread. */
|
|
35
|
+
ipc?: IpcEndpoint;
|
|
36
|
+
/** Set when this process is a worker thread. */
|
|
37
|
+
thread?: ThreadInfo;
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
export interface NodeInstance {
|
|
@@ -48,7 +56,14 @@ const SIGNAL_CODES: Record<string, number> = { SIGINT: 130, SIGTERM: 143, SIGKIL
|
|
|
48
56
|
|
|
49
57
|
export async function startNode(options: NodeOptions): Promise<NodeInstance> {
|
|
50
58
|
await esmReady;
|
|
51
|
-
const { system
|
|
59
|
+
const { system } = options;
|
|
60
|
+
// Once the process has finished it prints nothing — a host promise settling late
|
|
61
|
+
// must not write past the exit the parent already saw.
|
|
62
|
+
const io: ProgramIO = {
|
|
63
|
+
...options.io,
|
|
64
|
+
stdout: (chunk) => finished || options.io.stdout(chunk),
|
|
65
|
+
stderr: (chunk) => finished || options.io.stderr(chunk),
|
|
66
|
+
};
|
|
52
67
|
let resolveExit!: (code: number) => void;
|
|
53
68
|
const exit = new Promise<number>((resolve) => (resolveExit = resolve));
|
|
54
69
|
let finished = false;
|
|
@@ -89,8 +104,12 @@ export async function startNode(options: NodeOptions): Promise<NodeInstance> {
|
|
|
89
104
|
error = nested;
|
|
90
105
|
}
|
|
91
106
|
}
|
|
92
|
-
|
|
93
|
-
|
|
107
|
+
// A worker thread's fatal error is the parent's 'error' event, not output.
|
|
108
|
+
if (options.thread && options.ipc?.connected) options.ipc.send(threadErrorEnvelope(error));
|
|
109
|
+
else {
|
|
110
|
+
const text = error instanceof Error ? error.stack ?? `${error.name}: ${error.message}` : `Uncaught ${inspect(error)}`;
|
|
111
|
+
io.stderr(`${text}\n\nNode.js v${proc.versions.node}\n`);
|
|
112
|
+
}
|
|
94
113
|
exitQuietly(proc.exitCode && proc.exitCode !== 0 ? proc.exitCode : 1);
|
|
95
114
|
};
|
|
96
115
|
|
|
@@ -125,8 +144,9 @@ export async function startNode(options: NodeOptions): Promise<NodeInstance> {
|
|
|
125
144
|
loop,
|
|
126
145
|
onExit: finish,
|
|
127
146
|
});
|
|
147
|
+
if (options.ipc && !options.thread) attachProcessChannel(proc, options.ipc, loop);
|
|
128
148
|
const console = createConsole(proc);
|
|
129
|
-
const builtins = createBuiltins({ fs: system.fs, process: proc, loop, system: processSystem, io, console });
|
|
149
|
+
const builtins = createBuiltins({ fs: system.fs, process: proc, loop, system: processSystem, io, console, thread: options.thread, ipc: options.ipc });
|
|
130
150
|
|
|
131
151
|
const globals: Record<string, unknown> = {
|
|
132
152
|
process: proc,
|
|
@@ -140,17 +160,9 @@ export async function startNode(options: NodeOptions): Promise<NodeInstance> {
|
|
|
140
160
|
clearImmediate: loop.clearImmediate,
|
|
141
161
|
fetch: builtins.fetch,
|
|
142
162
|
};
|
|
143
|
-
|
|
144
|
-
Object.assign(globalThis, globals, { global: globalThis });
|
|
145
|
-
globals.global = globalThis;
|
|
146
|
-
} else {
|
|
147
|
-
// A per-process view of the global object: reads fall through to the real
|
|
148
|
-
// one, Node's own names resolve to this process's.
|
|
149
|
-
globals.global = Object.assign(Object.create(globalThis), globals);
|
|
150
|
-
}
|
|
151
|
-
globals.globalThis = globals.global;
|
|
163
|
+
const scope = installGlobalObject(globals, !!options.installGlobals);
|
|
152
164
|
|
|
153
|
-
const modules = createModuleSystem({ fs: system.fs, isBuiltin: builtins.isBuiltin, builtin: builtins.get, globals });
|
|
165
|
+
const modules = createModuleSystem({ fs: system.fs, isBuiltin: builtins.isBuiltin, builtin: builtins.get, globals, scope });
|
|
154
166
|
(modules.Module as unknown as { builtinModules: string[] }).builtinModules = builtins.names;
|
|
155
167
|
Object.defineProperty(proc, 'mainModule', { get: () => modules.main, configurable: true });
|
|
156
168
|
|
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { EventEmitterBase } from '../events.js';
|
|
8
8
|
import { Buffer } from '../buffer.js';
|
|
9
|
+
import { queueCallback } from '../processExit.js';
|
|
9
10
|
|
|
10
|
-
export const tick =
|
|
11
|
+
export const tick = queueCallback;
|
|
11
12
|
|
|
12
13
|
export interface ReadableOptions {
|
|
13
14
|
highWaterMark?: number;
|
package/src/node/urlqs.ts
CHANGED
|
@@ -62,7 +62,7 @@ export interface LegacyUrl {
|
|
|
62
62
|
href: string;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export function createUrlModule(qs: Record<string, any
|
|
65
|
+
export function createUrlModule(qs: Record<string, any>, resolvePath: (path: string) => string): Record<string, unknown> {
|
|
66
66
|
function parse(input: string, parseQueryString = false, slashesDenoteHost = false): LegacyUrl {
|
|
67
67
|
let rest = input.trim();
|
|
68
68
|
const out: LegacyUrl = { protocol: null, slashes: null, auth: null, host: null, port: null, hostname: null, hash: null, search: null, query: null, pathname: null, path: null, href: '' };
|
|
@@ -153,7 +153,15 @@ export function createUrlModule(qs: Record<string, any>): Record<string, unknown
|
|
|
153
153
|
if (u.protocol !== 'file:') throw Object.assign(new TypeError('The URL must be of scheme file'), { code: 'ERR_INVALID_URL_SCHEME' });
|
|
154
154
|
return decodeURIComponent(u.pathname);
|
|
155
155
|
},
|
|
156
|
-
|
|
156
|
+
// As Node: resolved against the working directory (a relative path never becomes the
|
|
157
|
+
// URL's host), a trailing slash kept, and the characters a pathname would misread escaped.
|
|
158
|
+
pathToFileURL: (path: string) => {
|
|
159
|
+
let resolved = resolvePath(path);
|
|
160
|
+
if (path.endsWith('/') && !resolved.endsWith('/')) resolved += '/';
|
|
161
|
+
const url = new URL('file://');
|
|
162
|
+
url.pathname = resolved.replace(/%/g, '%25').replace(/\\/g, '%5C').replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/\t/g, '%09');
|
|
163
|
+
return url;
|
|
164
|
+
},
|
|
157
165
|
domainToASCII: (d: string) => d,
|
|
158
166
|
domainToUnicode: (d: string) => d,
|
|
159
167
|
urlToHttpOptions: (u: URL) => ({ protocol: u.protocol, hostname: u.hostname, hash: u.hash, search: u.search, pathname: u.pathname, path: u.pathname + u.search, href: u.href, port: u.port ? Number(u.port) : undefined }),
|