@nodetool-ai/sandbox-runtime 0.0.0-stage → 0.8.9-beta.2
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/README.md +24 -2
- package/dist/.nodetool-build-stamp +1 -0
- package/dist/host-access-gate.d.ts +24 -0
- package/dist/host-access-gate.d.ts.map +1 -0
- package/dist/host-access-gate.js +31 -0
- package/dist/host-access-gate.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +318 -0
- package/dist/index.js.map +1 -0
- package/dist/js-sandbox-worker/engine.d.ts +4 -0
- package/dist/js-sandbox-worker/engine.d.ts.map +1 -0
- package/dist/js-sandbox-worker/engine.js +82 -0
- package/dist/js-sandbox-worker/engine.js.map +1 -0
- package/dist/js-sandbox-worker/host.d.ts +136 -0
- package/dist/js-sandbox-worker/host.d.ts.map +1 -0
- package/dist/js-sandbox-worker/host.js +482 -0
- package/dist/js-sandbox-worker/host.js.map +1 -0
- package/dist/js-sandbox-worker/interpreter.d.ts +276 -0
- package/dist/js-sandbox-worker/interpreter.d.ts.map +1 -0
- package/dist/js-sandbox-worker/interpreter.js +1318 -0
- package/dist/js-sandbox-worker/interpreter.js.map +1 -0
- package/dist/js-sandbox-worker/protocol.d.ts +168 -0
- package/dist/js-sandbox-worker/protocol.d.ts.map +1 -0
- package/dist/js-sandbox-worker/protocol.js +217 -0
- package/dist/js-sandbox-worker/protocol.js.map +1 -0
- package/dist/js-sandbox-worker/worker-entry.d.ts +26 -0
- package/dist/js-sandbox-worker/worker-entry.d.ts.map +1 -0
- package/dist/js-sandbox-worker/worker-entry.js +308 -0
- package/dist/js-sandbox-worker/worker-entry.js.map +1 -0
- package/dist/limits.d.ts +77 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +56 -0
- package/dist/limits.js.map +1 -0
- package/dist/sandbox-bootstrap-modules.d.ts +54 -0
- package/dist/sandbox-bootstrap-modules.d.ts.map +1 -0
- package/dist/sandbox-bootstrap-modules.js +104 -0
- package/dist/sandbox-bootstrap-modules.js.map +1 -0
- package/dist/sandbox-bytes.d.ts +43 -0
- package/dist/sandbox-bytes.d.ts.map +1 -0
- package/dist/sandbox-bytes.js +61 -0
- package/dist/sandbox-bytes.js.map +1 -0
- package/dist/sandbox-canvas-api.d.ts +30 -0
- package/dist/sandbox-canvas-api.d.ts.map +1 -0
- package/dist/sandbox-canvas-api.js +81 -0
- package/dist/sandbox-canvas-api.js.map +1 -0
- package/dist/sandbox-constants.d.ts +5 -0
- package/dist/sandbox-constants.d.ts.map +1 -0
- package/dist/sandbox-constants.js +13 -0
- package/dist/sandbox-constants.js.map +1 -0
- package/dist/sandbox-json-transport.d.ts +107 -0
- package/dist/sandbox-json-transport.d.ts.map +1 -0
- package/dist/sandbox-json-transport.js +324 -0
- package/dist/sandbox-json-transport.js.map +1 -0
- package/dist/timeline-capture.d.ts +21 -0
- package/dist/timeline-capture.d.ts.map +1 -0
- package/dist/timeline-capture.js +564 -0
- package/dist/timeline-capture.js.map +1 -0
- package/dist/utils/type-guards.d.ts +26 -0
- package/dist/utils/type-guards.d.ts.map +1 -0
- package/dist/utils/type-guards.js +44 -0
- package/dist/utils/type-guards.js.map +1 -0
- package/package.json +138 -4
- package/vendor/README.md +17 -0
- package/vendor/quickjs/LICENSE +21 -0
- package/vendor/quickjs/PROVENANCE.json +8 -0
- package/vendor/quickjs/adapter/fetch.d.ts +56 -0
- package/vendor/quickjs/adapter/fetch.js +140 -0
- package/vendor/quickjs/createTimeInterval.d.ts +9 -0
- package/vendor/quickjs/createTimeInterval.js +22 -0
- package/vendor/quickjs/createVirtualFileSystem.d.ts +13 -0
- package/vendor/quickjs/createVirtualFileSystem.js +191 -0
- package/vendor/quickjs/index.d.ts +77 -0
- package/vendor/quickjs/index.js +77 -0
- package/vendor/quickjs/loadAsyncQuickJs.d.ts +12 -0
- package/vendor/quickjs/loadAsyncQuickJs.js +66 -0
- package/vendor/quickjs/loadQuickJs.d.ts +12 -0
- package/vendor/quickjs/loadQuickJs.js +73 -0
- package/vendor/quickjs/modules/assert.d.ts +2 -0
- package/vendor/quickjs/modules/assert.js +47 -0
- package/vendor/quickjs/modules/buffer.d.ts +2 -0
- package/vendor/quickjs/modules/buffer.js +106 -0
- package/vendor/quickjs/modules/build/test-lib.js +2 -0
- package/vendor/quickjs/modules/events.d.ts +2 -0
- package/vendor/quickjs/modules/events.js +4 -0
- package/vendor/quickjs/modules/fs.d.ts +2 -0
- package/vendor/quickjs/modules/fs.js +76 -0
- package/vendor/quickjs/modules/fs_promises.d.ts +2 -0
- package/vendor/quickjs/modules/fs_promises.js +328 -0
- package/vendor/quickjs/modules/module.d.ts +2 -0
- package/vendor/quickjs/modules/module.js +33 -0
- package/vendor/quickjs/modules/nodeCompatibility/headers.d.ts +2 -0
- package/vendor/quickjs/modules/nodeCompatibility/headers.js +58 -0
- package/vendor/quickjs/modules/nodeCompatibility/request.d.ts +2 -0
- package/vendor/quickjs/modules/nodeCompatibility/request.js +5 -0
- package/vendor/quickjs/modules/nodeCompatibility/response.d.ts +2 -0
- package/vendor/quickjs/modules/nodeCompatibility/response.js +5 -0
- package/vendor/quickjs/modules/path.d.ts +2 -0
- package/vendor/quickjs/modules/path.js +504 -0
- package/vendor/quickjs/modules/process.d.ts +2 -0
- package/vendor/quickjs/modules/process.js +309 -0
- package/vendor/quickjs/modules/punycode.d.ts +2 -0
- package/vendor/quickjs/modules/punycode.js +443 -0
- package/vendor/quickjs/modules/querystring.d.ts +2 -0
- package/vendor/quickjs/modules/querystring.js +15 -0
- package/vendor/quickjs/modules/string_decoder.d.ts +2 -0
- package/vendor/quickjs/modules/string_decoder.js +223 -0
- package/vendor/quickjs/modules/timers.d.ts +2 -0
- package/vendor/quickjs/modules/timers.js +10 -0
- package/vendor/quickjs/modules/timers_promises.d.ts +2 -0
- package/vendor/quickjs/modules/timers_promises.js +40 -0
- package/vendor/quickjs/modules/url.d.ts +2 -0
- package/vendor/quickjs/modules/url.js +104 -0
- package/vendor/quickjs/modules/util.d.ts +2 -0
- package/vendor/quickjs/modules/util.js +233 -0
- package/vendor/quickjs/package.json +3 -0
- package/vendor/quickjs/sandbox/asyncVersion/createAsyncEvalCodeFunction.d.ts +4 -0
- package/vendor/quickjs/sandbox/asyncVersion/createAsyncEvalCodeFunction.js +76 -0
- package/vendor/quickjs/sandbox/asyncVersion/createAsyncValidateCodeFunction.d.ts +3 -0
- package/vendor/quickjs/sandbox/asyncVersion/createAsyncValidateCodeFunction.js +22 -0
- package/vendor/quickjs/sandbox/asyncVersion/executeAsyncSandboxFunction.d.ts +12 -0
- package/vendor/quickjs/sandbox/asyncVersion/executeAsyncSandboxFunction.js +9 -0
- package/vendor/quickjs/sandbox/asyncVersion/getAsyncModuleLoader.d.ts +4 -0
- package/vendor/quickjs/sandbox/asyncVersion/getAsyncModuleLoader.js +30 -0
- package/vendor/quickjs/sandbox/asyncVersion/modulePathNormalizerAsync.d.ts +2 -0
- package/vendor/quickjs/sandbox/asyncVersion/modulePathNormalizerAsync.js +12 -0
- package/vendor/quickjs/sandbox/asyncVersion/prepareAsyncNodeCompatibility.d.ts +3 -0
- package/vendor/quickjs/sandbox/asyncVersion/prepareAsyncNodeCompatibility.js +11 -0
- package/vendor/quickjs/sandbox/asyncVersion/prepareAsyncSandbox.d.ts +4 -0
- package/vendor/quickjs/sandbox/asyncVersion/prepareAsyncSandbox.js +10 -0
- package/vendor/quickjs/sandbox/expose/expose.d.ts +3 -0
- package/vendor/quickjs/sandbox/expose/expose.js +155 -0
- package/vendor/quickjs/sandbox/expose/isES2015Class.d.ts +1 -0
- package/vendor/quickjs/sandbox/expose/isES2015Class.js +4 -0
- package/vendor/quickjs/sandbox/expose/isObject.d.ts +1 -0
- package/vendor/quickjs/sandbox/expose/isObject.js +4 -0
- package/vendor/quickjs/sandbox/expose/pendingHostPromises.d.ts +6 -0
- package/vendor/quickjs/sandbox/expose/pendingHostPromises.js +40 -0
- package/vendor/quickjs/sandbox/getMaxIntervalAmount.d.ts +8 -0
- package/vendor/quickjs/sandbox/getMaxIntervalAmount.js +12 -0
- package/vendor/quickjs/sandbox/getMaxTimeoutAmount.d.ts +8 -0
- package/vendor/quickjs/sandbox/getMaxTimeoutAmount.js +12 -0
- package/vendor/quickjs/sandbox/handleEvalError.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleEvalError.js +21 -0
- package/vendor/quickjs/sandbox/handleToNative/handleToNative.d.ts +9 -0
- package/vendor/quickjs/sandbox/handleToNative/handleToNative.js +153 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/index.d.ts +10 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/index.js +36 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeArrayBuffer.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeArrayBuffer.js +4 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeBuffer.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeBuffer.js +16 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeDate.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeDate.js +16 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeError.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeError.js +18 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeHeaders.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeHeaders.js +17 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeMap.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeMap.js +18 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeSet.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeSet.js +16 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeURLSearchParams.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeURLSearchParams.js +17 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeUrl.d.ts +2 -0
- package/vendor/quickjs/sandbox/handleToNative/serializer/serializeUrl.js +17 -0
- package/vendor/quickjs/sandbox/helper.d.ts +2 -0
- package/vendor/quickjs/sandbox/helper.js +14 -0
- package/vendor/quickjs/sandbox/provide/provideConsole.d.ts +6 -0
- package/vendor/quickjs/sandbox/provide/provideConsole.js +53 -0
- package/vendor/quickjs/sandbox/provide/provideEnv.d.ts +3 -0
- package/vendor/quickjs/sandbox/provide/provideEnv.js +10 -0
- package/vendor/quickjs/sandbox/provide/provideFs.d.ts +4 -0
- package/vendor/quickjs/sandbox/provide/provideFs.js +159 -0
- package/vendor/quickjs/sandbox/provide/provideHttp.d.ts +9 -0
- package/vendor/quickjs/sandbox/provide/provideHttp.js +42 -0
- package/vendor/quickjs/sandbox/provide/provideTimingFunctions.d.ts +7 -0
- package/vendor/quickjs/sandbox/provide/provideTimingFunctions.js +119 -0
- package/vendor/quickjs/sandbox/setupFileSystem.d.ts +3 -0
- package/vendor/quickjs/sandbox/setupFileSystem.js +13 -0
- package/vendor/quickjs/sandbox/syncVersion/createEvalCodeFunction.d.ts +4 -0
- package/vendor/quickjs/sandbox/syncVersion/createEvalCodeFunction.js +76 -0
- package/vendor/quickjs/sandbox/syncVersion/createValidateCodeFunction.d.ts +3 -0
- package/vendor/quickjs/sandbox/syncVersion/createValidateCodeFunction.js +22 -0
- package/vendor/quickjs/sandbox/syncVersion/executeSandboxFunction.d.ts +12 -0
- package/vendor/quickjs/sandbox/syncVersion/executeSandboxFunction.js +9 -0
- package/vendor/quickjs/sandbox/syncVersion/getModuleLoader.d.ts +4 -0
- package/vendor/quickjs/sandbox/syncVersion/getModuleLoader.js +30 -0
- package/vendor/quickjs/sandbox/syncVersion/modulePathNormalizer.d.ts +2 -0
- package/vendor/quickjs/sandbox/syncVersion/modulePathNormalizer.js +12 -0
- package/vendor/quickjs/sandbox/syncVersion/prepareNodeCompatibility.d.ts +3 -0
- package/vendor/quickjs/sandbox/syncVersion/prepareNodeCompatibility.js +11 -0
- package/vendor/quickjs/sandbox/syncVersion/prepareSandbox.d.ts +4 -0
- package/vendor/quickjs/sandbox/syncVersion/prepareSandbox.js +10 -0
- package/vendor/quickjs/types/CodeFunctionInput.d.ts +12 -0
- package/vendor/quickjs/types/CodeFunctionInput.js +1 -0
- package/vendor/quickjs/types/ErrorResponse.d.ts +9 -0
- package/vendor/quickjs/types/ErrorResponse.js +1 -0
- package/vendor/quickjs/types/LoadQuickJsOptions.d.ts +3 -0
- package/vendor/quickjs/types/LoadQuickJsOptions.js +1 -0
- package/vendor/quickjs/types/OkResponse.d.ts +4 -0
- package/vendor/quickjs/types/OkResponse.js +1 -0
- package/vendor/quickjs/types/OkResponseCheck.d.ts +3 -0
- package/vendor/quickjs/types/OkResponseCheck.js +1 -0
- package/vendor/quickjs/types/Prettify.d.ts +3 -0
- package/vendor/quickjs/types/Prettify.js +1 -0
- package/vendor/quickjs/types/RuntimeOptions.d.ts +87 -0
- package/vendor/quickjs/types/RuntimeOptions.js +1 -0
- package/vendor/quickjs/types/SandboxEvalCode.d.ts +5 -0
- package/vendor/quickjs/types/SandboxEvalCode.js +1 -0
- package/vendor/quickjs/types/SandboxFunction.d.ts +16 -0
- package/vendor/quickjs/types/SandboxFunction.js +1 -0
- package/vendor/quickjs/types/SandboxOptions.d.ts +152 -0
- package/vendor/quickjs/types/SandboxOptions.js +1 -0
- package/vendor/quickjs/types/SandboxValidateCode.d.ts +9 -0
- package/vendor/quickjs/types/SandboxValidateCode.js +1 -0
- package/vendor/quickjs/types/Serializer.d.ts +2 -0
- package/vendor/quickjs/types/Serializer.js +1 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The interpreter half of the QuickJS sandbox: everything that happens *inside*
|
|
3
|
+
* one `runSandboxed` call.
|
|
4
|
+
*
|
|
5
|
+
* `runInSandbox` (../js-sandbox.ts) owns the host half — building the bridge
|
|
6
|
+
* record, the WASM/host/capability dispatchers, the cancellation race, the
|
|
7
|
+
* unhandled-rejection guard, and the result shape callers see. This module owns
|
|
8
|
+
* what runs against the guest context: the interrupt handler, the never-reject
|
|
9
|
+
* wrap layer, the init prelude, the entry module, and the sync-back extractor.
|
|
10
|
+
*
|
|
11
|
+
* The split exists so the guest can run on a `worker_threads` worker without a
|
|
12
|
+
* second copy of any of it. Both hosts call {@link runInterpreter}; the pieces
|
|
13
|
+
* that must live next to the guest (the module loader's closures, the
|
|
14
|
+
* serializers, the interrupt handler) live here, and the pieces that need live
|
|
15
|
+
* host objects reach this module as plain functions — {@link
|
|
16
|
+
* InterpreterParams.isAborted} and {@link InterpreterParams.suspendedMs} stand
|
|
17
|
+
* in for the `AbortSignal` and the `SandboxClock`, neither of which can cross a
|
|
18
|
+
* thread.
|
|
19
|
+
*
|
|
20
|
+
* Browser-safe: nothing here imports a Node builtin, statically or otherwise.
|
|
21
|
+
*/
|
|
22
|
+
import { loadQuickJs } from "../../vendor/quickjs/index.js";
|
|
23
|
+
import type { SandboxModuleResolution } from "@nodetool-ai/protocol/sandbox-package";
|
|
24
|
+
import type { ResolvedSandboxLimits } from "../limits.js";
|
|
25
|
+
/** The engine entry point one interpreter run drives. */
|
|
26
|
+
export type SandboxRunSandboxed = Awaited<ReturnType<typeof loadQuickJs>>["runSandboxed"];
|
|
27
|
+
type SandboxRunOptions = NonNullable<Parameters<SandboxRunSandboxed>[1]>;
|
|
28
|
+
/**
|
|
29
|
+
* The engine's wall-clock abort is a `setTimeout`, and Node fires a delay past
|
|
30
|
+
* this immediately instead of never — so this is the practical "no backstop".
|
|
31
|
+
*/
|
|
32
|
+
export declare const MAX_ENGINE_TIMEOUT_MS = 2147483647;
|
|
33
|
+
/**
|
|
34
|
+
* What the guest is told when it calls `stream` in a run that has no input
|
|
35
|
+
* stream behind it. Shared by the host bridge and the guest prelude so both
|
|
36
|
+
* paths say the same thing.
|
|
37
|
+
*/
|
|
38
|
+
export declare const NO_INPUT_STREAM_MESSAGE = "stream() requires streaming-input mode; this run has no input stream";
|
|
39
|
+
/**
|
|
40
|
+
* The library's serializer table is module-global, so this registers once —
|
|
41
|
+
* and a worker, holding its own module instance, registers its own.
|
|
42
|
+
*/
|
|
43
|
+
export declare function registerTypedArraySerializers(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Marker key placed on an object to signal a sandboxed error. A host async
|
|
46
|
+
* function that would normally `throw` instead resolves with one of these
|
|
47
|
+
* objects; a prelude inside the guest rewraps them as real guest-side errors.
|
|
48
|
+
*
|
|
49
|
+
* This indirection exists because the current `@sebastianwessel/quickjs`
|
|
50
|
+
* runtime leaks handles whenever a host-backed guest Promise is *rejected*,
|
|
51
|
+
* tripping an internal assertion (`list_empty(&rt->gc_obj_list)`) in
|
|
52
|
+
* quickjs-ng when the runtime is freed. Routing failures through a resolved
|
|
53
|
+
* tagged value sidesteps the leak while preserving the guest-visible
|
|
54
|
+
* behaviour (the user still gets a thrown Error with name + message).
|
|
55
|
+
*/
|
|
56
|
+
export declare const SANDBOX_ERROR_MARKER = "__nodetool_sandbox_error__";
|
|
57
|
+
/**
|
|
58
|
+
* Names injected as bridge bindings into the QuickJS guest. The rest of the
|
|
59
|
+
* `buildSandbox` record (JSON, Math, Date, URL, etc.) is deliberately NOT
|
|
60
|
+
* marshaled — QuickJS already provides native implementations, and re-exposing
|
|
61
|
+
* host versions creates thousands of handles that slow execution and leak on
|
|
62
|
+
* teardown.
|
|
63
|
+
*/
|
|
64
|
+
export declare const EXPOSED_BRIDGE_NAMES: readonly ["console", "fetch", "crypto", "sleep", "getSecret", "workspace", "assetToSandbox", "sandboxToAsset", "progress", "emit", "output", "format", "image", "audio", "video", "canvas", "media", "__maxIter", "__secretScope"];
|
|
65
|
+
export type ExposedBridgeName = (typeof EXPOSED_BRIDGE_NAMES)[number];
|
|
66
|
+
/**
|
|
67
|
+
* Globals the init prelude removes. `eval` and `Function` go so the guest
|
|
68
|
+
* cannot re-enter code generation. The rest are stubs
|
|
69
|
+
* `@sebastianwessel/quickjs` installs unconditionally (its node-compatibility
|
|
70
|
+
* layer and `provideEnv` have no off switch): `Buffer`, `process`, `env`,
|
|
71
|
+
* `Headers`, `Request`, `Response`, `performance`. Nothing in the sandbox's
|
|
72
|
+
* own machinery uses them — the fetch bridge returns plain objects and the
|
|
73
|
+
* guest-to-host serializers dispatch on constructor names no guest code can
|
|
74
|
+
* reach once the classes are gone — so they are deleted to keep the guest
|
|
75
|
+
* surface minimal instead of documented as stubs.
|
|
76
|
+
*/
|
|
77
|
+
export declare const DELETED_GUEST_GLOBALS: readonly ["eval", "Function", "Buffer", "process", "env", "Headers", "Request", "Response", "performance"];
|
|
78
|
+
/** Name the host parks the raw (never-rejecting) dispatcher bridge on. */
|
|
79
|
+
export declare const SANDBOX_WASM_BRIDGE_BINDING = "__wasmCall";
|
|
80
|
+
/** Name the host parks the raw (never-rejecting) host dispatcher bridge on. */
|
|
81
|
+
export declare const SANDBOX_HOST_BRIDGE_BINDING = "__hostCall";
|
|
82
|
+
/** Name the host parks the raw (never-rejecting) capability dispatcher on. */
|
|
83
|
+
export declare const SANDBOX_CAPABILITY_BRIDGE_BINDING = "__capabilityCall";
|
|
84
|
+
/**
|
|
85
|
+
* Names the host parks the input bridges on, which the prelude captures and
|
|
86
|
+
* then deletes — the guest reaches them only through `stream`.
|
|
87
|
+
*/
|
|
88
|
+
export declare const SANDBOX_INPUT_TAKE_BINDING = "__takeInput";
|
|
89
|
+
export declare const SANDBOX_STREAM_OPEN_BINDING = "__streamOpen";
|
|
90
|
+
/**
|
|
91
|
+
* Timer globals the guest must not see. The documented contract has always
|
|
92
|
+
* been "`sleep` is the only timer", but `@sebastianwessel/quickjs` installs
|
|
93
|
+
* host-backed `setTimeout`/`setInterval`/`setImmediate` into the context on
|
|
94
|
+
* every `evalCode` call — *after* the init prelude runs — so a prelude-time
|
|
95
|
+
* `delete` does not stick. {@link wrapCode} deletes them inside the user-code
|
|
96
|
+
* module itself, which evaluates after the library's re-install. They are
|
|
97
|
+
* blocked deliberately: their callbacks fire through `ctx.callFunction` with
|
|
98
|
+
* errors silently discarded, outside the never-reject and abort-guard
|
|
99
|
+
* conventions every bridge follows. Concurrency does not need them —
|
|
100
|
+
* bridge calls run in parallel under `Promise.all`/`parallelMap`.
|
|
101
|
+
*/
|
|
102
|
+
export declare const BLOCKED_TIMER_GLOBALS: readonly ["setTimeout", "clearTimeout", "setInterval", "clearInterval", "setImmediate", "clearImmediate"];
|
|
103
|
+
/**
|
|
104
|
+
* Wrap user code as the default export of an ES module with a top-level-awaited
|
|
105
|
+
* async IIFE body, so `return <value>` inside the snippet becomes the module's
|
|
106
|
+
* default export and `await` at the top level works. The module first drops
|
|
107
|
+
* the timer globals the engine re-installs per evaluation (see
|
|
108
|
+
* {@link BLOCKED_TIMER_GLOBALS}).
|
|
109
|
+
*/
|
|
110
|
+
export declare function wrapCode(code: string, prelude?: string, encodeResult?: boolean): string;
|
|
111
|
+
/**
|
|
112
|
+
* Build the entry module for user code, hoisting static `import` declarations
|
|
113
|
+
* out of the async IIFE {@link wrapCode} wraps the body in.
|
|
114
|
+
*
|
|
115
|
+
* An `import` is only legal at module top level, so the IIFE body cannot hold
|
|
116
|
+
* one. The imports are spliced out by source range and re-emitted on a single
|
|
117
|
+
* line above the wrapper; the ranges they vacate are blanked with spaces (never
|
|
118
|
+
* with fewer newlines), so every remaining line of user code keeps its content
|
|
119
|
+
* and its offset from the body's start. The whole body shifts down by exactly
|
|
120
|
+
* one line relative to {@link wrapCode}'s output.
|
|
121
|
+
*
|
|
122
|
+
* Import-free code takes the {@link wrapCode} path unchanged, and so does code
|
|
123
|
+
* acorn cannot parse — a syntax error must reach the guest exactly as the user
|
|
124
|
+
* wrote it, not as this transform's complaint about it.
|
|
125
|
+
*
|
|
126
|
+
* Dynamic `import()` expressions are left where they are. The module loader
|
|
127
|
+
* denies them at runtime (see {@link createGuestModuleHost}); rewriting them
|
|
128
|
+
* here would only move a runtime denial into a confusing compile-time one.
|
|
129
|
+
*/
|
|
130
|
+
export declare function buildEntryModule(source: string, prelude?: string, encodeResult?: boolean): string;
|
|
131
|
+
/**
|
|
132
|
+
* How many module lines {@link buildEntryModule} (or {@link wrapCode}) place
|
|
133
|
+
* before the first line of the code it was given.
|
|
134
|
+
*
|
|
135
|
+
* QuickJS reports stack positions in the wrapped entry module, so an error on
|
|
136
|
+
* the guest's line N sits at `N + offset` in the source the user wrote. The
|
|
137
|
+
* hoisted-import path spends one extra leading line (the joined imports) over
|
|
138
|
+
* the plain wrapper, and which path applied is decided by the same acorn parse
|
|
139
|
+
* {@link buildEntryModule} runs — so the offset must come from here, not be
|
|
140
|
+
* guessed by callers.
|
|
141
|
+
*/
|
|
142
|
+
export declare function entryBodyLineOffset(code: string): number;
|
|
143
|
+
export interface GuestModuleHost {
|
|
144
|
+
/** Loader and normalizer to hand `runSandboxed`. */
|
|
145
|
+
readonly options: Pick<SandboxRunOptions, "getModuleLoader" | "modulePathNormalizer">;
|
|
146
|
+
/** Stop delegating to the wrapper's own Node-compat resolution. */
|
|
147
|
+
enterGuestPhase(): void;
|
|
148
|
+
/** After the entry's static graph is linked, every further resolve is dynamic. */
|
|
149
|
+
lockStaticGraph(): void;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Install the run's declared sandbox modules as the *only* importable modules.
|
|
153
|
+
*
|
|
154
|
+
* The enforcement point is the **normalizer**, not the loader: QuickJS resolves
|
|
155
|
+
* an already-loaded module straight out of its cache without consulting the
|
|
156
|
+
* loader, but it normalizes every specifier first. A denial decided in the
|
|
157
|
+
* normalizer therefore also holds for `node:buffer` and friends, which the
|
|
158
|
+
* wrapper imports into the cache during its own bootstrap
|
|
159
|
+
* (`prepareNodeCompatibility`) before guest code exists.
|
|
160
|
+
*
|
|
161
|
+
* Three phases, in order:
|
|
162
|
+
*
|
|
163
|
+
* 1. **bootstrap** — the wrapper's `import 'node:buffer'` and the rest of its
|
|
164
|
+
* compat preamble run before our sandboxed function is called. They resolve
|
|
165
|
+
* through the library's own normalizer, and through its loader for anything
|
|
166
|
+
* {@link BOOTSTRAP_MODULE_SOURCES} does not serve a cheaper source for, or
|
|
167
|
+
* the runtime never starts.
|
|
168
|
+
* 2. **guest, linking** — only the run's declared specifiers and their
|
|
169
|
+
* intra-pack siblings resolve. Everything else — `node:*`, the compat
|
|
170
|
+
* modules the bootstrap warmed, absolute paths, `../` escapes, encoded
|
|
171
|
+
* traversals, another pack's internal files — is denied by name.
|
|
172
|
+
* 3. **guest, locked** — the entry module's static graph is linked, so any
|
|
173
|
+
* further resolution is a dynamic `import()`, and every one of those is
|
|
174
|
+
* denied. A computed specifier is unknowable ahead of the call, and the
|
|
175
|
+
* declaration is the whole contract, so the stricter rule is the honest one.
|
|
176
|
+
*
|
|
177
|
+
* Loaded module sources are prefixed with the timer deletions
|
|
178
|
+
* ({@link BLOCKED_TIMER_GLOBALS}) on the same line as their first statement:
|
|
179
|
+
* dependencies evaluate *before* the entry body, so the entry's own deletions
|
|
180
|
+
* come too late to harden them. `eval`/`Function` need no such treatment — the
|
|
181
|
+
* init prelude deletes them for the whole context before any user `evalCode`.
|
|
182
|
+
*/
|
|
183
|
+
export declare function createGuestModuleHost(modules: SandboxModuleResolution | undefined, capabilityFacades?: ReadonlyMap<string, string>): GuestModuleHost;
|
|
184
|
+
/** One dispatcher call: WASM, host module, or platform capability. */
|
|
185
|
+
export type SandboxDispatchCall = (moduleKey: unknown, exportName: unknown, args: unknown) => Promise<unknown>;
|
|
186
|
+
export interface InterpreterParams {
|
|
187
|
+
/** The engine entry point. One process (or worker) keeps one. */
|
|
188
|
+
runSandboxed: SandboxRunSandboxed;
|
|
189
|
+
/** The user's code, unwrapped. */
|
|
190
|
+
code: string;
|
|
191
|
+
/**
|
|
192
|
+
* The bridge record `buildSandbox` produced, including the two input
|
|
193
|
+
* bindings. Only the names this module reaches for are read.
|
|
194
|
+
*/
|
|
195
|
+
sandbox: Record<string, unknown>;
|
|
196
|
+
/**
|
|
197
|
+
* Caller-injected globals, already filtered against
|
|
198
|
+
* `RESERVED_SANDBOX_NAMES` and the identifier rule.
|
|
199
|
+
*/
|
|
200
|
+
globals: Record<string, unknown>;
|
|
201
|
+
/**
|
|
202
|
+
* Names in {@link globals} that hold objects, so their contents are read back
|
|
203
|
+
* out of the guest and returned as {@link InterpreterOutcome.syncedGlobals}.
|
|
204
|
+
*/
|
|
205
|
+
syncTargetNames: readonly string[];
|
|
206
|
+
/** Exposed only when the run declares WASM modules. */
|
|
207
|
+
wasmCall?: SandboxDispatchCall;
|
|
208
|
+
/** Exposed only when the run declares host modules. */
|
|
209
|
+
hostCall?: SandboxDispatchCall;
|
|
210
|
+
/** Exposed only when the host mounted platform capability modules. */
|
|
211
|
+
capabilityCall?: SandboxDispatchCall;
|
|
212
|
+
/** Sandbox modules this run may import. */
|
|
213
|
+
modules?: SandboxModuleResolution;
|
|
214
|
+
/** Platform modules this run mounts: guest specifier → generated facade. */
|
|
215
|
+
capabilityFacades?: ReadonlyMap<string, string>;
|
|
216
|
+
/** Guest execution budget, enforced by the interrupt handler. */
|
|
217
|
+
timeoutMs: number;
|
|
218
|
+
limits: ResolvedSandboxLimits;
|
|
219
|
+
/** Total suspension allowed before the engine's own backstop fires. */
|
|
220
|
+
suspendAllowanceMs: number;
|
|
221
|
+
/**
|
|
222
|
+
* Whether a clock is metering this run. With one, the engine's wall-clock
|
|
223
|
+
* abort moves out to cover {@link suspendAllowanceMs}; the interrupt handler
|
|
224
|
+
* keeps the exact bound on guest execution either way.
|
|
225
|
+
*/
|
|
226
|
+
hasClock: boolean;
|
|
227
|
+
/**
|
|
228
|
+
* Has the run been cancelled? Stands in for the caller's `AbortSignal`.
|
|
229
|
+
* Absent when the caller passed none, which leaves every bridge unwrapped
|
|
230
|
+
* exactly as before.
|
|
231
|
+
*/
|
|
232
|
+
isAborted?: () => boolean;
|
|
233
|
+
/** Milliseconds the run has spent suspended. Absent means none. */
|
|
234
|
+
suspendedMs?: () => number;
|
|
235
|
+
/** Internal slow-run diagnostics; never crosses the public sandbox API. */
|
|
236
|
+
onTiming?: (timing: InterpreterTiming) => void;
|
|
237
|
+
}
|
|
238
|
+
export interface InterpreterTiming {
|
|
239
|
+
readonly wrapperSetupMs: number;
|
|
240
|
+
readonly initMs: number;
|
|
241
|
+
readonly userCodeMs: number;
|
|
242
|
+
readonly syncMs: number;
|
|
243
|
+
readonly callbackRemainderMs: number;
|
|
244
|
+
readonly wrapperCleanupMs: number;
|
|
245
|
+
readonly totalMs: number;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* What one interpreter run produced: the guest's own eval response, plus the
|
|
249
|
+
* object-globals read back out of the guest. The caller writes those back into
|
|
250
|
+
* its host objects — the guest's heap is isolated, and a thread boundary makes
|
|
251
|
+
* that separation literal.
|
|
252
|
+
*/
|
|
253
|
+
export type InterpreterOutcome = {
|
|
254
|
+
ok: true;
|
|
255
|
+
data: unknown;
|
|
256
|
+
syncedGlobals?: Record<string, unknown>;
|
|
257
|
+
} | {
|
|
258
|
+
ok: false;
|
|
259
|
+
error: {
|
|
260
|
+
name: string;
|
|
261
|
+
message: string;
|
|
262
|
+
stack?: string;
|
|
263
|
+
};
|
|
264
|
+
syncedGlobals?: Record<string, unknown>;
|
|
265
|
+
};
|
|
266
|
+
/**
|
|
267
|
+
* Run one piece of user code against a fresh guest context.
|
|
268
|
+
*
|
|
269
|
+
* Resolves with the guest's verdict — including a guest-side error, which is a
|
|
270
|
+
* result and not a failure. It rejects only when the engine itself fails
|
|
271
|
+
* (an Emscripten abort, a marshaling error, a module host that could not be
|
|
272
|
+
* built), which the caller translates.
|
|
273
|
+
*/
|
|
274
|
+
export declare function runInterpreter(params: InterpreterParams): Promise<InterpreterOutcome>;
|
|
275
|
+
export {};
|
|
276
|
+
//# sourceMappingURL=interpreter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interpreter.d.ts","sourceRoot":"","sources":["../../src/js-sandbox-worker/interpreter.ts"],"names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