@seanhogg/builderforce-webcontainers-core 2026.10.7 → 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/archive.d.ts +2 -0
- package/dist/archive.d.ts.map +1 -1
- package/dist/archive.js +6 -4
- package/dist/archive.js.map +1 -1
- 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 +4 -8
- package/dist/html.d.ts.map +1 -1
- package/dist/html.js +15 -19
- package/dist/html.js.map +1 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -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/client.d.ts +3 -1
- package/dist/node/http/client.d.ts.map +1 -1
- package/dist/node/http/client.js +74 -5
- package/dist/node/http/client.js.map +1 -1
- package/dist/node/http/index.d.ts.map +1 -1
- package/dist/node/http/index.js +6 -4
- package/dist/node/http/index.js.map +1 -1
- package/dist/node/http/messages.d.ts +87 -0
- package/dist/node/http/messages.d.ts.map +1 -0
- package/dist/node/http/messages.js +146 -0
- package/dist/node/http/messages.js.map +1 -0
- package/dist/node/http/server.d.ts +13 -98
- package/dist/node/http/server.d.ts.map +1 -1
- package/dist/node/http/server.js +41 -206
- package/dist/node/http/server.js.map +1 -1
- package/dist/node/http/serverResponse.d.ts +45 -0
- package/dist/node/http/serverResponse.d.ts.map +1 -0
- package/dist/node/http/serverResponse.js +96 -0
- package/dist/node/http/serverResponse.js.map +1 -0
- package/dist/node/http/upgradeSocket.d.ts +34 -0
- package/dist/node/http/upgradeSocket.d.ts.map +1 -0
- package/dist/node/http/upgradeSocket.js +73 -0
- package/dist/node/http/upgradeSocket.js.map +1 -0
- 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 +25 -17
- 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/previewBridge.d.ts +42 -0
- package/dist/previewBridge.d.ts.map +1 -0
- package/dist/previewBridge.js +192 -0
- package/dist/previewBridge.js.map +1 -0
- package/dist/shell/ast.d.ts +101 -0
- package/dist/shell/ast.d.ts.map +1 -0
- package/dist/shell/ast.js +7 -0
- package/dist/shell/ast.js.map +1 -0
- package/dist/shell/builtins.d.ts +14 -0
- package/dist/shell/builtins.d.ts.map +1 -1
- package/dist/shell/builtins.js +87 -7
- package/dist/shell/builtins.js.map +1 -1
- package/dist/shell/compound.d.ts +30 -0
- package/dist/shell/compound.d.ts.map +1 -0
- package/dist/shell/compound.js +73 -0
- package/dist/shell/compound.js.map +1 -0
- 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 +27 -6
- package/dist/shell/exec.d.ts.map +1 -1
- package/dist/shell/exec.js +133 -46
- package/dist/shell/exec.js.map +1 -1
- package/dist/shell/expand.d.ts +5 -1
- package/dist/shell/expand.d.ts.map +1 -1
- package/dist/shell/expand.js +7 -5
- package/dist/shell/expand.js.map +1 -1
- package/dist/shell/lineReader.d.ts +9 -0
- package/dist/shell/lineReader.d.ts.map +1 -0
- package/dist/shell/lineReader.js +42 -0
- package/dist/shell/lineReader.js.map +1 -0
- package/dist/shell/npm.js +2 -2
- package/dist/shell/npm.js.map +1 -1
- package/dist/shell/packageManagers.d.ts +20 -0
- package/dist/shell/packageManagers.d.ts.map +1 -0
- package/dist/shell/packageManagers.js +74 -0
- package/dist/shell/packageManagers.js.map +1 -0
- package/dist/shell/parse.d.ts +16 -66
- package/dist/shell/parse.d.ts.map +1 -1
- package/dist/shell/parse.js +244 -243
- package/dist/shell/parse.js.map +1 -1
- package/dist/shell/program.d.ts.map +1 -1
- package/dist/shell/program.js +7 -6
- package/dist/shell/program.js.map +1 -1
- package/dist/shell/testCommand.d.ts +9 -0
- package/dist/shell/testCommand.d.ts.map +1 -0
- package/dist/shell/testCommand.js +105 -0
- package/dist/shell/testCommand.js.map +1 -0
- package/dist/shell/tokenize.d.ts +25 -0
- package/dist/shell/tokenize.d.ts.map +1 -0
- package/dist/shell/tokenize.js +279 -0
- package/dist/shell/tokenize.js.map +1 -0
- 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/cookieJar.d.ts +59 -0
- package/dist/system/cookieJar.d.ts.map +1 -0
- package/dist/system/cookieJar.js +187 -0
- package/dist/system/cookieJar.js.map +1 -0
- package/dist/system/httpStream.d.ts +40 -0
- package/dist/system/httpStream.d.ts.map +1 -0
- package/dist/system/httpStream.js +156 -0
- package/dist/system/httpStream.js.map +1 -0
- package/dist/system/index.d.ts +11 -1
- package/dist/system/index.d.ts.map +1 -1
- package/dist/system/index.js +5 -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 +10 -2
- package/dist/system/kernel.d.ts.map +1 -1
- package/dist/system/kernel.js +18 -6
- package/dist/system/kernel.js.map +1 -1
- package/dist/system/ports.d.ts +9 -4
- package/dist/system/ports.d.ts.map +1 -1
- package/dist/system/ports.js +21 -2
- package/dist/system/ports.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/programs.d.ts +1 -1
- package/dist/system/programs.d.ts.map +1 -1
- package/dist/system/programs.js +2 -0
- package/dist/system/programs.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 +65 -13
- package/dist/system/types.d.ts.map +1 -1
- package/dist/system/webSocketClient.d.ts +37 -0
- package/dist/system/webSocketClient.d.ts.map +1 -0
- package/dist/system/webSocketClient.js +152 -0
- package/dist/system/webSocketClient.js.map +1 -0
- package/dist/system/webSocketFrames.d.ts +39 -0
- package/dist/system/webSocketFrames.d.ts.map +1 -0
- package/dist/system/webSocketFrames.js +117 -0
- package/dist/system/webSocketFrames.js.map +1 -0
- 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/archive.ts +8 -4
- 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 +17 -21
- package/src/index.ts +20 -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/client.ts +69 -6
- package/src/node/http/index.ts +6 -4
- package/src/node/http/messages.ts +150 -0
- package/src/node/http/server.ts +44 -206
- package/src/node/http/serverResponse.ts +105 -0
- package/src/node/http/upgradeSocket.ts +79 -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 +29 -17
- 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/previewBridge.ts +194 -0
- package/src/shell/ast.ts +85 -0
- package/src/shell/builtins.ts +97 -7
- package/src/shell/compound.ts +89 -0
- package/src/shell/context.ts +2 -2
- package/src/shell/devHandoff.ts +16 -9
- package/src/shell/exec.ts +134 -45
- package/src/shell/expand.ts +8 -6
- package/src/shell/lineReader.ts +46 -0
- package/src/shell/npm.ts +2 -2
- package/src/shell/packageManagers.ts +75 -0
- package/src/shell/parse.ts +233 -236
- package/src/shell/program.ts +7 -6
- package/src/shell/testCommand.ts +105 -0
- package/src/shell/tokenize.ts +257 -0
- 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/cookieJar.ts +187 -0
- package/src/system/httpStream.ts +168 -0
- package/src/system/index.ts +28 -1
- package/src/system/ipc.ts +89 -0
- package/src/system/kernel.ts +22 -7
- package/src/system/ports.ts +23 -4
- package/src/system/process.ts +57 -30
- package/src/system/programs.ts +3 -1
- package/src/system/terminal.ts +34 -12
- package/src/system/types.ts +71 -13
- package/src/system/webSocketClient.ts +177 -0
- package/src/system/webSocketFrames.ts +118 -0
- package/src/vueCompiler.ts +11 -8
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ModuleGraph } from './moduleGraph.js';
|
|
2
|
+
import { isCssModule, isStylesheet } from '../styles/languages.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Decide what an edit means for the open preview, from the module graph:
|
|
6
|
+
*
|
|
7
|
+
* • a plain stylesheet (or a partial one pulls in) is swapped in place;
|
|
8
|
+
* • a module is re-run at the nearest refresh BOUNDARIES above it — the
|
|
9
|
+
* changed module itself when it only exports components, else the modules
|
|
10
|
+
* importing it, and so on — when React Refresh is on;
|
|
11
|
+
* • reaching a module nothing imports (the entry) without a boundary, or a
|
|
12
|
+
* component whose hooks changed, means a full reload;
|
|
13
|
+
* • a file the page never loaded changes nothing.
|
|
14
|
+
*
|
|
15
|
+
* `touched` is every module the update re-runs or passes through: each needs a
|
|
16
|
+
* fresh stamp, so the re-run imports fresh copies all the way down.
|
|
17
|
+
*/
|
|
18
|
+
export type UpdatePlan = { reload: true } | { reload: false; styles: string[]; boundaries: string[]; touched: string[] };
|
|
19
|
+
|
|
20
|
+
export interface PlanInput {
|
|
21
|
+
changed: readonly string[];
|
|
22
|
+
graph: ModuleGraph;
|
|
23
|
+
/** React Refresh is available in this preview. */
|
|
24
|
+
refresh: boolean;
|
|
25
|
+
/** The module's hook calls differ from the ones the preview is running. */
|
|
26
|
+
hooksChanged(path: string): boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A stylesheet whose edit is only a style swap: not a CSS Module, whose class map is JS. */
|
|
30
|
+
const isPlainStylesheet = (path: string) => isStylesheet(path) && !isCssModule(path);
|
|
31
|
+
|
|
32
|
+
export function planUpdate({ changed, graph, refresh, hooksChanged }: PlanInput): UpdatePlan {
|
|
33
|
+
const styles = new Set<string>();
|
|
34
|
+
const boundaries = new Set<string>();
|
|
35
|
+
const touched = new Set<string>();
|
|
36
|
+
const seen = new Set<string>();
|
|
37
|
+
|
|
38
|
+
for (const start of changed) {
|
|
39
|
+
if (!graph.isServed(start) && graph.importers(start).size === 0) continue;
|
|
40
|
+
const queue = [start];
|
|
41
|
+
while (queue.length) {
|
|
42
|
+
const path = queue.shift()!;
|
|
43
|
+
if (seen.has(path)) continue;
|
|
44
|
+
seen.add(path);
|
|
45
|
+
touched.add(path);
|
|
46
|
+
|
|
47
|
+
if (isPlainStylesheet(path)) {
|
|
48
|
+
if (graph.isServed(path)) styles.add(path);
|
|
49
|
+
// A partial's edit reaches the stylesheets that pulled it in — never the JS
|
|
50
|
+
// importing a stylesheet, which a swap leaves untouched.
|
|
51
|
+
for (const importer of graph.importers(path)) if (isStylesheet(importer)) queue.push(importer);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (refresh && hooksChanged(path)) return { reload: true };
|
|
56
|
+
if (refresh && graph.factsOf(path)?.refreshBoundary) {
|
|
57
|
+
boundaries.add(path);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const importers = graph.importers(path);
|
|
61
|
+
if (importers.size === 0) return { reload: true };
|
|
62
|
+
queue.push(...importers);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { reload: false, styles: [...styles], boundaries: [...boundaries], touched: [...touched] };
|
|
66
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { init, parse } from 'es-module-lexer';
|
|
2
|
+
import { contentHash } from '../hash.js';
|
|
3
|
+
import type { ProjectConfig } from '../projectConfig.js';
|
|
4
|
+
import type { ModuleFacts } from './moduleGraph.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* React Fast Refresh without Babel. The official runtime (`react-refresh/runtime`)
|
|
8
|
+
* does the hard part — swapping a component's implementation while React keeps
|
|
9
|
+
* its state — and only needs two things from the compiler:
|
|
10
|
+
*
|
|
11
|
+
* • each component REGISTERED under a stable id when its module runs, which
|
|
12
|
+
* the dev server appends to compiled modules (`refreshFooter`);
|
|
13
|
+
* • to know when a component's hooks changed, because state cannot carry
|
|
14
|
+
* across a different hook sequence. Babel's plugin signs each component; here
|
|
15
|
+
* the module's hook calls are signed as a whole (`hookSignature`), and a
|
|
16
|
+
* change makes the update a full reload rather than a refresh.
|
|
17
|
+
*
|
|
18
|
+
* A module is a refresh BOUNDARY when every export is a registered component:
|
|
19
|
+
* re-running it is all an update needs. Edits to anything else propagate to the
|
|
20
|
+
* modules importing it until they reach boundaries, or the entry (a reload).
|
|
21
|
+
*/
|
|
22
|
+
export const REFRESH_GLOBAL = '__bfwc_refresh';
|
|
23
|
+
|
|
24
|
+
/** React with a DOM renderer and the automatic or classic React JSX runtime. */
|
|
25
|
+
export function usesReactRefresh(config: ProjectConfig): boolean {
|
|
26
|
+
return 'react-dom' in config.dependencies && config.jsx.importSource === 'react';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Inline module script, placed before the app's: installs the runtime into React's
|
|
31
|
+
* DevTools hook, which react-dom reads once, when it first loads — so it must
|
|
32
|
+
* run first. Module scripts run in document order, which guarantees that.
|
|
33
|
+
*/
|
|
34
|
+
export function refreshPreambleScript(runtimeUrl: string): string {
|
|
35
|
+
return [
|
|
36
|
+
`<script type="module">`,
|
|
37
|
+
`import * as runtime from ${JSON.stringify(runtimeUrl)};`,
|
|
38
|
+
`const refresh = runtime.default || runtime;`,
|
|
39
|
+
`refresh.injectIntoGlobalHook(window);`,
|
|
40
|
+
`window.$RefreshReg$ = () => {};`,
|
|
41
|
+
`window.$RefreshSig$ = () => (type) => type;`,
|
|
42
|
+
`window.${REFRESH_GLOBAL} = refresh;`,
|
|
43
|
+
`</script>`,
|
|
44
|
+
].join('\n');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Top-level bindings in compiled code that look like components: PascalCase
|
|
49
|
+
* function and class declarations, and PascalCase consts bound to a function,
|
|
50
|
+
* arrow, `memo(…)` or `forwardRef(…)`. Compiled output puts top-level statements
|
|
51
|
+
* at column 0, which keeps this to line-anchored patterns.
|
|
52
|
+
*/
|
|
53
|
+
export function componentNames(code: string): string[] {
|
|
54
|
+
const names = new Set<string>();
|
|
55
|
+
const declaration = /^(?:export\s+(?:default\s+)?)?(?:async\s+)?(?:function\*?|class)\s+([A-Z][\w$]*)/gm;
|
|
56
|
+
const binding = /^(?:export\s+)?(?:const|let|var)\s+([A-Z][\w$]*)\s*(?::[^=]+)?=\s*(?:async\s+)?(?:function\b|\(|[\w$]+\s*=>|(?:[\w$]+\.)?(?:memo|forwardRef)\s*\()/gm;
|
|
57
|
+
for (const match of code.matchAll(declaration)) names.add(match[1]!);
|
|
58
|
+
for (const match of code.matchAll(binding)) names.add(match[1]!);
|
|
59
|
+
return [...names];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Appended to a module: register each component under `<path> <name>`. */
|
|
63
|
+
export function refreshFooter(path: string, names: readonly string[]): string {
|
|
64
|
+
if (!names.length) return '';
|
|
65
|
+
const lines = names.map((name) => ` refresh.register(${name}, ${JSON.stringify(`${path} ${name}`)});`);
|
|
66
|
+
return `\n{ const refresh = window.${REFRESH_GLOBAL};\nif (refresh) {\n${lines.join('\n')}\n} }\n`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The module's hook calls in order, hashed. */
|
|
70
|
+
export function hookSignature(source: string): string {
|
|
71
|
+
return contentHash((source.match(/\buse[A-Z][\w$]*\s*\(/g) ?? []).map((call) => call.replace(/\s*\($/, '')).join(','));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* What the dev server learns about a compiled module: whether it is a refresh
|
|
76
|
+
* boundary (it exports something, and every export is one of `components`),
|
|
77
|
+
* and its hook signature (from `source`, the code as written).
|
|
78
|
+
*/
|
|
79
|
+
export async function refreshFacts(code: string, source: string, components: readonly string[]): Promise<ModuleFacts> {
|
|
80
|
+
await init;
|
|
81
|
+
const [, exports] = parse(code);
|
|
82
|
+
const known = new Set(components);
|
|
83
|
+
// The lexer names the local behind most exports; spell out the default forms it may not.
|
|
84
|
+
const defaultExpression =
|
|
85
|
+
/^export\s+default\s+(?:async\s+)?(?:function\*?|class)\s+([A-Z][\w$]*)/m.exec(code)?.[1] ?? /^export\s+default\s+([A-Z][\w$]*)\s*;?\s*$/m.exec(code)?.[1];
|
|
86
|
+
const refreshBoundary =
|
|
87
|
+
exports.length > 0 &&
|
|
88
|
+
exports.every((entry) => {
|
|
89
|
+
const local = entry.ln ?? (entry.n === 'default' ? defaultExpression : entry.n);
|
|
90
|
+
return local !== undefined && known.has(local);
|
|
91
|
+
});
|
|
92
|
+
return { refreshBoundary, hooks: hookSignature(source) };
|
|
93
|
+
}
|
package/src/html.ts
CHANGED
|
@@ -5,11 +5,16 @@
|
|
|
5
5
|
* • CRA's `%PUBLIC_URL%` placeholder is filled;
|
|
6
6
|
* • a page with no module script (CRA) gets its entry injected;
|
|
7
7
|
* • the error bridge is prepended, so runtime errors and console output reach the host;
|
|
8
|
+
* • the WebSocket bridge follows, so `new WebSocket('ws://localhost:3000')` reaches a virtual server;
|
|
8
9
|
* • the host's preview scripts run next, before the app;
|
|
9
|
-
* • the
|
|
10
|
+
* • the hot-update listener is prepended, so an edit swaps styles, re-renders
|
|
11
|
+
* components or reloads — and, for React, the Fast Refresh runtime before the app;
|
|
10
12
|
* • the attribution badge is appended unless the host turned it off.
|
|
11
13
|
*/
|
|
12
14
|
import { injectAttribution } from './attribution.js';
|
|
15
|
+
import { previewSocketScript } from './previewBridge.js';
|
|
16
|
+
import { hotUpdateScript } from './hot/client.js';
|
|
17
|
+
import { refreshPreambleScript } from './hot/reactRefresh.js';
|
|
13
18
|
|
|
14
19
|
export const ERROR_MESSAGE_TYPE = 'bfwc:error';
|
|
15
20
|
export const CONSOLE_MESSAGE_TYPE = 'bfwc:console';
|
|
@@ -50,23 +55,6 @@ export function errorBridgeScript(): string {
|
|
|
50
55
|
})();</script>`;
|
|
51
56
|
}
|
|
52
57
|
|
|
53
|
-
/**
|
|
54
|
-
* The channel a preview listens on for "files changed, reload". Keyed by the
|
|
55
|
-
* preview base, so two runtimes on one origin never reload each other's frames.
|
|
56
|
-
* A BroadcastChannel rather than `postMessage` because the host does not hold a
|
|
57
|
-
* reference to the frame — any number of frames (or tabs) may show one preview.
|
|
58
|
-
*/
|
|
59
|
-
export function reloadChannelName(base: string): string {
|
|
60
|
-
return `bfwc:reload:${base}`;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function liveReloadScript(base: string): string {
|
|
64
|
-
return `<script>(function(){
|
|
65
|
-
if (typeof BroadcastChannel === 'undefined') return;
|
|
66
|
-
new BroadcastChannel(${JSON.stringify(reloadChannelName(base))}).onmessage = function () { location.reload(); };
|
|
67
|
-
})();</script>`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
58
|
const ABSOLUTE_ATTR = /(\s(?:src|href)\s*=\s*)(["'])\/(?!\/)([^"']*)\2/gi;
|
|
71
59
|
const MODULE_SCRIPT = /<script\b[^>]*type\s*=\s*["']module["'][^>]*>/i;
|
|
72
60
|
|
|
@@ -79,10 +67,12 @@ export interface HtmlOptions {
|
|
|
79
67
|
attribution?: boolean;
|
|
80
68
|
/** Host scripts (JavaScript source) run in every preview page before the app. */
|
|
81
69
|
scripts?: readonly string[];
|
|
70
|
+
/** URL of `react-refresh/runtime`; installed before the app so components re-render in place on edits. */
|
|
71
|
+
refreshRuntime?: string;
|
|
82
72
|
}
|
|
83
73
|
|
|
84
|
-
/** `</script>` inside
|
|
85
|
-
const scriptTag = (source: string) => `<script>${source.replace(/<\/script/gi, '<\\/script')}</script>`;
|
|
74
|
+
/** A host script as a tag. `</script>` inside the body would end the tag early, so it is escaped. */
|
|
75
|
+
export const scriptTag = (source: string): string => `<script>${source.replace(/<\/script/gi, '<\\/script')}</script>`;
|
|
86
76
|
|
|
87
77
|
export function transformHtml(html: string, options: HtmlOptions): string {
|
|
88
78
|
const { base } = options;
|
|
@@ -97,7 +87,13 @@ export function transformHtml(html: string, options: HtmlOptions): string {
|
|
|
97
87
|
}
|
|
98
88
|
if (options.attribution !== false) out = injectAttribution(out);
|
|
99
89
|
|
|
100
|
-
const bridge = [
|
|
90
|
+
const bridge = [
|
|
91
|
+
errorBridgeScript(),
|
|
92
|
+
previewSocketScript(),
|
|
93
|
+
hotUpdateScript(base),
|
|
94
|
+
...(options.scripts ?? []).map(scriptTag),
|
|
95
|
+
...(options.refreshRuntime ? [refreshPreambleScript(options.refreshRuntime)] : []),
|
|
96
|
+
].join('\n');
|
|
101
97
|
if (/<head[^>]*>/i.test(out)) return out.replace(/<head[^>]*>/i, (head) => `${head}\n${bridge}`);
|
|
102
98
|
return bridge + out;
|
|
103
99
|
}
|
package/src/index.ts
CHANGED
|
@@ -44,7 +44,26 @@ export { rewriteImports } from './rewriteImports.js';
|
|
|
44
44
|
export { resolveLocal, resolveAlias, isBareSpecifier, splitPackageSpecifier, resolvePackage, RESOLVE_EXTENSIONS } from './resolve.js';
|
|
45
45
|
export { compileScript, compileModule, isScriptPath, mapSpecifier, moduleUrl, buildDefine } from './compileScript.js';
|
|
46
46
|
export { compileComponent } from './compileComponent.js';
|
|
47
|
-
export { transformHtml,
|
|
47
|
+
export { transformHtml, scriptTag, errorBridgeScript, ERROR_MESSAGE_TYPE, CONSOLE_MESSAGE_TYPE, CONSOLE_MESSAGE_LIMIT } from './html.js';
|
|
48
|
+
export { hotUpdateScript, updateChannelName } from './hot/client.js';
|
|
49
|
+
export type { PreviewUpdate, HotUpdate, HotStyle } from './hot/client.js';
|
|
50
|
+
export { ModuleGraph } from './hot/moduleGraph.js';
|
|
51
|
+
export type { ModuleFacts } from './hot/moduleGraph.js';
|
|
52
|
+
export { planUpdate } from './hot/planUpdate.js';
|
|
53
|
+
export type { UpdatePlan, PlanInput } from './hot/planUpdate.js';
|
|
54
|
+
export { usesReactRefresh, refreshPreambleScript, componentNames, refreshFooter, hookSignature, refreshFacts, REFRESH_GLOBAL } from './hot/reactRefresh.js';
|
|
55
|
+
export { styleLanguageOf, isStylesheet, isCssModule, styleLanguageOfLang } from './styles/languages.js';
|
|
56
|
+
export type { StyleLanguage } from './styles/languages.js';
|
|
57
|
+
export { createSassCompiler, createLessCompiler, staticStyleCompilers, noStyleCompilers, sassCandidates, lessCandidates } from './styles/compilers.js';
|
|
58
|
+
export type { StyleCompiler, StyleCompilers, StyleCompileRequest, CompiledStyle, PreprocessedLanguage, SassLike, LessLike } from './styles/compilers.js';
|
|
59
|
+
export { createStyleImportHost, jsDelivrFileUrl } from './styles/imports.js';
|
|
60
|
+
export type { StyleImportHost, StyleImportHostOptions, StyleSource } from './styles/imports.js';
|
|
61
|
+
export { compileStylesheet, preprocessStyleBlock } from './styles/compile.js';
|
|
62
|
+
export type { StylesheetContext } from './styles/compile.js';
|
|
63
|
+
export { scopeCssModule, cssModuleToModule } from './styles/cssModules.js';
|
|
64
|
+
export type { ScopedCssModule, ComposedClass } from './styles/cssModules.js';
|
|
65
|
+
export { PACKAGE_STYLE_DIR, packageStylePath, packageStyleSpecifier, linkStylesheetModule } from './styles/packageStyles.js';
|
|
66
|
+
export { previewSocketScript, previewCookieScript, bridgeInsertionPoint, SOCKET_MESSAGE_TYPE, COOKIE_MESSAGE_TYPE, SOCKET_CONNECT_TIMEOUT_MS } from './previewBridge.js';
|
|
48
67
|
export { mimeFor } from './mime.js';
|
|
49
68
|
export { attributionScript, injectAttribution, ATTRIBUTION_URL, ATTRIBUTION_LABEL } from './attribution.js';
|
|
50
69
|
export type { AttributionOptions } from './attribution.js';
|
package/src/installer/tree.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import type { PackageManifest } from './registry.js';
|
|
14
14
|
import { parseDependency, type PackageJson, type ParsedSpec } from './spec.js';
|
|
15
15
|
import { satisfies, validRange } from './semver.js';
|
|
16
|
+
import { napiWasmSibling, wasmBuildOf } from './wasmBuilds.js';
|
|
16
17
|
|
|
17
18
|
export type EdgeType = 'prod' | 'dev' | 'optional' | 'peer';
|
|
18
19
|
|
|
@@ -31,6 +32,8 @@ interface Dependency {
|
|
|
31
32
|
name: string;
|
|
32
33
|
range: string;
|
|
33
34
|
type: EdgeType;
|
|
35
|
+
/** Added by this installer (a napi-rs wasm binding), not declared: missing is silent. */
|
|
36
|
+
implied?: boolean;
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
interface Edge extends Dependency {
|
|
@@ -90,12 +93,14 @@ function bundledNames(m: { bundleDependencies?: string[] | boolean; bundledDepen
|
|
|
90
93
|
|
|
91
94
|
function dependenciesOf(m: PackageJson | PackageManifest, isRoot: boolean, production: boolean): Dependency[] {
|
|
92
95
|
const out = new Map<string, Dependency>();
|
|
93
|
-
const add = (record: Record<string, string> | undefined, type: EdgeType) => {
|
|
94
|
-
for (const [name, range] of Object.entries(record ?? {})) if (!out.has(name) || type === 'optional') out.set(name, { name, range, type });
|
|
96
|
+
const add = (record: Record<string, string> | undefined, type: EdgeType, implied?: boolean) => {
|
|
97
|
+
for (const [name, range] of Object.entries(record ?? {})) if (!out.has(name) || type === 'optional') out.set(name, { name, range, type, implied });
|
|
95
98
|
};
|
|
96
99
|
if (isRoot && !production) add((m as PackageJson).devDependencies, 'dev');
|
|
97
100
|
add(m.dependencies, 'prod');
|
|
98
101
|
add(m.optionalDependencies, 'optional');
|
|
102
|
+
const sibling = napiWasmSibling(m.optionalDependencies);
|
|
103
|
+
if (sibling) add({ [sibling[0]]: sibling[1] }, 'optional', true);
|
|
99
104
|
if (!isRoot) {
|
|
100
105
|
const meta = (m as PackageManifest).peerDependenciesMeta ?? {};
|
|
101
106
|
for (const [name, range] of Object.entries(m.peerDependencies ?? {})) {
|
|
@@ -106,6 +111,19 @@ function dependenciesOf(m: PackageJson | PackageManifest, isRoot: boolean, produ
|
|
|
106
111
|
return [...out.values()].filter((d) => !bundled.has(d.name)).sort((a, b) => (a.name < b.name ? -1 : 1));
|
|
107
112
|
}
|
|
108
113
|
|
|
114
|
+
/** A native-binary package's wasm twin when one fits the range (`wasmBuilds.ts`), else the package itself. */
|
|
115
|
+
async function pickPreferringWasm(options: ResolveTreeOptions, spec: ParsedSpec): Promise<PackageManifest> {
|
|
116
|
+
const twin = wasmBuildOf(spec);
|
|
117
|
+
if (twin) {
|
|
118
|
+
try {
|
|
119
|
+
return await options.pick(twin);
|
|
120
|
+
} catch {
|
|
121
|
+
// no twin release for this range: the original package, as written
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return options.pick(spec);
|
|
125
|
+
}
|
|
126
|
+
|
|
109
127
|
/** Does `version` satisfy the edge's range? A dist-tag never "matches" without a pick. */
|
|
110
128
|
function matches(version: string, range: string): boolean {
|
|
111
129
|
const r = range || '*';
|
|
@@ -180,10 +198,10 @@ export async function resolveTree(options: ResolveTreeOptions): Promise<Map<stri
|
|
|
180
198
|
|
|
181
199
|
let manifest: PackageManifest;
|
|
182
200
|
try {
|
|
183
|
-
manifest = await options
|
|
201
|
+
manifest = await pickPreferringWasm(options, spec);
|
|
184
202
|
} catch (error) {
|
|
185
203
|
if (dep.type !== 'optional') throw error;
|
|
186
|
-
options.warn(`skipped optional ${dep.name}: ${(error as Error).message}`);
|
|
204
|
+
if (!dep.implied) options.warn(`skipped optional ${dep.name}: ${(error as Error).message}`);
|
|
187
205
|
continue;
|
|
188
206
|
}
|
|
189
207
|
if (!fitsPlatform(manifest)) {
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native binaries → the same projects' own WebAssembly builds, in two shapes:
|
|
3
|
+
*
|
|
4
|
+
* - A twin package (`WASM_BUILDS`), installed under the original name: code that
|
|
5
|
+
* requires `esbuild` gets `esbuild-wasm` at the same version, with the same
|
|
6
|
+
* JavaScript API. Vite, Nuxt, SvelteKit, tsx and tsup all reach for these.
|
|
7
|
+
* Data, not branches: another tool with an official wasm twin is one more row.
|
|
8
|
+
* - A napi-rs `-wasm32-wasi` binding (`napiWasmSibling`), added beside the
|
|
9
|
+
* per-platform ones the package lists.
|
|
10
|
+
*/
|
|
11
|
+
import type { ParsedSpec } from './spec.js';
|
|
12
|
+
|
|
13
|
+
const WASM_BUILDS: Readonly<Record<string, string>> = {
|
|
14
|
+
esbuild: 'esbuild-wasm',
|
|
15
|
+
rollup: '@rollup/wasm-node',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** A napi-rs per-platform binding name: `@oxc-parser/binding-linux-x64-gnu`, `lightningcss-darwin-arm64`. */
|
|
19
|
+
const NAPI_PLATFORM = /-(?:darwin|linux|win32|android|freebsd|openharmony|sunos|aix)-[a-z0-9_-]+$/;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* napi-rs packages list a binding per platform as optional dependencies, none of
|
|
23
|
+
* which fits here; most also publish a `-wasm32-wasi` sibling they look for next
|
|
24
|
+
* but do not list (oxc-parser, rolldown, @tailwindcss/oxide). This names that
|
|
25
|
+
* sibling, at the bindings' range, so it installs as one more optional dependency
|
|
26
|
+
* (absent from the registry → skipped like any optional miss).
|
|
27
|
+
*/
|
|
28
|
+
export function napiWasmSibling(optional: Readonly<Record<string, string>> | undefined): [name: string, range: string] | undefined {
|
|
29
|
+
const bindings = Object.entries(optional ?? {}).filter(([name]) => NAPI_PLATFORM.test(name));
|
|
30
|
+
if (bindings.length < 2) return undefined;
|
|
31
|
+
const [first, range] = bindings[0]!;
|
|
32
|
+
const sibling = first.replace(NAPI_PLATFORM, '-wasm32-wasi');
|
|
33
|
+
return optional![sibling] || bindings.some(([name]) => name.replace(NAPI_PLATFORM, '') !== first.replace(NAPI_PLATFORM, '')) ? undefined : [sibling, range];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The wasm twin of `spec`, when it has one and was asked for by its own name
|
|
38
|
+
* (an explicit `npm:` alias is left alone). The caller falls back to `spec` when
|
|
39
|
+
* the twin has no matching version: Rollup 3 is plain JavaScript and has no
|
|
40
|
+
* `@rollup/wasm-node` release, and needs no substitute.
|
|
41
|
+
*/
|
|
42
|
+
export function wasmBuildOf(spec: ParsedSpec): ParsedSpec | undefined {
|
|
43
|
+
const twin = spec.registryName === spec.name ? WASM_BUILDS[spec.name] : undefined;
|
|
44
|
+
return twin ? { ...spec, registryName: twin } : undefined;
|
|
45
|
+
}
|
package/src/node/builtins.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* `path` and `process` close over that process's cwd, IO and event loop.
|
|
5
5
|
*/
|
|
6
6
|
import type { VirtualFileSystem } from '../vfs.js';
|
|
7
|
-
import type { ProgramIO, System } from '../system/types.js';
|
|
7
|
+
import type { ProgramIO, StdioChunk, System, ThreadInfo } from '../system/types.js';
|
|
8
|
+
import type { IpcEndpoint } from '../system/ipc.js';
|
|
8
9
|
import type { EventLoop } from './loop.js';
|
|
9
10
|
import type { NodeProcess } from './process.js';
|
|
10
11
|
import { createEventsModule } from './events.js';
|
|
@@ -13,6 +14,7 @@ import { createPathModule } from './path.js';
|
|
|
13
14
|
import { createUtilModule, types as utilTypes } from './util.js';
|
|
14
15
|
import { createStreamModule } from './stream/index.js';
|
|
15
16
|
import { createFsModule } from './fs/index.js';
|
|
17
|
+
import { createStdinRead } from './fs/stdinFd.js';
|
|
16
18
|
import { constants as fsConstants } from './fs/stats.js';
|
|
17
19
|
import { createCryptoModule } from './crypto.js';
|
|
18
20
|
import { createQuerystringModule, createUrlModule } from './urlqs.js';
|
|
@@ -20,6 +22,7 @@ import { createAssertModule } from './assert.js';
|
|
|
20
22
|
import { createZlibModule } from './zlib.js';
|
|
21
23
|
import { createHttpModule, createNetModule, createFetch } from './http/index.js';
|
|
22
24
|
import { createChildProcessModule } from './childProcess.js';
|
|
25
|
+
import { createWorkerThreadsModule } from './workerThreads.js';
|
|
23
26
|
import { createReadlineModule } from './readline.js';
|
|
24
27
|
import { createAsyncHooksModule, createDiagnosticsChannelModule, createOsModule, createStringDecoderModule, createStubModules, createTimersModule, createTtyModule, createVmModule } from './misc.js';
|
|
25
28
|
|
|
@@ -31,6 +34,10 @@ export interface BuiltinDeps {
|
|
|
31
34
|
system: System;
|
|
32
35
|
io: ProgramIO;
|
|
33
36
|
console: Console;
|
|
37
|
+
/** Set when this process is a worker thread (`worker_threads.isMainThread` false). */
|
|
38
|
+
thread?: ThreadInfo;
|
|
39
|
+
/** The process's channel to its parent: a worker thread's `parentPort`. */
|
|
40
|
+
ipc?: IpcEndpoint;
|
|
34
41
|
}
|
|
35
42
|
|
|
36
43
|
type Factory = (get: (name: string) => any) => unknown;
|
|
@@ -40,28 +47,30 @@ export function createBuiltins(deps: BuiltinDeps) {
|
|
|
40
47
|
const env = () => proc.env as Record<string, string | undefined>;
|
|
41
48
|
const stubs = createStubModules();
|
|
42
49
|
const clientDeps = { system, loop };
|
|
50
|
+
/** What starting other processes needs: child_process and worker_threads alike. */
|
|
51
|
+
const spawnDeps = {
|
|
52
|
+
system,
|
|
53
|
+
loop,
|
|
54
|
+
cwd: () => proc.cwd() as string,
|
|
55
|
+
env,
|
|
56
|
+
stdout: (c: StdioChunk) => void proc.stdout.write(c),
|
|
57
|
+
stderr: (c: StdioChunk) => void proc.stderr.write(c),
|
|
58
|
+
};
|
|
43
59
|
|
|
44
60
|
const factories: Record<string, Factory> = {
|
|
45
61
|
assert: () => createAssertModule(false),
|
|
46
62
|
'assert/strict': () => createAssertModule(true),
|
|
47
63
|
async_hooks: () => createAsyncHooksModule(),
|
|
48
64
|
buffer: () => createBufferModule(),
|
|
49
|
-
child_process: () =>
|
|
50
|
-
|
|
51
|
-
system,
|
|
52
|
-
loop,
|
|
53
|
-
cwd: () => proc.cwd(),
|
|
54
|
-
env,
|
|
55
|
-
stdout: (c) => proc.stdout.write(c),
|
|
56
|
-
stderr: (c) => proc.stderr.write(c),
|
|
57
|
-
}),
|
|
65
|
+
child_process: () => createChildProcessModule(spawnDeps),
|
|
66
|
+
worker_threads: () => createWorkerThreadsModule({ ...spawnDeps, thread: deps.thread, ipc: deps.ipc }),
|
|
58
67
|
console: () => Object.assign(Object.create(deps.console), { Console: function Console() { return deps.console; } }),
|
|
59
68
|
constants: (get) => ({ ...fsConstants, ...(get('os').constants.signals as object) }),
|
|
60
69
|
crypto: () => createCryptoModule(),
|
|
61
70
|
diagnostics_channel: () => createDiagnosticsChannelModule(),
|
|
62
71
|
'dns/promises': (get) => get('dns').promises,
|
|
63
72
|
events: () => createEventsModule(),
|
|
64
|
-
fs: () => createFsModule({ vfs: deps.fs, cwd: () => proc.cwd(), stdout: deps.io.stdout, stderr: deps.io.stderr, hold: () => loop.hold() }),
|
|
73
|
+
fs: () => createFsModule({ vfs: deps.fs, cwd: () => proc.cwd(), stdout: deps.io.stdout, stderr: deps.io.stderr, hold: () => loop.hold(), readStdin: createStdinRead(deps.io, () => loop.hold()) }),
|
|
65
74
|
'fs/promises': (get) => get('fs').promises,
|
|
66
75
|
http: () => createHttpModule(clientDeps, 'http:'),
|
|
67
76
|
https: () => createHttpModule(clientDeps, 'https:'),
|
|
@@ -94,7 +103,7 @@ export function createBuiltins(deps: BuiltinDeps) {
|
|
|
94
103
|
timers: () => createTimersModule(loop),
|
|
95
104
|
'timers/promises': (get) => get('timers').promises,
|
|
96
105
|
tty: () => createTtyModule(!!deps.io.terminal),
|
|
97
|
-
url: (get) => createUrlModule(get('querystring')),
|
|
106
|
+
url: (get) => createUrlModule(get('querystring'), (path) => get('path').resolve(path)),
|
|
98
107
|
util: () => createUtilModule(env, (message) => proc.stderr.write(message + '\n')),
|
|
99
108
|
'util/types': () => utilTypes,
|
|
100
109
|
vm: () => createVmModule(),
|
package/src/node/childProcess.ts
CHANGED
|
@@ -9,7 +9,10 @@ import { EventEmitterBase } from './events.js';
|
|
|
9
9
|
import { Readable } from './stream/readable.js';
|
|
10
10
|
import { Writable } from './stream/writable.js';
|
|
11
11
|
import type { EventLoop } from './loop.js';
|
|
12
|
-
import type { ChildHandle, System } from '../system/types.js';
|
|
12
|
+
import type { ChildHandle, StdioChunk, System } from '../system/types.js';
|
|
13
|
+
import { Buffer } from './buffer.js';
|
|
14
|
+
import type { IpcEndpoint } from '../system/ipc.js';
|
|
15
|
+
import { queueCallback } from './processExit.js';
|
|
13
16
|
|
|
14
17
|
interface SpawnOptions {
|
|
15
18
|
cwd?: string;
|
|
@@ -19,6 +22,12 @@ interface SpawnOptions {
|
|
|
19
22
|
signal?: AbortSignal;
|
|
20
23
|
encoding?: string;
|
|
21
24
|
maxBuffer?: number;
|
|
25
|
+
/** `fork`: open a message channel (`child.send` ↔ `process.send`). */
|
|
26
|
+
ipc?: boolean;
|
|
27
|
+
/** `fork`: pipe the child's stdout/stderr to `child.stdout`/`stderr` instead of the parent's. */
|
|
28
|
+
silent?: boolean;
|
|
29
|
+
/** Keep running after this process exits (otherwise it is killed with it). */
|
|
30
|
+
detached?: boolean;
|
|
22
31
|
}
|
|
23
32
|
|
|
24
33
|
export interface ChildProcessDeps {
|
|
@@ -27,8 +36,8 @@ export interface ChildProcessDeps {
|
|
|
27
36
|
cwd(): string;
|
|
28
37
|
env(): Record<string, string | undefined>;
|
|
29
38
|
/** Parent's stdio for `stdio: 'inherit'`. */
|
|
30
|
-
stdout(chunk:
|
|
31
|
-
stderr(chunk:
|
|
39
|
+
stdout(chunk: StdioChunk): void;
|
|
40
|
+
stderr(chunk: StdioChunk): void;
|
|
32
41
|
}
|
|
33
42
|
|
|
34
43
|
const quote = (arg: string) => (/^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replace(/'/g, `'\\''`)}'`);
|
|
@@ -46,6 +55,11 @@ class ChildProcess extends EventEmitterBase {
|
|
|
46
55
|
stderr: Readable;
|
|
47
56
|
stdio: [Writable, Readable, Readable];
|
|
48
57
|
private handle?: ChildHandle;
|
|
58
|
+
private ipc?: IpcEndpoint;
|
|
59
|
+
/** Lets go of the parent's event loop; undefined once released (exit, or `unref()`). */
|
|
60
|
+
private release?: () => void;
|
|
61
|
+
private exited = false;
|
|
62
|
+
private readonly hold: () => () => void;
|
|
49
63
|
|
|
50
64
|
constructor(deps: ChildProcessDeps, command: string, args: string[], options: SpawnOptions) {
|
|
51
65
|
super();
|
|
@@ -55,7 +69,7 @@ class ChildProcess extends EventEmitterBase {
|
|
|
55
69
|
this.stderr = new Readable({ read() {} });
|
|
56
70
|
this.stdin = new Writable({
|
|
57
71
|
write: (chunk: Uint8Array | string, _enc: string, cb: () => void) => {
|
|
58
|
-
this.handle?.write(typeof chunk === 'string' ? chunk : new
|
|
72
|
+
this.handle?.write(typeof chunk === 'string' ? chunk : new Uint8Array(chunk)); // bytes stay bytes
|
|
59
73
|
cb();
|
|
60
74
|
},
|
|
61
75
|
final: (cb: () => void) => {
|
|
@@ -64,30 +78,45 @@ class ChildProcess extends EventEmitterBase {
|
|
|
64
78
|
},
|
|
65
79
|
});
|
|
66
80
|
this.stdio = [this.stdin, this.stdout, this.stderr];
|
|
67
|
-
|
|
81
|
+
// Per stream, as Node reads it: `'inherit'`, or `['pipe', 'pipe', 'inherit']` (esbuild's service: its stderr is the parent's).
|
|
82
|
+
const inherits = (fd: 1 | 2) => options.stdio === 'inherit' || (Array.isArray(options.stdio) && options.stdio[fd] === 'inherit');
|
|
83
|
+
const [inheritOut, inheritErr] = [inherits(1), inherits(2)];
|
|
68
84
|
|
|
69
85
|
const env: Record<string, string> = {};
|
|
70
86
|
for (const [k, v] of Object.entries(options.env ?? deps.env())) if (v !== undefined) env[k] = String(v);
|
|
71
87
|
const [file, argv] = options.shell ? ['bfsh', ['-c', [command, ...args].join(' ')]] : [command, args];
|
|
72
|
-
|
|
88
|
+
this.hold = () => deps.loop.hold();
|
|
89
|
+
this.release = this.hold();
|
|
73
90
|
try {
|
|
74
|
-
this.handle = deps.system.spawn(file, argv, { cwd: options.cwd ?? deps.cwd(), env });
|
|
91
|
+
this.handle = deps.system.spawn(file, argv, { cwd: options.cwd ?? deps.cwd(), env, ipc: options.ipc, detached: options.detached });
|
|
75
92
|
} catch (error) {
|
|
76
|
-
|
|
93
|
+
this.unref();
|
|
77
94
|
const code = (error as { code?: string }).code ?? 'ENOENT';
|
|
78
95
|
const failure = Object.assign(new Error(`spawn ${command} ${code}`), { code, errno: -2, syscall: `spawn ${command}`, path: command, spawnargs: args });
|
|
79
|
-
|
|
96
|
+
queueCallback(() => {
|
|
80
97
|
this.emit('error', failure);
|
|
81
98
|
this.emit('close', -2, null);
|
|
82
99
|
});
|
|
83
100
|
return;
|
|
84
101
|
}
|
|
85
102
|
this.pid = this.handle.pid;
|
|
86
|
-
|
|
87
|
-
|
|
103
|
+
const ipc = this.handle.ipc;
|
|
104
|
+
if (ipc) {
|
|
105
|
+
this.ipc = ipc;
|
|
106
|
+
this.connected = true;
|
|
107
|
+
ipc.onMessage((message) => deps.loop._run(() => this.emit('message', message, undefined)));
|
|
108
|
+
ipc.onDisconnect(() => {
|
|
109
|
+
this.connected = false;
|
|
110
|
+
deps.loop._run(() => this.emit('disconnect'));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// Piped output arrives as Buffers, as in Node.
|
|
114
|
+
this.handle.onStdout((chunk) => (inheritOut ? deps.stdout(chunk) : this.stdout.push(Buffer.from(chunk))));
|
|
115
|
+
this.handle.onStderr((chunk) => (inheritErr ? deps.stderr(chunk) : this.stderr.push(Buffer.from(chunk))));
|
|
88
116
|
options.signal?.addEventListener('abort', () => this.kill());
|
|
89
|
-
|
|
117
|
+
queueCallback(() => this.emit('spawn'));
|
|
90
118
|
void this.handle.exit.then((code) => {
|
|
119
|
+
this.exited = true;
|
|
91
120
|
this.exitCode = this.killed ? null : code;
|
|
92
121
|
this.stdout.push(null);
|
|
93
122
|
this.stderr.push(null);
|
|
@@ -95,7 +124,7 @@ class ChildProcess extends EventEmitterBase {
|
|
|
95
124
|
this.emit('exit', this.exitCode, this.signalCode);
|
|
96
125
|
this.emit('close', this.exitCode, this.signalCode);
|
|
97
126
|
});
|
|
98
|
-
|
|
127
|
+
this.unref();
|
|
99
128
|
});
|
|
100
129
|
}
|
|
101
130
|
|
|
@@ -107,12 +136,33 @@ class ChildProcess extends EventEmitterBase {
|
|
|
107
136
|
return true;
|
|
108
137
|
}
|
|
109
138
|
|
|
110
|
-
send(): boolean {
|
|
111
|
-
|
|
139
|
+
send(message: unknown, ...rest: unknown[]): boolean {
|
|
140
|
+
const callback = rest.find((arg): arg is (error: Error | null) => void => typeof arg === 'function');
|
|
141
|
+
const fail = (error: Error) => {
|
|
142
|
+
if (callback) queueCallback(() => callback(error));
|
|
143
|
+
else queueCallback(() => this.emit('error', error));
|
|
144
|
+
return false;
|
|
145
|
+
};
|
|
146
|
+
if (!this.ipc) return fail(Object.assign(new Error('Channel closed'), { code: 'ERR_IPC_CHANNEL_CLOSED' }));
|
|
147
|
+
try {
|
|
148
|
+
this.ipc.send(message);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
return fail(error as Error);
|
|
151
|
+
}
|
|
152
|
+
if (callback) queueCallback(() => callback(null));
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
disconnect(): void {
|
|
156
|
+
this.ipc?.disconnect();
|
|
157
|
+
}
|
|
158
|
+
/** A running child keeps the parent alive until `unref()` (then `ref()` to hold it again). */
|
|
159
|
+
ref(): void {
|
|
160
|
+
if (!this.release && this.handle && !this.exited) this.release = this.hold();
|
|
161
|
+
}
|
|
162
|
+
unref(): void {
|
|
163
|
+
this.release?.();
|
|
164
|
+
this.release = undefined;
|
|
112
165
|
}
|
|
113
|
-
disconnect(): void {}
|
|
114
|
-
ref(): void {}
|
|
115
|
-
unref(): void {}
|
|
116
166
|
}
|
|
117
167
|
|
|
118
168
|
type ExecCallback = (error: (Error & { code?: number | string }) | null, stdout: string, stderr: string) => void;
|
|
@@ -152,9 +202,12 @@ export function createChildProcessModule(deps: ChildProcessDeps): Record<string,
|
|
|
152
202
|
if (typeof options === 'function') [callback, options] = [options, {}];
|
|
153
203
|
return collect(new ChildProcess(deps, file, args, options ?? {}), [file, ...args].map(quote).join(' '), callback);
|
|
154
204
|
},
|
|
155
|
-
fork(modulePath: string, args?: string[] | SpawnOptions, options?: SpawnOptions) {
|
|
205
|
+
fork(modulePath: string, args?: string[] | SpawnOptions, options?: SpawnOptions & { execArgv?: string[] }) {
|
|
156
206
|
if (!Array.isArray(args)) [options, args] = [args, []];
|
|
157
|
-
|
|
207
|
+
const opts = options ?? {};
|
|
208
|
+
// A forked child shares the parent's stdout/stderr unless `silent`, as in Node.
|
|
209
|
+
const stdio = opts.stdio ?? (opts.silent ? 'pipe' : 'inherit');
|
|
210
|
+
return new ChildProcess(deps, 'node', [...(opts.execArgv ?? []), modulePath, ...args.map(String)], { ...opts, stdio, ipc: true });
|
|
158
211
|
},
|
|
159
212
|
execSync: noSync('execSync'),
|
|
160
213
|
execFileSync: noSync('execFileSync'),
|