@nimbus-sh/core 0.3.0 → 0.5.0
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/LICENSE +21 -0
- package/README.md +140 -0
- package/dist/_shared/error-text.d.ts +15 -0
- package/dist/_shared/error-text.d.ts.map +1 -0
- package/dist/_shared/error-text.js +21 -0
- package/dist/_shared/exports-resolver.d.ts +19 -7
- package/dist/_shared/exports-resolver.d.ts.map +1 -1
- package/dist/_shared/exports-resolver.js +1 -1
- package/dist/_shared/retry.d.ts.map +1 -1
- package/dist/_shared/retry.js +3 -1
- package/dist/_shared/tarball-stream.d.ts +93 -0
- package/dist/_shared/tarball-stream.d.ts.map +1 -0
- package/dist/_shared/tarball-stream.js +235 -0
- package/dist/_shared/tarball.d.ts +17 -0
- package/dist/_shared/tarball.d.ts.map +1 -0
- package/dist/_shared/tarball.js +39 -0
- package/dist/_shared/w7-frame.js +2 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +30 -16
- package/dist/observability/oom-classify.d.ts +1 -1
- package/dist/observability/oom-classify.d.ts.map +1 -1
- package/dist/observability/oom-classify.js +7 -0
- package/dist/observability/oom-discriminator.d.ts +1 -1
- package/dist/runtime/clang-runner.d.ts +38 -0
- package/dist/runtime/clang-runner.d.ts.map +1 -0
- package/dist/runtime/clang-runner.js +866 -0
- package/dist/runtime/esbuild-service.d.ts +4 -3
- package/dist/runtime/esbuild-service.d.ts.map +1 -1
- package/dist/runtime/esbuild-service.js +9 -4
- package/dist/runtime/facet-host.d.ts +12 -0
- package/dist/runtime/facet-host.d.ts.map +1 -1
- package/dist/runtime/installed-runtimes.d.ts +4 -3
- package/dist/runtime/installed-runtimes.d.ts.map +1 -1
- package/dist/runtime/local-facet-host.d.ts.map +1 -1
- package/dist/runtime/local-facet-host.js +29 -9
- package/dist/runtime/long-running-handle.d.ts +3 -3
- package/dist/runtime/long-running-handle.d.ts.map +1 -1
- package/dist/runtime/os-contracts.d.ts +7 -5
- package/dist/runtime/os-contracts.d.ts.map +1 -1
- package/dist/runtime/port-registry.d.ts +28 -0
- package/dist/runtime/port-registry.d.ts.map +1 -1
- package/dist/runtime/port-registry.js +79 -9
- package/dist/runtime/process-logs.d.ts +9 -1
- package/dist/runtime/process-logs.d.ts.map +1 -1
- package/dist/runtime/project-detect.d.ts +6 -1
- package/dist/runtime/project-detect.d.ts.map +1 -1
- package/dist/runtime/require-resolver.d.ts.map +1 -1
- package/dist/runtime/require-resolver.js +3 -4
- package/dist/runtime/ruby-gems.d.ts +30 -0
- package/dist/runtime/ruby-gems.d.ts.map +1 -0
- package/dist/runtime/ruby-gems.js +636 -0
- package/dist/runtime/ruby-runner.d.ts +127 -0
- package/dist/runtime/ruby-runner.d.ts.map +1 -0
- package/dist/runtime/ruby-runner.js +1357 -0
- package/dist/runtime/runtime-registry.d.ts +4 -3
- package/dist/runtime/runtime-registry.d.ts.map +1 -1
- package/dist/runtime/runtime-registry.js +2 -1
- package/dist/runtime/session-process-supervisor.d.ts +15 -0
- package/dist/runtime/session-process-supervisor.d.ts.map +1 -1
- package/dist/runtime/session-process-supervisor.js +30 -0
- package/dist/runtime/sqlite-runtime-fs-bridge.js +1 -5
- package/dist/runtime/virtual-socket-kernel.d.ts.map +1 -1
- package/dist/runtime/virtual-socket-kernel.generated.d.ts +1 -1
- package/dist/runtime/virtual-socket-kernel.generated.d.ts.map +1 -1
- package/dist/runtime/virtual-socket-kernel.generated.js +2 -2
- package/dist/runtime/virtual-socket-kernel.js +10 -5
- package/dist/runtime/wasm-runner.d.ts.map +1 -1
- package/dist/runtime/wasm-runner.js +22 -14
- package/dist/shell/features.d.ts.map +1 -1
- package/dist/shell/features.js +5 -4
- package/dist/shell/unix-commands.d.ts +14 -1
- package/dist/shell/unix-commands.d.ts.map +1 -1
- package/dist/shell/unix-commands.js +115 -69
- package/dist/substrate/lifo/commands/net/ip.d.ts.map +1 -1
- package/dist/substrate/lifo/commands/system/ps.d.ts +2 -1
- package/dist/substrate/lifo/commands/system/ps.d.ts.map +1 -1
- package/dist/substrate/lifo/kernel/ServiceManager.d.ts.map +1 -1
- package/dist/substrate/lifo/kernel/ServiceManager.js +0 -2
- package/dist/substrate/lifo/kernel/network/NetworkStack.d.ts +22 -11
- package/dist/substrate/lifo/kernel/network/NetworkStack.d.ts.map +1 -1
- package/dist/substrate/lifo/kernel/network/NetworkStack.js +0 -2
- package/dist/substrate/lifo/kernel/network/tunnel/WebSocketTunnel.d.ts +2 -1
- package/dist/substrate/lifo/kernel/network/tunnel/WebSocketTunnel.d.ts.map +1 -1
- package/dist/substrate/lifo/kernel/network/tunnel/WebSocketTunnel.js +3 -4
- package/dist/substrate/lifo/kernel/vfs/VFS.d.ts +17 -0
- package/dist/substrate/lifo/kernel/vfs/VFS.d.ts.map +1 -1
- package/dist/substrate/lifo/kernel/vfs/VFS.js +33 -0
- package/dist/substrate/lifo/node-compat/esbuild.d.ts.map +1 -1
- package/dist/substrate/lifo/node-compat/esbuild.js +2 -4
- package/dist/substrate/lifo/node-compat/index.d.ts.map +1 -1
- package/dist/substrate/lifo/node-compat/index.js +3 -2
- package/dist/substrate/lifo/node-compat/loose-equality.d.ts +19 -0
- package/dist/substrate/lifo/node-compat/loose-equality.d.ts.map +1 -0
- package/dist/substrate/lifo/node-compat/loose-equality.js +91 -0
- package/dist/substrate/lifo/node-compat/process.d.ts.map +1 -1
- package/dist/substrate/lifo/node-compat/process.js +6 -3
- package/dist/substrate/lifo/shell/Shell.d.ts +19 -0
- package/dist/substrate/lifo/shell/Shell.d.ts.map +1 -1
- package/dist/substrate/lifo/shell/Shell.js +86 -75
- package/dist/substrate/lifo/shell/lexer.d.ts +9 -0
- package/dist/substrate/lifo/shell/lexer.d.ts.map +1 -1
- package/dist/substrate/lifo/shell/lexer.js +40 -0
- package/dist/vfs/seed-project.d.ts.map +1 -1
- package/dist/vfs/seed-project.js +8 -2
- package/dist/vfs/sqlite-vfs.d.ts +61 -1
- package/dist/vfs/sqlite-vfs.d.ts.map +1 -1
- package/dist/vfs/sqlite-vfs.js +199 -35
- package/dist/workspace/nimbus-workspace.d.ts.map +1 -1
- package/dist/workspace/nimbus-workspace.js +9 -2
- package/package.json +4 -2
- package/src/_shared/error-text.ts +21 -0
- package/src/_shared/exports-resolver.ts +24 -4
- package/src/_shared/retry.ts +4 -2
- package/src/_shared/tarball-stream.ts +263 -0
- package/src/_shared/tarball.ts +46 -0
- package/src/_shared/w7-frame.ts +2 -1
- package/src/constants.ts +30 -16
- package/src/observability/oom-classify.ts +7 -0
- package/src/observability/oom-discriminator.ts +1 -1
- package/src/runtime/clang-runner.ts +924 -0
- package/src/runtime/esbuild-service.ts +16 -11
- package/src/runtime/facet-host.ts +12 -0
- package/src/runtime/installed-runtimes.ts +4 -3
- package/src/runtime/local-facet-host.ts +28 -8
- package/src/runtime/long-running-handle.ts +3 -3
- package/src/runtime/os-contracts.ts +7 -5
- package/src/runtime/port-registry.ts +97 -9
- package/src/runtime/process-logs.ts +12 -3
- package/src/runtime/project-detect.ts +7 -1
- package/src/runtime/require-resolver.ts +7 -7
- package/src/runtime/ruby-gems.ts +682 -0
- package/src/runtime/ruby-runner.ts +1484 -0
- package/src/runtime/runtime-registry.ts +9 -7
- package/src/runtime/session-process-supervisor.ts +27 -0
- package/src/runtime/sqlite-runtime-fs-bridge.ts +9 -6
- package/src/runtime/virtual-socket-kernel.generated.ts +2 -2
- package/src/runtime/virtual-socket-kernel.ts +10 -5
- package/src/runtime/wasm-runner.ts +55 -29
- package/src/shell/features.ts +5 -4
- package/src/shell/unix-commands.ts +273 -128
- package/src/substrate/lifo/commands/net/ip.ts +15 -15
- package/src/substrate/lifo/commands/system/ps.ts +2 -1
- package/src/substrate/lifo/kernel/ServiceManager.ts +0 -2
- package/src/substrate/lifo/kernel/network/NetworkStack.ts +24 -17
- package/src/substrate/lifo/kernel/network/tunnel/WebSocketTunnel.ts +37 -12
- package/src/substrate/lifo/kernel/vfs/VFS.ts +34 -0
- package/src/substrate/lifo/node-compat/esbuild.ts +16 -8
- package/src/substrate/lifo/node-compat/index.ts +3 -2
- package/src/substrate/lifo/node-compat/loose-equality.ts +81 -0
- package/src/substrate/lifo/node-compat/process.ts +5 -2
- package/src/substrate/lifo/shell/Shell.ts +92 -70
- package/src/substrate/lifo/shell/lexer.ts +38 -0
- package/src/vfs/seed-project.ts +10 -3
- package/src/vfs/sqlite-vfs.ts +209 -36
- package/src/workspace/nimbus-workspace.ts +10 -1
|
@@ -40,6 +40,7 @@ import type { SqliteVFS } from '../vfs/sqlite-vfs.js';
|
|
|
40
40
|
import { type VfsCred } from './os-contracts.js';
|
|
41
41
|
import type { EsbuildService } from './esbuild-service.js';
|
|
42
42
|
import { type FacetBundleProfile } from './bundle-profile.js';
|
|
43
|
+
import type { Command, CommandContext } from '../substrate/lifo/commands/types.js';
|
|
43
44
|
/**
|
|
44
45
|
* Result shape that runtime-registry expects from a runner. Mirrors
|
|
45
46
|
* the existing RunFreshResult / RunBunResult shapes — kept narrow so
|
|
@@ -96,7 +97,7 @@ export declare function resolveRuntimeScriptPath(fs: ScriptResolutionFs, cwd: st
|
|
|
96
97
|
* to the very same execution path `bun <file>` takes, rather than growing
|
|
97
98
|
* a second one.
|
|
98
99
|
*/
|
|
99
|
-
export type RuntimeSubcommand = (ctx:
|
|
100
|
+
export type RuntimeSubcommand = (ctx: CommandContext, registry: ShellRegistry, runAsRuntime: (args: string[]) => Promise<number>) => Promise<number>;
|
|
100
101
|
export interface RuntimeSpec {
|
|
101
102
|
/** Shell-command name: 'node' / 'bun' / 'wasm-runner' / 'python'. */
|
|
102
103
|
name: string;
|
|
@@ -142,7 +143,7 @@ export interface RuntimeSpec {
|
|
|
142
143
|
* shell registry type tree when the runtime path only needs resolve().
|
|
143
144
|
*/
|
|
144
145
|
export interface ShellRegistry {
|
|
145
|
-
resolve(name: string): Promise<
|
|
146
|
+
resolve(name: string): Promise<Command | null | undefined> | Command | null | undefined;
|
|
146
147
|
}
|
|
147
148
|
/**
|
|
148
149
|
* Build a shell-handler function for a runtime. The returned function
|
|
@@ -158,5 +159,5 @@ export declare function buildRuntimeHandler(spec: RuntimeSpec, ctx0: {
|
|
|
158
159
|
* the module is loaded eagerly or on this call. */
|
|
159
160
|
getEsbuild(): EsbuildService | Promise<EsbuildService>;
|
|
160
161
|
registry: ShellRegistry;
|
|
161
|
-
}):
|
|
162
|
+
}): Command;
|
|
162
163
|
//# sourceMappingURL=runtime-registry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime-registry.d.ts","sourceRoot":"","sources":["../../src/runtime/runtime-registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAAe,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAA2B,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"runtime-registry.d.ts","sourceRoot":"","sources":["../../src/runtime/runtime-registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAAe,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAA2B,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEvF,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AAGnF;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAA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1B,qEAAqE;IACrE,IAAI,EAAE,MAAM,CAAC;IACb;oEACgE;IAChE,OAAO,EAAE,MAAM,CAAC;IAChB,gDAAgD;IAChD,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;;OAMG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IACnE;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;IAChD;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;;;;OAMG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;CACzF;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,WAAW,EACjB,IAAI,EAAE;IACJ,GAAG,EAAE,SAAS,CAAC;IACf;;wDAEoD;IACpD,UAAU,IAAI,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IACvD,QAAQ,EAAE,aAAa,CAAC;CACzB,GACA,OAAO,CAmTT"}
|
|
@@ -40,6 +40,7 @@ import { normalizeVfsPath, resolveVfsPath, vfsPathExtension } from '../vfs/path.
|
|
|
40
40
|
import { CRED_KERNEL } from './os-contracts.js';
|
|
41
41
|
import { parseFacetBundleProfile } from './bundle-profile.js';
|
|
42
42
|
import { bindImportMetaResolve, importMetaDefines } from './import-meta-transform.js';
|
|
43
|
+
import { errorText } from '../_shared/error-text.js';
|
|
43
44
|
/** Extensions probed when a target names no exact file, in Node's order. */
|
|
44
45
|
const SCRIPT_RESOLUTION_CANDIDATES = ['.js', '.ts', '.tsx', '.mjs', '.jsx', '/index.js', '/index.ts'];
|
|
45
46
|
/**
|
|
@@ -311,7 +312,7 @@ export function buildRuntimeHandler(spec, ctx0) {
|
|
|
311
312
|
code = bindImportMetaResolve(transformed.code, absUrl);
|
|
312
313
|
}
|
|
313
314
|
catch (e) {
|
|
314
|
-
ctx.stderr.write(`${name}: transform error for ${scriptPath}: ${e
|
|
315
|
+
ctx.stderr.write(`${name}: transform error for ${scriptPath}: ${errorText(e)}\n`);
|
|
315
316
|
return 1;
|
|
316
317
|
}
|
|
317
318
|
}
|
|
@@ -55,6 +55,8 @@ export declare class SessionProcessSupervisor {
|
|
|
55
55
|
private terminators;
|
|
56
56
|
/** Fires after every appendOutput/markExit once log persistence is wired. */
|
|
57
57
|
private logActivity;
|
|
58
|
+
/** Fires once per pid on its first terminal transition; see setOnTerminal. */
|
|
59
|
+
private onTerminalCb;
|
|
58
60
|
/** Allocate a PID and register a new process. */
|
|
59
61
|
spawn(command: string, argv: string[], cwd: string, opts?: ProcessSpawnOptions): ProcessEntry;
|
|
60
62
|
/** Mark an existing entry as long-running. Idempotent. */
|
|
@@ -76,6 +78,19 @@ export declare class SessionProcessSupervisor {
|
|
|
76
78
|
private terminate;
|
|
77
79
|
cred(pid: number): VfsCred;
|
|
78
80
|
setUmask(pid: number, umask: number): number;
|
|
81
|
+
/**
|
|
82
|
+
* Observe every pid's FIRST transition out of `running`, whichever door it
|
|
83
|
+
* leaves by — exit(), kill(), a facet's self-reported exit, a timeout abort:
|
|
84
|
+
* all of them end here, which is what makes this one callback a complete
|
|
85
|
+
* seam for per-pid durable state (the resident-launch journal) that must be
|
|
86
|
+
* released exactly when the process ends and never before.
|
|
87
|
+
*
|
|
88
|
+
* One slot, owned by the FacetManager. A second subscriber would mean two
|
|
89
|
+
* owners of process-end policy; grow this into a list only when a second
|
|
90
|
+
* genuine owner exists.
|
|
91
|
+
*/
|
|
92
|
+
setOnTerminal(cb: (pid: number) => void): void;
|
|
93
|
+
private fireTerminal;
|
|
79
94
|
/** Mark a process as exited. First terminal state wins. */
|
|
80
95
|
exit(pid: number, exitCode: number): void;
|
|
81
96
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-process-supervisor.d.ts","sourceRoot":"","sources":["../../src/runtime/session-process-supervisor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAqB,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAChF,OAAO,EACL,eAAe,EACf,KAAK,QAAQ,EACb,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,EACvB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,WAAW,mBAAmB;IAClC,sFAAsF;IACtF,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,wFAAwF;IACxF,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,0EAA0E;IAC1E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6EAA6E;IAC7E,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;GAKG;AACH,MAAM,WAAW,yBAAyB;IACxC,GAAG,EAAE,MAAM,CAAC;IACZ,wEAAwE;IACxE,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,qBAAa,wBAAwB;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAsB;IAC5C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA2B;IACjD,OAAO,CAAC,IAAI,CAAyB;IACrC,2EAA2E;IAC3E,OAAO,CAAC,WAAW,CAAiC;IACpD,6EAA6E;IAC7E,OAAO,CAAC,WAAW,CAA6B;
|
|
1
|
+
{"version":3,"file":"session-process-supervisor.d.ts","sourceRoot":"","sources":["../../src/runtime/session-process-supervisor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAqB,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAChF,OAAO,EACL,eAAe,EACf,KAAK,QAAQ,EACb,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,EACvB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,WAAW,mBAAmB;IAClC,sFAAsF;IACtF,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,wFAAwF;IACxF,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,0EAA0E;IAC1E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6EAA6E;IAC7E,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;GAKG;AACH,MAAM,WAAW,yBAAyB;IACxC,GAAG,EAAE,MAAM,CAAC;IACZ,wEAAwE;IACxE,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,qBAAa,wBAAwB;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAsB;IAC5C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA2B;IACjD,OAAO,CAAC,IAAI,CAAyB;IACrC,2EAA2E;IAC3E,OAAO,CAAC,WAAW,CAAiC;IACpD,6EAA6E;IAC7E,OAAO,CAAC,WA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AG,EAAE,MAAM,KAAK,OAAO,GAAG,MAAM;IAI9E,WAAW,IAAI,UAAU,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;IAItD;;;;OAIG;IACH,aAAa,IAAI,IAAI;CAItB"}
|
|
@@ -30,6 +30,8 @@ export class SessionProcessSupervisor {
|
|
|
30
30
|
terminators = new Map();
|
|
31
31
|
/** Fires after every appendOutput/markExit once log persistence is wired. */
|
|
32
32
|
logActivity = null;
|
|
33
|
+
/** Fires once per pid on its first terminal transition; see setOnTerminal. */
|
|
34
|
+
onTerminalCb = null;
|
|
33
35
|
// ── Lifecycle / PID authority ─────────────────────────────────────────
|
|
34
36
|
/** Allocate a PID and register a new process. */
|
|
35
37
|
spawn(command, argv, cwd, opts = {}) {
|
|
@@ -86,19 +88,47 @@ export class SessionProcessSupervisor {
|
|
|
86
88
|
setUmask(pid, umask) {
|
|
87
89
|
return this.table.setUmask(pid, umask);
|
|
88
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Observe every pid's FIRST transition out of `running`, whichever door it
|
|
93
|
+
* leaves by — exit(), kill(), a facet's self-reported exit, a timeout abort:
|
|
94
|
+
* all of them end here, which is what makes this one callback a complete
|
|
95
|
+
* seam for per-pid durable state (the resident-launch journal) that must be
|
|
96
|
+
* released exactly when the process ends and never before.
|
|
97
|
+
*
|
|
98
|
+
* One slot, owned by the FacetManager. A second subscriber would mean two
|
|
99
|
+
* owners of process-end policy; grow this into a list only when a second
|
|
100
|
+
* genuine owner exists.
|
|
101
|
+
*/
|
|
102
|
+
setOnTerminal(cb) {
|
|
103
|
+
this.onTerminalCb = cb;
|
|
104
|
+
}
|
|
105
|
+
fireTerminal(pid, wasRunning) {
|
|
106
|
+
if (!wasRunning || !this.onTerminalCb)
|
|
107
|
+
return;
|
|
108
|
+
if (this.table.get(pid)?.state === 'running')
|
|
109
|
+
return;
|
|
110
|
+
try {
|
|
111
|
+
this.onTerminalCb(pid);
|
|
112
|
+
}
|
|
113
|
+
catch { /* the process is gone regardless */ }
|
|
114
|
+
}
|
|
89
115
|
/** Mark a process as exited. First terminal state wins. */
|
|
90
116
|
exit(pid, exitCode) {
|
|
117
|
+
const wasRunning = this.table.get(pid)?.state === 'running';
|
|
91
118
|
this.table.exit(pid, exitCode);
|
|
92
119
|
this.terminators.delete(pid);
|
|
120
|
+
this.fireTerminal(pid, wasRunning);
|
|
93
121
|
}
|
|
94
122
|
/**
|
|
95
123
|
* Mark a process as killed and tear down its input channel so queued
|
|
96
124
|
* stdin can't outlive the process.
|
|
97
125
|
*/
|
|
98
126
|
kill(pid) {
|
|
127
|
+
const wasRunning = this.table.get(pid)?.state === 'running';
|
|
99
128
|
const killed = this.table.kill(pid);
|
|
100
129
|
this.terminate(pid);
|
|
101
130
|
this.input.close(pid);
|
|
131
|
+
this.fireTerminal(pid, wasRunning);
|
|
102
132
|
return killed;
|
|
103
133
|
}
|
|
104
134
|
/** Clean up exited processes older than maxAge ms. */
|
|
@@ -414,11 +414,7 @@ function normalizeOpenFlags(flags) {
|
|
|
414
414
|
};
|
|
415
415
|
}
|
|
416
416
|
function fsError(code, syscall, path) {
|
|
417
|
-
|
|
418
|
-
err.code = code;
|
|
419
|
-
err.syscall = syscall;
|
|
420
|
-
err.path = path;
|
|
421
|
-
return err;
|
|
417
|
+
return Object.assign(new Error(`${code}: ${syscall} '${path}'`), { code, syscall, path });
|
|
422
418
|
}
|
|
423
419
|
function hasErrorCode(error, code) {
|
|
424
420
|
return typeof error === 'object' && error !== null && 'code' in error && error.code === code;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"virtual-socket-kernel.d.ts","sourceRoot":"","sources":["../../src/runtime/virtual-socket-kernel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,6EAA6E;AAC7E,MAAM,WAAW,iBAAiB;IAChC,kGAAkG;IAClG,8BAA8B,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;I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kB;IAU1B,2FAA2F;IAC3F,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE;IAM5C;;;;;OAKG;IACG,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAMhE;;;;OAIG;IACH,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAK1B,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,sBAAsB,GAAG,MAAM;IAM3D,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAOvB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAI7B,kBAAkB,IAAI,MAAM,GAAG,IAAI;IAKnC,uFAAuF;IACvF,YAAY,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAuBnF,aAAa,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAgBzD,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC;IAyD1E,OAAO,CAAC,cAAc;CAUvB;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,GAAE,wBAAiE,GACvE,mBAAmB,CAKrB"}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* Consumed by python-runner.ts and ruby-runner.ts: spliced into the
|
|
9
9
|
* socket process worker module source passed to NimbusLoaderPool.
|
|
10
10
|
*
|
|
11
|
-
* Size: 33.
|
|
11
|
+
* Size: 33.76 KiB
|
|
12
12
|
*/
|
|
13
13
|
export declare const VIRTUAL_SOCKET_KERNEL_SRC: string;
|
|
14
14
|
//# sourceMappingURL=virtual-socket-kernel.generated.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"virtual-socket-kernel.generated.d.ts","sourceRoot":"","sources":["../../src/runtime/virtual-socket-kernel.generated.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,eAAO,MAAM,yBAAyB,EAAE,
|
|
1
|
+
{"version":3,"file":"virtual-socket-kernel.generated.d.ts","sourceRoot":"","sources":["../../src/runtime/virtual-socket-kernel.generated.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,eAAO,MAAM,yBAAyB,EAAE,MAAo8lC,CAAC"}
|
|
@@ -8,6 +8,6 @@
|
|
|
8
8
|
* Consumed by python-runner.ts and ruby-runner.ts: spliced into the
|
|
9
9
|
* socket process worker module source passed to NimbusLoaderPool.
|
|
10
10
|
*
|
|
11
|
-
* Size: 33.
|
|
11
|
+
* Size: 33.76 KiB
|
|
12
12
|
*/
|
|
13
|
-
export const VIRTUAL_SOCKET_KERNEL_SRC = "(() => {\n // ../core/src/runtime/virtual-socket-kernel.ts\n var DEFAULT_LIMITS = {\n responseTimeoutMs: 3e4,\n maxRequestBodyBytes: 32 * 1024 * 1024,\n maxResponseBufferBytes: 64 * 1024 * 1024\n };\n var EMPTY_BYTES = new Uint8Array(0);\n var Deferred = class {\n promise;\n resolve;\n reject;\n constructor() {\n let resolve = () => {\n };\n let reject = () => {\n };\n this.promise = new Promise((res, rej) => {\n resolve = res;\n reject = rej;\n });\n this.resolve = resolve;\n this.reject = reject;\n }\n };\n function isPyodideProxyLike(value) {\n return value !== null && typeof value === \"object\" && typeof value.toJs === \"function\";\n }\n function toBytes(value) {\n if (value instanceof Uint8Array) return value;\n if (value instanceof ArrayBuffer) return new Uint8Array(value);\n if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);\n if (Array.isArray(value)) return Uint8Array.from(value);\n if (typeof value === \"string\") return new TextEncoder().encode(value);\n if (isPyodideProxyLike(value)) return toBytes(value.toJs());\n return EMPTY_BYTES;\n }\n function concatBytes(parts) {\n let length = 0;\n for (const part of parts) length += part.byteLength;\n const out = new Uint8Array(length);\n let offset = 0;\n for (const part of parts) {\n out.set(part, offset);\n offset += part.byteLength;\n }\n return out;\n }\n function responseCanHaveBody(requestMethod, status) {\n if (requestMethod.toUpperCase() === \"HEAD\") return false;\n return status !== 204 && status !== 205 && status !== 304;\n }\n function encodeHttpRequest(request, body) {\n const url = new URL(request.url);\n const path = (url.pathname || \"/\") + url.search;\n const headers = new Headers(request.headers);\n if (!headers.has(\"Host\")) headers.set(\"Host\", url.host || \"nimbus.local\");\n if (body.byteLength > 0 && !headers.has(\"Content-Length\")) {\n headers.set(\"Content-Length\", String(body.byteLength));\n }\n const lines = [`${request.method} ${path} HTTP/1.1`];\n headers.forEach((value, key) => lines.push(`${key}: ${value}`));\n lines.push(\"\", \"\");\n return concatBytes([new TextEncoder().encode(lines.join(\"\\r\\n\")), body]);\n }\n function encodeHttpResponseHead(status, statusText, headers, chunkedBody) {\n const lines = [`HTTP/1.1 ${status} ${statusText}`];\n headers.forEach((value, key) => lines.push(`${key}: ${value}`));\n if (chunkedBody) lines.push(\"Transfer-Encoding: chunked\");\n lines.push(\"Connection: close\");\n lines.push(\"\", \"\");\n return new TextEncoder().encode(lines.join(\"\\r\\n\"));\n }\n function encodeChunkedFrame(bytes) {\n return concatBytes([\n new TextEncoder().encode(`${bytes.byteLength.toString(16)}\\r\n`),\n bytes,\n new TextEncoder().encode(\"\\r\\n\")\n ]);\n }\n var CHUNKED_TERMINATOR = new TextEncoder().encode(\"0\\r\\n\\r\\n\");\n var LOOPBACK_READ_HIGH_WATER_BYTES = 1024 * 1024;\n var LOOPBACK_STREAM_CHUNK_BYTES = 64 * 1024;\n var ByteChunkQueue = class {\n constructor(limitTotalBytes, overflowLabel) {\n this.limitTotalBytes = limitTotalBytes;\n this.overflowLabel = overflowLabel;\n }\n limitTotalBytes;\n overflowLabel;\n chunks = [];\n headOffset = 0;\n enqueuedTotalBytes = 0;\n undrainedBytes = 0;\n /** Bytes enqueued but not yet read. Drives flow control on streamed queues. */\n get pendingBytes() {\n return this.undrainedBytes;\n }\n enqueue(bytes) {\n if (bytes.byteLength === 0) return;\n if (this.enqueuedTotalBytes + bytes.byteLength > this.limitTotalBytes) {\n throw new Error(\n `Nimbus virtual socket: ${this.overflowLabel} exceeds ${this.limitTotalBytes} bytes`\n );\n }\n this.enqueuedTotalBytes += bytes.byteLength;\n this.undrainedBytes += bytes.byteLength;\n this.chunks.push(bytes);\n }\n /** Drain up to maxBytes from the head chunk; empty result means no data is queued. */\n readUpTo(maxBytes) {\n const head = this.chunks[0];\n if (!head) return EMPTY_BYTES;\n const available = head.byteLength - this.headOffset;\n const take = Math.min(available, Math.max(1, maxBytes));\n const out = head.subarray(this.headOffset, this.headOffset + take);\n this.headOffset += take;\n this.undrainedBytes -= take;\n if (this.headOffset >= head.byteLength) {\n this.chunks.shift();\n this.headOffset = 0;\n }\n return out;\n }\n };\n var HttpMessageParser = class {\n phase = \"headers\";\n settled = null;\n headerBuffer = EMPTY_BYTES;\n headerScanOffset = 0;\n headerPairs = [];\n framing = \"none\";\n expectedBodyBytes = 0;\n bodyChunks = [];\n bodyByteCount = 0;\n chunkState = \"size\";\n chunkSizeLine = [];\n chunkDataRemaining = 0;\n get outcome() {\n return this.settled;\n }\n feed(chunk) {\n if (this.phase === \"done\" || chunk.byteLength === 0) return;\n if (this.phase === \"headers\") {\n this.feedHeaders(chunk);\n return;\n }\n this.feedBody(chunk);\n }\n /** EOF from the peer closing the connection (or the request being torn down). */\n finish() {\n if (this.phase === \"done\") return;\n if (this.phase === \"headers\") {\n this.fail(`connection closed before ${this.messageLabel} headers`);\n return;\n }\n if (this.framing === \"until-close\") {\n this.completeWithBody(concatBytes(this.bodyChunks));\n return;\n }\n this.fail(`connection closed before the ${this.messageLabel} completed`);\n }\n feedHeaders(chunk) {\n this.headerBuffer = concatBytes([this.headerBuffer, chunk]);\n const headerEnd = this.findHeaderEnd();\n if (headerEnd < 0) {\n this.headerScanOffset = Math.max(0, this.headerBuffer.byteLength - 3);\n return;\n }\n const parsed = this.parseHeaderBlock(this.headerBuffer.subarray(0, headerEnd));\n const leftover = this.headerBuffer.subarray(headerEnd);\n this.headerBuffer = EMPTY_BYTES;\n if (!parsed) return;\n if (!this.bodyAllowed()) {\n this.completeWithBody(null);\n return;\n }\n const transferEncoding = this.headerValue(\"Transfer-Encoding\");\n if (transferEncoding !== null && /chunked/i.test(transferEncoding)) {\n this.framing = \"chunked\";\n } else {\n const contentLength = this.headerValue(\"Content-Length\");\n const expected = contentLength === null ? Number.NaN : parseInt(contentLength, 10);\n if (Number.isFinite(expected) && expected >= 0) {\n this.framing = \"content-length\";\n this.expectedBodyBytes = expected;\n if (expected === 0) {\n this.completeWithBody(EMPTY_BYTES);\n return;\n }\n } else {\n this.framing = this.defaultFramingWhenUnframed();\n if (this.framing === \"none\") {\n this.completeWithBody(null);\n return;\n }\n }\n }\n this.phase = \"body\";\n this.feedBody(leftover);\n }\n feedBody(chunk) {\n if (this.framing === \"chunked\") {\n this.feedChunkedBody(chunk);\n return;\n }\n if (chunk.byteLength === 0) return;\n if (this.framing === \"content-length\") {\n const needed = this.expectedBodyBytes - this.bodyByteCount;\n this.appendBody(chunk.subarray(0, Math.min(needed, chunk.byteLength)));\n if (this.bodyByteCount >= this.expectedBodyBytes) {\n this.completeWithBody(concatBytes(this.bodyChunks));\n }\n return;\n }\n this.appendBody(chunk);\n }\n feedChunkedBody(chunk) {\n let i = 0;\n while (i < chunk.byteLength && this.phase === \"body\") {\n if (this.chunkState === \"size\") {\n const byte = chunk[i++];\n if (byte === 10) {\n const line = new TextDecoder().decode(Uint8Array.from(this.chunkSizeLine)).trim();\n this.chunkSizeLine.length = 0;\n const size = parseInt(line.split(\";\", 1)[0], 16);\n if (!Number.isFinite(size) || size < 0) {\n this.fail(\"malformed chunked transfer encoding\");\n return;\n }\n if (size === 0) {\n this.completeWithBody(concatBytes(this.bodyChunks));\n return;\n }\n this.chunkState = \"data\";\n this.chunkDataRemaining = size;\n } else if (byte !== 13) {\n this.chunkSizeLine.push(byte);\n }\n } else if (this.chunkState === \"data\") {\n const take = Math.min(this.chunkDataRemaining, chunk.byteLength - i);\n this.appendBody(chunk.subarray(i, i + take));\n i += take;\n this.chunkDataRemaining -= take;\n if (this.chunkDataRemaining === 0) this.chunkState = \"data-end\";\n } else {\n const byte = chunk[i++];\n if (byte === 10) {\n this.chunkState = \"size\";\n } else if (byte !== 13) {\n this.fail(\"malformed chunked transfer encoding\");\n return;\n }\n }\n }\n }\n appendBody(bytes) {\n if (bytes.byteLength === 0) return;\n this.bodyChunks.push(bytes);\n this.bodyByteCount += bytes.byteLength;\n }\n findHeaderEnd() {\n const bytes = this.headerBuffer;\n for (let i = this.headerScanOffset; i + 3 < bytes.byteLength; i++) {\n if (bytes[i] === 13 && bytes[i + 1] === 10 && bytes[i + 2] === 13 && bytes[i + 3] === 10) {\n return i + 4;\n }\n }\n return -1;\n }\n /** Returns false when the start line is malformed (the parser has already failed). */\n parseHeaderBlock(headerBytes) {\n const headerText = new TextDecoder().decode(headerBytes);\n const lines = headerText.replace(/\\r\\n/g, \"\\n\").split(\"\\n\").filter((line) => line.length > 0);\n const startLine = lines.shift() ?? \"\";\n if (!this.parseStartLine(startLine)) return false;\n for (const line of lines) {\n const separator = line.indexOf(\":\");\n if (separator <= 0) continue;\n this.headerPairs.push([line.slice(0, separator), line.slice(separator + 1).trimStart()]);\n }\n return true;\n }\n headerValue(name) {\n const target = name.toLowerCase();\n for (const [key, value] of this.headerPairs) {\n if (key.toLowerCase() === target) return value;\n }\n return null;\n }\n completeWithBody(body) {\n this.phase = \"done\";\n this.settled = { kind: \"message\", message: this.buildMessage(body) };\n }\n fail(message) {\n this.phase = \"done\";\n this.settled = { kind: \"failed\", message };\n }\n };\n var HttpResponseParser = class extends HttpMessageParser {\n constructor(requestMethod) {\n super();\n this.requestMethod = requestMethod;\n }\n requestMethod;\n status = 200;\n statusText = \"\";\n get messageLabel() {\n return \"response\";\n }\n parseStartLine(line) {\n const match = /^HTTP\\/\\d(?:\\.\\d)?\\s+(\\d{3})(?:\\s+(.*))?$/.exec(line || \"HTTP/1.1 200 OK\");\n this.status = match ? parseInt(match[1], 10) : 200;\n this.statusText = match?.[2] ?? \"\";\n return true;\n }\n bodyAllowed() {\n return responseCanHaveBody(this.requestMethod, this.status);\n }\n defaultFramingWhenUnframed() {\n return \"until-close\";\n }\n buildMessage(body) {\n return { status: this.status, statusText: this.statusText, headerPairs: this.headerPairs, body };\n }\n };\n var HttpRequestParser = class extends HttpMessageParser {\n method = \"GET\";\n target = \"/\";\n get messageLabel() {\n return \"request\";\n }\n parseStartLine(line) {\n const match = /^([A-Za-z]+)\\s+(\\S+)\\s+HTTP\\/\\d(?:\\.\\d)?$/.exec(line);\n if (!match) {\n this.fail(`malformed HTTP request line: ${JSON.stringify(line.slice(0, 120))}`);\n return false;\n }\n this.method = match[1].toUpperCase();\n this.target = match[2];\n return true;\n }\n bodyAllowed() {\n return true;\n }\n defaultFramingWhenUnframed() {\n return \"none\";\n }\n buildMessage(body) {\n return { method: this.method, target: this.target, headerPairs: this.headerPairs, body };\n }\n };\n function errorResponse(status, message) {\n return {\n status,\n statusText: \"\",\n headerPairs: [[\"content-type\", \"text/plain; charset=utf-8\"]],\n body: new TextEncoder().encode(`Nimbus virtual socket: ${message}`)\n };\n }\n function toResponse(settled) {\n const headers = new Headers();\n for (const [key, value] of settled.headerPairs) headers.append(key, value);\n return new Response(settled.body, {\n status: settled.status,\n statusText: settled.statusText,\n headers\n });\n }\n var VirtualConnection = class {\n constructor(id, requestMethod, requestBytes, limits) {\n this.id = id;\n this.inbound = new ByteChunkQueue(requestBytes.byteLength, \"request buffer\");\n this.inbound.enqueue(requestBytes);\n this.outbound = new ByteChunkQueue(limits.maxResponseBufferBytes, \"response buffer\");\n this.parser = new HttpResponseParser(requestMethod);\n }\n id;\n /** Request bytes the guest server reads; filled in one shot in stage 1. */\n inbound;\n /** Response bytes the guest server writes; drained into the parser. */\n outbound;\n parser;\n responseReady = new Deferred();\n settled = false;\n closed = false;\n read(maxBytes) {\n return Array.from(this.inbound.readUpTo(Math.max(1, maxBytes | 0)));\n }\n /** The whole request is buffered before the connection is accepted, so nothing is ever awaited. */\n readAsync(maxBytes) {\n return Promise.resolve(this.read(maxBytes));\n }\n readBytesAsync(maxBytes) {\n return Promise.resolve(this.inbound.readUpTo(Math.max(1, maxBytes | 0)));\n }\n /** Same reason: an empty read on an accepted connection is always genuine EOF. */\n atEof() {\n return true;\n }\n write(bytesLike) {\n const bytes = toBytes(bytesLike);\n if (bytes.byteLength === 0 || this.settled || this.closed) return bytes.byteLength;\n this.outbound.enqueue(bytes.slice());\n this.pumpParser();\n return bytes.byteLength;\n }\n /** EOF from the server side (or request teardown); completes until-close bodies. */\n close() {\n if (this.closed) return;\n this.closed = true;\n if (this.settled) return;\n this.parser.finish();\n this.pumpParser();\n }\n /** Abort propagation: settle the pending preview request with a terminal status. */\n abort(message, status) {\n this.closed = true;\n this.settle(errorResponse(status, message));\n }\n async response(timeoutMs) {\n const timer = setTimeout(() => {\n this.settle(errorResponse(504, \"timed out waiting for response\"));\n }, Math.max(1, timeoutMs));\n try {\n return toResponse(await this.responseReady.promise);\n } finally {\n clearTimeout(timer);\n }\n }\n pumpParser() {\n for (; ; ) {\n const chunk = this.outbound.readUpTo(Number.MAX_SAFE_INTEGER);\n if (chunk.byteLength === 0) break;\n this.parser.feed(chunk);\n }\n const outcome = this.parser.outcome;\n if (!outcome) return;\n if (outcome.kind === \"failed\") {\n this.settle(errorResponse(502, outcome.message));\n return;\n }\n const { status, statusText, headerPairs, body } = outcome.message;\n this.settle({\n status,\n statusText,\n // The framing is consumed here; the Worker Response re-frames the body itself.\n headerPairs: headerPairs.filter(([key]) => !/^transfer-encoding$/i.test(key)),\n body\n });\n }\n settle(response) {\n if (this.settled) return;\n this.settled = true;\n this.responseReady.resolve(response);\n }\n };\n var LoopbackClientConnection = class {\n constructor(id, port, route, limits) {\n this.id = id;\n this.port = port;\n this.route = route;\n this.limits = limits;\n }\n id;\n port;\n route;\n limits;\n parser = new HttpRequestParser();\n /** Response bytes the guest client reads. */\n inbound = new ByteChunkQueue(Number.MAX_SAFE_INTEGER, \"loopback response buffer\");\n readable = null;\n drained = null;\n dispatched = false;\n eof = false;\n closed = false;\n write(bytesLike) {\n const bytes = toBytes(bytesLike);\n if (bytes.byteLength === 0 || this.closed) return bytes.byteLength;\n if (this.dispatched) {\n throw new Error(\n `Nimbus loopback socket: port ${this.port} answered with \"Connection: close\"; open a new connection for the next request`\n );\n }\n this.parser.feed(bytes.slice());\n const outcome = this.parser.outcome;\n if (!outcome) return bytes.byteLength;\n this.dispatched = true;\n if (outcome.kind === \"failed\") this.failWith(400, outcome.message);\n else void this.dispatch(outcome.message);\n return bytes.byteLength;\n }\n read(maxBytes) {\n return Array.from(this.readBytes(maxBytes));\n }\n /** Byte-array read. The fd-backed path uses this so a stream never round-trips through number[]. */\n readBytes(maxBytes) {\n const out = this.inbound.readUpTo(Math.max(1, maxBytes | 0));\n if (this.drained && this.inbound.pendingBytes <= LOOPBACK_READ_HIGH_WATER_BYTES) {\n const waiter = this.drained;\n this.drained = null;\n waiter.resolve();\n }\n return out;\n }\n atEof() {\n return (this.eof || this.closed) && this.inbound.pendingBytes === 0;\n }\n /** Blocks the guest until response bytes arrive; an empty result is EOF. */\n async readAsync(maxBytes) {\n return Array.from(await this.readBytesAsync(maxBytes));\n }\n async readBytesAsync(maxBytes) {\n for (; ; ) {\n const chunk = this.readBytes(maxBytes);\n if (chunk.byteLength > 0) return chunk;\n if (this.eof || this.closed) return EMPTY_BYTES;\n if (!this.dispatched) {\n throw new Error(\n \"Nimbus loopback socket: read before a complete HTTP request was written (loopback sockets carry HTTP requests, not arbitrary byte streams)\"\n );\n }\n const waiter = this.readable ??= new Deferred();\n await waiter.promise;\n }\n }\n close() {\n if (this.closed) return;\n this.closed = true;\n this.wakeReader();\n if (this.drained) {\n const waiter = this.drained;\n this.drained = null;\n waiter.resolve();\n }\n }\n async dispatch(parsed) {\n let request;\n try {\n request = this.buildRequest(parsed);\n } catch (error) {\n this.failWith(400, describeError(error));\n return;\n }\n let response;\n try {\n response = await this.withTimeout(this.route(this.port, request));\n } catch (error) {\n this.failWith(502, describeError(error));\n return;\n }\n await this.streamResponse(request.method, response);\n }\n buildRequest(parsed) {\n const headers = new Headers();\n let authority = `127.0.0.1:${this.port}`;\n for (const [key, value] of parsed.headerPairs) {\n if (/^host$/i.test(key)) {\n authority = value;\n continue;\n }\n if (/^(transfer-encoding|connection|content-length|keep-alive)$/i.test(key)) continue;\n headers.append(key, value);\n }\n const target = parsed.target.startsWith(\"/\") ? parsed.target : `/${parsed.target}`;\n const url = /^https?:\\/\\//i.test(parsed.target) ? parsed.target : `http://${authority}${target}`;\n const body = parsed.body && parsed.body.byteLength > 0 ? parsed.body : null;\n const methodTakesBody = parsed.method !== \"GET\" && parsed.method !== \"HEAD\";\n return new Request(url, {\n method: parsed.method,\n headers,\n body: methodTakesBody ? body : null\n });\n }\n async streamResponse(method, response) {\n const withBody = responseCanHaveBody(method, response.status) && response.body !== null;\n const headers = new Headers(response.headers);\n for (const hopByHop of [\"transfer-encoding\", \"content-length\", \"connection\", \"keep-alive\"]) {\n headers.delete(hopByHop);\n }\n this.pushBytes(encodeHttpResponseHead(response.status, response.statusText, headers, withBody));\n if (!withBody || !response.body) {\n this.finishStream();\n return;\n }\n const reader = response.body.getReader();\n try {\n for (; ; ) {\n const { value, done } = await reader.read();\n if (done) break;\n if (this.closed) return;\n const bytes = value instanceof Uint8Array ? value : new Uint8Array(value);\n if (bytes.byteLength === 0) continue;\n this.pushBytes(encodeChunkedFrame(bytes));\n await this.awaitDrain();\n }\n this.pushBytes(CHUNKED_TERMINATOR);\n } catch {\n } finally {\n try {\n await reader.cancel();\n } catch {\n }\n this.finishStream();\n }\n }\n /** Synthesize a complete response so the guest sees an HTTP error, never a hang. */\n failWith(status, detail) {\n const body = new TextEncoder().encode(`Nimbus loopback socket: ${detail}`);\n const headers = new Headers({\n \"content-type\": \"text/plain; charset=utf-8\",\n \"content-length\": String(body.byteLength)\n });\n this.pushBytes(encodeHttpResponseHead(status, \"\", headers, false));\n this.pushBytes(body);\n this.finishStream();\n }\n pushBytes(bytes) {\n if (this.closed) return;\n this.inbound.enqueue(bytes);\n this.wakeReader();\n }\n finishStream() {\n this.eof = true;\n this.wakeReader();\n }\n wakeReader() {\n const waiter = this.readable;\n if (!waiter) return;\n this.readable = null;\n waiter.resolve();\n }\n /** Pause the body pump while the guest is behind, so a big stream cannot grow unbounded. */\n awaitDrain() {\n if (this.inbound.pendingBytes <= LOOPBACK_READ_HIGH_WATER_BYTES) return Promise.resolve();\n return (this.drained ??= new Deferred()).promise;\n }\n withTimeout(promise) {\n let timer = null;\n const timeout = new Promise((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new Error(`port ${this.port} did not respond within ${this.limits.responseTimeoutMs}ms`)),\n Math.max(1, this.limits.responseTimeoutMs)\n );\n });\n return Promise.race([promise, timeout]).finally(() => {\n if (timer !== null) clearTimeout(timer);\n });\n }\n };\n function describeError(error) {\n return error instanceof Error ? error.message : String(error);\n }\n function socketStreamFor(conn) {\n return {\n // Neither direction has a handshake: an accepted connection already holds\n // the request, and a dialed one starts its exchange on the first write.\n opened: Promise.resolve(),\n readable: new ReadableStream(\n {\n async pull(controller) {\n const bytes = await conn.readBytesAsync(LOOPBACK_STREAM_CHUNK_BYTES);\n if (bytes.byteLength === 0) controller.close();\n else controller.enqueue(bytes);\n },\n cancel() {\n conn.close();\n }\n },\n // highWaterMark 0 so the stream never pulls speculatively. A default\n // strategy pulls once at construction, which on a client connection\n // means reading before the guest has written its request.\n { highWaterMark: 0 }\n ),\n writable: new WritableStream({\n write(chunk) {\n conn.write(chunk);\n },\n abort() {\n conn.close();\n }\n }),\n close() {\n conn.close();\n return Promise.resolve();\n }\n };\n }\n var VirtualListener = class {\n constructor(port) {\n this.port = port;\n }\n port;\n queue = [];\n acceptWaiters = [];\n push(conn) {\n const waiter = this.acceptWaiters.shift();\n if (waiter) waiter.resolve(conn);\n else this.queue.push(conn);\n }\n accept() {\n const queued = this.queue.shift();\n if (queued) return Promise.resolve(queued);\n const waiter = new Deferred();\n this.acceptWaiters.push(waiter);\n return waiter.promise;\n }\n take() {\n return this.queue.shift() ?? null;\n }\n pending() {\n return this.queue.length;\n }\n drainQueued() {\n return this.queue.splice(0);\n }\n rejectPendingAccepts(error) {\n for (const waiter of this.acceptWaiters.splice(0)) waiter.reject(error);\n }\n };\n var VirtualSocketKernel = class {\n constructor(host, limits) {\n this.host = host;\n this.limits = { ...DEFAULT_LIMITS, ...limits };\n }\n host;\n /** Public: runner glue inspects listeners.keys() for the default preview port. */\n listeners = /* @__PURE__ */ new Map();\n connections = /* @__PURE__ */ new Map();\n limits;\n nextConnectionId = 1;\n nextEphemeralPort = 49152;\n listenWaiters = [];\n readableWaiters = /* @__PURE__ */ new Set();\n listen(port) {\n let n = Number(port);\n if (!Number.isInteger(n) || n < 0 || n >= 65536) throw new Error(`invalid port: ${port}`);\n if (n === 0) {\n while (this.listeners.has(this.nextEphemeralPort)) {\n this.nextEphemeralPort++;\n if (this.nextEphemeralPort >= 65535) this.nextEphemeralPort = 49152;\n }\n n = this.nextEphemeralPort++;\n if (this.nextEphemeralPort >= 65535) this.nextEphemeralPort = 49152;\n }\n if (!this.listeners.has(n)) {\n this.listeners.set(n, new VirtualListener(n));\n try {\n this.host.__nimbusVirtualSocketDidListen?.(n);\n } catch {\n }\n for (const waiter of this.listenWaiters.splice(0)) waiter.resolve(n);\n }\n return n;\n }\n closeListener(port) {\n const n = Number(port);\n const listener = this.listeners.get(n);\n if (!listener) return;\n this.listeners.delete(n);\n for (const conn of listener.drainQueued()) {\n this.connections.delete(conn.id);\n conn.abort(`listener closed on port ${n}`, 502);\n }\n listener.rejectPendingAccepts(new Error(`port is not listening: ${n}`));\n }\n async accept(port) {\n const listener = this.listeners.get(Number(port));\n if (!listener) throw new Error(`port is not listening: ${port}`);\n const conn = await listener.accept();\n return { id: conn.id, host: \"127.0.0.1\", port: 0 };\n }\n acceptNow(port) {\n const listener = this.listeners.get(Number(port));\n if (!listener) throw new Error(`port is not listening: ${port}`);\n const conn = listener.take();\n return conn ? { id: conn.id, host: \"127.0.0.1\", port: 0 } : null;\n }\n /**\n * Open a client connection to `port` on the session's loopback.\n *\n * The returned id is an ordinary connection id: send() writes the\n * request, recv()/recvAsync() read the response, close() releases it.\n * Only HTTP/1.1 crosses a loopback connection - see\n * LoopbackClientConnection for why - and each one carries a single\n * request/response exchange.\n */\n connect(port) {\n const conn = this.openLoopbackClient(port);\n this.connections.set(conn.id, conn);\n return conn.id;\n }\n /**\n * The same client connection as `connect`, handed back in Cloudflare's\n * `Socket` shape.\n *\n * Guests whose sockets are real WASI file descriptors (ruby.wasm, and any\n * future wasm32-wasi program) reach loopback through this: the WASI shim\n * stores it in exactly the fd slot a `cloudflare:sockets` connection would\n * occupy, so `fd_read` on an in-session port is the same suspending read as\n * `fd_read` on a remote host.\n */\n connectStream(port) {\n return socketStreamFor(this.openLoopbackClient(port));\n }\n /**\n * An already-accepted connection in the same `Socket` shape, so a server's\n * accepted socket is the same kind of file descriptor as a client's dialed\n * one. `accept`/`acceptNow` still hand out the connection id, because accept\n * itself stays on the cooperative pump - this only binds the result.\n */\n streamFor(id) {\n const conn = this.connections.get(Number(id));\n if (!conn) throw new Error(`connection is closed: ${id}`);\n return socketStreamFor(conn);\n }\n openLoopbackClient(port) {\n const n = Number(port);\n if (!Number.isInteger(n) || n <= 0 || n >= 65536) throw new Error(`invalid port: ${port}`);\n const route = this.host.__nimbusVirtualSocketRouteLoopback;\n if (typeof route !== \"function\") {\n throw new Error(\"Nimbus loopback sockets are unavailable in this runtime\");\n }\n return new LoopbackClientConnection(this.nextConnectionId++, n, route, this.limits);\n }\n /** Plain number array: Pyodide bytes() and the ruby.wasm base64 bridge both consume it. */\n recv(id, maxBytes) {\n const conn = this.connections.get(Number(id));\n if (!conn) return [];\n return conn.read(Number(maxBytes));\n }\n /**\n * Suspending read. A client connection has nothing to hand back until\n * the loopback response arrives, so guests that can suspend (Pyodide\n * `run_sync`, a JSPI-suspending WASI call) await this instead of\n * spinning on recv(). An empty result is EOF.\n */\n async recvAsync(id, maxBytes) {\n const conn = this.connections.get(Number(id));\n if (!conn) return [];\n return conn.readAsync(Number(maxBytes));\n }\n /**\n * Whether an empty recv() means end-of-response rather than \"not yet\".\n * Guests that poll instead of suspending (ruby.wasm, whose JS bridge is\n * synchronous) need the two apart to avoid truncating a response.\n */\n atEof(id) {\n const conn = this.connections.get(Number(id));\n return conn ? conn.atEof() : true;\n }\n send(id, bytesLike) {\n const conn = this.connections.get(Number(id));\n if (!conn) throw new Error(`connection is closed: ${id}`);\n return conn.write(bytesLike);\n }\n close(id) {\n const conn = this.connections.get(Number(id));\n if (!conn) return;\n conn.close();\n this.connections.delete(Number(id));\n }\n pending(port) {\n return this.listeners.get(Number(port))?.pending() ?? 0;\n }\n firstListeningPort() {\n for (const port of this.listeners.keys()) return port;\n return null;\n }\n /** select()-style readiness: resolves ports with queued connections, [] on timeout. */\n waitReadable(ports, timeoutSeconds) {\n const normalized = (Array.isArray(ports) ? ports : []).map((p) => Number(p)).filter((p) => Number.isInteger(p));\n const readyNow = normalized.filter((port) => this.pending(port) > 0);\n if (readyNow.length > 0) return Promise.resolve(readyNow);\n const timeoutMs = timeoutSeconds == null ? this.limits.responseTimeoutMs : Math.max(0, Number(timeoutSeconds) * 1e3);\n const deferred = new Deferred();\n const waiter = {\n ports: normalized,\n deferred,\n timer: setTimeout(() => {\n this.readableWaiters.delete(waiter);\n deferred.resolve([]);\n }, timeoutMs)\n };\n this.readableWaiters.add(waiter);\n return deferred.promise;\n }\n async waitForListen(timeoutMs) {\n const existing = this.firstListeningPort();\n if (existing) return existing;\n const waiter = new Deferred();\n this.listenWaiters.push(waiter);\n let timer = null;\n const timeout = new Promise((resolve) => {\n timer = setTimeout(() => resolve(null), Math.max(1, timeoutMs ?? 5e3));\n });\n try {\n return await Promise.race([waiter.promise, timeout]);\n } finally {\n if (timer !== null) clearTimeout(timer);\n }\n }\n async handleHttpRequest(port, request) {\n const n = Number(port);\n let listener = this.listeners.get(n);\n if (!listener) {\n try {\n await this.host.__nimbusVirtualSocketEnsureListener?.(n);\n } catch {\n }\n listener = this.listeners.get(n);\n }\n if (!listener) {\n return new Response(`Nimbus virtual socket: no listener on port ${port}`, { status: 502 });\n }\n const body = request.method === \"GET\" || request.method === \"HEAD\" ? EMPTY_BYTES : new Uint8Array(await request.arrayBuffer());\n if (body.byteLength > this.limits.maxRequestBodyBytes) {\n return new Response(\n `Nimbus virtual socket: request body exceeds ${this.limits.maxRequestBodyBytes} bytes`,\n { status: 413 }\n );\n }\n const id = this.nextConnectionId++;\n const conn = new VirtualConnection(id, request.method, encodeHttpRequest(request, body), this.limits);\n this.connections.set(id, conn);\n listener.push(conn);\n this.notifyReadable(n);\n const signal = request.signal;\n const onAbort = () => conn.abort(\"client aborted the request\", 499);\n if (signal?.aborted) onAbort();\n else signal?.addEventListener(\"abort\", onAbort);\n try {\n try {\n const accepted = await this.host.__nimbusVirtualSocketRequestQueued?.(n);\n if (accepted === false) {\n const detail = typeof this.host.__nimbusVirtualSocketLastError === \"string\" ? this.host.__nimbusVirtualSocketLastError.trim() : \"\";\n const suffix = detail ? `: ${detail}` : \"\";\n return new Response(\n `Nimbus virtual socket: runtime handler did not accept the request${suffix}`,\n { status: 502 }\n );\n }\n } catch {\n }\n return await conn.response(this.limits.responseTimeoutMs);\n } finally {\n signal?.removeEventListener(\"abort\", onAbort);\n conn.close();\n this.connections.delete(id);\n }\n }\n notifyReadable(port) {\n for (const waiter of Array.from(this.readableWaiters)) {\n if (!waiter.ports.includes(port)) continue;\n const ready = waiter.ports.filter((p) => this.pending(p) > 0);\n if (ready.length === 0) continue;\n clearTimeout(waiter.timer);\n this.readableWaiters.delete(waiter);\n waiter.deferred.resolve(ready);\n }\n }\n };\n function installVirtualSocketKernel(scope = globalThis) {\n if (!scope.__nimbusVirtualSockets) {\n scope.__nimbusVirtualSockets = new VirtualSocketKernel(scope);\n }\n return scope.__nimbusVirtualSockets;\n }\n\n // <stdin>\n installVirtualSocketKernel();\n})();\n";
|
|
13
|
+
export const VIRTUAL_SOCKET_KERNEL_SRC = "(() => {\n // ../core/src/runtime/virtual-socket-kernel.ts\n var DEFAULT_LIMITS = {\n responseTimeoutMs: 3e4,\n maxRequestBodyBytes: 32 * 1024 * 1024,\n maxResponseBufferBytes: 64 * 1024 * 1024\n };\n var EMPTY_BYTES = new Uint8Array(0);\n var Deferred = class {\n promise;\n resolve;\n reject;\n constructor() {\n let resolve = () => {\n };\n let reject = () => {\n };\n this.promise = new Promise((res, rej) => {\n resolve = res;\n reject = rej;\n });\n this.resolve = resolve;\n this.reject = reject;\n }\n };\n function isPyodideProxyLike(value) {\n return value !== null && typeof value === \"object\" && typeof value.toJs === \"function\";\n }\n function toBytes(value) {\n if (value instanceof Uint8Array) return value;\n if (value instanceof ArrayBuffer) return new Uint8Array(value);\n if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);\n if (Array.isArray(value)) return Uint8Array.from(value);\n if (typeof value === \"string\") return new TextEncoder().encode(value);\n if (isPyodideProxyLike(value)) return toBytes(value.toJs());\n return EMPTY_BYTES;\n }\n function concatBytes(parts) {\n let length = 0;\n for (const part of parts) length += part.byteLength;\n const out = new Uint8Array(length);\n let offset = 0;\n for (const part of parts) {\n out.set(part, offset);\n offset += part.byteLength;\n }\n return out;\n }\n function responseCanHaveBody(requestMethod, status) {\n if (requestMethod.toUpperCase() === \"HEAD\") return false;\n return status !== 204 && status !== 205 && status !== 304;\n }\n function encodeHttpRequest(request, body) {\n const url = new URL(request.url);\n const path = (url.pathname || \"/\") + url.search;\n const headers = new Headers(request.headers);\n if (!headers.has(\"Host\")) headers.set(\"Host\", url.host || \"nimbus.local\");\n if (body.byteLength > 0 && !headers.has(\"Content-Length\")) {\n headers.set(\"Content-Length\", String(body.byteLength));\n }\n const lines = [`${request.method} ${path} HTTP/1.1`];\n headers.forEach((value, key) => {\n lines.push(`${key}: ${value}`);\n });\n lines.push(\"\", \"\");\n return concatBytes([new TextEncoder().encode(lines.join(\"\\r\\n\")), body]);\n }\n function encodeHttpResponseHead(status, statusText, headers, chunkedBody) {\n const lines = [`HTTP/1.1 ${status} ${statusText}`];\n headers.forEach((value, key) => {\n lines.push(`${key}: ${value}`);\n });\n if (chunkedBody) lines.push(\"Transfer-Encoding: chunked\");\n lines.push(\"Connection: close\");\n lines.push(\"\", \"\");\n return new TextEncoder().encode(lines.join(\"\\r\\n\"));\n }\n function encodeChunkedFrame(bytes) {\n return concatBytes([\n new TextEncoder().encode(`${bytes.byteLength.toString(16)}\\r\n`),\n bytes,\n new TextEncoder().encode(\"\\r\\n\")\n ]);\n }\n var CHUNKED_TERMINATOR = new TextEncoder().encode(\"0\\r\\n\\r\\n\");\n var LOOPBACK_READ_HIGH_WATER_BYTES = 1024 * 1024;\n var LOOPBACK_STREAM_CHUNK_BYTES = 64 * 1024;\n var ByteChunkQueue = class {\n constructor(limitTotalBytes, overflowLabel) {\n this.limitTotalBytes = limitTotalBytes;\n this.overflowLabel = overflowLabel;\n }\n limitTotalBytes;\n overflowLabel;\n chunks = [];\n headOffset = 0;\n enqueuedTotalBytes = 0;\n undrainedBytes = 0;\n /** Bytes enqueued but not yet read. Drives flow control on streamed queues. */\n get pendingBytes() {\n return this.undrainedBytes;\n }\n enqueue(bytes) {\n if (bytes.byteLength === 0) return;\n if (this.enqueuedTotalBytes + bytes.byteLength > this.limitTotalBytes) {\n throw new Error(\n `Nimbus virtual socket: ${this.overflowLabel} exceeds ${this.limitTotalBytes} bytes`\n );\n }\n this.enqueuedTotalBytes += bytes.byteLength;\n this.undrainedBytes += bytes.byteLength;\n this.chunks.push(bytes);\n }\n /** Drain up to maxBytes from the head chunk; empty result means no data is queued. */\n readUpTo(maxBytes) {\n const head = this.chunks[0];\n if (!head) return EMPTY_BYTES;\n const available = head.byteLength - this.headOffset;\n const take = Math.min(available, Math.max(1, maxBytes));\n const out = head.subarray(this.headOffset, this.headOffset + take);\n this.headOffset += take;\n this.undrainedBytes -= take;\n if (this.headOffset >= head.byteLength) {\n this.chunks.shift();\n this.headOffset = 0;\n }\n return out;\n }\n };\n var HttpMessageParser = class {\n phase = \"headers\";\n settled = null;\n headerBuffer = EMPTY_BYTES;\n headerScanOffset = 0;\n headerPairs = [];\n framing = \"none\";\n expectedBodyBytes = 0;\n bodyChunks = [];\n bodyByteCount = 0;\n chunkState = \"size\";\n chunkSizeLine = [];\n chunkDataRemaining = 0;\n get outcome() {\n return this.settled;\n }\n feed(chunk) {\n if (this.phase === \"done\" || chunk.byteLength === 0) return;\n if (this.phase === \"headers\") {\n this.feedHeaders(chunk);\n return;\n }\n this.feedBody(chunk);\n }\n /** EOF from the peer closing the connection (or the request being torn down). */\n finish() {\n if (this.phase === \"done\") return;\n if (this.phase === \"headers\") {\n this.fail(`connection closed before ${this.messageLabel} headers`);\n return;\n }\n if (this.framing === \"until-close\") {\n this.completeWithBody(concatBytes(this.bodyChunks));\n return;\n }\n this.fail(`connection closed before the ${this.messageLabel} completed`);\n }\n feedHeaders(chunk) {\n this.headerBuffer = concatBytes([this.headerBuffer, chunk]);\n const headerEnd = this.findHeaderEnd();\n if (headerEnd < 0) {\n this.headerScanOffset = Math.max(0, this.headerBuffer.byteLength - 3);\n return;\n }\n const parsed = this.parseHeaderBlock(this.headerBuffer.subarray(0, headerEnd));\n const leftover = this.headerBuffer.subarray(headerEnd);\n this.headerBuffer = EMPTY_BYTES;\n if (!parsed) return;\n if (!this.bodyAllowed()) {\n this.completeWithBody(null);\n return;\n }\n const transferEncoding = this.headerValue(\"Transfer-Encoding\");\n if (transferEncoding !== null && /chunked/i.test(transferEncoding)) {\n this.framing = \"chunked\";\n } else {\n const contentLength = this.headerValue(\"Content-Length\");\n const expected = contentLength === null ? Number.NaN : parseInt(contentLength, 10);\n if (Number.isFinite(expected) && expected >= 0) {\n this.framing = \"content-length\";\n this.expectedBodyBytes = expected;\n if (expected === 0) {\n this.completeWithBody(EMPTY_BYTES);\n return;\n }\n } else {\n this.framing = this.defaultFramingWhenUnframed();\n if (this.framing === \"none\") {\n this.completeWithBody(null);\n return;\n }\n }\n }\n this.phase = \"body\";\n this.feedBody(leftover);\n }\n feedBody(chunk) {\n if (this.framing === \"chunked\") {\n this.feedChunkedBody(chunk);\n return;\n }\n if (chunk.byteLength === 0) return;\n if (this.framing === \"content-length\") {\n const needed = this.expectedBodyBytes - this.bodyByteCount;\n this.appendBody(chunk.subarray(0, Math.min(needed, chunk.byteLength)));\n if (this.bodyByteCount >= this.expectedBodyBytes) {\n this.completeWithBody(concatBytes(this.bodyChunks));\n }\n return;\n }\n this.appendBody(chunk);\n }\n feedChunkedBody(chunk) {\n let i = 0;\n while (i < chunk.byteLength && this.phase === \"body\") {\n if (this.chunkState === \"size\") {\n const byte = chunk[i++];\n if (byte === 10) {\n const line = new TextDecoder().decode(Uint8Array.from(this.chunkSizeLine)).trim();\n this.chunkSizeLine.length = 0;\n const size = parseInt(line.split(\";\", 1)[0], 16);\n if (!Number.isFinite(size) || size < 0) {\n this.fail(\"malformed chunked transfer encoding\");\n return;\n }\n if (size === 0) {\n this.completeWithBody(concatBytes(this.bodyChunks));\n return;\n }\n this.chunkState = \"data\";\n this.chunkDataRemaining = size;\n } else if (byte !== 13) {\n this.chunkSizeLine.push(byte);\n }\n } else if (this.chunkState === \"data\") {\n const take = Math.min(this.chunkDataRemaining, chunk.byteLength - i);\n this.appendBody(chunk.subarray(i, i + take));\n i += take;\n this.chunkDataRemaining -= take;\n if (this.chunkDataRemaining === 0) this.chunkState = \"data-end\";\n } else {\n const byte = chunk[i++];\n if (byte === 10) {\n this.chunkState = \"size\";\n } else if (byte !== 13) {\n this.fail(\"malformed chunked transfer encoding\");\n return;\n }\n }\n }\n }\n appendBody(bytes) {\n if (bytes.byteLength === 0) return;\n this.bodyChunks.push(bytes);\n this.bodyByteCount += bytes.byteLength;\n }\n findHeaderEnd() {\n const bytes = this.headerBuffer;\n for (let i = this.headerScanOffset; i + 3 < bytes.byteLength; i++) {\n if (bytes[i] === 13 && bytes[i + 1] === 10 && bytes[i + 2] === 13 && bytes[i + 3] === 10) {\n return i + 4;\n }\n }\n return -1;\n }\n /** Returns false when the start line is malformed (the parser has already failed). */\n parseHeaderBlock(headerBytes) {\n const headerText = new TextDecoder().decode(headerBytes);\n const lines = headerText.replace(/\\r\\n/g, \"\\n\").split(\"\\n\").filter((line) => line.length > 0);\n const startLine = lines.shift() ?? \"\";\n if (!this.parseStartLine(startLine)) return false;\n for (const line of lines) {\n const separator = line.indexOf(\":\");\n if (separator <= 0) continue;\n this.headerPairs.push([line.slice(0, separator), line.slice(separator + 1).trimStart()]);\n }\n return true;\n }\n headerValue(name) {\n const target = name.toLowerCase();\n for (const [key, value] of this.headerPairs) {\n if (key.toLowerCase() === target) return value;\n }\n return null;\n }\n completeWithBody(body) {\n this.phase = \"done\";\n this.settled = { kind: \"message\", message: this.buildMessage(body) };\n }\n fail(message) {\n this.phase = \"done\";\n this.settled = { kind: \"failed\", message };\n }\n };\n var HttpResponseParser = class extends HttpMessageParser {\n constructor(requestMethod) {\n super();\n this.requestMethod = requestMethod;\n }\n requestMethod;\n status = 200;\n statusText = \"\";\n get messageLabel() {\n return \"response\";\n }\n parseStartLine(line) {\n const match = /^HTTP\\/\\d(?:\\.\\d)?\\s+(\\d{3})(?:\\s+(.*))?$/.exec(line || \"HTTP/1.1 200 OK\");\n this.status = match ? parseInt(match[1], 10) : 200;\n this.statusText = match?.[2] ?? \"\";\n return true;\n }\n bodyAllowed() {\n return responseCanHaveBody(this.requestMethod, this.status);\n }\n defaultFramingWhenUnframed() {\n return \"until-close\";\n }\n buildMessage(body) {\n return { status: this.status, statusText: this.statusText, headerPairs: this.headerPairs, body };\n }\n };\n var HttpRequestParser = class extends HttpMessageParser {\n method = \"GET\";\n target = \"/\";\n get messageLabel() {\n return \"request\";\n }\n parseStartLine(line) {\n const match = /^([A-Za-z]+)\\s+(\\S+)\\s+HTTP\\/\\d(?:\\.\\d)?$/.exec(line);\n if (!match) {\n this.fail(`malformed HTTP request line: ${JSON.stringify(line.slice(0, 120))}`);\n return false;\n }\n this.method = match[1].toUpperCase();\n this.target = match[2];\n return true;\n }\n bodyAllowed() {\n return true;\n }\n defaultFramingWhenUnframed() {\n return \"none\";\n }\n buildMessage(body) {\n return { method: this.method, target: this.target, headerPairs: this.headerPairs, body };\n }\n };\n function errorResponse(status, message) {\n return {\n status,\n statusText: \"\",\n headerPairs: [[\"content-type\", \"text/plain; charset=utf-8\"]],\n body: new TextEncoder().encode(`Nimbus virtual socket: ${message}`)\n };\n }\n function toResponse(settled) {\n const headers = new Headers();\n for (const [key, value] of settled.headerPairs) headers.append(key, value);\n return new Response(settled.body, {\n status: settled.status,\n statusText: settled.statusText,\n headers\n });\n }\n var VirtualConnection = class {\n constructor(id, requestMethod, requestBytes, limits) {\n this.id = id;\n this.inbound = new ByteChunkQueue(requestBytes.byteLength, \"request buffer\");\n this.inbound.enqueue(requestBytes);\n this.outbound = new ByteChunkQueue(limits.maxResponseBufferBytes, \"response buffer\");\n this.parser = new HttpResponseParser(requestMethod);\n }\n id;\n /** Request bytes the guest server reads; filled in one shot in stage 1. */\n inbound;\n /** Response bytes the guest server writes; drained into the parser. */\n outbound;\n parser;\n responseReady = new Deferred();\n settled = false;\n closed = false;\n read(maxBytes) {\n return Array.from(this.inbound.readUpTo(Math.max(1, maxBytes | 0)));\n }\n /** The whole request is buffered before the connection is accepted, so nothing is ever awaited. */\n readAsync(maxBytes) {\n return Promise.resolve(this.read(maxBytes));\n }\n readBytesAsync(maxBytes) {\n return Promise.resolve(this.inbound.readUpTo(Math.max(1, maxBytes | 0)));\n }\n /** Same reason: an empty read on an accepted connection is always genuine EOF. */\n atEof() {\n return true;\n }\n write(bytesLike) {\n const bytes = toBytes(bytesLike);\n if (bytes.byteLength === 0 || this.settled || this.closed) return bytes.byteLength;\n this.outbound.enqueue(bytes.slice());\n this.pumpParser();\n return bytes.byteLength;\n }\n /** EOF from the server side (or request teardown); completes until-close bodies. */\n close() {\n if (this.closed) return;\n this.closed = true;\n if (this.settled) return;\n this.parser.finish();\n this.pumpParser();\n }\n /** Abort propagation: settle the pending preview request with a terminal status. */\n abort(message, status) {\n this.closed = true;\n this.settle(errorResponse(status, message));\n }\n async response(timeoutMs) {\n const timer = setTimeout(() => {\n this.settle(errorResponse(504, \"timed out waiting for response\"));\n }, Math.max(1, timeoutMs));\n try {\n return toResponse(await this.responseReady.promise);\n } finally {\n clearTimeout(timer);\n }\n }\n pumpParser() {\n for (; ; ) {\n const chunk = this.outbound.readUpTo(Number.MAX_SAFE_INTEGER);\n if (chunk.byteLength === 0) break;\n this.parser.feed(chunk);\n }\n const outcome = this.parser.outcome;\n if (!outcome) return;\n if (outcome.kind === \"failed\") {\n this.settle(errorResponse(502, outcome.message));\n return;\n }\n const { status, statusText, headerPairs, body } = outcome.message;\n this.settle({\n status,\n statusText,\n // The framing is consumed here; the Worker Response re-frames the body itself.\n headerPairs: headerPairs.filter(([key]) => !/^transfer-encoding$/i.test(key)),\n body\n });\n }\n settle(response) {\n if (this.settled) return;\n this.settled = true;\n this.responseReady.resolve(response);\n }\n };\n var LoopbackClientConnection = class {\n constructor(id, port, route, limits) {\n this.id = id;\n this.port = port;\n this.route = route;\n this.limits = limits;\n }\n id;\n port;\n route;\n limits;\n parser = new HttpRequestParser();\n /** Response bytes the guest client reads. */\n inbound = new ByteChunkQueue(Number.MAX_SAFE_INTEGER, \"loopback response buffer\");\n readable = null;\n drained = null;\n dispatched = false;\n eof = false;\n closed = false;\n write(bytesLike) {\n const bytes = toBytes(bytesLike);\n if (bytes.byteLength === 0 || this.closed) return bytes.byteLength;\n if (this.dispatched) {\n throw new Error(\n `Nimbus loopback socket: port ${this.port} answered with \"Connection: close\"; open a new connection for the next request`\n );\n }\n this.parser.feed(bytes.slice());\n const outcome = this.parser.outcome;\n if (!outcome) return bytes.byteLength;\n this.dispatched = true;\n if (outcome.kind === \"failed\") this.failWith(400, outcome.message);\n else void this.dispatch(outcome.message);\n return bytes.byteLength;\n }\n read(maxBytes) {\n return Array.from(this.readBytes(maxBytes));\n }\n /** Byte-array read. The fd-backed path uses this so a stream never round-trips through number[]. */\n readBytes(maxBytes) {\n const out = this.inbound.readUpTo(Math.max(1, maxBytes | 0));\n if (this.drained && this.inbound.pendingBytes <= LOOPBACK_READ_HIGH_WATER_BYTES) {\n const waiter = this.drained;\n this.drained = null;\n waiter.resolve();\n }\n return out;\n }\n atEof() {\n return (this.eof || this.closed) && this.inbound.pendingBytes === 0;\n }\n /** Blocks the guest until response bytes arrive; an empty result is EOF. */\n async readAsync(maxBytes) {\n return Array.from(await this.readBytesAsync(maxBytes));\n }\n async readBytesAsync(maxBytes) {\n for (; ; ) {\n const chunk = this.readBytes(maxBytes);\n if (chunk.byteLength > 0) return chunk;\n if (this.eof || this.closed) return EMPTY_BYTES;\n if (!this.dispatched) {\n throw new Error(\n \"Nimbus loopback socket: read before a complete HTTP request was written (loopback sockets carry HTTP requests, not arbitrary byte streams)\"\n );\n }\n this.readable ??= new Deferred();\n await this.readable.promise;\n }\n }\n close() {\n if (this.closed) return;\n this.closed = true;\n this.wakeReader();\n if (this.drained) {\n const waiter = this.drained;\n this.drained = null;\n waiter.resolve();\n }\n }\n async dispatch(parsed) {\n let request;\n try {\n request = this.buildRequest(parsed);\n } catch (error) {\n this.failWith(400, describeError(error));\n return;\n }\n let response;\n try {\n response = await this.withTimeout(this.route(this.port, request));\n } catch (error) {\n this.failWith(502, describeError(error));\n return;\n }\n await this.streamResponse(request.method, response);\n }\n buildRequest(parsed) {\n const headers = new Headers();\n let authority = `127.0.0.1:${this.port}`;\n for (const [key, value] of parsed.headerPairs) {\n if (/^host$/i.test(key)) {\n authority = value;\n continue;\n }\n if (/^(transfer-encoding|connection|content-length|keep-alive)$/i.test(key)) continue;\n headers.append(key, value);\n }\n const target = parsed.target.startsWith(\"/\") ? parsed.target : `/${parsed.target}`;\n const url = /^https?:\\/\\//i.test(parsed.target) ? parsed.target : `http://${authority}${target}`;\n const body = parsed.body && parsed.body.byteLength > 0 ? parsed.body : null;\n const methodTakesBody = parsed.method !== \"GET\" && parsed.method !== \"HEAD\";\n return new Request(url, {\n method: parsed.method,\n headers,\n body: methodTakesBody ? body : null\n });\n }\n async streamResponse(method, response) {\n const withBody = responseCanHaveBody(method, response.status) && response.body !== null;\n const headers = new Headers(response.headers);\n for (const hopByHop of [\"transfer-encoding\", \"content-length\", \"connection\", \"keep-alive\"]) {\n headers.delete(hopByHop);\n }\n this.pushBytes(encodeHttpResponseHead(response.status, response.statusText, headers, withBody));\n if (!withBody || !response.body) {\n this.finishStream();\n return;\n }\n const reader = response.body.getReader();\n try {\n for (; ; ) {\n const { value, done } = await reader.read();\n if (done) break;\n if (this.closed) return;\n const bytes = value instanceof Uint8Array ? value : new Uint8Array(value);\n if (bytes.byteLength === 0) continue;\n this.pushBytes(encodeChunkedFrame(bytes));\n await this.awaitDrain();\n }\n this.pushBytes(CHUNKED_TERMINATOR);\n } catch {\n } finally {\n try {\n await reader.cancel();\n } catch {\n }\n this.finishStream();\n }\n }\n /** Synthesize a complete response so the guest sees an HTTP error, never a hang. */\n failWith(status, detail) {\n const body = new TextEncoder().encode(`Nimbus loopback socket: ${detail}`);\n const headers = new Headers({\n \"content-type\": \"text/plain; charset=utf-8\",\n \"content-length\": String(body.byteLength)\n });\n this.pushBytes(encodeHttpResponseHead(status, \"\", headers, false));\n this.pushBytes(body);\n this.finishStream();\n }\n pushBytes(bytes) {\n if (this.closed) return;\n this.inbound.enqueue(bytes);\n this.wakeReader();\n }\n finishStream() {\n this.eof = true;\n this.wakeReader();\n }\n wakeReader() {\n const waiter = this.readable;\n if (!waiter) return;\n this.readable = null;\n waiter.resolve();\n }\n /** Pause the body pump while the guest is behind, so a big stream cannot grow unbounded. */\n awaitDrain() {\n if (this.inbound.pendingBytes <= LOOPBACK_READ_HIGH_WATER_BYTES) return Promise.resolve();\n this.drained ??= new Deferred();\n return this.drained.promise;\n }\n withTimeout(promise) {\n let timer = null;\n const timeout = new Promise((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new Error(`port ${this.port} did not respond within ${this.limits.responseTimeoutMs}ms`)),\n Math.max(1, this.limits.responseTimeoutMs)\n );\n });\n return Promise.race([promise, timeout]).finally(() => {\n if (timer !== null) clearTimeout(timer);\n });\n }\n };\n function describeError(error) {\n return error instanceof Error ? error.message : String(error);\n }\n function socketStreamFor(conn) {\n return {\n // Neither direction has a handshake: an accepted connection already holds\n // the request, and a dialed one starts its exchange on the first write.\n opened: Promise.resolve(),\n readable: new ReadableStream(\n {\n async pull(controller) {\n const bytes = await conn.readBytesAsync(LOOPBACK_STREAM_CHUNK_BYTES);\n if (bytes.byteLength === 0) controller.close();\n else controller.enqueue(bytes);\n },\n cancel() {\n conn.close();\n }\n },\n // highWaterMark 0 so the stream never pulls speculatively. A default\n // strategy pulls once at construction, which on a client connection\n // means reading before the guest has written its request.\n { highWaterMark: 0 }\n ),\n writable: new WritableStream({\n write(chunk) {\n conn.write(chunk);\n },\n abort() {\n conn.close();\n }\n }),\n close() {\n conn.close();\n return Promise.resolve();\n }\n };\n }\n var VirtualListener = class {\n constructor(port) {\n this.port = port;\n }\n port;\n queue = [];\n acceptWaiters = [];\n push(conn) {\n const waiter = this.acceptWaiters.shift();\n if (waiter) waiter.resolve(conn);\n else this.queue.push(conn);\n }\n accept() {\n const queued = this.queue.shift();\n if (queued) return Promise.resolve(queued);\n const waiter = new Deferred();\n this.acceptWaiters.push(waiter);\n return waiter.promise;\n }\n take() {\n return this.queue.shift() ?? null;\n }\n pending() {\n return this.queue.length;\n }\n drainQueued() {\n return this.queue.splice(0);\n }\n rejectPendingAccepts(error) {\n for (const waiter of this.acceptWaiters.splice(0)) waiter.reject(error);\n }\n };\n var VirtualSocketKernel = class {\n constructor(host, limits) {\n this.host = host;\n this.limits = { ...DEFAULT_LIMITS, ...limits };\n }\n host;\n /** Public: runner glue inspects listeners.keys() for the default preview port. */\n listeners = /* @__PURE__ */ new Map();\n connections = /* @__PURE__ */ new Map();\n limits;\n nextConnectionId = 1;\n nextEphemeralPort = 49152;\n listenWaiters = [];\n readableWaiters = /* @__PURE__ */ new Set();\n listen(port) {\n let n = Number(port);\n if (!Number.isInteger(n) || n < 0 || n >= 65536) throw new Error(`invalid port: ${port}`);\n if (n === 0) {\n while (this.listeners.has(this.nextEphemeralPort)) {\n this.nextEphemeralPort++;\n if (this.nextEphemeralPort >= 65535) this.nextEphemeralPort = 49152;\n }\n n = this.nextEphemeralPort++;\n if (this.nextEphemeralPort >= 65535) this.nextEphemeralPort = 49152;\n }\n if (!this.listeners.has(n)) {\n this.listeners.set(n, new VirtualListener(n));\n try {\n this.host.__nimbusVirtualSocketDidListen?.(n);\n } catch {\n }\n for (const waiter of this.listenWaiters.splice(0)) waiter.resolve(n);\n }\n return n;\n }\n closeListener(port) {\n const n = Number(port);\n const listener = this.listeners.get(n);\n if (!listener) return;\n this.listeners.delete(n);\n for (const conn of listener.drainQueued()) {\n this.connections.delete(conn.id);\n conn.abort(`listener closed on port ${n}`, 502);\n }\n listener.rejectPendingAccepts(new Error(`port is not listening: ${n}`));\n }\n async accept(port) {\n const listener = this.listeners.get(Number(port));\n if (!listener) throw new Error(`port is not listening: ${port}`);\n const conn = await listener.accept();\n return { id: conn.id, host: \"127.0.0.1\", port: 0 };\n }\n acceptNow(port) {\n const listener = this.listeners.get(Number(port));\n if (!listener) throw new Error(`port is not listening: ${port}`);\n const conn = listener.take();\n return conn ? { id: conn.id, host: \"127.0.0.1\", port: 0 } : null;\n }\n /**\n * Open a client connection to `port` on the session's loopback.\n *\n * The returned id is an ordinary connection id: send() writes the\n * request, recv()/recvAsync() read the response, close() releases it.\n * Only HTTP/1.1 crosses a loopback connection - see\n * LoopbackClientConnection for why - and each one carries a single\n * request/response exchange.\n */\n connect(port) {\n const conn = this.openLoopbackClient(port);\n this.connections.set(conn.id, conn);\n return conn.id;\n }\n /**\n * The same client connection as `connect`, handed back in Cloudflare's\n * `Socket` shape.\n *\n * Guests whose sockets are real WASI file descriptors (ruby.wasm, and any\n * future wasm32-wasi program) reach loopback through this: the WASI shim\n * stores it in exactly the fd slot a `cloudflare:sockets` connection would\n * occupy, so `fd_read` on an in-session port is the same suspending read as\n * `fd_read` on a remote host.\n */\n connectStream(port) {\n return socketStreamFor(this.openLoopbackClient(port));\n }\n /**\n * An already-accepted connection in the same `Socket` shape, so a server's\n * accepted socket is the same kind of file descriptor as a client's dialed\n * one. `accept`/`acceptNow` still hand out the connection id, because accept\n * itself stays on the cooperative pump - this only binds the result.\n */\n streamFor(id) {\n const conn = this.connections.get(Number(id));\n if (!conn) throw new Error(`connection is closed: ${id}`);\n return socketStreamFor(conn);\n }\n openLoopbackClient(port) {\n const n = Number(port);\n if (!Number.isInteger(n) || n <= 0 || n >= 65536) throw new Error(`invalid port: ${port}`);\n const route = this.host.__nimbusVirtualSocketRouteLoopback;\n if (typeof route !== \"function\") {\n throw new Error(\"Nimbus loopback sockets are unavailable in this runtime\");\n }\n return new LoopbackClientConnection(this.nextConnectionId++, n, route, this.limits);\n }\n /** Plain number array: Pyodide bytes() and the ruby.wasm base64 bridge both consume it. */\n recv(id, maxBytes) {\n const conn = this.connections.get(Number(id));\n if (!conn) return [];\n return conn.read(Number(maxBytes));\n }\n /**\n * Suspending read. A client connection has nothing to hand back until\n * the loopback response arrives, so guests that can suspend (Pyodide\n * `run_sync`, a JSPI-suspending WASI call) await this instead of\n * spinning on recv(). An empty result is EOF.\n */\n async recvAsync(id, maxBytes) {\n const conn = this.connections.get(Number(id));\n if (!conn) return [];\n return conn.readAsync(Number(maxBytes));\n }\n /**\n * Whether an empty recv() means end-of-response rather than \"not yet\".\n * Guests that poll instead of suspending (ruby.wasm, whose JS bridge is\n * synchronous) need the two apart to avoid truncating a response.\n */\n atEof(id) {\n const conn = this.connections.get(Number(id));\n return conn ? conn.atEof() : true;\n }\n send(id, bytesLike) {\n const conn = this.connections.get(Number(id));\n if (!conn) throw new Error(`connection is closed: ${id}`);\n return conn.write(bytesLike);\n }\n close(id) {\n const conn = this.connections.get(Number(id));\n if (!conn) return;\n conn.close();\n this.connections.delete(Number(id));\n }\n pending(port) {\n return this.listeners.get(Number(port))?.pending() ?? 0;\n }\n firstListeningPort() {\n for (const port of this.listeners.keys()) return port;\n return null;\n }\n /** select()-style readiness: resolves ports with queued connections, [] on timeout. */\n waitReadable(ports, timeoutSeconds) {\n const normalized = (Array.isArray(ports) ? ports : []).map((p) => Number(p)).filter((p) => Number.isInteger(p));\n const readyNow = normalized.filter((port) => this.pending(port) > 0);\n if (readyNow.length > 0) return Promise.resolve(readyNow);\n const timeoutMs = timeoutSeconds == null ? this.limits.responseTimeoutMs : Math.max(0, Number(timeoutSeconds) * 1e3);\n const deferred = new Deferred();\n const waiter = {\n ports: normalized,\n deferred,\n timer: setTimeout(() => {\n this.readableWaiters.delete(waiter);\n deferred.resolve([]);\n }, timeoutMs)\n };\n this.readableWaiters.add(waiter);\n return deferred.promise;\n }\n async waitForListen(timeoutMs) {\n const existing = this.firstListeningPort();\n if (existing) return existing;\n const waiter = new Deferred();\n this.listenWaiters.push(waiter);\n let timer = null;\n const timeout = new Promise((resolve) => {\n timer = setTimeout(() => resolve(null), Math.max(1, timeoutMs ?? 5e3));\n });\n try {\n return await Promise.race([waiter.promise, timeout]);\n } finally {\n if (timer !== null) clearTimeout(timer);\n }\n }\n async handleHttpRequest(port, request) {\n const n = Number(port);\n let listener = this.listeners.get(n);\n if (!listener) {\n try {\n await this.host.__nimbusVirtualSocketEnsureListener?.(n);\n } catch {\n }\n listener = this.listeners.get(n);\n }\n if (!listener) {\n return new Response(`Nimbus virtual socket: no listener on port ${port}`, { status: 502 });\n }\n const body = request.method === \"GET\" || request.method === \"HEAD\" ? EMPTY_BYTES : new Uint8Array(await request.arrayBuffer());\n if (body.byteLength > this.limits.maxRequestBodyBytes) {\n return new Response(\n `Nimbus virtual socket: request body exceeds ${this.limits.maxRequestBodyBytes} bytes`,\n { status: 413 }\n );\n }\n const id = this.nextConnectionId++;\n const conn = new VirtualConnection(id, request.method, encodeHttpRequest(request, body), this.limits);\n this.connections.set(id, conn);\n listener.push(conn);\n this.notifyReadable(n);\n const signal = request.signal;\n const onAbort = () => conn.abort(\"client aborted the request\", 499);\n if (signal?.aborted) onAbort();\n else signal?.addEventListener(\"abort\", onAbort);\n try {\n try {\n const accepted = await this.host.__nimbusVirtualSocketRequestQueued?.(n);\n if (accepted === false) {\n const detail = typeof this.host.__nimbusVirtualSocketLastError === \"string\" ? this.host.__nimbusVirtualSocketLastError.trim() : \"\";\n const suffix = detail ? `: ${detail}` : \"\";\n return new Response(\n `Nimbus virtual socket: runtime handler did not accept the request${suffix}`,\n { status: 502 }\n );\n }\n } catch {\n }\n return await conn.response(this.limits.responseTimeoutMs);\n } finally {\n signal?.removeEventListener(\"abort\", onAbort);\n conn.close();\n this.connections.delete(id);\n }\n }\n notifyReadable(port) {\n for (const waiter of Array.from(this.readableWaiters)) {\n if (!waiter.ports.includes(port)) continue;\n const ready = waiter.ports.filter((p) => this.pending(p) > 0);\n if (ready.length === 0) continue;\n clearTimeout(waiter.timer);\n this.readableWaiters.delete(waiter);\n waiter.deferred.resolve(ready);\n }\n }\n };\n function installVirtualSocketKernel(scope = globalThis) {\n if (!scope.__nimbusVirtualSockets) {\n scope.__nimbusVirtualSockets = new VirtualSocketKernel(scope);\n }\n return scope.__nimbusVirtualSockets;\n }\n\n // <stdin>\n installVirtualSocketKernel();\n})();\n";
|
|
@@ -99,7 +99,9 @@ function encodeHttpRequest(request, body) {
|
|
|
99
99
|
headers.set('Content-Length', String(body.byteLength));
|
|
100
100
|
}
|
|
101
101
|
const lines = [`${request.method} ${path} HTTP/1.1`];
|
|
102
|
-
headers.forEach((value, key) =>
|
|
102
|
+
headers.forEach((value, key) => {
|
|
103
|
+
lines.push(`${key}: ${value}`);
|
|
104
|
+
});
|
|
103
105
|
lines.push('', '');
|
|
104
106
|
return concatBytes([new TextEncoder().encode(lines.join('\r\n')), body]);
|
|
105
107
|
}
|
|
@@ -115,7 +117,9 @@ function encodeHttpRequest(request, body) {
|
|
|
115
117
|
*/
|
|
116
118
|
function encodeHttpResponseHead(status, statusText, headers, chunkedBody) {
|
|
117
119
|
const lines = [`HTTP/1.1 ${status} ${statusText}`];
|
|
118
|
-
headers.forEach((value, key) =>
|
|
120
|
+
headers.forEach((value, key) => {
|
|
121
|
+
lines.push(`${key}: ${value}`);
|
|
122
|
+
});
|
|
119
123
|
if (chunkedBody)
|
|
120
124
|
lines.push('Transfer-Encoding: chunked');
|
|
121
125
|
lines.push('Connection: close');
|
|
@@ -647,8 +651,8 @@ class LoopbackClientConnection {
|
|
|
647
651
|
throw new Error('Nimbus loopback socket: read before a complete HTTP request was written ' +
|
|
648
652
|
'(loopback sockets carry HTTP requests, not arbitrary byte streams)');
|
|
649
653
|
}
|
|
650
|
-
|
|
651
|
-
await
|
|
654
|
+
this.readable ??= new Deferred();
|
|
655
|
+
await this.readable.promise;
|
|
652
656
|
}
|
|
653
657
|
}
|
|
654
658
|
close() {
|
|
@@ -775,7 +779,8 @@ class LoopbackClientConnection {
|
|
|
775
779
|
awaitDrain() {
|
|
776
780
|
if (this.inbound.pendingBytes <= LOOPBACK_READ_HIGH_WATER_BYTES)
|
|
777
781
|
return Promise.resolve();
|
|
778
|
-
|
|
782
|
+
this.drained ??= new Deferred();
|
|
783
|
+
return this.drained.promise;
|
|
779
784
|
}
|
|
780
785
|
withTimeout(promise) {
|
|
781
786
|
let timer = null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wasm-runner.d.ts","sourceRoot":"","sources":["../../src/runtime/wasm-runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAC3F,OAAO,KAAK,EAAS,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAChF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"wasm-runner.d.ts","sourceRoot":"","sources":["../../src/runtime/wasm-runner.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAC3F,OAAO,KAAK,EAAS,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAChF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAmDtD,eAAO,MAAM,mBAAmB,UAAU,CAAC;AAE3C,eAAO,MAAM,gBAAgB,QAoCwC,CAAC;AAEtE,wBAAgB,wBAAwB,IAAI,MAAM,CASjD;AAyCD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE;IACnC,GAAG,EAAE,SAAS,CAAC;IACf,MAAM,EAAE,SAAS,CAAC;IAClB,SAAS,EAAE,wBAAwB,CAAC;CACrC,IAEG,OAAO,MAAM,EACb,MAAM,cAAc,KACnB,OAAO,CAAC,gBAAgB,CAAC,CA+mB7B;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE;IACnC,GAAG,EAAE,SAAS,CAAC;IACf,MAAM,EAAE,SAAS,CAAC;IAClB,SAAS,EAAE,wBAAwB,CAAC;CACrC,GAAG,WAAW,CAgBd"}
|
|
@@ -50,6 +50,7 @@ import { requireVfsCred, WASM32_WASI_NIMBUS_ABI } from './os-contracts.js';
|
|
|
50
50
|
import { WASI_INSTANCE_PREAMBLE_SRC, WASI_IMPLEMENTED_FNS, WASI_ABI_NAMESPACE } from './wasi-instance.js';
|
|
51
51
|
import { inspectWasmThreads, wasiThreadsLoadError } from './wasi-threads.js';
|
|
52
52
|
import { withMemoryLimit, DEFAULT_WASM_PROCESS_LIMIT_BYTES } from './wasm-memory.js';
|
|
53
|
+
import { errorText } from '../_shared/error-text.js';
|
|
53
54
|
export const WASM_RUNNER_VERSION = '0.3.0';
|
|
54
55
|
export const WASM_RUNNER_HELP = 'Usage: wasm-runner [options] <file.wasm> [exportName] [int args...]\n' +
|
|
55
56
|
' wasm-runner --version\n' +
|
|
@@ -271,6 +272,12 @@ export function makeWasmRunner(deps) {
|
|
|
271
272
|
// overly conservative; cast to ArrayBuffer is correct.
|
|
272
273
|
const buf = limited.buffer.slice(limited.byteOffset, limited.byteOffset + limited.byteLength);
|
|
273
274
|
const facetFn = async function wasmFacetCall(args, facetEnv) {
|
|
275
|
+
// This body is serialized into the facet isolate, where the supervisor's
|
|
276
|
+
// module graph — and so _shared/error-text.js — does not exist. Same
|
|
277
|
+
// fallback as errorText, spelled out locally.
|
|
278
|
+
const errText = (err) => typeof err === 'object' && err !== null && 'message' in err && err.message
|
|
279
|
+
? String(err.message)
|
|
280
|
+
: String(err);
|
|
274
281
|
const wasmTable = globalThis.__NIMBUS_WASM || {};
|
|
275
282
|
const mod = wasmTable['user.wasm'];
|
|
276
283
|
if (!mod) {
|
|
@@ -390,7 +397,7 @@ export function makeWasmRunner(deps) {
|
|
|
390
397
|
mode: 'wasi',
|
|
391
398
|
error: `wasi-threads: could not reserve the shared memory the module declares `
|
|
392
399
|
+ `(${args.threads.memory.initial}–${args.threads.memory.maximum} pages, `
|
|
393
|
-
+ `${(args.threads.memory.maximum * 64) / 1024} MiB): ${e
|
|
400
|
+
+ `${(args.threads.memory.maximum * 64) / 1024} MiB): ${errText(e)}. `
|
|
394
401
|
+ 'A shared memory reserves its maximum immediately — lower --max-memory.',
|
|
395
402
|
};
|
|
396
403
|
}
|
|
@@ -414,11 +421,14 @@ export function makeWasmRunner(deps) {
|
|
|
414
421
|
return {
|
|
415
422
|
ok: false,
|
|
416
423
|
mode: 'wasi',
|
|
417
|
-
error: `instantiate failed: ${e
|
|
424
|
+
error: `instantiate failed: ${errText(e)}`,
|
|
418
425
|
};
|
|
419
426
|
}
|
|
420
|
-
if (!memRef.mem)
|
|
421
|
-
|
|
427
|
+
if (!memRef.mem) {
|
|
428
|
+
const exported = inst.exports.memory;
|
|
429
|
+
if (exported instanceof WebAssembly.Memory)
|
|
430
|
+
memRef.mem = exported;
|
|
431
|
+
}
|
|
422
432
|
if (!memRef.mem) {
|
|
423
433
|
return {
|
|
424
434
|
ok: false,
|
|
@@ -464,7 +474,7 @@ export function makeWasmRunner(deps) {
|
|
|
464
474
|
return {
|
|
465
475
|
ok: false,
|
|
466
476
|
mode: 'direct',
|
|
467
|
-
error: `instantiate failed: ${e
|
|
477
|
+
error: `instantiate failed: ${errText(e)}`,
|
|
468
478
|
};
|
|
469
479
|
}
|
|
470
480
|
const exportNames = Object.keys(inst.exports);
|
|
@@ -487,7 +497,7 @@ export function makeWasmRunner(deps) {
|
|
|
487
497
|
ok: false,
|
|
488
498
|
mode: 'direct',
|
|
489
499
|
exports: exportNames,
|
|
490
|
-
error: `${args.exportName}(${(args.intArgs || []).join(', ')}) threw: ${e
|
|
500
|
+
error: `${args.exportName}(${(args.intArgs || []).join(', ')}) threw: ${errText(e)}`,
|
|
491
501
|
};
|
|
492
502
|
}
|
|
493
503
|
// BigInt (i64) → string; everything else → as-is.
|
|
@@ -585,7 +595,7 @@ export function makeWasmRunner(deps) {
|
|
|
585
595
|
}));
|
|
586
596
|
}
|
|
587
597
|
catch (e) {
|
|
588
|
-
outcome = { ok: false, error: `dispatch failed: ${e
|
|
598
|
+
outcome = { ok: false, error: `dispatch failed: ${errorText(e)}` };
|
|
589
599
|
}
|
|
590
600
|
finally {
|
|
591
601
|
facet?.dispose();
|
|
@@ -603,15 +613,13 @@ export function makeWasmRunner(deps) {
|
|
|
603
613
|
// If runStart reported an `error` (wasm trapped, _start missing,
|
|
604
614
|
// …), append it to stderr but still surface its exitCode (default
|
|
605
615
|
// 1 from runStart on trap) so callers can distinguish.
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
stderr = wasiOut.stderr || '';
|
|
610
|
-
if (wasiOut.error) {
|
|
616
|
+
stdout = outcome.stdout || '';
|
|
617
|
+
stderr = outcome.stderr || '';
|
|
618
|
+
if (outcome.error) {
|
|
611
619
|
stderr = (stderr ? stderr : '') +
|
|
612
|
-
`wasm-runner: wasi trap: ${
|
|
620
|
+
`wasm-runner: wasi trap: ${outcome.error}\n`;
|
|
613
621
|
}
|
|
614
|
-
exitCode =
|
|
622
|
+
exitCode = outcome.exitCode ?? (outcome.ok ? 0 : 1);
|
|
615
623
|
}
|
|
616
624
|
else if (!outcome.ok) {
|
|
617
625
|
// Direct-mode failure or pre-instantiate dispatch failure — shell
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features.d.ts","sourceRoot":"","sources":["../../src/shell/features.ts"],"names":[],"mappings":"AAGA,UAAU,SAAS;IACjB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACzC,WAAW,IAAI,IAAI,CAAC;IACpB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,eAAe,IAAI,IAAI,CAAC;IACxB,UAAU,IAAI,IAAI,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB;AAED,UAAU,YAAY;IACpB,KAAK,CAAC,IA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,sFAAsF;IACtF,OAAO,CAAC,QAAQ,CAAc;IAE9B;0EACsE;IACtE,OAAO,CAAC,gBAAgB,CAAc;IACtC;uEACmE;IACnE,OAAO,CAAC,sBAAsB,CAAa;IAE3C,uCAAuC;IACvC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,YAAY,CAAM;IAC1B;8EAC0E;IAC1E,OAAO,CAAC,iBAAiB,CAAM;IAE/B,0EAA0E;IAC1E,OAAO,CAAC,MAAM,CAAC,UAAU;gBAWb,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY;IAKpD,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,GAAG,kBAAkB;IAM5E,OAAO,CAAC,MAAM;IA+Bd;;;OAGG;IACH,OAAO,CAAC,cAAc;IA2MtB,kEAAkE;IAClE,OAAO,CAAC,cAAc;IAStB,4DAA4D;IAC5D,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,kBAAkB;IAuB1B,OAAO,CAAC,YAAY;IAiCpB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,cAAc;IA0EtB,qEAAqE;IACrE,OAAO,CAAC,eAAe;IAavB,8DAA8D;IAC9D,OAAO,CAAC,cAAc;IAgBtB,OAAO,CAAC,oBAAoB;IAM5B,qCAAqC;IACrC,OAAO,CAAC,cAAc;IAWtB;;;;OAIG;IACH,OAAO,CAAC,YAAY;CAWrB"}
|
package/dist/shell/features.js
CHANGED
|
@@ -121,7 +121,7 @@ export class HeredocHandler {
|
|
|
121
121
|
}
|
|
122
122
|
return;
|
|
123
123
|
}
|
|
124
|
-
|
|
124
|
+
this._handleOriginalInput(data);
|
|
125
125
|
};
|
|
126
126
|
this.shell.executeLine = async (line) => {
|
|
127
127
|
if (this.active) {
|
|
@@ -144,7 +144,7 @@ export class HeredocHandler {
|
|
|
144
144
|
this._accumulateLine('');
|
|
145
145
|
return;
|
|
146
146
|
}
|
|
147
|
-
|
|
147
|
+
this._printPrompt();
|
|
148
148
|
};
|
|
149
149
|
}
|
|
150
150
|
_printPrompt() {
|
|
@@ -353,7 +353,8 @@ export class LineEditorExtender {
|
|
|
353
353
|
// shell-prompt input. Process-stdin input has its own model
|
|
354
354
|
// (terminalStdin / stdinLineBuffer) handled by the shell.
|
|
355
355
|
if (this.shell.running) {
|
|
356
|
-
|
|
356
|
+
this.originalHandleInput(data);
|
|
357
|
+
return;
|
|
357
358
|
}
|
|
358
359
|
// Reverse-i-search sub-mode owns ALL input until exited.
|
|
359
360
|
if (this.rsearchActive) {
|
|
@@ -368,7 +369,7 @@ export class LineEditorExtender {
|
|
|
368
369
|
// Try each binding. First-match wins; on no match fall through.
|
|
369
370
|
if (this._handleBinding(data))
|
|
370
371
|
return;
|
|
371
|
-
|
|
372
|
+
this.originalHandleInput(data);
|
|
372
373
|
};
|
|
373
374
|
}
|
|
374
375
|
/**
|