@seanhogg/builderforce-webcontainers-core 2026.10.2 → 2026.10.3
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 +42 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +65 -0
- package/dist/build.js.map +1 -0
- package/dist/buildHost.d.ts +31 -0
- package/dist/buildHost.d.ts.map +1 -0
- package/dist/buildHost.js +125 -0
- package/dist/buildHost.js.map +1 -0
- package/dist/buildHtml.d.ts +28 -0
- package/dist/buildHtml.d.ts.map +1 -0
- package/dist/buildHtml.js +74 -0
- package/dist/buildHtml.js.map +1 -0
- package/dist/bundler.d.ts +129 -0
- package/dist/bundler.d.ts.map +1 -0
- package/dist/bundler.js +79 -0
- package/dist/bundler.js.map +1 -0
- package/dist/check/acquire.d.ts +46 -0
- package/dist/check/acquire.d.ts.map +1 -0
- package/dist/check/acquire.js +179 -0
- package/dist/check/acquire.js.map +1 -0
- package/dist/check/compilerHost.d.ts +22 -0
- package/dist/check/compilerHost.d.ts.map +1 -0
- package/dist/check/compilerHost.js +128 -0
- package/dist/check/compilerHost.js.map +1 -0
- package/dist/check/diagnostics.d.ts +23 -0
- package/dist/check/diagnostics.d.ts.map +1 -0
- package/dist/check/diagnostics.js +28 -0
- package/dist/check/diagnostics.js.map +1 -0
- package/dist/check/index.d.ts +19 -0
- package/dist/check/index.d.ts.map +1 -0
- package/dist/check/index.js +13 -0
- package/dist/check/index.js.map +1 -0
- package/dist/check/projects.d.ts +26 -0
- package/dist/check/projects.d.ts.map +1 -0
- package/dist/check/projects.js +124 -0
- package/dist/check/projects.js.map +1 -0
- package/dist/check/shims.d.ts +29 -0
- package/dist/check/shims.d.ts.map +1 -0
- package/dist/check/shims.js +76 -0
- package/dist/check/shims.js.map +1 -0
- package/dist/check/typeStore.d.ts +57 -0
- package/dist/check/typeStore.d.ts.map +1 -0
- package/dist/check/typeStore.js +89 -0
- package/dist/check/typeStore.js.map +1 -0
- package/dist/check/typecheck.d.ts +29 -0
- package/dist/check/typecheck.d.ts.map +1 -0
- package/dist/check/typecheck.js +91 -0
- package/dist/check/typecheck.js.map +1 -0
- package/dist/compileComponent.d.ts +8 -0
- package/dist/compileComponent.d.ts.map +1 -0
- package/dist/compileComponent.js +22 -0
- package/dist/compileComponent.js.map +1 -0
- package/dist/compileScript.d.ts +19 -4
- package/dist/compileScript.d.ts.map +1 -1
- package/dist/compileScript.js +37 -12
- package/dist/compileScript.js.map +1 -1
- package/dist/components.d.ts +41 -0
- package/dist/components.d.ts.map +1 -0
- package/dist/components.js +14 -0
- package/dist/components.js.map +1 -0
- package/dist/detectProject.d.ts +3 -0
- package/dist/detectProject.d.ts.map +1 -1
- package/dist/detectProject.js +16 -6
- package/dist/detectProject.js.map +1 -1
- package/dist/devServer.d.ts +5 -2
- package/dist/devServer.d.ts.map +1 -1
- package/dist/devServer.js +10 -6
- package/dist/devServer.js.map +1 -1
- package/dist/hash.d.ts +2 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +22 -0
- package/dist/hash.js.map +1 -0
- package/dist/index.d.ts +24 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -3
- package/dist/index.js.map +1 -1
- package/dist/installer/bins.d.ts +24 -0
- package/dist/installer/bins.d.ts.map +1 -0
- package/dist/installer/bins.js +56 -0
- package/dist/installer/bins.js.map +1 -0
- package/dist/installer/index.d.ts +14 -0
- package/dist/installer/index.d.ts.map +1 -0
- package/dist/installer/index.js +9 -0
- package/dist/installer/index.js.map +1 -0
- package/dist/installer/install.d.ts +46 -0
- package/dist/installer/install.d.ts.map +1 -0
- package/dist/installer/install.js +245 -0
- package/dist/installer/install.js.map +1 -0
- package/dist/installer/lockfile.d.ts +49 -0
- package/dist/installer/lockfile.d.ts.map +1 -0
- package/dist/installer/lockfile.js +110 -0
- package/dist/installer/lockfile.js.map +1 -0
- package/dist/installer/registry.d.ts +84 -0
- package/dist/installer/registry.d.ts.map +1 -0
- package/dist/installer/registry.js +130 -0
- package/dist/installer/registry.js.map +1 -0
- package/dist/installer/semver.d.ts +38 -0
- package/dist/installer/semver.d.ts.map +1 -0
- package/dist/installer/semver.js +187 -0
- package/dist/installer/semver.js.map +1 -0
- package/dist/installer/spec.d.ts +30 -0
- package/dist/installer/spec.d.ts.map +1 -0
- package/dist/installer/spec.js +30 -0
- package/dist/installer/spec.js.map +1 -0
- package/dist/installer/tar.d.ts +17 -0
- package/dist/installer/tar.d.ts.map +1 -0
- package/dist/installer/tar.js +150 -0
- package/dist/installer/tar.js.map +1 -0
- package/dist/installer/tree.d.ts +48 -0
- package/dist/installer/tree.d.ts.map +1 -0
- package/dist/installer/tree.js +179 -0
- package/dist/installer/tree.js.map +1 -0
- package/dist/node/assert.d.ts +15 -0
- package/dist/node/assert.d.ts.map +1 -0
- package/dist/node/assert.js +137 -0
- package/dist/node/assert.js.map +1 -0
- package/dist/node/buffer.d.ts +84 -0
- package/dist/node/buffer.d.ts.map +1 -0
- package/dist/node/buffer.js +304 -0
- package/dist/node/buffer.js.map +1 -0
- package/dist/node/builtins.d.ts +25 -0
- package/dist/node/builtins.d.ts.map +1 -0
- package/dist/node/builtins.js +101 -0
- package/dist/node/builtins.js.map +1 -0
- package/dist/node/callable.d.ts +13 -0
- package/dist/node/callable.d.ts.map +1 -0
- package/dist/node/callable.js +26 -0
- package/dist/node/callable.js.map +1 -0
- package/dist/node/childProcess.d.ts +13 -0
- package/dist/node/childProcess.d.ts.map +1 -0
- package/dist/node/childProcess.js +143 -0
- package/dist/node/childProcess.js.map +1 -0
- package/dist/node/crypto.d.ts +3 -0
- package/dist/node/crypto.d.ts.map +1 -0
- package/dist/node/crypto.js +180 -0
- package/dist/node/crypto.js.map +1 -0
- package/dist/node/esm.d.ts +9 -0
- package/dist/node/esm.d.ts.map +1 -0
- package/dist/node/esm.js +202 -0
- package/dist/node/esm.js.map +1 -0
- package/dist/node/events.d.ts +40 -0
- package/dist/node/events.d.ts.map +1 -0
- package/dist/node/events.js +160 -0
- package/dist/node/events.js.map +1 -0
- package/dist/node/fs/index.d.ts +7 -0
- package/dist/node/fs/index.d.ts.map +1 -0
- package/dist/node/fs/index.js +270 -0
- package/dist/node/fs/index.js.map +1 -0
- package/dist/node/fs/stats.d.ts +93 -0
- package/dist/node/fs/stats.d.ts.map +1 -0
- package/dist/node/fs/stats.js +104 -0
- package/dist/node/fs/stats.js.map +1 -0
- package/dist/node/fs/sync.d.ts +98 -0
- package/dist/node/fs/sync.d.ts.map +1 -0
- package/dist/node/fs/sync.js +348 -0
- package/dist/node/fs/sync.js.map +1 -0
- package/dist/node/hashes.d.ts +15 -0
- package/dist/node/hashes.d.ts.map +1 -0
- package/dist/node/hashes.js +195 -0
- package/dist/node/hashes.js.map +1 -0
- package/dist/node/http/client.d.ts +35 -0
- package/dist/node/http/client.d.ts.map +1 -0
- package/dist/node/http/client.js +161 -0
- package/dist/node/http/client.js.map +1 -0
- package/dist/node/http/index.d.ts +5 -0
- package/dist/node/http/index.d.ts.map +1 -0
- package/dist/node/http/index.js +125 -0
- package/dist/node/http/index.js.map +1 -0
- package/dist/node/http/server.d.ts +135 -0
- package/dist/node/http/server.d.ts.map +1 -0
- package/dist/node/http/server.js +308 -0
- package/dist/node/http/server.js.map +1 -0
- package/dist/node/http/status.d.ts +3 -0
- package/dist/node/http/status.d.ts.map +1 -0
- package/dist/node/http/status.js +13 -0
- package/dist/node/http/status.js.map +1 -0
- package/dist/node/inspect.d.ts +22 -0
- package/dist/node/inspect.d.ts.map +1 -0
- package/dist/node/inspect.js +172 -0
- package/dist/node/inspect.js.map +1 -0
- package/dist/node/loader.d.ts +91 -0
- package/dist/node/loader.d.ts.map +1 -0
- package/dist/node/loader.js +265 -0
- package/dist/node/loader.js.map +1 -0
- package/dist/node/loop.d.ts +71 -0
- package/dist/node/loop.d.ts.map +1 -0
- package/dist/node/loop.js +189 -0
- package/dist/node/loop.js.map +1 -0
- package/dist/node/misc.d.ts +10 -0
- package/dist/node/misc.d.ts.map +1 -0
- package/dist/node/misc.js +256 -0
- package/dist/node/misc.js.map +1 -0
- package/dist/node/path.d.ts +14 -0
- package/dist/node/path.d.ts.map +1 -0
- package/dist/node/path.js +128 -0
- package/dist/node/path.js.map +1 -0
- package/dist/node/process.d.ts +31 -0
- package/dist/node/process.d.ts.map +1 -0
- package/dist/node/process.js +309 -0
- package/dist/node/process.js.map +1 -0
- package/dist/node/program.d.ts +19 -0
- package/dist/node/program.d.ts.map +1 -0
- package/dist/node/program.js +165 -0
- package/dist/node/program.js.map +1 -0
- package/dist/node/readline.d.ts +2 -0
- package/dist/node/readline.d.ts.map +1 -0
- package/dist/node/readline.js +155 -0
- package/dist/node/readline.js.map +1 -0
- package/dist/node/resolve.d.ts +45 -0
- package/dist/node/resolve.d.ts.map +1 -0
- package/dist/node/resolve.js +206 -0
- package/dist/node/resolve.js.map +1 -0
- package/dist/node/runtime.d.ts +40 -0
- package/dist/node/runtime.d.ts.map +1 -0
- package/dist/node/runtime.js +149 -0
- package/dist/node/runtime.js.map +1 -0
- package/dist/node/stream/index.d.ts +52 -0
- package/dist/node/stream/index.d.ts.map +1 -0
- package/dist/node/stream/index.js +184 -0
- package/dist/node/stream/index.js.map +1 -0
- package/dist/node/stream/readable.d.ts +83 -0
- package/dist/node/stream/readable.d.ts.map +1 -0
- package/dist/node/stream/readable.js +325 -0
- package/dist/node/stream/readable.js.map +1 -0
- package/dist/node/stream/writable.d.ts +80 -0
- package/dist/node/stream/writable.d.ts.map +1 -0
- package/dist/node/stream/writable.js +224 -0
- package/dist/node/stream/writable.js.map +1 -0
- package/dist/node/urlqs.d.ts +22 -0
- package/dist/node/urlqs.d.ts.map +1 -0
- package/dist/node/urlqs.js +165 -0
- package/dist/node/urlqs.js.map +1 -0
- package/dist/node/util.d.ts +37 -0
- package/dist/node/util.d.ts.map +1 -0
- package/dist/node/util.js +160 -0
- package/dist/node/util.js.map +1 -0
- package/dist/node/zlib.d.ts +2 -0
- package/dist/node/zlib.d.ts.map +1 -0
- package/dist/node/zlib.js +133 -0
- package/dist/node/zlib.js.map +1 -0
- package/dist/packageCdn.d.ts +12 -0
- package/dist/packageCdn.d.ts.map +1 -1
- package/dist/packageCdn.js +3 -1
- package/dist/packageCdn.js.map +1 -1
- package/dist/projectConfig.d.ts +6 -2
- package/dist/projectConfig.d.ts.map +1 -1
- package/dist/projectConfig.js +32 -8
- package/dist/projectConfig.js.map +1 -1
- package/dist/shell/builtins.d.ts +23 -0
- package/dist/shell/builtins.d.ts.map +1 -0
- package/dist/shell/builtins.js +131 -0
- package/dist/shell/builtins.js.map +1 -0
- package/dist/shell/context.d.ts +41 -0
- package/dist/shell/context.d.ts.map +1 -0
- package/dist/shell/context.js +65 -0
- package/dist/shell/context.js.map +1 -0
- package/dist/shell/devHandoff.d.ts +18 -0
- package/dist/shell/devHandoff.d.ts.map +1 -0
- package/dist/shell/devHandoff.js +36 -0
- package/dist/shell/devHandoff.js.map +1 -0
- package/dist/shell/exec.d.ts +67 -0
- package/dist/shell/exec.d.ts.map +1 -0
- package/dist/shell/exec.js +247 -0
- package/dist/shell/exec.js.map +1 -0
- package/dist/shell/expand.d.ts +28 -0
- package/dist/shell/expand.d.ts.map +1 -0
- package/dist/shell/expand.js +149 -0
- package/dist/shell/expand.js.map +1 -0
- package/dist/shell/fileCommands.d.ts +10 -0
- package/dist/shell/fileCommands.d.ts.map +1 -0
- package/dist/shell/fileCommands.js +208 -0
- package/dist/shell/fileCommands.js.map +1 -0
- package/dist/shell/lineEditor.d.ts +43 -0
- package/dist/shell/lineEditor.d.ts.map +1 -0
- package/dist/shell/lineEditor.js +234 -0
- package/dist/shell/lineEditor.js.map +1 -0
- package/dist/shell/npm.d.ts +6 -0
- package/dist/shell/npm.d.ts.map +1 -0
- package/dist/shell/npm.js +234 -0
- package/dist/shell/npm.js.map +1 -0
- package/dist/shell/parse.d.ts +73 -0
- package/dist/shell/parse.d.ts.map +1 -0
- package/dist/shell/parse.js +300 -0
- package/dist/shell/parse.js.map +1 -0
- package/dist/shell/program.d.ts +12 -0
- package/dist/shell/program.d.ts.map +1 -0
- package/dist/shell/program.js +219 -0
- package/dist/shell/program.js.map +1 -0
- package/dist/shell/textCommands.d.ts +15 -0
- package/dist/shell/textCommands.d.ts.map +1 -0
- package/dist/shell/textCommands.js +185 -0
- package/dist/shell/textCommands.js.map +1 -0
- package/dist/styleModules.d.ts +6 -0
- package/dist/styleModules.d.ts.map +1 -1
- package/dist/styleModules.js +14 -4
- package/dist/styleModules.js.map +1 -1
- package/dist/svelteCompiler.d.ts +28 -0
- package/dist/svelteCompiler.d.ts.map +1 -0
- package/dist/svelteCompiler.js +13 -0
- package/dist/svelteCompiler.js.map +1 -0
- package/dist/system/commands.d.ts +26 -0
- package/dist/system/commands.d.ts.map +1 -0
- package/dist/system/commands.js +53 -0
- package/dist/system/commands.js.map +1 -0
- package/dist/system/index.d.ts +23 -0
- package/dist/system/index.d.ts.map +1 -0
- package/dist/system/index.js +16 -0
- package/dist/system/index.js.map +1 -0
- package/dist/system/kernel.d.ts +54 -0
- package/dist/system/kernel.d.ts.map +1 -0
- package/dist/system/kernel.js +81 -0
- package/dist/system/kernel.js.map +1 -0
- package/dist/system/ports.d.ts +32 -0
- package/dist/system/ports.d.ts.map +1 -0
- package/dist/system/ports.js +57 -0
- package/dist/system/ports.js.map +1 -0
- package/dist/system/process.d.ts +32 -0
- package/dist/system/process.d.ts.map +1 -0
- package/dist/system/process.js +130 -0
- package/dist/system/process.js.map +1 -0
- package/dist/system/programs.d.ts +10 -0
- package/dist/system/programs.d.ts.map +1 -0
- package/dist/system/programs.js +22 -0
- package/dist/system/programs.js.map +1 -0
- package/dist/system/terminal.d.ts +45 -0
- package/dist/system/terminal.d.ts.map +1 -0
- package/dist/system/terminal.js +127 -0
- package/dist/system/terminal.js.map +1 -0
- package/dist/system/types.d.ts +100 -0
- package/dist/system/types.d.ts.map +1 -0
- package/dist/system/types.js +2 -0
- package/dist/system/types.js.map +1 -0
- package/dist/vfs.d.ts +28 -4
- package/dist/vfs.d.ts.map +1 -1
- package/dist/vfs.js +95 -32
- package/dist/vfs.js.map +1 -1
- package/dist/vueCompiler.d.ts +75 -0
- package/dist/vueCompiler.d.ts.map +1 -0
- package/dist/vueCompiler.js +67 -0
- package/dist/vueCompiler.js.map +1 -0
- package/package.json +18 -2
- package/src/build.ts +105 -0
- package/src/buildHost.ts +154 -0
- package/src/buildHtml.ts +101 -0
- package/src/bundler.ts +190 -0
- package/src/check/acquire.ts +212 -0
- package/src/check/compilerHost.ts +138 -0
- package/src/check/diagnostics.ts +48 -0
- package/src/check/index.ts +28 -0
- package/src/check/projects.ts +149 -0
- package/src/check/shims.ts +88 -0
- package/src/check/typeStore.ts +103 -0
- package/src/check/typecheck.ts +134 -0
- package/src/compileComponent.ts +21 -0
- package/src/compileScript.ts +42 -13
- package/src/components.ts +59 -0
- package/src/detectProject.ts +18 -6
- package/src/devServer.ts +13 -8
- package/src/hash.ts +22 -0
- package/src/index.ts +35 -6
- package/src/installer/bins.ts +64 -0
- package/src/installer/index.ts +13 -0
- package/src/installer/install.ts +275 -0
- package/src/installer/lockfile.ts +146 -0
- package/src/installer/registry.ts +175 -0
- package/src/installer/semver.ts +190 -0
- package/src/installer/spec.ts +53 -0
- package/src/installer/tar.ts +155 -0
- package/src/installer/tree.ts +236 -0
- package/src/node/assert.ts +127 -0
- package/src/node/buffer.ts +304 -0
- package/src/node/builtins.ts +123 -0
- package/src/node/callable.ts +25 -0
- package/src/node/childProcess.ts +164 -0
- package/src/node/crypto.ts +187 -0
- package/src/node/esm.ts +196 -0
- package/src/node/events.ts +176 -0
- package/src/node/fs/index.ts +277 -0
- package/src/node/fs/stats.ts +110 -0
- package/src/node/fs/sync.ts +335 -0
- package/src/node/hashes.ts +171 -0
- package/src/node/http/client.ts +173 -0
- package/src/node/http/index.ts +122 -0
- package/src/node/http/server.ts +308 -0
- package/src/node/http/status.ts +13 -0
- package/src/node/inspect.ts +166 -0
- package/src/node/loader.ts +293 -0
- package/src/node/loop.ts +193 -0
- package/src/node/misc.ts +250 -0
- package/src/node/path.ts +135 -0
- package/src/node/process.ts +324 -0
- package/src/node/program.ts +170 -0
- package/src/node/readline.ts +157 -0
- package/src/node/resolve.ts +216 -0
- package/src/node/runtime.ts +184 -0
- package/src/node/stream/index.ts +196 -0
- package/src/node/stream/readable.ts +345 -0
- package/src/node/stream/writable.ts +244 -0
- package/src/node/urlqs.ts +162 -0
- package/src/node/util.ts +145 -0
- package/src/node/zlib.ts +145 -0
- package/src/packageCdn.ts +11 -1
- package/src/projectConfig.ts +34 -7
- package/src/shell/builtins.ts +141 -0
- package/src/shell/context.ts +89 -0
- package/src/shell/devHandoff.ts +49 -0
- package/src/shell/exec.ts +268 -0
- package/src/shell/expand.ts +145 -0
- package/src/shell/fileCommands.ts +206 -0
- package/src/shell/lineEditor.ts +233 -0
- package/src/shell/npm.ts +229 -0
- package/src/shell/parse.ts +295 -0
- package/src/shell/program.ts +223 -0
- package/src/shell/textCommands.ts +177 -0
- package/src/styleModules.ts +15 -4
- package/src/svelteCompiler.ts +31 -0
- package/src/system/commands.ts +72 -0
- package/src/system/index.ts +22 -0
- package/src/system/kernel.ts +106 -0
- package/src/system/ports.ts +73 -0
- package/src/system/process.ts +158 -0
- package/src/system/programs.ts +31 -0
- package/src/system/terminal.ts +135 -0
- package/src/system/types.ts +103 -0
- package/src/vfs.ts +92 -28
- package/src/vueCompiler.ts +121 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `process` object of one runtime: argv/env/cwd, `exit` (unwinds via
|
|
3
|
+
* {@link ProcessExit}), `nextTick`, stdio streams wired to the program's IO
|
|
4
|
+
* port, and the identity a Linux Node 20 reports — packages branch on
|
|
5
|
+
* `process.platform`, `process.versions.node` and `process.stdout.isTTY`.
|
|
6
|
+
*/
|
|
7
|
+
import { EventEmitterBase } from './events.js';
|
|
8
|
+
import { Readable } from './stream/readable.js';
|
|
9
|
+
import { Writable } from './stream/writable.js';
|
|
10
|
+
import type { EventLoop } from './loop.js';
|
|
11
|
+
import type { ProgramIO } from '../system/types.js';
|
|
12
|
+
import type { VirtualFileSystem } from '../vfs.js';
|
|
13
|
+
import { join, normalizePath } from '../paths.js';
|
|
14
|
+
import { fsError } from './fs/stats.js';
|
|
15
|
+
import { format } from './inspect.js';
|
|
16
|
+
|
|
17
|
+
export const NODE_VERSION = '20.18.0';
|
|
18
|
+
|
|
19
|
+
/** Thrown by `process.exit()` to unwind user code; the runtime catches it. */
|
|
20
|
+
export class ProcessExit {
|
|
21
|
+
constructor(readonly code: number) {}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let nextPid = 100;
|
|
25
|
+
|
|
26
|
+
export interface ProcessOptions {
|
|
27
|
+
argv: string[];
|
|
28
|
+
env: Record<string, string>;
|
|
29
|
+
cwd: string;
|
|
30
|
+
fs: VirtualFileSystem;
|
|
31
|
+
io: ProgramIO;
|
|
32
|
+
loop: EventLoop;
|
|
33
|
+
/** Called once with the exit code; the runtime tears down. */
|
|
34
|
+
onExit(code: number): void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class TtyWriteStream extends Writable {
|
|
38
|
+
isTTY: boolean;
|
|
39
|
+
fd: number;
|
|
40
|
+
columns?: number;
|
|
41
|
+
rows?: number;
|
|
42
|
+
|
|
43
|
+
constructor(fd: number, private readonly sink: (chunk: string | Uint8Array) => void, terminal: ProgramIO['terminal']) {
|
|
44
|
+
super({ decodeStrings: false });
|
|
45
|
+
this.fd = fd;
|
|
46
|
+
this.isTTY = !!terminal;
|
|
47
|
+
if (terminal) {
|
|
48
|
+
this.columns = terminal.cols;
|
|
49
|
+
this.rows = terminal.rows;
|
|
50
|
+
terminal.onResize((cols, rows) => {
|
|
51
|
+
this.columns = cols;
|
|
52
|
+
this.rows = rows;
|
|
53
|
+
this.emit('resize');
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
override _write(chunk: string | Uint8Array, _encoding: string, callback: () => void): void {
|
|
59
|
+
this.sink(chunk);
|
|
60
|
+
callback();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Synchronous, like Node's stdout to a TTY or file: output order matches call order. */
|
|
64
|
+
override write(chunk: unknown, encoding?: any, callback?: any): boolean {
|
|
65
|
+
if (typeof encoding === 'function') callback = encoding;
|
|
66
|
+
this.sink(typeof chunk === 'string' || chunk instanceof Uint8Array ? chunk : String(chunk));
|
|
67
|
+
if (typeof callback === 'function') queueMicrotask(callback);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
getColorDepth(): number {
|
|
72
|
+
return this.isTTY ? 8 : 1;
|
|
73
|
+
}
|
|
74
|
+
hasColors(count = 16): boolean {
|
|
75
|
+
return this.isTTY && count <= 256;
|
|
76
|
+
}
|
|
77
|
+
getWindowSize(): [number, number] {
|
|
78
|
+
return [this.columns ?? 80, this.rows ?? 24];
|
|
79
|
+
}
|
|
80
|
+
cursorTo(x: number, y?: number | (() => void), cb?: () => void): boolean {
|
|
81
|
+
this.write(typeof y === 'number' ? `\x1b[${y + 1};${x + 1}H` : `\x1b[${x + 1}G`);
|
|
82
|
+
(typeof y === 'function' ? y : cb)?.();
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
moveCursor(dx: number, dy: number, cb?: () => void): boolean {
|
|
86
|
+
this.write((dx < 0 ? `\x1b[${-dx}D` : dx > 0 ? `\x1b[${dx}C` : '') + (dy < 0 ? `\x1b[${-dy}A` : dy > 0 ? `\x1b[${dy}B` : ''));
|
|
87
|
+
cb?.();
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
clearLine(dir = 0, cb?: () => void): boolean {
|
|
91
|
+
this.write(dir < 0 ? '\x1b[1K' : dir > 0 ? '\x1b[0K' : '\x1b[2K');
|
|
92
|
+
cb?.();
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
clearScreenDown(cb?: () => void): boolean {
|
|
96
|
+
this.write('\x1b[0J');
|
|
97
|
+
cb?.();
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
class StdinStream extends Readable {
|
|
103
|
+
isTTY: boolean;
|
|
104
|
+
isRaw = false;
|
|
105
|
+
fd = 0;
|
|
106
|
+
private subscription?: () => void;
|
|
107
|
+
private release?: () => void;
|
|
108
|
+
|
|
109
|
+
constructor(private readonly io: ProgramIO, private readonly loop: EventLoop) {
|
|
110
|
+
super({ encoding: 'utf8' });
|
|
111
|
+
this.isTTY = !!io.terminal;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
override _read(): void {
|
|
115
|
+
this.listen();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
override resume(): this {
|
|
119
|
+
this.listen();
|
|
120
|
+
return super.resume();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
override pause(): this {
|
|
124
|
+
this.stopListening();
|
|
125
|
+
return super.pause();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
setRawMode(raw: boolean): this {
|
|
129
|
+
this.isRaw = raw;
|
|
130
|
+
this.io.terminal?.setRawMode(raw);
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
ref(): this {
|
|
135
|
+
if (this.subscription && !this.release) this.release = this.loop.hold();
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
unref(): this {
|
|
140
|
+
this.release?.();
|
|
141
|
+
this.release = undefined;
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private listen(): void {
|
|
146
|
+
if (this.subscription) return;
|
|
147
|
+
this.release = this.loop.hold();
|
|
148
|
+
this.subscription = this.io.onStdin((chunk) => {
|
|
149
|
+
if (chunk === null) {
|
|
150
|
+
this.stopListening();
|
|
151
|
+
this.push(null);
|
|
152
|
+
} else this.push(chunk);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private stopListening(): void {
|
|
157
|
+
this.subscription?.();
|
|
158
|
+
this.subscription = undefined;
|
|
159
|
+
this.release?.();
|
|
160
|
+
this.release = undefined;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export type NodeProcess = EventEmitterBase & Record<string, any>;
|
|
165
|
+
|
|
166
|
+
export function createProcess(options: ProcessOptions): NodeProcess {
|
|
167
|
+
const { io, loop, fs } = options;
|
|
168
|
+
let cwd = normalizePath(options.cwd);
|
|
169
|
+
let exited = false;
|
|
170
|
+
const started = performance.now();
|
|
171
|
+
const proc = new EventEmitterBase() as NodeProcess;
|
|
172
|
+
const pid = nextPid++;
|
|
173
|
+
|
|
174
|
+
const exit = (code?: number) => {
|
|
175
|
+
if (code !== undefined) proc.exitCode = code;
|
|
176
|
+
if (!exited) {
|
|
177
|
+
exited = true;
|
|
178
|
+
const final = Number(proc.exitCode ?? 0) || 0;
|
|
179
|
+
try {
|
|
180
|
+
proc.emit('exit', final);
|
|
181
|
+
} catch {
|
|
182
|
+
// an 'exit' listener that throws must not stop the exit
|
|
183
|
+
}
|
|
184
|
+
options.onExit(Number(proc.exitCode ?? final) || 0);
|
|
185
|
+
}
|
|
186
|
+
throw new ProcessExit(Number(proc.exitCode ?? 0) || 0);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const hrtime = (previous?: [number, number]): [number, number] => {
|
|
190
|
+
const now = performance.now();
|
|
191
|
+
let seconds = Math.floor(now / 1000);
|
|
192
|
+
let nanos = Math.floor((now % 1000) * 1e6);
|
|
193
|
+
if (previous) {
|
|
194
|
+
seconds -= previous[0];
|
|
195
|
+
nanos -= previous[1];
|
|
196
|
+
if (nanos < 0) {
|
|
197
|
+
seconds--;
|
|
198
|
+
nanos += 1e9;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return [seconds, nanos];
|
|
202
|
+
};
|
|
203
|
+
hrtime.bigint = () => BigInt(Math.floor(performance.now() * 1e6));
|
|
204
|
+
|
|
205
|
+
const memoryUsage = () => ({ rss: 50e6, heapTotal: 30e6, heapUsed: 20e6, external: 1e6, arrayBuffers: 1e5 });
|
|
206
|
+
memoryUsage.rss = () => 50e6;
|
|
207
|
+
|
|
208
|
+
Object.assign(proc, {
|
|
209
|
+
title: 'node',
|
|
210
|
+
version: `v${NODE_VERSION}`,
|
|
211
|
+
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' },
|
|
212
|
+
release: { name: 'node', lts: 'Iron' },
|
|
213
|
+
platform: 'linux',
|
|
214
|
+
arch: 'x64',
|
|
215
|
+
pid,
|
|
216
|
+
ppid: pid - 1,
|
|
217
|
+
argv: options.argv,
|
|
218
|
+
argv0: 'node',
|
|
219
|
+
execPath: '/usr/local/bin/node',
|
|
220
|
+
execArgv: [],
|
|
221
|
+
env: options.env,
|
|
222
|
+
exitCode: undefined as number | undefined,
|
|
223
|
+
config: { variables: {} },
|
|
224
|
+
features: { inspector: false, ipv6: true, tls: true, typescript: false },
|
|
225
|
+
allowedNodeEnvironmentFlags: new Set<string>(),
|
|
226
|
+
stdout: new TtyWriteStream(1, io.stdout, io.terminal),
|
|
227
|
+
stderr: new TtyWriteStream(2, io.stderr, io.terminal),
|
|
228
|
+
stdin: new StdinStream(io, loop),
|
|
229
|
+
cwd: () => cwd,
|
|
230
|
+
chdir(dir: string) {
|
|
231
|
+
const target = dir.startsWith('/') ? normalizePath(dir) : join(cwd, dir);
|
|
232
|
+
if (!fs.isDirectory(target)) throw fsError(fs.isFile(target) ? 'ENOTDIR' : 'ENOENT', 'chdir', target);
|
|
233
|
+
cwd = target;
|
|
234
|
+
},
|
|
235
|
+
exit,
|
|
236
|
+
reallyExit: exit,
|
|
237
|
+
abort: () => exit(134),
|
|
238
|
+
kill(target: number, signal: string | number = 'SIGTERM') {
|
|
239
|
+
if (target !== pid) throw Object.assign(new Error('kill ESRCH'), { code: 'ESRCH' });
|
|
240
|
+
if (proc.listenerCount(String(signal))) proc.emit(String(signal), String(signal));
|
|
241
|
+
else exit(143);
|
|
242
|
+
return true;
|
|
243
|
+
},
|
|
244
|
+
nextTick(fn: (...args: unknown[]) => void, ...args: unknown[]) {
|
|
245
|
+
queueMicrotask(() => loop._run(() => fn(...args)));
|
|
246
|
+
},
|
|
247
|
+
hrtime,
|
|
248
|
+
memoryUsage,
|
|
249
|
+
cpuUsage: () => ({ user: Math.floor((performance.now() - started) * 1000), system: 0 }),
|
|
250
|
+
resourceUsage: () => ({ userCPUTime: 0, systemCPUTime: 0, maxRSS: 50000 }),
|
|
251
|
+
uptime: () => (performance.now() - started) / 1000,
|
|
252
|
+
umask: () => 0o22,
|
|
253
|
+
getuid: () => 1000,
|
|
254
|
+
geteuid: () => 1000,
|
|
255
|
+
getgid: () => 1000,
|
|
256
|
+
getegid: () => 1000,
|
|
257
|
+
getgroups: () => [1000],
|
|
258
|
+
emitWarning(warning: string | Error, type?: string | { type?: string }) {
|
|
259
|
+
const name = typeof type === 'string' ? type : type?.type ?? (warning instanceof Error ? warning.name : 'Warning');
|
|
260
|
+
const message = warning instanceof Error ? warning.message : warning;
|
|
261
|
+
proc.emit('warning', Object.assign(new Error(message), { name }));
|
|
262
|
+
io.stderr(`(node:${pid}) ${name}: ${message}\n`);
|
|
263
|
+
},
|
|
264
|
+
binding(name: string) {
|
|
265
|
+
throw new Error(`No such module: ${name}`);
|
|
266
|
+
},
|
|
267
|
+
getActiveResourcesInfo: () => [],
|
|
268
|
+
setUncaughtExceptionCaptureCallback: () => undefined,
|
|
269
|
+
hasUncaughtExceptionCaptureCallback: () => false,
|
|
270
|
+
setSourceMapsEnabled: () => undefined,
|
|
271
|
+
report: { getReport: () => ({ header: { glibcVersionRuntime: '2.36' } }) },
|
|
272
|
+
connected: false,
|
|
273
|
+
});
|
|
274
|
+
return proc;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** console bound to a process's stdio (`console.log` in user code prints to the terminal). */
|
|
278
|
+
export function createConsole(proc: NodeProcess): Console {
|
|
279
|
+
const out = (stream: 'stdout' | 'stderr') => (...args: unknown[]) => proc[stream].write(format(...args) + '\n');
|
|
280
|
+
const counts = new Map<string, number>();
|
|
281
|
+
const timers = new Map<string, number>();
|
|
282
|
+
let indent = '';
|
|
283
|
+
const withIndent = (fn: (...args: unknown[]) => void) => (...args: unknown[]) => (indent ? fn(indent + format(...args).replace(/\n/g, '\n' + indent)) : fn(...args));
|
|
284
|
+
const log = withIndent(out('stdout'));
|
|
285
|
+
const error = withIndent(out('stderr'));
|
|
286
|
+
return {
|
|
287
|
+
log,
|
|
288
|
+
info: log,
|
|
289
|
+
debug: log,
|
|
290
|
+
warn: error,
|
|
291
|
+
error,
|
|
292
|
+
trace: (...args: unknown[]) => error(`Trace: ${format(...args)}\n${new Error().stack?.split('\n').slice(2).join('\n') ?? ''}`),
|
|
293
|
+
dir: (value: unknown) => log(value),
|
|
294
|
+
dirxml: log,
|
|
295
|
+
table: (value: unknown) => log(value),
|
|
296
|
+
assert: (condition: unknown, ...args: unknown[]) => {
|
|
297
|
+
if (!condition) error('Assertion failed' + (args.length ? `: ${format(...args)}` : ''));
|
|
298
|
+
},
|
|
299
|
+
count: (label = 'default') => {
|
|
300
|
+
counts.set(label, (counts.get(label) ?? 0) + 1);
|
|
301
|
+
log(`${label}: ${counts.get(label)}`);
|
|
302
|
+
},
|
|
303
|
+
countReset: (label = 'default') => void counts.delete(label),
|
|
304
|
+
group: (...args: unknown[]) => {
|
|
305
|
+
if (args.length) log(...args);
|
|
306
|
+
indent += ' ';
|
|
307
|
+
},
|
|
308
|
+
groupCollapsed: (...args: unknown[]) => {
|
|
309
|
+
if (args.length) log(...args);
|
|
310
|
+
indent += ' ';
|
|
311
|
+
},
|
|
312
|
+
groupEnd: () => void (indent = indent.slice(2)),
|
|
313
|
+
time: (label = 'default') => void timers.set(label, performance.now()),
|
|
314
|
+
timeLog: (label = 'default', ...args: unknown[]) => log(`${label}: ${(performance.now() - (timers.get(label) ?? 0)).toFixed(3)}ms`, ...args),
|
|
315
|
+
timeEnd: (label = 'default') => {
|
|
316
|
+
log(`${label}: ${(performance.now() - (timers.get(label) ?? 0)).toFixed(3)}ms`);
|
|
317
|
+
timers.delete(label);
|
|
318
|
+
},
|
|
319
|
+
clear: () => undefined,
|
|
320
|
+
profile: () => undefined,
|
|
321
|
+
profileEnd: () => undefined,
|
|
322
|
+
timeStamp: () => undefined,
|
|
323
|
+
} as unknown as Console;
|
|
324
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `node` command: CLI flags, the entry script (or `-e`/`-p`, or stdin), and
|
|
3
|
+
* a small REPL when attached to a terminal with nothing to run.
|
|
4
|
+
*/
|
|
5
|
+
import type { Program, ProgramContext } from '../system/types.js';
|
|
6
|
+
import { join } from '../paths.js';
|
|
7
|
+
import { readBinLink } from '../installer/bins.js';
|
|
8
|
+
import { startNode, type NodeInstance } from './runtime.js';
|
|
9
|
+
import { NODE_VERSION } from './process.js';
|
|
10
|
+
import { inspect } from './inspect.js';
|
|
11
|
+
import { parseDotenv } from '../projectConfig.js';
|
|
12
|
+
|
|
13
|
+
interface NodeCli {
|
|
14
|
+
entry?: string;
|
|
15
|
+
args: string[];
|
|
16
|
+
evaluate?: string;
|
|
17
|
+
print?: boolean;
|
|
18
|
+
preload: string[];
|
|
19
|
+
envFiles: string[];
|
|
20
|
+
version?: boolean;
|
|
21
|
+
help?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Flags that take the next argument as their value. */
|
|
25
|
+
const WITH_VALUE = new Set(['-r', '--require', '--import', '--loader', '--experimental-loader', '-C', '--conditions', '--input-type', '--title', '--env-file', '--stack-size']);
|
|
26
|
+
|
|
27
|
+
export function parseNodeArgs(argv: string[]): NodeCli {
|
|
28
|
+
const cli: NodeCli = { args: [], preload: [], envFiles: [] };
|
|
29
|
+
for (let i = 0; i < argv.length; i++) {
|
|
30
|
+
const arg = argv[i]!;
|
|
31
|
+
if (arg === '--') {
|
|
32
|
+
[cli.entry, ...cli.args] = argv.slice(i + 1);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
if (!arg.startsWith('-') || arg === '-') {
|
|
36
|
+
cli.entry = arg;
|
|
37
|
+
cli.args = argv.slice(i + 1);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
const [flag, inline] = arg.includes('=') ? [arg.slice(0, arg.indexOf('=')), arg.slice(arg.indexOf('=') + 1)] : [arg, undefined];
|
|
41
|
+
const value = () => inline ?? argv[++i] ?? '';
|
|
42
|
+
if (flag === '-v' || flag === '--version') cli.version = true;
|
|
43
|
+
else if (flag === '-h' || flag === '--help') cli.help = true;
|
|
44
|
+
else if (flag === '-e' || flag === '--eval') cli.evaluate = value();
|
|
45
|
+
else if (flag === '-p' || flag === '--print') {
|
|
46
|
+
cli.evaluate = value();
|
|
47
|
+
cli.print = true;
|
|
48
|
+
} else if (flag === '-r' || flag === '--require' || flag === '--import') cli.preload.push(value());
|
|
49
|
+
else if (flag === '--env-file') cli.envFiles.push(value());
|
|
50
|
+
else if (WITH_VALUE.has(flag) && inline === undefined) i++;
|
|
51
|
+
// everything else (--inspect, --no-warnings, --max-old-space-size=…) is accepted and ignored
|
|
52
|
+
}
|
|
53
|
+
if (cli.evaluate !== undefined && cli.entry !== undefined) {
|
|
54
|
+
cli.args = [cli.entry, ...cli.args];
|
|
55
|
+
cli.entry = undefined;
|
|
56
|
+
}
|
|
57
|
+
return cli;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const HELP = `Usage: node [options] [ script.js ] [arguments]
|
|
61
|
+
node -e "script" evaluate script
|
|
62
|
+
node -p "expr" evaluate and print
|
|
63
|
+
Options: -v, -r <module>, --env-file=<file>. Running in BuilderForce WebContainers.
|
|
64
|
+
`;
|
|
65
|
+
|
|
66
|
+
async function readAllStdin(ctx: ProgramContext): Promise<string> {
|
|
67
|
+
let text = '';
|
|
68
|
+
await new Promise<void>((resolve) => {
|
|
69
|
+
const stop = ctx.io.onStdin((chunk) => {
|
|
70
|
+
if (chunk === null) {
|
|
71
|
+
stop();
|
|
72
|
+
resolve();
|
|
73
|
+
} else text += chunk;
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
return text;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Evaluate REPL lines in one persistent sloppy-mode scope (`const`/`let` become `var` so they survive the line). */
|
|
80
|
+
function startRepl(instance: NodeInstance, ctx: ProgramContext): void {
|
|
81
|
+
const { io } = ctx;
|
|
82
|
+
const scope = evalInProcess(instance, ctx.cwd, '[repl]', `(function* () { let __r; while (true) { const __line = yield __r; try { __r = { value: eval(__line) }; } catch (e) { __r = { error: e }; } } })()`) as Generator<{ value?: unknown; error?: unknown } | undefined, never, string>;
|
|
83
|
+
scope.next();
|
|
84
|
+
const release = instance.hold();
|
|
85
|
+
io.stdout(`Welcome to Node.js v${NODE_VERSION}.\nType ".exit" to leave.\n> `);
|
|
86
|
+
const leave = () => {
|
|
87
|
+
stop();
|
|
88
|
+
release();
|
|
89
|
+
instance.run(() => instance.process.exit(0));
|
|
90
|
+
};
|
|
91
|
+
const stop = io.onStdin((chunk) => {
|
|
92
|
+
if (chunk === null) return leave();
|
|
93
|
+
for (const raw of chunk.split(/\r?\n/)) {
|
|
94
|
+
const line = raw.trim();
|
|
95
|
+
if (!line) continue;
|
|
96
|
+
if (line === '.exit') return leave();
|
|
97
|
+
const result = scope.next(line.replace(/^(const|let)\s/, 'var ')).value!;
|
|
98
|
+
if ('error' in result) io.stdout(`Uncaught ${result.error instanceof Error ? result.error.stack ?? result.error.message : inspect(result.error)}\n`);
|
|
99
|
+
else io.stdout(inspect(result.value, { colors: false }) + '\n');
|
|
100
|
+
}
|
|
101
|
+
io.stdout('> ');
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Evaluate `expression` inside the CommonJS wrapper, so it sees require, process, timers… of this process. */
|
|
106
|
+
function evalInProcess(instance: NodeInstance, cwd: string, name: string, expression: string): unknown {
|
|
107
|
+
const filename = join(cwd, name);
|
|
108
|
+
const module = new instance.modules.Module(filename);
|
|
109
|
+
return module._compile(`return eval(${JSON.stringify(expression)});`, filename);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const nodeProgram: Program = async (ctx) => {
|
|
113
|
+
const cli = parseNodeArgs(ctx.args);
|
|
114
|
+
if (cli.version) {
|
|
115
|
+
ctx.io.stdout(`v${NODE_VERSION}\n`);
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
if (cli.help) {
|
|
119
|
+
ctx.io.stdout(HELP);
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
const env = { ...ctx.env };
|
|
123
|
+
for (const file of cli.envFiles) {
|
|
124
|
+
const text = ctx.system.fs.readText(join(ctx.cwd, file));
|
|
125
|
+
if (text === undefined) {
|
|
126
|
+
ctx.io.stderr(`node: ${file}: not found\n`);
|
|
127
|
+
return 9;
|
|
128
|
+
}
|
|
129
|
+
Object.assign(env, parseDotenv(text));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let entry: string | undefined;
|
|
133
|
+
if (cli.entry !== undefined && cli.entry !== '-') {
|
|
134
|
+
entry = cli.entry.startsWith('/') ? cli.entry : join(ctx.cwd, cli.entry);
|
|
135
|
+
entry = readBinLink(ctx.system.fs, entry) ?? entry;
|
|
136
|
+
}
|
|
137
|
+
const source = cli.evaluate ?? (cli.entry === '-' || (!entry && !ctx.io.terminal) ? await readAllStdin(ctx) : undefined);
|
|
138
|
+
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 });
|
|
140
|
+
const onAbort = () => instance.kill(typeof ctx.signal.reason === 'string' ? ctx.signal.reason : 'SIGTERM');
|
|
141
|
+
if (ctx.signal.aborted) onAbort();
|
|
142
|
+
ctx.signal.addEventListener('abort', onAbort);
|
|
143
|
+
const untrap = ctx.system.trapUncaught?.((error) => instance.reportUncaught(error));
|
|
144
|
+
|
|
145
|
+
instance.run(() => {
|
|
146
|
+
const require = instance.modules.createRequire(join(ctx.cwd, '[eval]'));
|
|
147
|
+
for (const id of cli.preload) require(id.startsWith('.') ? join(ctx.cwd, id) : id);
|
|
148
|
+
if (entry) {
|
|
149
|
+
let resolved: string;
|
|
150
|
+
try {
|
|
151
|
+
resolved = instance.modules.Module._resolveFilename(entry, undefined);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if ((error as { code?: string }).code === 'MODULE_NOT_FOUND') throw Object.assign(new Error(`Cannot find module '${entry}'`), { code: 'MODULE_NOT_FOUND', requireStack: [] });
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
return instance.modules.runMain(resolved).__tla;
|
|
157
|
+
}
|
|
158
|
+
if (source !== undefined) {
|
|
159
|
+
const result = evalInProcess(instance, ctx.cwd, '[eval]', source);
|
|
160
|
+
if (cli.print) instance.process.stdout.write(inspect(result) + '\n');
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
startRepl(instance, ctx);
|
|
164
|
+
return undefined;
|
|
165
|
+
});
|
|
166
|
+
const code = await instance.exit;
|
|
167
|
+
ctx.signal.removeEventListener('abort', onAbort);
|
|
168
|
+
untrap?.();
|
|
169
|
+
return code;
|
|
170
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `readline` and `readline/promises`: line splitting over any input stream,
|
|
3
|
+
* `question`, prompts and async iteration — what interactive CLIs (create-*,
|
|
4
|
+
* inquirer's simple paths, REPL-ish scripts) use. Input arrives already
|
|
5
|
+
* line-edited by the terminal's line discipline, so no keypress decoding is
|
|
6
|
+
* needed beyond `emitKeypressEvents` delivering characters.
|
|
7
|
+
*/
|
|
8
|
+
import { EventEmitterBase } from './events.js';
|
|
9
|
+
|
|
10
|
+
interface Input extends EventEmitterBase {
|
|
11
|
+
resume?(): unknown;
|
|
12
|
+
pause?(): unknown;
|
|
13
|
+
setEncoding?(e: string): unknown;
|
|
14
|
+
}
|
|
15
|
+
interface Output {
|
|
16
|
+
write(chunk: string): unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class Interface extends EventEmitterBase {
|
|
20
|
+
line = '';
|
|
21
|
+
cursor = 0;
|
|
22
|
+
terminal: boolean;
|
|
23
|
+
closed = false;
|
|
24
|
+
private buffer = '';
|
|
25
|
+
private promptText = '> ';
|
|
26
|
+
private pendingQuestion?: (answer: string) => void;
|
|
27
|
+
private readonly onData = (chunk: unknown) => this.feed(String(chunk));
|
|
28
|
+
private readonly onEnd = () => {
|
|
29
|
+
if (this.buffer) this.emitLine(this.buffer);
|
|
30
|
+
this.buffer = '';
|
|
31
|
+
this.close();
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
constructor(readonly input: Input, readonly output?: Output, options: { prompt?: string; terminal?: boolean } = {}) {
|
|
35
|
+
super();
|
|
36
|
+
this.terminal = options.terminal ?? !!(output as { isTTY?: boolean } | undefined)?.isTTY;
|
|
37
|
+
if (options.prompt !== undefined) this.promptText = options.prompt;
|
|
38
|
+
input.setEncoding?.('utf8');
|
|
39
|
+
input.on('data', this.onData);
|
|
40
|
+
input.on('end', this.onEnd);
|
|
41
|
+
input.resume?.();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setPrompt(prompt: string): void {
|
|
45
|
+
this.promptText = prompt;
|
|
46
|
+
}
|
|
47
|
+
getPrompt(): string {
|
|
48
|
+
return this.promptText;
|
|
49
|
+
}
|
|
50
|
+
prompt(): void {
|
|
51
|
+
this.output?.write(this.promptText);
|
|
52
|
+
}
|
|
53
|
+
question(query: string, options: unknown, callback?: (answer: string) => void): void {
|
|
54
|
+
const cb = (typeof options === 'function' ? options : callback) as (answer: string) => void;
|
|
55
|
+
this.output?.write(query);
|
|
56
|
+
this.pendingQuestion = cb;
|
|
57
|
+
}
|
|
58
|
+
write(data: string): void {
|
|
59
|
+
this.feed(data);
|
|
60
|
+
}
|
|
61
|
+
pause(): this {
|
|
62
|
+
this.input.pause?.();
|
|
63
|
+
this.emit('pause');
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
resume(): this {
|
|
67
|
+
this.input.resume?.();
|
|
68
|
+
this.emit('resume');
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
close(): void {
|
|
72
|
+
if (this.closed) return;
|
|
73
|
+
this.closed = true;
|
|
74
|
+
this.input.removeListener('data', this.onData);
|
|
75
|
+
this.input.removeListener('end', this.onEnd);
|
|
76
|
+
this.input.pause?.();
|
|
77
|
+
this.emit('close');
|
|
78
|
+
}
|
|
79
|
+
getCursorPos(): { rows: number; cols: number } {
|
|
80
|
+
return { rows: 0, cols: this.cursor };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<string> {
|
|
84
|
+
const queue: string[] = [];
|
|
85
|
+
let wake: (() => void) | undefined;
|
|
86
|
+
this.on('line', (line: string) => {
|
|
87
|
+
queue.push(line);
|
|
88
|
+
wake?.();
|
|
89
|
+
});
|
|
90
|
+
this.on('close', () => wake?.());
|
|
91
|
+
while (true) {
|
|
92
|
+
if (queue.length) yield queue.shift()!;
|
|
93
|
+
else if (this.closed) return;
|
|
94
|
+
else await new Promise<void>((resolve) => (wake = resolve));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private feed(text: string): void {
|
|
99
|
+
this.buffer += text;
|
|
100
|
+
for (;;) {
|
|
101
|
+
const nl = this.buffer.search(/\r\n|\n|\r/);
|
|
102
|
+
if (nl < 0) break;
|
|
103
|
+
const line = this.buffer.slice(0, nl);
|
|
104
|
+
this.buffer = this.buffer.slice(this.buffer[nl] === '\r' && this.buffer[nl + 1] === '\n' ? nl + 2 : nl + 1);
|
|
105
|
+
this.emitLine(line);
|
|
106
|
+
}
|
|
107
|
+
this.line = this.buffer;
|
|
108
|
+
this.cursor = this.buffer.length;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private emitLine(line: string): void {
|
|
112
|
+
const question = this.pendingQuestion;
|
|
113
|
+
if (question) {
|
|
114
|
+
this.pendingQuestion = undefined;
|
|
115
|
+
question(line);
|
|
116
|
+
} else this.emit('line', line);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const ansi = (stream: Output | undefined, sequence: string, cb?: () => void) => {
|
|
121
|
+
stream?.write(sequence);
|
|
122
|
+
cb?.();
|
|
123
|
+
return true;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export function createReadlineModule(promises = false): Record<string, unknown> {
|
|
127
|
+
const createInterface = (input: Input | { input: Input; output?: Output; prompt?: string; terminal?: boolean }, output?: Output) => {
|
|
128
|
+
const opts = 'input' in input && (input as { input?: unknown }).input ? (input as { input: Input; output?: Output; prompt?: string; terminal?: boolean }) : { input: input as Input, output };
|
|
129
|
+
const rl = new Interface(opts.input, opts.output, opts);
|
|
130
|
+
if (promises) {
|
|
131
|
+
const ask = rl.question.bind(rl);
|
|
132
|
+
(rl as unknown as { question: (q: string) => Promise<string> }).question = (query: string) => new Promise((resolve) => ask(query, undefined, resolve));
|
|
133
|
+
}
|
|
134
|
+
return rl;
|
|
135
|
+
};
|
|
136
|
+
const mod: Record<string, unknown> = {
|
|
137
|
+
createInterface,
|
|
138
|
+
Interface,
|
|
139
|
+
clearLine: (stream: Output, dir: number, cb?: () => void) => ansi(stream, dir < 0 ? '\x1b[1K' : dir > 0 ? '\x1b[0K' : '\x1b[2K', cb),
|
|
140
|
+
clearScreenDown: (stream: Output, cb?: () => void) => ansi(stream, '\x1b[0J', cb),
|
|
141
|
+
cursorTo: (stream: Output, x: number, y?: number | (() => void), cb?: () => void) => ansi(stream, typeof y === 'number' ? `\x1b[${y + 1};${x + 1}H` : `\x1b[${x + 1}G`, typeof y === 'function' ? y : cb),
|
|
142
|
+
moveCursor: (stream: Output, dx: number, dy: number, cb?: () => void) =>
|
|
143
|
+
ansi(stream, (dx < 0 ? `\x1b[${-dx}D` : dx > 0 ? `\x1b[${dx}C` : '') + (dy < 0 ? `\x1b[${-dy}A` : dy > 0 ? `\x1b[${dy}B` : ''), cb),
|
|
144
|
+
emitKeypressEvents(stream: EventEmitterBase) {
|
|
145
|
+
if ((stream as { __keypress?: boolean }).__keypress) return;
|
|
146
|
+
(stream as { __keypress?: boolean }).__keypress = true;
|
|
147
|
+
stream.on('data', (chunk: unknown) => {
|
|
148
|
+
for (const ch of String(chunk)) {
|
|
149
|
+
const name = ch === '\r' || ch === '\n' ? 'return' : ch === '\x7f' ? 'backspace' : ch === '\x03' ? 'c' : /[a-z]/i.test(ch) ? ch.toLowerCase() : undefined;
|
|
150
|
+
stream.emit('keypress', ch, { sequence: ch, name, ctrl: ch === '\x03', meta: false, shift: /[A-Z]/.test(ch) });
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
if (!promises) mod.promises = createReadlineModule(true);
|
|
156
|
+
return mod;
|
|
157
|
+
}
|