@iso4/sandbox 0.9.0 → 0.11.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/README.md +38 -23
- package/dist/index.d.mts +1209 -1188
- package/dist/index.mjs +66 -68
- package/dist/{names-BY-6pKkh.mjs → names-BW1pUf2B.mjs} +4 -5
- package/dist/utils.d.mts +36 -39
- package/dist/utils.mjs +1 -2
- package/package.json +13 -13
package/dist/index.d.mts
CHANGED
|
@@ -1,702 +1,693 @@
|
|
|
1
|
-
//#region src/types.d.ts
|
|
2
1
|
/**
|
|
3
|
-
* \@iso4/sandbox — public API types.
|
|
4
|
-
*
|
|
5
|
-
* Execution model: prepare a prefix once (validated and cached by the
|
|
6
|
-
* runtime), then run many dynamic postfixes against it (typically
|
|
7
|
-
* agent-generated) with `prefix.execute({ code })` or invoke one of its
|
|
8
|
-
* exports with `prefix.call({ export, args })`. Prefix runs are served by
|
|
9
|
-
* resident warm instances, which skip isolate boot and prefix evaluation on
|
|
10
|
-
* reuse; a one-off `sandbox.run({ code })` always gets a fresh isolate.
|
|
11
|
-
* Multiple concurrent callers each get their own pool slot and run in
|
|
12
|
-
* parallel. See DESIGN.md §1 and §13.
|
|
13
|
-
*/
|
|
2
|
+
* \@iso4/sandbox — public API types.
|
|
3
|
+
*
|
|
4
|
+
* Execution model: prepare a prefix once (validated and cached by the
|
|
5
|
+
* runtime), then run many dynamic postfixes against it (typically
|
|
6
|
+
* agent-generated) with `prefix.execute({ code })` or invoke one of its
|
|
7
|
+
* exports with `prefix.call({ export, args })`. Prefix runs are served by
|
|
8
|
+
* resident warm instances, which skip isolate boot and prefix evaluation on
|
|
9
|
+
* reuse; a one-off `sandbox.run({ code })` always gets a fresh isolate.
|
|
10
|
+
* Multiple concurrent callers each get their own pool slot and run in
|
|
11
|
+
* parallel. See DESIGN.md §1 and §13.
|
|
12
|
+
*/
|
|
14
13
|
/**
|
|
15
|
-
* The two
|
|
16
|
-
* is not what you want. One-off `run()` isolates are never reused and take a
|
|
17
|
-
* plain `memoryMb` number
|
|
18
|
-
*
|
|
19
|
-
* - `hard` **terminates**:
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* its
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* it
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
|
|
14
|
+
* The two memory lines a REUSED isolate is held to, when the managed default
|
|
15
|
+
* is not what you want. One-off `run()` isolates are never reused and take a
|
|
16
|
+
* plain `memoryMb` number with no soft line.
|
|
17
|
+
*
|
|
18
|
+
* - `hard` **terminates**: one cap for the JS heap and buffer memory
|
|
19
|
+
* (ArrayBuffers and typed arrays, including `resize()` growth, and stream
|
|
20
|
+
* chunks) together. Crossing it kills whatever JS is
|
|
21
|
+
* executing, taints the instance, and fails its co-resident runs with
|
|
22
|
+
* `ERR_INSTANCE_RESET`. This is the ceiling the container reserves against
|
|
23
|
+
* its admission line.
|
|
24
|
+
* - `soft` **retires**: a warm instance whose memory (JS heap plus buffers)
|
|
25
|
+
* sits above it at two consecutive finishes takes no further runs and is
|
|
26
|
+
* dropped once the runs it is already serving complete. No run fails and nothing is terminated —
|
|
27
|
+
* it is how a prefix that accumulates memory across reuse gets replaced
|
|
28
|
+
* instead of eventually killing a run. Omit it for a hard line only.
|
|
29
|
+
*
|
|
30
|
+
* A plain `memoryMb: number` is the managed form of this: the number is the
|
|
31
|
+
* soft line and the hard line sits a headroom band above it (`+sqrt(8 × n)`
|
|
32
|
+
* MB, so 128 → 160). Write the object when you want the exact numbers.
|
|
33
|
+
*/
|
|
33
34
|
interface MemoryLimit {
|
|
34
35
|
/**
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
* The retirement line, in megabytes. Omit for no soft line (a `hard`-only
|
|
37
|
+
* cap behaves exactly like the pre-#169 `memoryMb`). Must be an integer
|
|
38
|
+
* ≥ 1 and below `hard`.
|
|
39
|
+
*/
|
|
39
40
|
soft?: number;
|
|
40
41
|
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
* The terminating cap, in megabytes. Must be an integer ≥ 1 — `0` is not
|
|
43
|
+
* accepted here, because an uncapped isolate has no line to enforce and a
|
|
44
|
+
* runaway allocation would take the whole runtime process down with a
|
|
45
|
+
* fatal OOM. Write `memoryMb: 0` for uncapped.
|
|
46
|
+
*/
|
|
46
47
|
hard: number;
|
|
47
48
|
}
|
|
48
49
|
/**
|
|
49
|
-
* Limits for a one-off `sandbox.run()`, which always gets a fresh isolate —
|
|
50
|
-
* so unlike prefix runs (whose isolates are shared and reused, cap set at
|
|
51
|
-
* `prepare({ memoryMb })` or the sandbox default) it may cap its own heap.
|
|
52
|
-
*/
|
|
50
|
+
* Limits for a one-off `sandbox.run()`, which always gets a fresh isolate —
|
|
51
|
+
* so unlike prefix runs (whose isolates are shared and reused, cap set at
|
|
52
|
+
* `prepare({ memoryMb })` or the sandbox default) it may cap its own heap.
|
|
53
|
+
*/
|
|
53
54
|
interface OneOffResourceLimits extends ResourceLimits {
|
|
54
55
|
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
* Hard cap on this run's isolate memory, in megabytes — one limit for the
|
|
57
|
+
* JS heap and buffer memory together. The run is ended once it holds more,
|
|
58
|
+
* checked every ~10 ms and at every turn end, so it can overshoot briefly.
|
|
59
|
+
*
|
|
60
|
+
* A one-off always gets a fresh isolate, so — unlike a prefix's reused
|
|
61
|
+
* instances — there is nothing to retire and no headroom band. The number
|
|
62
|
+
* you write is the number that terminates. A plain number only, for the
|
|
63
|
+
* same reason: {@link MemoryLimit}'s soft line would be inert here.
|
|
64
|
+
*
|
|
65
|
+
* `0` = uncapped (admitted against the memory budget instead of the 90%
|
|
66
|
+
* line — see `memoryBudgetMb`). Must be an integer ≥ 0.
|
|
67
|
+
* @default the sandbox-level `memoryMb` (128)
|
|
68
|
+
*/
|
|
67
69
|
memoryMb?: number;
|
|
68
70
|
}
|
|
69
71
|
/**
|
|
70
|
-
* Per-run resource caps. Every field is optional at the call site
|
|
71
|
-
* (`limits?: ResourceLimits`): pass only the limits you want to
|
|
72
|
-
* override. The client sends just those over the wire and the **runtime owns
|
|
73
|
-
* the defaults** — it fills in any field you leave unset. The `@default` values
|
|
74
|
-
* documented below mirror the runtime's defaults (defined in
|
|
75
|
-
* `native/v8-runtime/src/ipc.rs`, the source of truth); if they ever drift, the
|
|
76
|
-
* runtime wins.
|
|
77
|
-
*
|
|
78
|
-
* A field set to `0` is not the same as leaving it unset: `0` explicitly
|
|
79
|
-
* disables that limit, whereas unset yields the runtime default.
|
|
80
|
-
*
|
|
81
|
-
* Every field must be an integer from `0` to `2³² − 1`. Anything else
|
|
82
|
-
* (negative, fractional, `NaN`, `Infinity`, a non-number) rejects the call
|
|
83
|
-
* with a `TypeError` before anything runs.
|
|
84
|
-
*/
|
|
72
|
+
* Per-run resource caps. Every field is optional at the call site
|
|
73
|
+
* (`limits?: ResourceLimits`): pass only the limits you want to
|
|
74
|
+
* override. The client sends just those over the wire and the **runtime owns
|
|
75
|
+
* the defaults** — it fills in any field you leave unset. The `@default` values
|
|
76
|
+
* documented below mirror the runtime's defaults (defined in
|
|
77
|
+
* `native/v8-runtime/src/ipc.rs`, the source of truth); if they ever drift, the
|
|
78
|
+
* runtime wins.
|
|
79
|
+
*
|
|
80
|
+
* A field set to `0` is not the same as leaving it unset: `0` explicitly
|
|
81
|
+
* disables that limit, whereas unset yields the runtime default.
|
|
82
|
+
*
|
|
83
|
+
* Every field must be an integer from `0` to `2³² − 1`. Anything else
|
|
84
|
+
* (negative, fractional, `NaN`, `Infinity`, a non-number) rejects the call
|
|
85
|
+
* with a `TypeError` before anything runs.
|
|
86
|
+
*/
|
|
85
87
|
interface ResourceLimits {
|
|
86
88
|
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
* Maximum CPU time in milliseconds — what the run's own thread actually
|
|
90
|
+
* burns on a core. Time waiting on host bridge calls (globals, host
|
|
91
|
+
* imports) is excluded, and so is time the thread spends descheduled while
|
|
92
|
+
* co-resident runs use the cores: a run gets its full allowance however
|
|
93
|
+
* busy the host is, and `wallTimeMs` bounds how long that may take.
|
|
94
|
+
* @default 5_000
|
|
95
|
+
*/
|
|
94
96
|
cpuTimeMs?: number;
|
|
95
97
|
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
* Cap on the run's own logic time — execution + async waits (the result's
|
|
99
|
+
* `wallTimeMs`). Engine time for co-resident runs doesn't count.
|
|
100
|
+
* @default 30_000
|
|
101
|
+
*/
|
|
100
102
|
wallTimeMs?: number;
|
|
101
103
|
/**
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
104
|
+
* Maximum bytes the sandbox may send as arguments in a single bridge call
|
|
105
|
+
* (sandbox → host direction). Protects the host from receiving unexpectedly
|
|
106
|
+
* large payloads generated by untrusted sandbox code.
|
|
107
|
+
*
|
|
108
|
+
* A violation terminates the run immediately with
|
|
109
|
+
* `ERR_BRIDGE_PAYLOAD_TOO_LARGE`; sandbox code cannot catch it.
|
|
110
|
+
*
|
|
111
|
+
* Bridge responses (host → sandbox) are not separately capped here. The
|
|
112
|
+
* binding limit is the host framing ceiling of 64 MiB, below the runtime's own
|
|
113
|
+
* inbound allowance of `memoryMb`: a handler returning more than that cannot
|
|
114
|
+
* be encoded, and the sandbox receives `ERR_HOST_BRIDGE` for that call rather
|
|
115
|
+
* than the value. Cap large handler return values yourself if you want a
|
|
116
|
+
* cleaner failure.
|
|
117
|
+
*
|
|
118
|
+
* Zero disables the per-call cap.
|
|
119
|
+
* @default 16 * 1024 * 1024
|
|
120
|
+
*/
|
|
119
121
|
maxBridgeCallBytes?: number;
|
|
120
122
|
/**
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
* Maximum bytes allowed for the serialised export payload — one V8
|
|
124
|
+
* serialization blob holding all exports combined. Exceeding this limit
|
|
125
|
+
* terminates the run with `ERR_EXPORT_TOO_LARGE`.
|
|
126
|
+
* Zero disables the cap.
|
|
127
|
+
* @default 16 * 1024 * 1024
|
|
128
|
+
*/
|
|
127
129
|
maxExportBytes?: number;
|
|
128
130
|
/**
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
* Maximum bytes captured from `console.log` / `console.debug` / `console.info`
|
|
132
|
+
* combined. Lines that would push the total over the limit are silently dropped.
|
|
133
|
+
* Zero disables the cap.
|
|
134
|
+
* @default 1 * 1024 * 1024
|
|
135
|
+
*/
|
|
134
136
|
maxStdoutBytes?: number;
|
|
135
137
|
/**
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
* Maximum bytes captured from `console.warn` / `console.error` combined.
|
|
139
|
+
* Lines that would push the total over the limit are silently dropped.
|
|
140
|
+
* Zero disables the cap.
|
|
141
|
+
* @default 1 * 1024 * 1024
|
|
142
|
+
*/
|
|
141
143
|
maxStderrBytes?: number;
|
|
142
144
|
/**
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
145
|
+
* Maximum number of bridge calls (globals + host imports combined) a
|
|
146
|
+
* single run may make. Zero disables the per-run limit entirely.
|
|
147
|
+
*
|
|
148
|
+
* Protects against untrusted sandbox code making unbounded calls to host
|
|
149
|
+
* handlers (e.g. `while(true) { await myTool() }`) which consumes near-zero
|
|
150
|
+
* CPU budget but can exhaust host resources (network, DB, LLM quota).
|
|
151
|
+
*
|
|
152
|
+
* A violation terminates the run immediately with
|
|
153
|
+
* `ERR_BRIDGE_CALL_LIMIT_EXCEEDED`; sandbox code cannot catch it (unlike
|
|
154
|
+
* host handler errors, which reject the call's promise and are catchable).
|
|
155
|
+
*
|
|
156
|
+
* @default 10
|
|
157
|
+
*/
|
|
156
158
|
maxBridgeCalls?: number;
|
|
157
159
|
/**
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
160
|
+
* Wall budget in milliseconds for `waitUntil` background work after the
|
|
161
|
+
* run's value has been delivered. One budget for the whole registered
|
|
162
|
+
* set, started when the result ships. Grace work additionally gets a fresh
|
|
163
|
+
* CPU allotment sized like `cpuTimeMs`. `0` disables the grace phase
|
|
164
|
+
* entirely: registered work dies when the value settles.
|
|
165
|
+
* @default 30_000
|
|
166
|
+
*/
|
|
165
167
|
graceMs?: number;
|
|
166
168
|
}
|
|
167
169
|
/**
|
|
168
|
-
* Host-provided globals. Any string not reserved by the runtime is permitted
|
|
169
|
-
* as a name. Each value becomes a property of the sandbox's `globalThis`.
|
|
170
|
-
*
|
|
171
|
-
* Reserved names (rejected with `ERR_RESERVED_NAME`): `console`,
|
|
172
|
-
* `waitUntil`, `setTimeout`, `clearTimeout`, `Headers`, `Request`,
|
|
173
|
-
* `Response`, `TextEncoder`, `TextDecoder`, `URL`, `URLSearchParams`, plus
|
|
174
|
-
* `NaN`, `undefined`, `Infinity` and `__proto__`. The first are owned by the
|
|
175
|
-
* runtime; shadowing `Response` would silently break serialization. `NaN`,
|
|
176
|
-
* `undefined` and `Infinity` are non-writable on the global object, so a host
|
|
177
|
-
* global could never take effect; assigning `__proto__` replaces a prototype
|
|
178
|
-
* instead of creating a property.
|
|
179
|
-
*
|
|
180
|
-
* A name that is not a JavaScript identifier (`my-tool`, `user.lookup`), or
|
|
181
|
-
* is in `RESERVED_WORDS` other than `eval` and `arguments`, is
|
|
182
|
-
* reachable only as `globalThis[name]`, not by bare name. A name that is
|
|
183
|
-
* not a well-formed string (`'y\uD800'`) throws a `TypeError` before
|
|
184
|
-
* anything is sent. Names starting with `__iso4_` are iso4's internal
|
|
185
|
-
* namespace (the dispatcher, shim handlers): a host global there may be
|
|
186
|
-
* silently overwritten or dropped, and `isValidGlobalName` does not flag it.
|
|
187
|
-
*
|
|
188
|
-
* `SANDBOX_GLOBALS` holds every name a fresh sandbox defines, V8 built-ins
|
|
189
|
-
* included. Both sets and `isValidGlobalName`, which checks a name before
|
|
190
|
-
* `prepare()`, come from `@iso4/sandbox/utils`.
|
|
191
|
-
*
|
|
192
|
-
* Common usage:
|
|
193
|
-
* ```ts
|
|
194
|
-
* globals: {
|
|
195
|
-
* fetch: myFetchHandler, // \@iso4/fetch provides createSafeFetch()
|
|
196
|
-
* myTool: async (arg) => doSomething(arg),
|
|
197
|
-
* }
|
|
198
|
-
* ```
|
|
199
|
-
*
|
|
200
|
-
* `fetch` is not special — it goes through the same bridge path as any
|
|
201
|
-
* other entry.
|
|
202
|
-
*
|
|
203
|
-
* Three value shapes are supported — see `HostGlobalValue`.
|
|
204
|
-
*/
|
|
170
|
+
* Host-provided globals. Any string not reserved by the runtime is permitted
|
|
171
|
+
* as a name. Each value becomes a property of the sandbox's `globalThis`.
|
|
172
|
+
*
|
|
173
|
+
* Reserved names (rejected with `ERR_RESERVED_NAME`): `console`,
|
|
174
|
+
* `waitUntil`, `setTimeout`, `clearTimeout`, `Headers`, `Request`,
|
|
175
|
+
* `Response`, `TextEncoder`, `TextDecoder`, `URL`, `URLSearchParams`, plus
|
|
176
|
+
* `NaN`, `undefined`, `Infinity` and `__proto__`. The first are owned by the
|
|
177
|
+
* runtime; shadowing `Response` would silently break serialization. `NaN`,
|
|
178
|
+
* `undefined` and `Infinity` are non-writable on the global object, so a host
|
|
179
|
+
* global could never take effect; assigning `__proto__` replaces a prototype
|
|
180
|
+
* instead of creating a property.
|
|
181
|
+
*
|
|
182
|
+
* A name that is not a JavaScript identifier (`my-tool`, `user.lookup`), or
|
|
183
|
+
* is in `RESERVED_WORDS` other than `eval` and `arguments`, is
|
|
184
|
+
* reachable only as `globalThis[name]`, not by bare name. A name that is
|
|
185
|
+
* not a well-formed string (`'y\uD800'`) throws a `TypeError` before
|
|
186
|
+
* anything is sent. Names starting with `__iso4_` are iso4's internal
|
|
187
|
+
* namespace (the dispatcher, shim handlers): a host global there may be
|
|
188
|
+
* silently overwritten or dropped, and `isValidGlobalName` does not flag it.
|
|
189
|
+
*
|
|
190
|
+
* `SANDBOX_GLOBALS` holds every name a fresh sandbox defines, V8 built-ins
|
|
191
|
+
* included. Both sets and `isValidGlobalName`, which checks a name before
|
|
192
|
+
* `prepare()`, come from `@iso4/sandbox/utils`.
|
|
193
|
+
*
|
|
194
|
+
* Common usage:
|
|
195
|
+
* ```ts
|
|
196
|
+
* globals: {
|
|
197
|
+
* fetch: myFetchHandler, // \@iso4/fetch provides createSafeFetch()
|
|
198
|
+
* myTool: async (arg) => doSomething(arg),
|
|
199
|
+
* }
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* `fetch` is not special — it goes through the same bridge path as any
|
|
203
|
+
* other entry.
|
|
204
|
+
*
|
|
205
|
+
* Three value shapes are supported — see `HostGlobalValue`.
|
|
206
|
+
*/
|
|
205
207
|
type HostGlobals = Record<string, HostGlobalValue>;
|
|
206
208
|
/**
|
|
207
|
-
* The value types accepted for a named global.
|
|
208
|
-
*
|
|
209
|
-
* - `HostExportFunction` — bridge call: every sandbox invocation dispatches
|
|
210
|
-
* to the host handler. Shorthand for `BridgeGlobal` with the defaults.
|
|
211
|
-
* - `string` — a JS expression the runtime evaluates once and installs as
|
|
212
|
-
* `globalThis.<name>`. Use for pure sandbox utilities that need no host
|
|
213
|
-
* involvement. The expression is evaluated by Rust as its own script, so it
|
|
214
|
-
* never shifts the line numbers of user code. Shorthand for `StringGlobal`
|
|
215
|
-
* with the defaults.
|
|
216
|
-
* - `BridgeGlobal` / `StringGlobal` — the same two as object forms, for when
|
|
217
|
-
* an option (`enumerable`) is needed.
|
|
218
|
-
* - `DataGlobal` — a plain constant carried as data and installed natively;
|
|
219
|
-
* no code text, no host involvement.
|
|
220
|
-
* - `BridgeWithShim` — bridge call + in-sandbox result wrapper. The handler
|
|
221
|
-
* can be rebound per `prefix.run()`; the shim is part of the prefix state.
|
|
222
|
-
*
|
|
223
|
-
* Every **object** form accepts `enumerable?: boolean` (default `true`).
|
|
224
|
-
* The bare-function and bare-string shorthands are always enumerable.
|
|
225
|
-
*/
|
|
209
|
+
* The value types accepted for a named global.
|
|
210
|
+
*
|
|
211
|
+
* - `HostExportFunction` — bridge call: every sandbox invocation dispatches
|
|
212
|
+
* to the host handler. Shorthand for `BridgeGlobal` with the defaults.
|
|
213
|
+
* - `string` — a JS expression the runtime evaluates once and installs as
|
|
214
|
+
* `globalThis.<name>`. Use for pure sandbox utilities that need no host
|
|
215
|
+
* involvement. The expression is evaluated by Rust as its own script, so it
|
|
216
|
+
* never shifts the line numbers of user code. Shorthand for `StringGlobal`
|
|
217
|
+
* with the defaults.
|
|
218
|
+
* - `BridgeGlobal` / `StringGlobal` — the same two as object forms, for when
|
|
219
|
+
* an option (`enumerable`) is needed.
|
|
220
|
+
* - `DataGlobal` — a plain constant carried as data and installed natively;
|
|
221
|
+
* no code text, no host involvement.
|
|
222
|
+
* - `BridgeWithShim` — bridge call + in-sandbox result wrapper. The handler
|
|
223
|
+
* can be rebound per `prefix.run()`; the shim is part of the prefix state.
|
|
224
|
+
*
|
|
225
|
+
* Every **object** form accepts `enumerable?: boolean` (default `true`).
|
|
226
|
+
* The bare-function and bare-string shorthands are always enumerable.
|
|
227
|
+
*/
|
|
226
228
|
type HostGlobalValue = HostExportFunction | string | BridgeWithShim<any> | DataGlobal | BridgeGlobal<any> | StringGlobal;
|
|
227
229
|
/**
|
|
228
|
-
* Shared option of every object-form global.
|
|
229
|
-
*
|
|
230
|
-
* `enumerable: false` keeps the name out of `for...in`, `Object.keys`, and
|
|
231
|
-
* spreads of `globalThis`, so enumeration-driven sandbox code (serializers,
|
|
232
|
-
* "copy the environment" patterns) never sweeps it up. This is hygiene, not
|
|
233
|
-
* secrecy: the property still appears in `Object.getOwnPropertyNames` (a
|
|
234
|
-
* language-level fact, identical in every engine), and code that knows the
|
|
235
|
-
* name can still call it. A capability that must be undiscoverable belongs in
|
|
236
|
-
* `imports`, which nothing can enumerate.
|
|
237
|
-
*/
|
|
230
|
+
* Shared option of every object-form global.
|
|
231
|
+
*
|
|
232
|
+
* `enumerable: false` keeps the name out of `for...in`, `Object.keys`, and
|
|
233
|
+
* spreads of `globalThis`, so enumeration-driven sandbox code (serializers,
|
|
234
|
+
* "copy the environment" patterns) never sweeps it up. This is hygiene, not
|
|
235
|
+
* secrecy: the property still appears in `Object.getOwnPropertyNames` (a
|
|
236
|
+
* language-level fact, identical in every engine), and code that knows the
|
|
237
|
+
* name can still call it. A capability that must be undiscoverable belongs in
|
|
238
|
+
* `imports`, which nothing can enumerate.
|
|
239
|
+
*/
|
|
238
240
|
interface GlobalOptions {
|
|
239
241
|
/**
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
242
|
+
* Whether the installed global shows up in enumeration. Defaults to `true`,
|
|
243
|
+
* matching how browsers keep `fetch` enumerable on `window`.
|
|
244
|
+
*/
|
|
243
245
|
enumerable?: boolean;
|
|
244
246
|
}
|
|
245
247
|
/**
|
|
246
|
-
* Object form of the bare-function bridge global, for when options are
|
|
247
|
-
* needed. `handler` is exactly the function the shorthand would be.
|
|
248
|
-
*
|
|
249
|
-
* ```ts
|
|
250
|
-
* globals: { myTool: { kind: 'bridge', handler: async (q) => search(q), enumerable: false } }
|
|
251
|
-
* ```
|
|
252
|
-
*/
|
|
248
|
+
* Object form of the bare-function bridge global, for when options are
|
|
249
|
+
* needed. `handler` is exactly the function the shorthand would be.
|
|
250
|
+
*
|
|
251
|
+
* ```ts
|
|
252
|
+
* globals: { myTool: { kind: 'bridge', handler: async (q) => search(q), enumerable: false } }
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
253
255
|
interface BridgeGlobal<H extends HostExportFunction = HostExportFunction> extends GlobalOptions {
|
|
254
256
|
kind: "bridge";
|
|
255
257
|
/**
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
258
|
+
* Bridge handler — every sandbox invocation dispatches to it. Can be
|
|
259
|
+
* rebound per `prefix.run()` like a shorthand bridge global.
|
|
260
|
+
*/
|
|
259
261
|
handler: H;
|
|
260
262
|
}
|
|
261
263
|
/**
|
|
262
|
-
* Object form of the bare-string expression global, for when options are
|
|
263
|
-
* needed. `expr` is exactly the expression the shorthand would be.
|
|
264
|
-
*/
|
|
264
|
+
* Object form of the bare-string expression global, for when options are
|
|
265
|
+
* needed. `expr` is exactly the expression the shorthand would be.
|
|
266
|
+
*/
|
|
265
267
|
interface StringGlobal extends GlobalOptions {
|
|
266
268
|
kind: "string";
|
|
267
269
|
/**
|
|
268
|
-
|
|
269
|
-
|
|
270
|
+
* JS expression the runtime evaluates once and installs under the name.
|
|
271
|
+
*/
|
|
270
272
|
expr: string;
|
|
271
273
|
}
|
|
272
274
|
/**
|
|
273
|
-
* A plain **constant** global — a data value installed directly on the sandbox
|
|
274
|
-
* global object, with no host involvement and no code text anywhere.
|
|
275
|
-
*
|
|
276
|
-
* The value crosses the wire as a V8 serialization blob and Rust materialises
|
|
277
|
-
* it natively with `blob::deserialize_value` (the same path host-module data
|
|
278
|
-
* leaves take):
|
|
279
|
-
*
|
|
280
|
-
* ```ts
|
|
281
|
-
* globals: { config: { kind: 'data', value: userConfig } }
|
|
282
|
-
* ```
|
|
283
|
-
*
|
|
284
|
-
* `value` accepts the same set as host-module data leaves (`HostExportData`):
|
|
285
|
-
* primitives, `bigint`, `string`, `Uint8Array`, and plain objects/arrays.
|
|
286
|
-
* Like string globals, a data global is a constant — it cannot be rebound per
|
|
287
|
-
* `prefix.run()`.
|
|
288
|
-
*/
|
|
275
|
+
* A plain **constant** global — a data value installed directly on the sandbox
|
|
276
|
+
* global object, with no host involvement and no code text anywhere.
|
|
277
|
+
*
|
|
278
|
+
* The value crosses the wire as a V8 serialization blob and Rust materialises
|
|
279
|
+
* it natively with `blob::deserialize_value` (the same path host-module data
|
|
280
|
+
* leaves take):
|
|
281
|
+
*
|
|
282
|
+
* ```ts
|
|
283
|
+
* globals: { config: { kind: 'data', value: userConfig } }
|
|
284
|
+
* ```
|
|
285
|
+
*
|
|
286
|
+
* `value` accepts the same set as host-module data leaves (`HostExportData`):
|
|
287
|
+
* primitives, `bigint`, `string`, `Uint8Array`, and plain objects/arrays.
|
|
288
|
+
* Like string globals, a data global is a constant — it cannot be rebound per
|
|
289
|
+
* `prefix.run()`.
|
|
290
|
+
*/
|
|
289
291
|
interface DataGlobal extends GlobalOptions {
|
|
290
292
|
kind: "data";
|
|
291
293
|
/**
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
294
|
+
* Any `HostExportData`, or a `Request`/`Response`/`Headers` — including nested
|
|
295
|
+
* inside a plain object or array, which the runtime rebuilds as real instances
|
|
296
|
+
* (`docs/protocol.md` §4.4).
|
|
297
|
+
*/
|
|
296
298
|
value: HostExportData | Request | Response | Headers;
|
|
297
299
|
}
|
|
298
300
|
/**
|
|
299
|
-
* A global that pairs a bridge handler with an in-sandbox shim that wraps
|
|
300
|
-
* its return value. Typical use: adding `.json()` / `.text()` convenience
|
|
301
|
-
* methods to a fetch response without extra bridge round-trips.
|
|
302
|
-
*
|
|
303
|
-
* The runtime installs this natively:
|
|
304
|
-
* 1. `handler` is registered as a bridge stub under the private dispatch
|
|
305
|
-
* name `__iso4_<name>_h`.
|
|
306
|
-
* 2. Rust evaluates the shim expression and builds the wrapper
|
|
307
|
-
* `async (...args) => await shim(await globalThis["__iso4_<name>_h"](...args))`,
|
|
308
|
-
* then sets it on the global object under `<name>` via the V8 API — the
|
|
309
|
-
* public name is a plain string passed to `object.set`, never code.
|
|
310
|
-
*
|
|
311
|
-
* The shim is a JS function expression `(result) => transformedResult` (sync
|
|
312
|
-
* or async). Sandbox built-ins (TextDecoder, JSON, …) are available in scope.
|
|
313
|
-
* The private stub `__iso4_<name>_h` stays on globalThis so the handler can
|
|
314
|
-
* be rebound per `prefix.run()` while the shim itself stays fixed.
|
|
315
|
-
*/
|
|
301
|
+
* A global that pairs a bridge handler with an in-sandbox shim that wraps
|
|
302
|
+
* its return value. Typical use: adding `.json()` / `.text()` convenience
|
|
303
|
+
* methods to a fetch response without extra bridge round-trips.
|
|
304
|
+
*
|
|
305
|
+
* The runtime installs this natively:
|
|
306
|
+
* 1. `handler` is registered as a bridge stub under the private dispatch
|
|
307
|
+
* name `__iso4_<name>_h`.
|
|
308
|
+
* 2. Rust evaluates the shim expression and builds the wrapper
|
|
309
|
+
* `async (...args) => await shim(await globalThis["__iso4_<name>_h"](...args))`,
|
|
310
|
+
* then sets it on the global object under `<name>` via the V8 API — the
|
|
311
|
+
* public name is a plain string passed to `object.set`, never code.
|
|
312
|
+
*
|
|
313
|
+
* The shim is a JS function expression `(result) => transformedResult` (sync
|
|
314
|
+
* or async). Sandbox built-ins (TextDecoder, JSON, …) are available in scope.
|
|
315
|
+
* The private stub `__iso4_<name>_h` stays on globalThis so the handler can
|
|
316
|
+
* be rebound per `prefix.run()` while the shim itself stays fixed.
|
|
317
|
+
*/
|
|
316
318
|
/**
|
|
317
|
-
* `H` is the specific handler function type. Carried through the type system
|
|
318
|
-
* so `RebindGlobals` can enforce that any per-run replacement has the same
|
|
319
|
-
* return type, keeping it compatible with the shim that is already compiled
|
|
320
|
-
* into the prefix state.
|
|
321
|
-
*/
|
|
319
|
+
* `H` is the specific handler function type. Carried through the type system
|
|
320
|
+
* so `RebindGlobals` can enforce that any per-run replacement has the same
|
|
321
|
+
* return type, keeping it compatible with the shim that is already compiled
|
|
322
|
+
* into the prefix state.
|
|
323
|
+
*/
|
|
322
324
|
interface BridgeWithShim<H extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown> extends GlobalOptions {
|
|
323
325
|
kind: "bridge-with-shim";
|
|
324
326
|
/**
|
|
325
|
-
|
|
326
|
-
|
|
327
|
+
* Bridge handler — registered as `__iso4_<name>_h` for rebinding.
|
|
328
|
+
*/
|
|
327
329
|
handler: H;
|
|
328
330
|
/**
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
331
|
+
* JS function expression that transforms the bridge return value.
|
|
332
|
+
* Runs in the sandbox. May be async.
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* shim: `(result) => ({
|
|
337
|
+
* ...result,
|
|
338
|
+
* ok: result.status >= 200 && result.status < 300,
|
|
339
|
+
* json: () => JSON.parse(new TextDecoder().decode(result.body)),
|
|
340
|
+
* text: () => new TextDecoder().decode(result.body),
|
|
341
|
+
* })`
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
342
344
|
shim: string;
|
|
343
345
|
}
|
|
344
346
|
/**
|
|
345
|
-
* What can be supplied as a per-run override for a global declared at
|
|
346
|
-
* precompile time:
|
|
347
|
-
*
|
|
348
|
-
* - `BridgeWithShim<H>` global — only the handler `H` itself. The shim is
|
|
349
|
-
* fixed with the prefix; rebinding must preserve the return type so the
|
|
350
|
-
* shim keeps working.
|
|
351
|
-
* - `HostExportFunction` global — any compatible `HostExportFunction`.
|
|
352
|
-
* - `string` global — cannot be rebound (fixed at prepare() time).
|
|
353
|
-
*/
|
|
347
|
+
* What can be supplied as a per-run override for a global declared at
|
|
348
|
+
* precompile time:
|
|
349
|
+
*
|
|
350
|
+
* - `BridgeWithShim<H>` global — only the handler `H` itself. The shim is
|
|
351
|
+
* fixed with the prefix; rebinding must preserve the return type so the
|
|
352
|
+
* shim keeps working.
|
|
353
|
+
* - `HostExportFunction` global — any compatible `HostExportFunction`.
|
|
354
|
+
* - `string` global — cannot be rebound (fixed at prepare() time).
|
|
355
|
+
*/
|
|
354
356
|
type RebindValue<V extends HostGlobalValue> = V extends BridgeWithShim<infer H> ? H : V extends BridgeGlobal<infer H> ? H : V extends HostExportFunction ? V : never;
|
|
355
357
|
/**
|
|
356
|
-
* The per-run globals override map for a `Prefix<G>`.
|
|
357
|
-
*
|
|
358
|
-
* String-valued and `DataGlobal` (constant) globals are filtered out entirely
|
|
359
|
-
* (key remapping to `never`) rather than mapped to `?: never` — they cannot be
|
|
360
|
-
* rebound because their value is fixed at prepare() time, and they should
|
|
361
|
-
* not appear as valid keys at all.
|
|
362
|
-
*
|
|
363
|
-
* Each remaining key is optional and typed as `RebindValue<G[K]>`, which
|
|
364
|
-
* enforces the correct constraint per global kind.
|
|
365
|
-
*/
|
|
366
|
-
type RebindGlobals<G extends HostGlobals> = { [K in keyof G as G[K] extends string | DataGlobal | StringGlobal ? never : K]?: RebindValue<G[K]
|
|
358
|
+
* The per-run globals override map for a `Prefix<G>`.
|
|
359
|
+
*
|
|
360
|
+
* String-valued and `DataGlobal` (constant) globals are filtered out entirely
|
|
361
|
+
* (key remapping to `never`) rather than mapped to `?: never` — they cannot be
|
|
362
|
+
* rebound because their value is fixed at prepare() time, and they should
|
|
363
|
+
* not appear as valid keys at all.
|
|
364
|
+
*
|
|
365
|
+
* Each remaining key is optional and typed as `RebindValue<G[K]>`, which
|
|
366
|
+
* enforces the correct constraint per global kind.
|
|
367
|
+
*/
|
|
368
|
+
type RebindGlobals<G extends HostGlobals> = { [K in keyof G as G[K] extends string | DataGlobal | StringGlobal ? never : K]?: RebindValue<G[K]>; };
|
|
367
369
|
/**
|
|
368
|
-
* Top-level `imports` shape. Keys are import specifiers; values are either
|
|
369
|
-
* a source string or a host-module description object. A specifier that is
|
|
370
|
-
* not a well-formed string (`'x\uD800'`) throws a `TypeError` before
|
|
371
|
-
* anything is sent.
|
|
372
|
-
*
|
|
373
|
-
* `M` is the literal shape of the map. `precompile()` infers it from the
|
|
374
|
-
* object literal so the declared specifiers and their value shapes flow
|
|
375
|
-
* into `Prefix<G, M>` and feed `RebindImports<M>`.
|
|
376
|
-
*/
|
|
370
|
+
* Top-level `imports` shape. Keys are import specifiers; values are either
|
|
371
|
+
* a source string or a host-module description object. A specifier that is
|
|
372
|
+
* not a well-formed string (`'x\uD800'`) throws a `TypeError` before
|
|
373
|
+
* anything is sent.
|
|
374
|
+
*
|
|
375
|
+
* `M` is the literal shape of the map. `precompile()` infers it from the
|
|
376
|
+
* object literal so the declared specifiers and their value shapes flow
|
|
377
|
+
* into `Prefix<G, M>` and feed `RebindImports<M>`.
|
|
378
|
+
*/
|
|
377
379
|
type Imports<M extends Record<string, ImportValue> = Record<string, ImportValue>> = M;
|
|
378
380
|
/**
|
|
379
|
-
* A single import value. String = source module. Object = host module
|
|
380
|
-
* description (nested up to 64 levels, see {@link HostModuleObject}).
|
|
381
|
-
*/
|
|
381
|
+
* A single import value. String = source module. Object = host module
|
|
382
|
+
* description (nested up to 64 levels, see {@link HostModuleObject}).
|
|
383
|
+
*/
|
|
382
384
|
type ImportValue = string | HostModuleObject;
|
|
383
385
|
/**
|
|
384
|
-
* A host-module description object. Each property is either:
|
|
385
|
-
*
|
|
386
|
-
* - A function — the runtime builds an async bridge trampoline for it.
|
|
387
|
-
* - A data value (anything in {@link HostExportData}) — materialised in the
|
|
388
|
-
* isolate from its V8 serialization blob.
|
|
389
|
-
* - A nested object that itself contains a mix of functions and data —
|
|
390
|
-
* walked recursively.
|
|
391
|
-
*
|
|
392
|
-
* Class instances with prototype methods and stateful handles (streams,
|
|
393
|
-
* sockets) are rejected at registration with a clear error naming the exact
|
|
394
|
-
* path: V8's format would carry them, but only as their own enumerable
|
|
395
|
-
* properties, so the methods you meant to send would vanish silently. Copy
|
|
396
|
-
* what you mean to send into a plain object.
|
|
397
|
-
*
|
|
398
|
-
* Shapes may nest at most 64 levels (a node's path length from its top-level
|
|
399
|
-
* export). A deeper shape is rejected at registration, naming the path. Plain
|
|
400
|
-
* objects always describe shape, so this cap also applies to deep plain-object
|
|
401
|
-
* data; data in any other container is bounded only by V8's deserializer.
|
|
402
|
-
*/
|
|
386
|
+
* A host-module description object. Each property is either:
|
|
387
|
+
*
|
|
388
|
+
* - A function — the runtime builds an async bridge trampoline for it.
|
|
389
|
+
* - A data value (anything in {@link HostExportData}) — materialised in the
|
|
390
|
+
* isolate from its V8 serialization blob.
|
|
391
|
+
* - A nested object that itself contains a mix of functions and data —
|
|
392
|
+
* walked recursively.
|
|
393
|
+
*
|
|
394
|
+
* Class instances with prototype methods and stateful handles (streams,
|
|
395
|
+
* sockets) are rejected at registration with a clear error naming the exact
|
|
396
|
+
* path: V8's format would carry them, but only as their own enumerable
|
|
397
|
+
* properties, so the methods you meant to send would vanish silently. Copy
|
|
398
|
+
* what you mean to send into a plain object.
|
|
399
|
+
*
|
|
400
|
+
* Shapes may nest at most 64 levels (a node's path length from its top-level
|
|
401
|
+
* export). A deeper shape is rejected at registration, naming the path. Plain
|
|
402
|
+
* objects always describe shape, so this cap also applies to deep plain-object
|
|
403
|
+
* data; data in any other container is bounded only by V8's deserializer.
|
|
404
|
+
*/
|
|
403
405
|
interface HostModuleObject {
|
|
404
406
|
[name: string]: HostModuleValue;
|
|
405
407
|
}
|
|
406
408
|
/**
|
|
407
|
-
* Any node the recursive walker may encounter inside a `HostModuleObject`.
|
|
408
|
-
*/
|
|
409
|
+
* Any node the recursive walker may encounter inside a `HostModuleObject`.
|
|
410
|
+
*/
|
|
409
411
|
type HostModuleValue = HostExportFunction | HostExportData | HostModuleObject;
|
|
410
412
|
/**
|
|
411
|
-
* The set of data values that can cross the sandbox boundary.
|
|
412
|
-
*
|
|
413
|
-
* This mirrors what V8's serialization format can represent, so every value
|
|
414
|
-
* here arrives on the other side as a **real instance** — a `Date` is a `Date`,
|
|
415
|
-
* a `Map` is a `Map`, a `Float64Array` keeps its element type. Cyclic and
|
|
416
|
-
* shared references are preserved, including object identity.
|
|
417
|
-
*
|
|
418
|
-
* What is *not* here cannot cross and is rejected loudly: functions (except as
|
|
419
|
-
* declared function leaves), `Promise`, `Symbol`, `WeakMap`, `WeakSet`, and
|
|
420
|
-
* `Proxy`. Class instances are also excluded — V8 would carry one, but only as
|
|
421
|
-
* its own enumerable properties, silently dropping its prototype and methods.
|
|
422
|
-
*
|
|
423
|
-
* Two notes for hosts:
|
|
424
|
-
*
|
|
425
|
-
* - Node `Buffer` is a `Uint8Array` subclass, so it crosses fine, but it
|
|
426
|
-
* arrives as a plain `Uint8Array` (data preserved, subclass identity not).
|
|
427
|
-
* - A `subarray` window carries only the bytes in its window.
|
|
428
|
-
*
|
|
429
|
-
* @see `docs/protocol.md` §4.2 for the full table, both directions.
|
|
430
|
-
*/
|
|
413
|
+
* The set of data values that can cross the sandbox boundary.
|
|
414
|
+
*
|
|
415
|
+
* This mirrors what V8's serialization format can represent, so every value
|
|
416
|
+
* here arrives on the other side as a **real instance** — a `Date` is a `Date`,
|
|
417
|
+
* a `Map` is a `Map`, a `Float64Array` keeps its element type. Cyclic and
|
|
418
|
+
* shared references are preserved, including object identity.
|
|
419
|
+
*
|
|
420
|
+
* What is *not* here cannot cross and is rejected loudly: functions (except as
|
|
421
|
+
* declared function leaves), `Promise`, `Symbol`, `WeakMap`, `WeakSet`, and
|
|
422
|
+
* `Proxy`. Class instances are also excluded — V8 would carry one, but only as
|
|
423
|
+
* its own enumerable properties, silently dropping its prototype and methods.
|
|
424
|
+
*
|
|
425
|
+
* Two notes for hosts:
|
|
426
|
+
*
|
|
427
|
+
* - Node `Buffer` is a `Uint8Array` subclass, so it crosses fine, but it
|
|
428
|
+
* arrives as a plain `Uint8Array` (data preserved, subclass identity not).
|
|
429
|
+
* - A `subarray` window carries only the bytes in its window.
|
|
430
|
+
*
|
|
431
|
+
* @see `docs/protocol.md` §4.2 for the full table, both directions.
|
|
432
|
+
*/
|
|
431
433
|
type HostExportData = null | undefined | boolean | number | bigint | string | Date | RegExp | Error | Map<HostExportData, HostExportData> | Set<HostExportData> | ArrayBuffer | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array | HostExportData[] | {
|
|
432
434
|
[key: string]: HostExportData;
|
|
433
435
|
};
|
|
434
436
|
/**
|
|
435
|
-
* A host-side function exposed as a global or import-tree leaf.
|
|
436
|
-
*
|
|
437
|
-
* Args and return are `unknown` — the bridge serialises/deserialises values
|
|
438
|
-
* with V8 ValueSerializer; we don't try to model that at the type level.
|
|
439
|
-
* The one invariant that matters (functions cannot cross the boundary as
|
|
440
|
-
* argument values) is enforced at runtime by the V8 layer, not here. Arrays
|
|
441
|
-
* with holes are refused as arguments too, host-side, and the sandbox call
|
|
442
|
-
* rejects with a catchable `TypeError` before the handler runs.
|
|
443
|
-
*
|
|
444
|
-
* Use specific function types for your handlers and let TypeScript infer
|
|
445
|
-
* them through `HostGlobals`, `RebindGlobals<G>`, and `RebindImports<M>`.
|
|
446
|
-
*/
|
|
437
|
+
* A host-side function exposed as a global or import-tree leaf.
|
|
438
|
+
*
|
|
439
|
+
* Args and return are `unknown` — the bridge serialises/deserialises values
|
|
440
|
+
* with V8 ValueSerializer; we don't try to model that at the type level.
|
|
441
|
+
* The one invariant that matters (functions cannot cross the boundary as
|
|
442
|
+
* argument values) is enforced at runtime by the V8 layer, not here. Arrays
|
|
443
|
+
* with holes are refused as arguments too, host-side, and the sandbox call
|
|
444
|
+
* rejects with a catchable `TypeError` before the handler runs.
|
|
445
|
+
*
|
|
446
|
+
* Use specific function types for your handlers and let TypeScript infer
|
|
447
|
+
* them through `HostGlobals`, `RebindGlobals<G>`, and `RebindImports<M>`.
|
|
448
|
+
*/
|
|
447
449
|
type HostExportFunction = (...args: unknown[]) => unknown;
|
|
448
450
|
/**
|
|
449
|
-
* The rebindable subset of an `Imports<M>` declared at precompile time.
|
|
450
|
-
*
|
|
451
|
-
* Mirrors `RebindGlobals<G>` over the recursive host-module shape:
|
|
452
|
-
*
|
|
453
|
-
* - **Source imports** (string values) are fixed at prepare() time and
|
|
454
|
-
* cannot be rebound — their keys are excluded from the result.
|
|
455
|
-
* - **Host imports** (object values) are walked recursively. Only function
|
|
456
|
-
* leaves survive; data leaves are excluded because their values are
|
|
457
|
-
* emitted as JS literals into the generated module.
|
|
458
|
-
*
|
|
459
|
-
* Passing a specifier or export name that was not declared at precompile
|
|
460
|
-
* is a TypeScript error at compile time and `ERR_UNDECLARED_BINDING` at
|
|
461
|
-
* runtime.
|
|
462
|
-
*
|
|
463
|
-
* When `M` is left at its wide default (no `imports` declared, or no
|
|
464
|
-
* literal shape inferred), the rebind keyspace collapses to
|
|
465
|
-
* `Record<string, never>` — the walker only kicks in for literal shapes,
|
|
466
|
-
* which have finite depth.
|
|
467
|
-
*/
|
|
468
|
-
type RebindImports<M extends Record<string, ImportValue>> = Record<string, ImportValue> extends M ? Record<string, never> : { [K in keyof M as M[K] extends string ? never : K]?: M[K] extends HostModuleObject ? RebindHostModule<M[K]> : never };
|
|
451
|
+
* The rebindable subset of an `Imports<M>` declared at precompile time.
|
|
452
|
+
*
|
|
453
|
+
* Mirrors `RebindGlobals<G>` over the recursive host-module shape:
|
|
454
|
+
*
|
|
455
|
+
* - **Source imports** (string values) are fixed at prepare() time and
|
|
456
|
+
* cannot be rebound — their keys are excluded from the result.
|
|
457
|
+
* - **Host imports** (object values) are walked recursively. Only function
|
|
458
|
+
* leaves survive; data leaves are excluded because their values are
|
|
459
|
+
* emitted as JS literals into the generated module.
|
|
460
|
+
*
|
|
461
|
+
* Passing a specifier or export name that was not declared at precompile
|
|
462
|
+
* is a TypeScript error at compile time and `ERR_UNDECLARED_BINDING` at
|
|
463
|
+
* runtime.
|
|
464
|
+
*
|
|
465
|
+
* When `M` is left at its wide default (no `imports` declared, or no
|
|
466
|
+
* literal shape inferred), the rebind keyspace collapses to
|
|
467
|
+
* `Record<string, never>` — the walker only kicks in for literal shapes,
|
|
468
|
+
* which have finite depth.
|
|
469
|
+
*/
|
|
470
|
+
type RebindImports<M extends Record<string, ImportValue>> = Record<string, ImportValue> extends M ? Record<string, never> : { [K in keyof M as M[K] extends string ? never : K]?: M[K] extends HostModuleObject ? RebindHostModule<M[K]> : never; };
|
|
469
471
|
/**
|
|
470
|
-
* Recursive walker over a `HostModuleObject`. Function leaves keep
|
|
471
|
-
* their declared signature; nested objects recurse; data leaves drop out
|
|
472
|
-
* entirely. Only used against literal shapes inferred by `precompile`, so
|
|
473
|
-
* the recursion is always finite.
|
|
474
|
-
*/
|
|
475
|
-
type RebindHostModule<T> = T extends HostExportFunction ? T : T extends HostModuleObject ? { [K in keyof T as T[K] extends HostExportFunction ? K : T[K] extends HostModuleObject ? K : never]?: T[K] extends HostExportFunction ? T[K] : T[K] extends HostModuleObject ? RebindHostModule<T[K]> : never } : never;
|
|
472
|
+
* Recursive walker over a `HostModuleObject`. Function leaves keep
|
|
473
|
+
* their declared signature; nested objects recurse; data leaves drop out
|
|
474
|
+
* entirely. Only used against literal shapes inferred by `precompile`, so
|
|
475
|
+
* the recursion is always finite.
|
|
476
|
+
*/
|
|
477
|
+
type RebindHostModule<T> = T extends HostExportFunction ? T : T extends HostModuleObject ? { [K in keyof T as T[K] extends HostExportFunction ? K : T[K] extends HostModuleObject ? K : never]?: T[K] extends HostExportFunction ? T[K] : T[K] extends HostModuleObject ? RebindHostModule<T[K]> : never; } : never;
|
|
476
478
|
interface SandboxOptions {
|
|
477
479
|
/**
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
480
|
+
* Pin how many runs execute at once, overriding the number the runtime
|
|
481
|
+
* derives for itself. Callers past it queue FIFO, honouring their
|
|
482
|
+
* `AbortSignal` while they wait (`AbortSignal.timeout()` composes as a
|
|
483
|
+
* queue-wait bound — there is no separate timeout option).
|
|
484
|
+
*
|
|
485
|
+
* **Leave this unset unless you have measured this workload on this
|
|
486
|
+
* hardware.** By default the runtime sizes concurrency from the shape of
|
|
487
|
+
* the runs it is serving and follows that shape as it changes; a fixed
|
|
488
|
+
* number cannot. Set it when you need a hard ceiling — to leave cores to
|
|
489
|
+
* the host app, or because you know the workload better than a
|
|
490
|
+
* measurement of it would.
|
|
491
|
+
*
|
|
492
|
+
* An admission number, not a socket count and not an isolate count: the
|
|
493
|
+
* runs spread over a variable number of shared isolates, and co-resident
|
|
494
|
+
* runs share that isolate's `memoryMb` heap cap between them.
|
|
495
|
+
*
|
|
496
|
+
* Must be an integer ≥ 1 — anything else throws at `createSandbox()`
|
|
497
|
+
* (`0` would queue every run forever).
|
|
498
|
+
*/
|
|
497
499
|
maxConcurrentRuns?: number;
|
|
498
500
|
/**
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
501
|
+
* How many callers may wait for a run slot before new ones are shed with
|
|
502
|
+
* `ERR_QUEUE_FULL`. Every queued caller holds its serialized arguments,
|
|
503
|
+
* so an unbounded queue turns sustained overload into a host
|
|
504
|
+
* out-of-memory crash.
|
|
505
|
+
*
|
|
506
|
+
* A fixed bound, deliberately not a multiple of the concurrency: that
|
|
507
|
+
* number now moves with the workload, and a shed threshold moving with it
|
|
508
|
+
* would tighten exactly when load rises.
|
|
509
|
+
*
|
|
510
|
+
* `0` is valid: no queue at all. Must be an integer ≥ 0. Bounds queue
|
|
511
|
+
* depth* only — a queued caller's wait *time* is bounded by its own
|
|
512
|
+
* `AbortSignal`.
|
|
513
|
+
* @default 10_000
|
|
514
|
+
*/
|
|
513
515
|
maxQueuedRuns?: number;
|
|
514
516
|
/**
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
517
|
+
* Default hard cap on memory each isolate can use, in megabytes. One
|
|
518
|
+
* limit for the JS heap and all buffer memory together; also determines
|
|
519
|
+
* the BridgeResponse frame read cap (the sandbox cannot receive a
|
|
520
|
+
* response larger than its own memory budget).
|
|
521
|
+
*
|
|
522
|
+
* This is the DEFAULT: `prepare({ memoryMb })` overrides it per prefix,
|
|
523
|
+
* and `run({ limits: { memoryMb } })` per one-off run (fresh isolate).
|
|
524
|
+
* What stays impossible is a per-run value on a warm instance — the cap
|
|
525
|
+
* is baked into the isolate at creation and instances are shared across
|
|
526
|
+
* runs of their prefix, which is why prefix `execute()`/`call()` limits
|
|
527
|
+
* reject it. Memory accumulates across calls on a warm instance —
|
|
528
|
+
* hitting the cap taints the instance and the next call cold-starts.
|
|
529
|
+
*
|
|
530
|
+
* A plain number is the MANAGED form for a prefix's reused instances: it
|
|
531
|
+
* is the retirement line, and the terminating cap sits a headroom band
|
|
532
|
+
* above it (`+sqrt(8 × n)` MB, so `128` terminates at 160). An instance
|
|
533
|
+
* over the number at two consecutive finishes is retired without failing
|
|
534
|
+
* anything; only a runaway allocation reaches the terminating cap. Pass a
|
|
535
|
+
* {@link MemoryLimit} to set the two lines yourself, or `{ hard }` alone
|
|
536
|
+
* for a single terminating cap and no retirement.
|
|
537
|
+
*
|
|
538
|
+
* One-off `run()` isolates take the same number as a plain hard cap — no
|
|
539
|
+
* band, no retirement, since a fresh isolate is never reused. So `128`
|
|
540
|
+
* means "retire a prefix instance at 128, kill it at 160" and "kill a
|
|
541
|
+
* one-off at 128".
|
|
542
|
+
*
|
|
543
|
+
* Zero means no cap (such runs are admitted against the memory budget
|
|
544
|
+
* instead of the 90% admission line). Must be an integer ≥ 0.
|
|
545
|
+
* @default 128
|
|
546
|
+
*/
|
|
545
547
|
memoryMb?: number | MemoryLimit;
|
|
546
548
|
/**
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
* that the byte math stops being exact) and a negative value to `0`; a
|
|
584
|
-
* non-finite one throws.
|
|
585
|
-
*/
|
|
549
|
+
* The runtime's memory budget (the shedding mark), in megabytes. The
|
|
550
|
+
* runtime watches its own process's resident memory (RSS) against it;
|
|
551
|
+
* the Node host is not measured. At/above the mark it evicts idle warm
|
|
552
|
+
* instances by `memoryBytes × idleTime^1.2` score (highest first) AND stops
|
|
553
|
+
* pooling new instances — prefix runs without an idle instance execute
|
|
554
|
+
* on cold one-off isolates — until usage falls back to 80 % of the mark
|
|
555
|
+
* (hysteresis, so eviction doesn't flap). Reusing an already-warm
|
|
556
|
+
* instance stays allowed under pressure; it adds no memory. Correctness
|
|
557
|
+
* never depends on warmth; the only observable cost of eviction is
|
|
558
|
+
* cold-start latency. There is no instance-count cap — concurrency is
|
|
559
|
+
* bounded by `maxConcurrentRuns`, memory by this mark.
|
|
560
|
+
*
|
|
561
|
+
* Above the budget sits one more, non-configurable line: the runtime
|
|
562
|
+
* never CREATES an isolate when its own RSS plus the run's `memoryMb`
|
|
563
|
+
* would cross 90 % of the container limit minus the `hostReserveMb`
|
|
564
|
+
* reserve (128 MB by default) — such a run fails with
|
|
565
|
+
* `ERR_CAPACITY_MEMORY` instead. As long as the host stays within
|
|
566
|
+
* `hostReserveMb`, the newest admission is never what tips the container
|
|
567
|
+
* into an OOM kill. Runs with `memoryMb: 0` (uncapped) are refused already from the
|
|
568
|
+
* budget mark — their worst case has no arithmetic.
|
|
569
|
+
*
|
|
570
|
+
* Must be > 0. To never evict, pass a large number: a budget above the
|
|
571
|
+
* container limit works. Independent of
|
|
572
|
+
* `memoryMb`: usage is measured, not derived from per-isolate caps.
|
|
573
|
+
*
|
|
574
|
+
* Default: 80 % of what remains of the container limit after the
|
|
575
|
+
* `hostReserveMb` reserve (128 MB) — container/cgroup-aware via
|
|
576
|
+
* `process.constrainedMemory()`, falling back to `os.totalmem()`.
|
|
577
|
+
* Services with a large host-side footprint should raise `hostReserveMb`.
|
|
578
|
+
* On a host too small for that subtraction the default floors at 64 MB
|
|
579
|
+
* (with a warning on stderr) rather than reaching 0 and disabling the
|
|
580
|
+
* mark: memory-starved hosts get aggressive eviction, never no protection.
|
|
581
|
+
* Explicit values are clamped to `Number.MAX_SAFE_INTEGER` bytes (above
|
|
582
|
+
* that the byte math stops being exact); `0`, a negative or a non-finite
|
|
583
|
+
* value throws.
|
|
584
|
+
*/
|
|
586
585
|
memoryBudgetMb?: number;
|
|
587
586
|
/**
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
* @default 128
|
|
602
|
-
*/
|
|
587
|
+
* Megabytes of the container limit reserved for the host process before
|
|
588
|
+
* either capacity line is drawn — the default budget is 80 % of what
|
|
589
|
+
* remains after it, and the runtime's admission line 90 %.
|
|
590
|
+
*
|
|
591
|
+
* Set it to what the Node host uses at its peak: the runtime measures
|
|
592
|
+
* only its own process, so Node's memory is never counted anywhere else.
|
|
593
|
+
* Raise it when the host caches heavily (a request-driven cache, parsed
|
|
594
|
+
* specs, a large in-memory index); lower it for a thin host that does
|
|
595
|
+
* little but drive the sandbox. `0` gives the sandbox the whole limit.
|
|
596
|
+
*
|
|
597
|
+
* Must be an integer ≥ 0.
|
|
598
|
+
* @default 128
|
|
599
|
+
*/
|
|
603
600
|
hostReserveMb?: number;
|
|
604
601
|
/**
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
602
|
+
* Override the path to the Rust V8 binary.
|
|
603
|
+
* Default: auto-detect from \@iso4/v8-* platform packages.
|
|
604
|
+
*/
|
|
608
605
|
binaryPath?: string;
|
|
609
606
|
}
|
|
610
607
|
/**
|
|
611
|
-
* A point-in-time capacity/usage snapshot from `sandbox.stats()`.
|
|
612
|
-
* The runtime-side numbers are mutually consistent (taken under one
|
|
613
|
-
* registry lock); `queueDepth` is a separate host-side read taken after
|
|
614
|
-
* the round trip. All of it is stale the moment it returns —
|
|
615
|
-
* diagnostics, not synchronization.
|
|
616
|
-
*/
|
|
608
|
+
* A point-in-time capacity/usage snapshot from `sandbox.stats()`.
|
|
609
|
+
* The runtime-side numbers are mutually consistent (taken under one
|
|
610
|
+
* registry lock); `queueDepth` is a separate host-side read taken after
|
|
611
|
+
* the round trip. All of it is stale the moment it returns —
|
|
612
|
+
* diagnostics, not synchronization.
|
|
613
|
+
*/
|
|
617
614
|
interface SandboxStats {
|
|
618
615
|
/**
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
616
|
+
* Runs executing right now: one-off `run()`s plus prefix runs on warm
|
|
617
|
+
* instances. Since #77 several prefix runs may share one instance, so
|
|
618
|
+
* this can exceed the busy-instance count.
|
|
619
|
+
*/
|
|
623
620
|
activeRuns: number;
|
|
624
621
|
/**
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
622
|
+
* Callers queued for a free run slot (`maxConcurrentRuns` runs are
|
|
623
|
+
* already executing).
|
|
624
|
+
*/
|
|
628
625
|
queueDepth: number;
|
|
629
626
|
/**
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
627
|
+
* Runs admitted concurrently right now. Follows the runtime's own figure
|
|
628
|
+
* as the workload changes, unless `maxConcurrentRuns` pinned it.
|
|
629
|
+
*/
|
|
633
630
|
slotLimit: number;
|
|
634
631
|
/**
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
632
|
+
* Host-side count of runtime connections as the pool tracks them (the
|
|
633
|
+
* dedicated `stats()` control connection not included). Runs are
|
|
634
|
+
* multiplexed onto shared connections — several concurrent runs per
|
|
635
|
+
* connection, another opened only when all are at the internal
|
|
636
|
+
* per-connection cap — and connections are kept for the process lifetime,
|
|
637
|
+
* so this rises to roughly the concurrency high-water mark divided by the
|
|
638
|
+
* cap and stays there. Capacity is `maxConcurrentRuns`, an admission
|
|
639
|
+
* number decoupled from sockets. It is a ledger, not a per-read socket
|
|
640
|
+
* probe: a connection that died stays counted until the next run
|
|
641
|
+
* observes and replaces it.
|
|
642
|
+
*/
|
|
646
643
|
openConnections: number;
|
|
647
644
|
/**
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
645
|
+
* Resident warm instances — busy plus idle. Bounded by memory (the RSS
|
|
646
|
+
* mark), not by a count.
|
|
647
|
+
*/
|
|
651
648
|
warmInstances: number;
|
|
652
649
|
/**
|
|
653
|
-
|
|
654
|
-
|
|
650
|
+
* Idle warm instances ready to serve their prefix without a cold start.
|
|
651
|
+
*/
|
|
655
652
|
idleInstances: number;
|
|
656
653
|
/**
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
654
|
+
* Summed memory of the idle instances in bytes (JS heap plus buffer
|
|
655
|
+
* memory, as {@link RunSuccess.memoryBytes}), each measured after its
|
|
656
|
+
* last call — or, where the runtime's idle settle is switched on, after
|
|
657
|
+
* the collection a parked instance runs, which is the smaller and truer
|
|
658
|
+
* number (a busy instance's current memory is unknown mid-call).
|
|
659
|
+
*/
|
|
660
|
+
idleMemoryBytes: number;
|
|
661
|
+
/**
|
|
662
|
+
* The memory mark the runtime sheds against, in bytes —
|
|
663
|
+
* `memoryBudgetMb` as the runtime received it.
|
|
664
|
+
* Utilization is `rssBytes / budgetBytes`.
|
|
665
|
+
*/
|
|
668
666
|
budgetBytes: number;
|
|
669
667
|
/**
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
668
|
+
* The runtime process's own resident set size in bytes at snapshot time
|
|
669
|
+
* — the signal both capacity lines act on. 0 when the platform offers
|
|
670
|
+
* no reader.
|
|
671
|
+
*/
|
|
674
672
|
rssBytes: number;
|
|
675
673
|
/**
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
usageBytes: number;
|
|
682
|
-
/**
|
|
683
|
-
* The isolate admission line in bytes: 90 % of the container limit minus
|
|
684
|
-
* the host reserve. A run needing a new isolate whose `memoryMb` would
|
|
685
|
-
* not fit under it fails with `ERR_CAPACITY_MEMORY`. 0 = no container limit
|
|
686
|
-
* readable, line disabled.
|
|
687
|
-
*/
|
|
674
|
+
* The isolate admission line in bytes: 90 % of the container limit minus
|
|
675
|
+
* the host reserve. A run needing a new isolate whose `memoryMb` would
|
|
676
|
+
* not fit under it fails with `ERR_CAPACITY_MEMORY`. 0 = no container limit
|
|
677
|
+
* readable, line disabled.
|
|
678
|
+
*/
|
|
688
679
|
hardLineBytes: number;
|
|
689
680
|
/**
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
681
|
+
* True while the runtime is shedding: usage reached the budget and
|
|
682
|
+
* has not yet fallen back to 80 % of it. While shedding, idle instances
|
|
683
|
+
* are evicted by score and new warm admissions stop (prefix runs without
|
|
684
|
+
* an idle instance degrade to cold one-off isolates).
|
|
685
|
+
*/
|
|
695
686
|
underPressure: boolean;
|
|
696
687
|
/**
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
688
|
+
* Per-prefix instance counts, keyed by prefix id — saturation of a single
|
|
689
|
+
* hot prefix is diagnosable here.
|
|
690
|
+
*/
|
|
700
691
|
prefixes: Record<string, {
|
|
701
692
|
idle: number;
|
|
702
693
|
busy: number;
|
|
@@ -704,404 +695,404 @@ interface SandboxStats {
|
|
|
704
695
|
}
|
|
705
696
|
interface Sandbox {
|
|
706
697
|
/**
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
698
|
+
* Execute a piece of JavaScript in a fresh V8 isolate.
|
|
699
|
+
* For repeated use with a shared context, prefer prepare() + prefix.execute().
|
|
700
|
+
*
|
|
701
|
+
* With `call`, the freshly evaluated module's export at `call.export` is
|
|
702
|
+
* invoked and the result carries its return `value` instead of `exports`:
|
|
703
|
+
*
|
|
704
|
+
* ```ts
|
|
705
|
+
* await sandbox.run({
|
|
706
|
+
* code: `export default { async fetch(request) { return new Response('hi') } }`,
|
|
707
|
+
* call: { export: 'default.fetch', args: [request] },
|
|
708
|
+
* })
|
|
709
|
+
* ```
|
|
710
|
+
*
|
|
711
|
+
* Invalid options reject, like `prepare()`: a global name the runtime owns
|
|
712
|
+
* rejects with an `Error` whose `code` is `ERR_RESERVED_NAME`; a malformed
|
|
713
|
+
* option (a bad limit or `memoryMb`, a global name that is not a
|
|
714
|
+
* well-formed string, an invalid host-module shape) rejects with a plain
|
|
715
|
+
* `Error`/`TypeError`.
|
|
716
|
+
* Nothing ran. Everything that happens once the run starts resolves as
|
|
717
|
+
* `{ ok: false, error }` — see {@link RunErrorCode}.
|
|
718
|
+
*/
|
|
728
719
|
run: {
|
|
729
720
|
(options: RunCallOptions): Promise<CallResult>;
|
|
730
721
|
(options: RunOptions): Promise<RunResult>;
|
|
731
722
|
};
|
|
732
723
|
/**
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
724
|
+
* Load a module once and read its serializable exports — the
|
|
725
|
+
* deploy/registration path (IaC-style declaration exports read when a
|
|
726
|
+
* function is created, never per request). Builds no prefix and retains
|
|
727
|
+
* nothing. Function exports are skipped and reported, so a module that
|
|
728
|
+
* also exports handlers reads cleanly. Rejects on a syntax or runtime
|
|
729
|
+
* error (`ERR_USER_CODE`), a missing import, or any other run failure.
|
|
730
|
+
*/
|
|
740
731
|
readExports: (options: ReadExportsOptions) => Promise<ReadExportsResult>;
|
|
741
732
|
/**
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
/**
|
|
782
|
-
|
|
783
|
-
|
|
733
|
+
* Prepare a prefix of code for repeated runs.
|
|
734
|
+
*
|
|
735
|
+
* `prepare()` stores the source and declared shape; it does not evaluate
|
|
736
|
+
* the prefix. It rejects only for options it can check without running
|
|
737
|
+
* anything: a reserved or malformed global name, an invalid import shape,
|
|
738
|
+
* a bad limit or `memoryMb`. A prefix that does not compile, throws at top
|
|
739
|
+
* level, imports something unknown, calls the bridge, never settles, or
|
|
740
|
+
* exceeds the warm-up budget fails on its first `execute()`/`call()` as an
|
|
741
|
+
* `{ ok: false }` result with the matching {@link RunErrorCode}
|
|
742
|
+
* (`ERR_USER_CODE`, `ERR_MODULE_NOT_FOUND`, `ERR_IMPORT`,
|
|
743
|
+
* `ERR_PREFIX_BRIDGE_CALL`, `ERR_PREFIX_DID_NOT_SETTLE`,
|
|
744
|
+
* `ERR_WARMUP_LIMIT`). Validating a prefix is the caller's job: parse the
|
|
745
|
+
* source yourself (an `oxc`/`acorn` pass catches syntax errors), or do one
|
|
746
|
+
* trial run after `prepare()` — its instance stays warm, so nothing is
|
|
747
|
+
* wasted:
|
|
748
|
+
*
|
|
749
|
+
* ```ts
|
|
750
|
+
* const prefix = await sandbox.prepare({ code })
|
|
751
|
+
* const check = await prefix.execute({ code: 'export default 0' })
|
|
752
|
+
* if (!check.ok) throw new Error(`broken prefix: ${check.error.message}`)
|
|
753
|
+
* ```
|
|
754
|
+
*
|
|
755
|
+
* The type parameter `G` is inferred from the `globals` you pass and flows
|
|
756
|
+
* into the returned `Prefix<G>`. This lets TypeScript enforce at the call
|
|
757
|
+
* site that `prefix.run()` can only rebind names declared here:
|
|
758
|
+
*
|
|
759
|
+
* ```ts
|
|
760
|
+
* const prefix = await sandbox.precompile({
|
|
761
|
+
* globals: { fetch: myHandler, myTool: otherHandler },
|
|
762
|
+
* })
|
|
763
|
+
* // prefix: Prefix<{ fetch: ..., myTool: ... }>
|
|
764
|
+
*
|
|
765
|
+
* prefix.run({ globals: { fetch: newHandler } }) // ✅
|
|
766
|
+
* prefix.run({ globals: { undeclared: someHandler } }) // ❌ TS error
|
|
767
|
+
* ```
|
|
768
|
+
*
|
|
769
|
+
* Globals declared here serve as **default** implementations for all runs.
|
|
770
|
+
* `prefix.run()` may override any subset; omitted names reuse the default.
|
|
771
|
+
*/
|
|
772
|
+
/**
|
|
773
|
+
* eslint-disable \@typescript-eslint/no-empty-object-type
|
|
774
|
+
*/
|
|
784
775
|
prepare: <G extends HostGlobals = {}, M extends Imports = {}>(options: PrecompileOptions<G, M>) => Promise<Prefix<G, M>>;
|
|
785
776
|
/**
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
777
|
+
* @deprecated Renamed to {@link Sandbox.prepare}. `precompile` stays as an
|
|
778
|
+
* alias (identical behavior) and is slated for removal in a future major.
|
|
779
|
+
*/
|
|
789
780
|
precompile: <G extends HostGlobals = {}, M extends Imports = {}>(options: PrecompileOptions<G, M>) => Promise<Prefix<G, M>>;
|
|
790
781
|
/**
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
782
|
+
* A point-in-time capacity/usage snapshot: active runs, queue
|
|
783
|
+
* depth, warm-instance counts and their measured memory. Served over a
|
|
784
|
+
* dedicated control connection, so it answers even while every run slot
|
|
785
|
+
* is busy. See {@link SandboxStats}.
|
|
786
|
+
*/
|
|
796
787
|
stats: () => Promise<SandboxStats>;
|
|
797
788
|
/**
|
|
798
|
-
|
|
799
|
-
|
|
789
|
+
* Tear down the Rust process. Outstanding prefixes are invalidated.
|
|
790
|
+
*/
|
|
800
791
|
dispose: () => Promise<void>;
|
|
801
792
|
/**
|
|
802
|
-
|
|
803
|
-
|
|
793
|
+
* Enables `await using sandbox = await createSandbox()` syntax.
|
|
794
|
+
*/
|
|
804
795
|
[Symbol.asyncDispose]: () => Promise<void>;
|
|
805
796
|
readonly alive: boolean;
|
|
806
797
|
}
|
|
807
798
|
type CreateSandbox = (options?: SandboxOptions) => Promise<Sandbox>;
|
|
808
799
|
/**
|
|
809
|
-
* Options for `sandbox.precompile()`.
|
|
810
|
-
*
|
|
811
|
-
* `G` is the shape of globals this prefix declares. TypeScript infers it
|
|
812
|
-
* from the `globals` field, so the returned `Prefix<G>` is typed to allow
|
|
813
|
-
* only those names to be rebound in subsequent `prefix.run()` calls.
|
|
814
|
-
*/
|
|
800
|
+
* Options for `sandbox.precompile()`.
|
|
801
|
+
*
|
|
802
|
+
* `G` is the shape of globals this prefix declares. TypeScript infers it
|
|
803
|
+
* from the `globals` field, so the returned `Prefix<G>` is typed to allow
|
|
804
|
+
* only those names to be rebound in subsequent `prefix.run()` calls.
|
|
805
|
+
*/
|
|
815
806
|
interface PrecompileOptions<G extends HostGlobals, M extends Imports> {
|
|
816
807
|
/**
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
808
|
+
* ESM prefix source, evaluated at the start of every run. Top-level await works.
|
|
809
|
+
*
|
|
810
|
+
* @example
|
|
811
|
+
* import { search } from 'tools:search';
|
|
812
|
+
* globalThis.search = search;
|
|
813
|
+
*/
|
|
823
814
|
code: string;
|
|
824
815
|
/**
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
816
|
+
* Cap on isolate memory for THIS prefix, in megabytes — V8 heap plus
|
|
817
|
+
* external `ArrayBuffer`s, baked into every instance serving it (e.g.
|
|
818
|
+
* 256 MB edge functions beside 32 MB codemode functions in one sandbox).
|
|
819
|
+
* Per-prefix and per-one-off are the only granularities: a per-run value
|
|
820
|
+
* on a warm instance stays impossible — the cap is fixed at isolate
|
|
821
|
+
* creation and instances are shared across runs.
|
|
822
|
+
*
|
|
823
|
+
* A plain number is the managed form (retirement line + headroom band);
|
|
824
|
+
* pass a {@link MemoryLimit} for exact lines. This is the granularity the
|
|
825
|
+
* soft line is really for: prefix instances are the ones that get reused
|
|
826
|
+
* long enough to accumulate. `0` = uncapped (admitted against the memory
|
|
827
|
+
* budget instead of the 90% line). Must be an integer ≥ 0.
|
|
828
|
+
* @default the sandbox-level `memoryMb` (128)
|
|
829
|
+
*/
|
|
839
830
|
memoryMb?: number | MemoryLimit;
|
|
840
831
|
/**
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
832
|
+
* Declares the globals that sandbox code will be able to call.
|
|
833
|
+
*
|
|
834
|
+
* This defines the **bridge surface shape** — the full set of names the
|
|
835
|
+
* sandbox may invoke as globals. Names declared here:
|
|
836
|
+
*
|
|
837
|
+
* - Become bridge stubs in the V8 context on every `prefix.run()` call.
|
|
838
|
+
* - Can be rebound per-run via `prefix.run({ globals })`.
|
|
839
|
+
* - Cannot be supplemented at run time — a name not declared here will
|
|
840
|
+
* cause `ERR_UNDECLARED_BINDING` if passed to `prefix.run()`.
|
|
841
|
+
*
|
|
842
|
+
* The handlers provided here are the **default** implementations reused by
|
|
843
|
+
* any `prefix.run()` call that does not override them.
|
|
844
|
+
*/
|
|
854
845
|
globals?: G;
|
|
855
846
|
/**
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
847
|
+
* Declares the imports the sandbox code may resolve.
|
|
848
|
+
*
|
|
849
|
+
* The inferred shape feeds `Prefix<G, M>` so `prefix.run()` knows which
|
|
850
|
+
* specifiers exist and which exports may be rebound.
|
|
851
|
+
*/
|
|
861
852
|
imports?: M;
|
|
862
853
|
/**
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
854
|
+
* Default limits for every `execute()`/`call()` on this prefix. A run's
|
|
855
|
+
* own `limits` overrides them field by field, so a run can change one
|
|
856
|
+
* knob without restating the rest; a field set in neither place takes the
|
|
857
|
+
* runtime default. Checked here like per-run limits (a `TypeError` for a
|
|
858
|
+
* bad value). The heap cap is not part of this: set it with `memoryMb`.
|
|
859
|
+
*
|
|
860
|
+
* Prefix evaluation itself runs under the fixed warm-up budget
|
|
861
|
+
* (1 s wall / 1 s CPU), not these limits.
|
|
862
|
+
*/
|
|
872
863
|
limits?: ResourceLimits;
|
|
873
864
|
/**
|
|
874
|
-
|
|
875
|
-
|
|
865
|
+
* Optional filename for stack traces. Default: "<prefix>".
|
|
866
|
+
*/
|
|
876
867
|
filename?: string;
|
|
877
868
|
}
|
|
878
869
|
/**
|
|
879
|
-
* Handle to a precompiled prefix.
|
|
880
|
-
*
|
|
881
|
-
* `G` is the shape of globals declared at precompile time. `prefix.run()`
|
|
882
|
-
* accepts only `Partial<G>` for its `globals` field — you may override any
|
|
883
|
-
* declared global's implementation per run, but you cannot add names that
|
|
884
|
-
* were not declared at precompile time.
|
|
885
|
-
*
|
|
886
|
-
* **Warm instances.** Runs on a prefix are served by resident
|
|
887
|
-
* isolates: the first run cold-starts one (prefix evaluated once, under the
|
|
888
|
-
* fixed warm-up budget), later runs reuse it and skip isolate boot and
|
|
889
|
-
* prefix re-evaluation. Warmth is a cache, never a guarantee — module-scope
|
|
890
|
-
* state MAY survive between runs on one instance, but any instance can be
|
|
891
|
-
* evicted at any moment (a CPU/memory limit firing mid-execution, an abort
|
|
892
|
-
* landing on running code, memory pressure,
|
|
893
|
-
* `dispose()`), and concurrent runs of one prefix are served by separate
|
|
894
|
-
* instances that share no state. Do not rely on carryover, and do not rely
|
|
895
|
-
* on per-run isolation within a prefix either: keep durable state outside
|
|
896
|
-
* the sandbox, and do per-request setup inside the handler — lazy init
|
|
897
|
-
* (`conn ??= await setup()`) runs under that call's budget and re-runs
|
|
898
|
-
* correctly after any eviction.
|
|
899
|
-
*/
|
|
870
|
+
* Handle to a precompiled prefix.
|
|
871
|
+
*
|
|
872
|
+
* `G` is the shape of globals declared at precompile time. `prefix.run()`
|
|
873
|
+
* accepts only `Partial<G>` for its `globals` field — you may override any
|
|
874
|
+
* declared global's implementation per run, but you cannot add names that
|
|
875
|
+
* were not declared at precompile time.
|
|
876
|
+
*
|
|
877
|
+
* **Warm instances.** Runs on a prefix are served by resident
|
|
878
|
+
* isolates: the first run cold-starts one (prefix evaluated once, under the
|
|
879
|
+
* fixed warm-up budget), later runs reuse it and skip isolate boot and
|
|
880
|
+
* prefix re-evaluation. Warmth is a cache, never a guarantee — module-scope
|
|
881
|
+
* state MAY survive between runs on one instance, but any instance can be
|
|
882
|
+
* evicted at any moment (a CPU/memory limit firing mid-execution, an abort
|
|
883
|
+
* landing on running code, memory pressure,
|
|
884
|
+
* `dispose()`), and concurrent runs of one prefix are served by separate
|
|
885
|
+
* instances that share no state. Do not rely on carryover, and do not rely
|
|
886
|
+
* on per-run isolation within a prefix either: keep durable state outside
|
|
887
|
+
* the sandbox, and do per-request setup inside the handler — lazy init
|
|
888
|
+
* (`conn ??= await setup()`) runs under that call's budget and re-runs
|
|
889
|
+
* correctly after any eviction.
|
|
890
|
+
*/
|
|
900
891
|
interface Prefix<G extends HostGlobals = {}, M extends Imports = {}> {
|
|
901
892
|
readonly id: string;
|
|
902
893
|
/**
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
894
|
+
* Execute dynamic code against this prefix's declared state.
|
|
895
|
+
*
|
|
896
|
+
* Globals declared at precompile time are active. You may rebind any subset
|
|
897
|
+
* of them via `options.globals`; the precompile-time handler is used for
|
|
898
|
+
* any name not overridden here. Same rule for `imports`, restricted to
|
|
899
|
+
* host-import function exports declared in `I`.
|
|
900
|
+
*
|
|
901
|
+
* @example
|
|
902
|
+
* const result = await prefix.execute({
|
|
903
|
+
* code: `export default await (${agentFn})()`,
|
|
904
|
+
* })
|
|
905
|
+
*/
|
|
915
906
|
execute: (options: PrefixRunOptions<G, M>) => Promise<RunResult>;
|
|
916
907
|
/**
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
908
|
+
* @deprecated Renamed to {@link Prefix.execute}. `run` stays as an alias
|
|
909
|
+
* (identical behavior) and is slated for removal in a future major.
|
|
910
|
+
*/
|
|
920
911
|
run: (options: PrefixRunOptions<G, M>) => Promise<RunResult>;
|
|
921
912
|
/**
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
913
|
+
* Call a function exported by this prefix's module — nothing is compiled
|
|
914
|
+
* per request. On a warm instance the prefix is not even re-evaluated:
|
|
915
|
+
* `export` resolves against the retained module and the result carries
|
|
916
|
+
* the function's return value:
|
|
917
|
+
*
|
|
918
|
+
* ```ts
|
|
919
|
+
* const prefix = await sandbox.prepare({ code: bundle })
|
|
920
|
+
* const result = await prefix.call({ export: 'default.fetch', args: [request] })
|
|
921
|
+
* ```
|
|
922
|
+
*
|
|
923
|
+
* `globals` / `imports` rebind per call exactly as they do for
|
|
924
|
+
* {@link Prefix.execute}.
|
|
925
|
+
*/
|
|
935
926
|
call: (options: PrefixCallOptions<G, M>) => Promise<CallResult>;
|
|
936
927
|
/**
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
928
|
+
* Release the prepared prefix. Subsequent run() calls reject with
|
|
929
|
+
* `ERR_PREFIX_DISPOSED`. Idempotent, and works on a broken prefix too.
|
|
930
|
+
*/
|
|
940
931
|
dispose: () => Promise<void>;
|
|
941
932
|
/**
|
|
942
|
-
|
|
943
|
-
|
|
933
|
+
* Enables `await using prefix = await sandbox.precompile(...)` syntax.
|
|
934
|
+
*/
|
|
944
935
|
[Symbol.asyncDispose]: () => Promise<void>;
|
|
945
936
|
/**
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
937
|
+
* `true` until `dispose()`. A broken prefix is still alive: it is held
|
|
938
|
+
* by the runtime and the host still owns its disposal.
|
|
939
|
+
*/
|
|
949
940
|
readonly alive: boolean;
|
|
950
941
|
/**
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
942
|
+
* `'ready'` until the runtime reports a prefix-stage failure, `'broken'`
|
|
943
|
+
* from then on (see {@link PrefixStatus}), `'disposed'` after
|
|
944
|
+
* `dispose()`. The client learns `'broken'` from the first run's
|
|
945
|
+
* `{ ok: false, error: { stage: 'prefix' } }` result, or from an
|
|
946
|
+
* `ERR_PREFIX_BROKEN` refusal if that result went to another client.
|
|
947
|
+
*/
|
|
957
948
|
readonly status: PrefixStatus;
|
|
958
949
|
/**
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
950
|
+
* The stored cold-start failure once the prefix is broken, `undefined`
|
|
951
|
+
* before. Kept through `dispose()`. Always has `code`, `name`, `message`
|
|
952
|
+
* and `stack`; `stage` and `fields` are present only when this client saw
|
|
953
|
+
* the failing run's own result rather than a later `ERR_PREFIX_BROKEN`
|
|
954
|
+
* refusal.
|
|
955
|
+
*/
|
|
965
956
|
readonly error: RunError | undefined;
|
|
966
957
|
}
|
|
967
958
|
/**
|
|
968
|
-
* Lifecycle of a {@link Prefix}. `'broken'` means the prefix failed to
|
|
969
|
-
* evaluate with one of its own, repeatable errors; every later run is
|
|
970
|
-
* refused with `ERR_PREFIX_BROKEN` and the only retry is a new `prepare()`.
|
|
971
|
-
* Load-dependent cold-start failures (`ERR_WARMUP_LIMIT`,
|
|
972
|
-
* `ERR_MEMORY_LIMIT`) never break a prefix — the next run cold-starts again.
|
|
973
|
-
*/
|
|
959
|
+
* Lifecycle of a {@link Prefix}. `'broken'` means the prefix failed to
|
|
960
|
+
* evaluate with one of its own, repeatable errors; every later run is
|
|
961
|
+
* refused with `ERR_PREFIX_BROKEN` and the only retry is a new `prepare()`.
|
|
962
|
+
* Load-dependent cold-start failures (`ERR_WARMUP_LIMIT`,
|
|
963
|
+
* `ERR_MEMORY_LIMIT`) never break a prefix — the next run cold-starts again.
|
|
964
|
+
*/
|
|
974
965
|
type PrefixStatus = "ready" | "broken" | "disposed";
|
|
975
966
|
/**
|
|
976
|
-
* Options for `prefix.run()`.
|
|
977
|
-
*
|
|
978
|
-
* `G` is the declared-globals shape from the matching `precompile()` call.
|
|
979
|
-
* The `globals` field is typed as `Partial<G>` — only names present in `G`
|
|
980
|
-
* may be overridden. Passing any other name is a TypeScript error at compile
|
|
981
|
-
* time and rejects with `ERR_UNDECLARED_BINDING` at runtime. Every rebind
|
|
982
|
-
* refusal rejects; nothing runs.
|
|
983
|
-
*/
|
|
967
|
+
* Options for `prefix.run()`.
|
|
968
|
+
*
|
|
969
|
+
* `G` is the declared-globals shape from the matching `precompile()` call.
|
|
970
|
+
* The `globals` field is typed as `Partial<G>` — only names present in `G`
|
|
971
|
+
* may be overridden. Passing any other name is a TypeScript error at compile
|
|
972
|
+
* time and rejects with `ERR_UNDECLARED_BINDING` at runtime. Every rebind
|
|
973
|
+
* refusal rejects; nothing runs.
|
|
974
|
+
*/
|
|
984
975
|
interface PrefixRunOptions<G extends HostGlobals, M extends Imports> {
|
|
985
976
|
/**
|
|
986
|
-
|
|
987
|
-
|
|
977
|
+
* Dynamic ESM source code to compile and run against the prefix state.
|
|
978
|
+
*/
|
|
988
979
|
code: string;
|
|
989
980
|
/**
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
981
|
+
* Override implementations for a **subset** of globals declared at
|
|
982
|
+
* precompile time. Only names present in `G` are accepted (TypeScript
|
|
983
|
+
* error + a runtime rejection with `ERR_UNDECLARED_BINDING` for anything
|
|
984
|
+
* else), but the
|
|
985
|
+
* function signature does not need to match the precompile-time handler
|
|
986
|
+
* exactly — any `HostExportFunction`-compatible value is accepted.
|
|
987
|
+
*/
|
|
997
988
|
globals?: RebindGlobals<G>;
|
|
998
989
|
/**
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
990
|
+
* Rebind host-module function exports declared at precompile time.
|
|
991
|
+
* Source module imports are frozen at prepare() time and cannot be rebound;
|
|
992
|
+
* data exports are baked into the synthetic module. TypeScript enforces
|
|
993
|
+
* this at the type level via `RebindImports<M>`; anything that slips
|
|
994
|
+
* through rejects the run at runtime — `ERR_UNDECLARED_BINDING` for locations
|
|
995
|
+
* never declared, `ERR_FROZEN_BINDING` for source modules and data leaves,
|
|
996
|
+
* `ERR_INVALID_REBIND` for a non-function override value.
|
|
997
|
+
*/
|
|
1007
998
|
imports?: RebindImports<M>;
|
|
1008
999
|
/**
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1000
|
+
* Limits for this run, applied field by field on top of the prefix's
|
|
1001
|
+
* `prepare({ limits })` defaults. `memoryMb` is not a per-run option here.
|
|
1002
|
+
*/
|
|
1012
1003
|
limits?: ResourceLimits;
|
|
1013
1004
|
/**
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1005
|
+
* Abort the run. Firing this is always SOFT: while queued the caller is
|
|
1006
|
+
* dequeued; a running run is abandoned at its next execution boundary —
|
|
1007
|
+
* nothing is interrupted mid-execution, the warm instance survives, and
|
|
1008
|
+
* co-resident runs are untouched. The result resolves with
|
|
1009
|
+
* `status: 'aborted'` carrying the runtime's real partial telemetry.
|
|
1010
|
+
*
|
|
1011
|
+
* A soft abort cannot reach code that never yields: a synchronous loop
|
|
1012
|
+
* with no `cpuTimeMs` cap keeps its thread until a hard abort
|
|
1013
|
+
* ({@link hardAbortSignal}) or dispose. Bound such code with `cpuTimeMs`.
|
|
1014
|
+
*/
|
|
1024
1015
|
signal?: AbortSignal;
|
|
1025
1016
|
/**
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1017
|
+
* HARD abort — the opt-in escalation. Firing this interrupts the run
|
|
1018
|
+
* wherever it is: executing JS is terminated immediately, and when that
|
|
1019
|
+
* lands mid-execution the shared instance is discarded and co-resident
|
|
1020
|
+
* runs fail with `ERR_INSTANCE_RESET` (cause `abort`). On a run that is
|
|
1021
|
+
* queued or suspended it degrades to the same clean abandon as `signal`,
|
|
1022
|
+
* so firing it is never worse than a soft abort. Typical use: fire
|
|
1023
|
+
* `signal` first, then this after your own patience runs out.
|
|
1024
|
+
*/
|
|
1034
1025
|
hardAbortSignal?: AbortSignal;
|
|
1035
1026
|
filename?: string;
|
|
1036
1027
|
}
|
|
1037
1028
|
/**
|
|
1038
|
-
* Options for `prefix.call()` — a {@link CallTarget} plus the same per-call
|
|
1039
|
-
* rebinding surface as {@link PrefixRunOptions}, minus `code`: a call frame
|
|
1040
|
-
* carries a postfix *or* a call, never both.
|
|
1041
|
-
*/
|
|
1029
|
+
* Options for `prefix.call()` — a {@link CallTarget} plus the same per-call
|
|
1030
|
+
* rebinding surface as {@link PrefixRunOptions}, minus `code`: a call frame
|
|
1031
|
+
* carries a postfix *or* a call, never both.
|
|
1032
|
+
*/
|
|
1042
1033
|
interface PrefixCallOptions<G extends HostGlobals, M extends Imports> extends CallTarget {
|
|
1043
1034
|
/**
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1035
|
+
* Override implementations for a subset of globals declared at
|
|
1036
|
+
* prepare() time — same rules as {@link PrefixRunOptions.globals}.
|
|
1037
|
+
*/
|
|
1047
1038
|
globals?: RebindGlobals<G>;
|
|
1048
1039
|
/**
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1040
|
+
* Rebind host-module function exports declared at prepare() time — same
|
|
1041
|
+
* rules as {@link PrefixRunOptions.imports}.
|
|
1042
|
+
*/
|
|
1052
1043
|
imports?: RebindImports<M>;
|
|
1053
1044
|
/**
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1045
|
+
* Limits for this call, applied field by field on top of the prefix's
|
|
1046
|
+
* `prepare({ limits })` defaults. `memoryMb` is not a per-run option here.
|
|
1047
|
+
*/
|
|
1057
1048
|
limits?: ResourceLimits;
|
|
1058
1049
|
/**
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1050
|
+
* Abort the run. Firing this is always SOFT: while queued the caller is
|
|
1051
|
+
* dequeued; a running run is abandoned at its next execution boundary —
|
|
1052
|
+
* nothing is interrupted mid-execution, the warm instance survives, and
|
|
1053
|
+
* co-resident runs are untouched. The result resolves with
|
|
1054
|
+
* `status: 'aborted'` carrying the runtime's real partial telemetry.
|
|
1055
|
+
*
|
|
1056
|
+
* A soft abort cannot reach code that never yields: a synchronous loop
|
|
1057
|
+
* with no `cpuTimeMs` cap keeps its thread until a hard abort
|
|
1058
|
+
* ({@link hardAbortSignal}) or dispose. Bound such code with `cpuTimeMs`.
|
|
1059
|
+
*/
|
|
1069
1060
|
signal?: AbortSignal;
|
|
1070
1061
|
/**
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1062
|
+
* HARD abort — the opt-in escalation. Firing this interrupts the run
|
|
1063
|
+
* wherever it is: executing JS is terminated immediately, and when that
|
|
1064
|
+
* lands mid-execution the shared instance is discarded and co-resident
|
|
1065
|
+
* runs fail with `ERR_INSTANCE_RESET` (cause `abort`). On a run that is
|
|
1066
|
+
* queued or suspended it degrades to the same clean abandon as `signal`,
|
|
1067
|
+
* so firing it is never worse than a soft abort. Typical use: fire
|
|
1068
|
+
* `signal` first, then this after your own patience runs out.
|
|
1069
|
+
*/
|
|
1079
1070
|
hardAbortSignal?: AbortSignal;
|
|
1080
1071
|
}
|
|
1081
1072
|
/**
|
|
1082
|
-
* Addresses a function that lives inside the sandbox, **relative to the
|
|
1083
|
-
* module's exports** — never `globalThis`. Either a top-level exported
|
|
1084
|
-
* function (`"handler"`) or a method on an exported object
|
|
1085
|
-
* (`"default.fetch"`).
|
|
1086
|
-
*
|
|
1087
|
-
* The receiver (`this`) is the object the final path segment was read from —
|
|
1088
|
-
* plain `a.b.c()` semantics — so `export default { fetch() { this.… } }` and
|
|
1089
|
-
* `export default new Worker()` (prototype methods included) both work.
|
|
1090
|
-
* A path that does not resolve, or resolves to something that is not
|
|
1091
|
-
* callable, fails the run with `ERR_CALL_TARGET_NOT_FOUND`.
|
|
1092
|
-
*/
|
|
1073
|
+
* Addresses a function that lives inside the sandbox, **relative to the
|
|
1074
|
+
* module's exports** — never `globalThis`. Either a top-level exported
|
|
1075
|
+
* function (`"handler"`) or a method on an exported object
|
|
1076
|
+
* (`"default.fetch"`).
|
|
1077
|
+
*
|
|
1078
|
+
* The receiver (`this`) is the object the final path segment was read from —
|
|
1079
|
+
* plain `a.b.c()` semantics — so `export default { fetch() { this.… } }` and
|
|
1080
|
+
* `export default new Worker()` (prototype methods included) both work.
|
|
1081
|
+
* A path that does not resolve, or resolves to something that is not
|
|
1082
|
+
* callable, fails the run with `ERR_CALL_TARGET_NOT_FOUND`.
|
|
1083
|
+
*/
|
|
1093
1084
|
interface CallTarget {
|
|
1094
1085
|
/**
|
|
1095
|
-
|
|
1096
|
-
|
|
1086
|
+
* Dot-separated export path, e.g. `"default.fetch"` or `"handler"`.
|
|
1087
|
+
*/
|
|
1097
1088
|
export: string;
|
|
1098
1089
|
/**
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1090
|
+
* Arguments for the call. They cross the boundary as **one** V8
|
|
1091
|
+
* serialization blob holding the array (identity between arguments
|
|
1092
|
+
* preserved), so anything in {@link HostExportData} is accepted — plus
|
|
1093
|
+
* `Request` / `Response` / `Headers` at any depth, which arrive in the
|
|
1094
|
+
* sandbox as real instances. Defaults to `[]`.
|
|
1095
|
+
*/
|
|
1105
1096
|
args?: unknown[];
|
|
1106
1097
|
}
|
|
1107
1098
|
interface RunOptions {
|
|
@@ -1110,146 +1101,157 @@ interface RunOptions {
|
|
|
1110
1101
|
globals?: HostGlobals;
|
|
1111
1102
|
imports?: Imports;
|
|
1112
1103
|
/**
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1104
|
+
* Abort the run. Firing this is always SOFT: while queued the caller is
|
|
1105
|
+
* dequeued; a running run is abandoned at its next execution boundary —
|
|
1106
|
+
* nothing is interrupted mid-execution, the warm instance survives, and
|
|
1107
|
+
* co-resident runs are untouched. The result resolves with
|
|
1108
|
+
* `status: 'aborted'` carrying the runtime's real partial telemetry.
|
|
1109
|
+
*
|
|
1110
|
+
* A soft abort cannot reach code that never yields: a synchronous loop
|
|
1111
|
+
* with no `cpuTimeMs` cap keeps its thread until a hard abort
|
|
1112
|
+
* ({@link hardAbortSignal}) or dispose. Bound such code with `cpuTimeMs`.
|
|
1113
|
+
*/
|
|
1123
1114
|
signal?: AbortSignal;
|
|
1124
1115
|
/**
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1116
|
+
* HARD abort — the opt-in escalation. Firing this interrupts the run
|
|
1117
|
+
* wherever it is: executing JS is terminated immediately, and when that
|
|
1118
|
+
* lands mid-execution the shared instance is discarded and co-resident
|
|
1119
|
+
* runs fail with `ERR_INSTANCE_RESET` (cause `abort`). On a run that is
|
|
1120
|
+
* queued or suspended it degrades to the same clean abandon as `signal`,
|
|
1121
|
+
* so firing it is never worse than a soft abort. Typical use: fire
|
|
1122
|
+
* `signal` first, then this after your own patience runs out.
|
|
1123
|
+
*/
|
|
1133
1124
|
hardAbortSignal?: AbortSignal;
|
|
1134
1125
|
filename?: string;
|
|
1135
1126
|
}
|
|
1136
1127
|
/**
|
|
1137
|
-
* `run()` options with a call: the module is evaluated, then
|
|
1138
|
-
* `call.export` is resolved against its freshly evaluated exports and
|
|
1139
|
-
* invoked. The result carries the function's return {@link CallSuccess.value}
|
|
1140
|
-
* instead of `exports` — never both.
|
|
1141
|
-
*/
|
|
1128
|
+
* `run()` options with a call: the module is evaluated, then
|
|
1129
|
+
* `call.export` is resolved against its freshly evaluated exports and
|
|
1130
|
+
* invoked. The result carries the function's return {@link CallSuccess.value}
|
|
1131
|
+
* instead of `exports` — never both.
|
|
1132
|
+
*/
|
|
1142
1133
|
interface RunCallOptions extends RunOptions {
|
|
1143
1134
|
call: CallTarget;
|
|
1144
1135
|
}
|
|
1145
1136
|
/**
|
|
1146
|
-
* Options for `sandbox.readExports()` — the deploy/registration path: load a
|
|
1147
|
-
* module once and read its declaration exports (IaC-style limits, connection
|
|
1148
|
-
* requirements, …). Function-valued exports are absent and reported in
|
|
1149
|
-
* {@link ReadExportsResult.skippedExports}, so reading the declarations of a
|
|
1150
|
-
* module that also exports handlers is not an error.
|
|
1151
|
-
*/
|
|
1137
|
+
* Options for `sandbox.readExports()` — the deploy/registration path: load a
|
|
1138
|
+
* module once and read its declaration exports (IaC-style limits, connection
|
|
1139
|
+
* requirements, …). Function-valued exports are absent and reported in
|
|
1140
|
+
* {@link ReadExportsResult.skippedExports}, so reading the declarations of a
|
|
1141
|
+
* module that also exports handlers is not an error.
|
|
1142
|
+
*/
|
|
1152
1143
|
interface ReadExportsOptions {
|
|
1153
1144
|
code: string;
|
|
1154
1145
|
limits?: ResourceLimits;
|
|
1155
1146
|
filename?: string;
|
|
1156
1147
|
}
|
|
1157
1148
|
/**
|
|
1158
|
-
* What `sandbox.readExports()` resolves to. Unlike `run()`, failures reject
|
|
1159
|
-
* the returned promise, since the deploy path treats a broken module as
|
|
1160
|
-
* exceptional rather than as a result variant.
|
|
1161
|
-
*/
|
|
1149
|
+
* What `sandbox.readExports()` resolves to. Unlike `run()`, failures reject
|
|
1150
|
+
* the returned promise, since the deploy path treats a broken module as
|
|
1151
|
+
* exceptional rather than as a result variant.
|
|
1152
|
+
*/
|
|
1162
1153
|
interface ReadExportsResult {
|
|
1163
1154
|
exports: SandboxExports;
|
|
1164
1155
|
/**
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1156
|
+
* Export names absent from `exports` because their value cannot cross the
|
|
1157
|
+
* boundary — see {@link RunSuccess.skippedExports}.
|
|
1158
|
+
*/
|
|
1168
1159
|
skippedExports: string[];
|
|
1169
1160
|
}
|
|
1170
1161
|
/**
|
|
1171
|
-
* Why a run was aborted — whatever value was passed to the aborting
|
|
1172
|
-
* `AbortController.abort(reason)` (or left `undefined`). Opaque to iso4 and
|
|
1173
|
-
* surfaced verbatim on {@link RunAborted.reason}; a consumer built on top
|
|
1174
|
-
* (e.g. a durable-workflow kernel expressing "suspend") gives it meaning.
|
|
1175
|
-
*/
|
|
1162
|
+
* Why a run was aborted — whatever value was passed to the aborting
|
|
1163
|
+
* `AbortController.abort(reason)` (or left `undefined`). Opaque to iso4 and
|
|
1164
|
+
* surfaced verbatim on {@link RunAborted.reason}; a consumer built on top
|
|
1165
|
+
* (e.g. a durable-workflow kernel expressing "suspend") gives it meaning.
|
|
1166
|
+
*/
|
|
1176
1167
|
type AbortReason = unknown;
|
|
1177
1168
|
type RunResult = RunSuccess | RunFailure | RunAborted;
|
|
1178
1169
|
/**
|
|
1179
|
-
* Result of a run that carried a {@link CallTarget}: on success the called
|
|
1180
|
-
* function's return value replaces `exports` — never both. Failures and
|
|
1181
|
-
* aborts are shared with {@link RunResult}.
|
|
1182
|
-
*/
|
|
1170
|
+
* Result of a run that carried a {@link CallTarget}: on success the called
|
|
1171
|
+
* function's return value replaces `exports` — never both. Failures and
|
|
1172
|
+
* aborts are shared with {@link RunResult}.
|
|
1173
|
+
*/
|
|
1183
1174
|
type CallResult = CallSuccess | RunFailure | RunAborted;
|
|
1184
1175
|
/**
|
|
1185
|
-
* The success arm of a {@link CallResult}. Logs, timings, and bridge records
|
|
1186
|
-
* are identical to {@link RunSuccess}; only the value slot differs.
|
|
1187
|
-
*/
|
|
1176
|
+
* The success arm of a {@link CallResult}. Logs, timings, and bridge records
|
|
1177
|
+
* are identical to {@link RunSuccess}; only the value slot differs.
|
|
1178
|
+
*/
|
|
1188
1179
|
interface CallSuccess {
|
|
1189
1180
|
status: "completed";
|
|
1190
1181
|
ok: true;
|
|
1191
1182
|
/**
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1183
|
+
* The called function's return value — awaited first when the function
|
|
1184
|
+
* returned a Promise. Anything V8's serialization format carries arrives
|
|
1185
|
+
* as a real instance, including a `Response`.
|
|
1186
|
+
*
|
|
1187
|
+
* A `Response`/`Request` whose body STREAMS (sandbox code returned
|
|
1188
|
+
* `new Response(asyncIterable)` or passed `request.body` through)
|
|
1189
|
+
* arrives with a real `ReadableStream` body: read it or cancel it. Your
|
|
1190
|
+
* reads drive the sandbox's production under flow control (a slow
|
|
1191
|
+
* reader pauses the source); a stream neither read nor cancelled for
|
|
1192
|
+
* 10 seconds is dropped by the runtime and later reads reject. Body
|
|
1193
|
+
* failures after this result was delivered reject the read, never
|
|
1194
|
+
* change this result — the same split as a `fetch` body dying
|
|
1195
|
+
* mid-transfer.
|
|
1196
|
+
*/
|
|
1206
1197
|
value: unknown;
|
|
1207
1198
|
stdout: string[];
|
|
1208
1199
|
stderr: string[];
|
|
1209
1200
|
/**
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1201
|
+
* Complete wall-clock from when the run starts executing to the processed
|
|
1202
|
+
* conclusion — async waits, engine time for co-resident runs, machinery:
|
|
1203
|
+
* everything. May exceed the `wallTimeMs` limit, which counts
|
|
1204
|
+
* {@link wallTimeMs} instead. Waiting for a free CPU before the run starts
|
|
1205
|
+
* is in none of the three clocks.
|
|
1206
|
+
*/
|
|
1214
1207
|
durationMs: number;
|
|
1215
1208
|
/**
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1209
|
+
* The call's own logic time: execution plus async waits, each wait counted
|
|
1210
|
+
* until its answer arrived — engine time is excluded and visible as
|
|
1211
|
+
* `durationMs - wallTimeMs`. This is the number `limits.wallTimeMs` caps.
|
|
1212
|
+
* `cpuTimeMs ≤ wallTimeMs ≤ durationMs`.
|
|
1213
|
+
*/
|
|
1221
1214
|
wallTimeMs: number;
|
|
1222
1215
|
/**
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1216
|
+
* CPU burned on the run's own thread; bridge waits excluded (itemized in
|
|
1217
|
+
* `bridgeCalls`), as is time spent descheduled under contention — so this
|
|
1218
|
+
* does not grow with how many runs share the host. V8's background threads
|
|
1219
|
+
* (GC, compilation) are not attributable to a run and are not included.
|
|
1220
|
+
*/
|
|
1228
1221
|
cpuTimeMs: number;
|
|
1229
1222
|
/**
|
|
1230
|
-
|
|
1231
|
-
|
|
1223
|
+
* See {@link RunSuccess.queueWaitMs}.
|
|
1224
|
+
*/
|
|
1232
1225
|
queueWaitMs?: number;
|
|
1233
1226
|
bridgeCalls: BridgeCallEntry[];
|
|
1234
1227
|
/**
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1228
|
+
* Memory of the isolate that served this call, in bytes: the JS heap plus
|
|
1229
|
+
* buffer memory (ArrayBuffers and typed arrays, including memory grown with
|
|
1230
|
+
* `resize()`, and stream chunks). Read once when the call ends, on prefix
|
|
1231
|
+
* and one-off calls alike; it includes garbage not yet collected (a warm
|
|
1232
|
+
* instance pushed over its soft line by buffers, with objects alone under
|
|
1233
|
+
* it, is collected once first). A call that sends its result early
|
|
1234
|
+
* (`waitUntil` work, open stream bodies) reports a reading taken at that
|
|
1235
|
+
* moment, before any run-end collection, so it can differ from the figure
|
|
1236
|
+
* the soft line uses. `0` on a prefix call whose instance could not be
|
|
1237
|
+
* measured (failed warm-up, dead instance). `undefined` when the call was
|
|
1238
|
+
* answered without a reading: refused before it started, or a result too
|
|
1239
|
+
* large to send.
|
|
1240
|
+
*/
|
|
1241
|
+
memoryBytes?: number;
|
|
1242
|
+
/**
|
|
1243
|
+
* Present when the call registered background work with `waitUntil` —
|
|
1244
|
+
* see {@link RunSuccess.waitUntil}. Never rejects.
|
|
1245
|
+
*/
|
|
1244
1246
|
waitUntil?: Promise<WaitUntilResult>;
|
|
1245
1247
|
}
|
|
1246
1248
|
/**
|
|
1247
|
-
* Metadata for a single bridge call attempt, recorded by the Rust runtime on
|
|
1248
|
-
* the run's own clock. No payloads — names, timing, and sizes only — so
|
|
1249
|
-
* recording is cheap enough to stay always-on. Unbounded: every attempt gets
|
|
1250
|
-
* an entry (~100 bytes); consumers that only care about a subset filter the
|
|
1251
|
-
* list themselves.
|
|
1252
|
-
*/
|
|
1249
|
+
* Metadata for a single bridge call attempt, recorded by the Rust runtime on
|
|
1250
|
+
* the run's own clock. No payloads — names, timing, and sizes only — so
|
|
1251
|
+
* recording is cheap enough to stay always-on. Unbounded: every attempt gets
|
|
1252
|
+
* an entry (~100 bytes); consumers that only care about a subset filter the
|
|
1253
|
+
* list themselves.
|
|
1254
|
+
*/
|
|
1253
1255
|
type BridgeCallEntry = BridgeCallEntryBase & ({
|
|
1254
1256
|
ok: true;
|
|
1255
1257
|
} | {
|
|
@@ -1257,196 +1259,209 @@ type BridgeCallEntry = BridgeCallEntryBase & ({
|
|
|
1257
1259
|
reason: BridgeCallFailureReason;
|
|
1258
1260
|
});
|
|
1259
1261
|
/**
|
|
1260
|
-
* Why a bridge call did not succeed ({@link BridgeCallEntry} with
|
|
1261
|
-
* `ok: false`).
|
|
1262
|
-
*
|
|
1263
|
-
* - `blocked`: refused inside the runtime (`maxBridgeCalls`, payload over
|
|
1264
|
-
* `maxBridgeCallBytes`, function argument, invalid import handle, failed
|
|
1265
|
-
* write). Never reached the host; the sandbox promise rejected.
|
|
1266
|
-
* - `error`: the host answered with an error — the handler threw or
|
|
1267
|
-
* rejected, the host could not decode the arguments, or the return value
|
|
1268
|
-
* could not be serialized. The sandbox promise rejected.
|
|
1269
|
-
* - `unanswered`: sent, but no answer by the time these records were taken —
|
|
1270
|
-
* abort, a limit, an uncaught throw, or the sandbox never awaited it. The
|
|
1271
|
-
* handler ran (or is still running); its answer is discarded. On a run
|
|
1272
|
-
* with pending `waitUntil` work the Result's records are a snapshot, so a
|
|
1273
|
-
* call still in flight shows here even if it settles during the epilogue.
|
|
1274
|
-
* - `dropped`: the run's signal was already aborted when the call reached
|
|
1275
|
-
* the host, so the handler was never invoked.
|
|
1276
|
-
*/
|
|
1262
|
+
* Why a bridge call did not succeed ({@link BridgeCallEntry} with
|
|
1263
|
+
* `ok: false`).
|
|
1264
|
+
*
|
|
1265
|
+
* - `blocked`: refused inside the runtime (`maxBridgeCalls`, payload over
|
|
1266
|
+
* `maxBridgeCallBytes`, function argument, invalid import handle, failed
|
|
1267
|
+
* write). Never reached the host; the sandbox promise rejected.
|
|
1268
|
+
* - `error`: the host answered with an error — the handler threw or
|
|
1269
|
+
* rejected, the host could not decode the arguments, or the return value
|
|
1270
|
+
* could not be serialized. The sandbox promise rejected.
|
|
1271
|
+
* - `unanswered`: sent, but no answer by the time these records were taken —
|
|
1272
|
+
* abort, a limit, an uncaught throw, or the sandbox never awaited it. The
|
|
1273
|
+
* handler ran (or is still running); its answer is discarded. On a run
|
|
1274
|
+
* with pending `waitUntil` work the Result's records are a snapshot, so a
|
|
1275
|
+
* call still in flight shows here even if it settles during the epilogue.
|
|
1276
|
+
* - `dropped`: the run's signal was already aborted when the call reached
|
|
1277
|
+
* the host, so the handler was never invoked.
|
|
1278
|
+
*/
|
|
1277
1279
|
type BridgeCallFailureReason = "blocked" | "error" | "unanswered" | "dropped";
|
|
1278
1280
|
/**
|
|
1279
|
-
* Fields shared by every {@link BridgeCallEntry}.
|
|
1280
|
-
*/
|
|
1281
|
+
* Fields shared by every {@link BridgeCallEntry}.
|
|
1282
|
+
*/
|
|
1281
1283
|
interface BridgeCallEntryBase {
|
|
1282
1284
|
/**
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1285
|
+
* The name sandbox code called. Plain globals appear as-is (`fetch`),
|
|
1286
|
+
* shimmed globals under their public name (not the private stub), and
|
|
1287
|
+
* host-module import leaves as `<specifier>.<path>` (e.g.
|
|
1288
|
+
* `tools:search.query`).
|
|
1289
|
+
*/
|
|
1288
1290
|
name: string;
|
|
1289
1291
|
/**
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1292
|
+
* Offset from run start to the attempt, in milliseconds — same clock as
|
|
1293
|
+
* the result's `durationMs`, so entries line up on a run timeline.
|
|
1294
|
+
*/
|
|
1293
1295
|
startMs: number;
|
|
1294
1296
|
/**
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1297
|
+
* Round-trip time the sandbox waited for the response (host handler + IPC),
|
|
1298
|
+
* in milliseconds. For `unanswered` and `dropped` calls: the time until
|
|
1299
|
+
* the run ended. `0` for blocked attempts.
|
|
1300
|
+
*/
|
|
1299
1301
|
durationMs: number;
|
|
1300
1302
|
/**
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1303
|
+
* Serialized size of the call payload (callId + target + arguments) in
|
|
1304
|
+
* bytes — the same size `maxBridgeCallBytes` is enforced against. `0` for
|
|
1305
|
+
* attempts blocked before argument serialization.
|
|
1306
|
+
*/
|
|
1305
1307
|
argBytes: number;
|
|
1306
1308
|
/**
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1309
|
+
* Serialized size of the successful response value in bytes. `0` when the
|
|
1310
|
+
* handler failed or the call never settled.
|
|
1311
|
+
*/
|
|
1310
1312
|
responseBytes: number;
|
|
1311
1313
|
}
|
|
1312
1314
|
/**
|
|
1313
|
-
* Outcome of a run's `waitUntil` grace phase, delivered through
|
|
1314
|
-
* {@link RunSuccess.waitUntil} / {@link CallSuccess.waitUntil}. The promise
|
|
1315
|
-
* always **resolves** (never rejects), so ignoring it — the intended common
|
|
1316
|
-
* case — produces no unhandled-rejection noise.
|
|
1317
|
-
*/
|
|
1315
|
+
* Outcome of a run's `waitUntil` grace phase, delivered through
|
|
1316
|
+
* {@link RunSuccess.waitUntil} / {@link CallSuccess.waitUntil}. The promise
|
|
1317
|
+
* always **resolves** (never rejects), so ignoring it — the intended common
|
|
1318
|
+
* case — produces no unhandled-rejection noise.
|
|
1319
|
+
*/
|
|
1318
1320
|
interface WaitUntilResult {
|
|
1319
1321
|
/**
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1322
|
+
* `settled`: every registered promise finished within the grace budget.
|
|
1323
|
+
* `truncated`: the budget (or a host abort) cut the work short.
|
|
1324
|
+
* `failed`: at least one registered promise rejected — a normal outcome,
|
|
1325
|
+
* the code finished via its error path; the rest were still driven.
|
|
1326
|
+
*/
|
|
1325
1327
|
status: "settled" | "truncated" | "failed";
|
|
1326
1328
|
/**
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
+
* Complete wall time of the grace phase (after the result shipped), ms.
|
|
1330
|
+
*/
|
|
1329
1331
|
durationMs: number;
|
|
1330
1332
|
/**
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1333
|
+
* The grace phase's own logic time — same rule as
|
|
1334
|
+
* {@link RunSuccess.wallTimeMs}, metered separately from the run's.
|
|
1335
|
+
*/
|
|
1334
1336
|
wallTimeMs: number;
|
|
1335
1337
|
/**
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
+
* CPU burned on the instance thread during the grace phase, ms.
|
|
1339
|
+
*/
|
|
1338
1340
|
cpuTimeMs: number;
|
|
1339
1341
|
/**
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1342
|
+
* Console lines written during the grace phase — the run's own lines
|
|
1343
|
+
* arrived on the result itself.
|
|
1344
|
+
*/
|
|
1343
1345
|
stdout: string[];
|
|
1344
1346
|
stderr: string[];
|
|
1345
1347
|
/**
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
+
* Bridge calls attempted during the grace phase.
|
|
1349
|
+
*/
|
|
1348
1350
|
bridgeCalls: BridgeCallEntry[];
|
|
1349
1351
|
/**
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1352
|
+
* The first rejection when `status === 'failed'`, or the fatal error that
|
|
1353
|
+
* ended a truncated phase, when one exists.
|
|
1354
|
+
*/
|
|
1353
1355
|
error?: {
|
|
1354
1356
|
name: string;
|
|
1355
1357
|
message: string;
|
|
1356
1358
|
};
|
|
1357
1359
|
}
|
|
1358
1360
|
/**
|
|
1359
|
-
* `status` is the primary discriminant across all three outcomes; `ok` is kept
|
|
1360
|
-
* as a convenience alias for `status === 'completed'`, so the common guard
|
|
1361
|
-
* stays `if (result.ok)`. Reach for `switch (result.status)` when a deliberate
|
|
1362
|
-
* abort must be told apart from a genuine failure.
|
|
1363
|
-
*/
|
|
1361
|
+
* `status` is the primary discriminant across all three outcomes; `ok` is kept
|
|
1362
|
+
* as a convenience alias for `status === 'completed'`, so the common guard
|
|
1363
|
+
* stays `if (result.ok)`. Reach for `switch (result.status)` when a deliberate
|
|
1364
|
+
* abort must be told apart from a genuine failure.
|
|
1365
|
+
*/
|
|
1364
1366
|
interface RunSuccess {
|
|
1365
1367
|
status: "completed";
|
|
1366
1368
|
ok: true;
|
|
1367
1369
|
exports: SandboxExports;
|
|
1368
1370
|
/**
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1371
|
+
* Export names absent from `exports` because their value cannot cross the
|
|
1372
|
+
* boundary — a function (`export default { fetch }`), a Promise, or a value
|
|
1373
|
+
* whose serialization failed. Skipping is never fatal; the names are
|
|
1374
|
+
* reported here so nothing is silently hidden.
|
|
1375
|
+
*
|
|
1376
|
+
* A Promise is skipped **whatever its state**: the export path never awaits,
|
|
1377
|
+
* so `export default Promise.resolve(42)` is dropped exactly like a pending
|
|
1378
|
+
* one. Export the value instead — `export default await something()` — which
|
|
1379
|
+
* works because the module's own evaluation settles before the runtime reads
|
|
1380
|
+
* the namespace. `prefix.call()` is the opposite: it awaits a returned
|
|
1381
|
+
* Promise and resolves to its value.
|
|
1382
|
+
*/
|
|
1381
1383
|
skippedExports: string[];
|
|
1382
1384
|
/**
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1385
|
+
* Lines this run wrote to `console.log` / `.debug` / `.info`, capped by
|
|
1386
|
+
* `limits.maxStdoutBytes`. Prefix code shares the same console: when a run
|
|
1387
|
+
* cold-starts a warm instance, what the prefix logged while evaluating
|
|
1388
|
+
* arrives here once, on that run's result, ahead of the run's own lines.
|
|
1389
|
+
*
|
|
1390
|
+
* Only `log`/`debug`/`info`/`warn`/`error` are captured. The rest of the
|
|
1391
|
+
* sandbox's `console` is callable but emits nothing — see
|
|
1392
|
+
* `docs/conformance.md`.
|
|
1393
|
+
*/
|
|
1392
1394
|
stdout: string[];
|
|
1393
1395
|
/**
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
+
* Same as {@link RunSuccess.stdout}, for `console.warn` / `.error`.
|
|
1397
|
+
*/
|
|
1396
1398
|
stderr: string[];
|
|
1397
1399
|
/**
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1400
|
+
* Complete wall-clock time of the run (from when it starts executing to
|
|
1401
|
+
* the processed conclusion), measured in the runtime — async waits, engine
|
|
1402
|
+
* time for co-resident runs, machinery: everything. May exceed the
|
|
1403
|
+
* `wallTimeMs` limit, which counts {@link wallTimeMs} instead. Waiting for
|
|
1404
|
+
* a free CPU before the run starts is in none of the three clocks.
|
|
1405
|
+
* Milliseconds with microsecond resolution.
|
|
1406
|
+
*/
|
|
1404
1407
|
durationMs: number;
|
|
1405
1408
|
/**
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1409
|
+
* The run's own logic time: execution plus async waits, each wait counted
|
|
1410
|
+
* until its answer arrived — engine time is excluded and visible as
|
|
1411
|
+
* `durationMs - wallTimeMs`. This is the number `limits.wallTimeMs` caps.
|
|
1412
|
+
* `cpuTimeMs ≤ wallTimeMs ≤ durationMs`.
|
|
1413
|
+
*/
|
|
1411
1414
|
wallTimeMs: number;
|
|
1412
1415
|
/**
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1416
|
+
* CPU burned on the run's own thread — time waiting on host bridge calls,
|
|
1417
|
+
* and time descheduled under contention, are excluded. Milliseconds with
|
|
1418
|
+
* microsecond resolution.
|
|
1419
|
+
*/
|
|
1417
1420
|
cpuTimeMs: number;
|
|
1418
1421
|
/**
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1422
|
+
* How long this run waited on the host for a free run slot
|
|
1423
|
+
* (`maxConcurrentRuns`), before its frame was sent. Absent when a slot was
|
|
1424
|
+
* free and the run went straight out. Milliseconds with microsecond
|
|
1425
|
+
* resolution.
|
|
1426
|
+
*
|
|
1427
|
+
* The wait sits outside the other three clocks, which the runtime measures
|
|
1428
|
+
* from when the run starts executing — end to end, a caller sees this plus
|
|
1429
|
+
* {@link durationMs} plus frame transit plus the runtime's own wait for a
|
|
1430
|
+
* free CPU before the run starts. That CPU wait is not counted in
|
|
1431
|
+
* `queueWaitMs`, `durationMs`, `wallTimeMs` or `cpuTimeMs`; later CPU waits
|
|
1432
|
+
* are inside {@link durationMs}. Rising `queueWaitMs` is the sign that
|
|
1433
|
+
* admission, not the code, is the bottleneck.
|
|
1434
|
+
*/
|
|
1429
1435
|
queueWaitMs?: number;
|
|
1430
1436
|
/**
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1437
|
+
* One entry per bridge call the sandbox attempted, in attempt order —
|
|
1438
|
+
* including attempts that never reached the host (see
|
|
1439
|
+
* {@link BridgeCallFailureReason}). Recorded by the Rust runtime.
|
|
1440
|
+
*/
|
|
1435
1441
|
bridgeCalls: BridgeCallEntry[];
|
|
1436
1442
|
/**
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1443
|
+
* Memory of the isolate that served this run, in bytes: the JS heap plus
|
|
1444
|
+
* buffer memory (ArrayBuffers and typed arrays, including memory grown with
|
|
1445
|
+
* `resize()`, and stream chunks). Read once when the run ends, on prefix
|
|
1446
|
+
* and one-off runs alike; on a prefix it feeds eviction, since warm
|
|
1447
|
+
* instances keep their memory between calls. It includes garbage not yet
|
|
1448
|
+
* collected (a warm instance pushed over its soft line by buffers, with
|
|
1449
|
+
* objects alone under it, is collected once first). A run that sends its
|
|
1450
|
+
* result early (`waitUntil` work, open stream bodies) reports a reading
|
|
1451
|
+
* taken at that moment, before any run-end collection, so it can differ
|
|
1452
|
+
* from the figure the soft line uses. `0` on a prefix run whose instance
|
|
1453
|
+
* could not be measured (failed warm-up, dead instance). `undefined` when
|
|
1454
|
+
* the run was answered without a reading: refused before it started, or a
|
|
1455
|
+
* result too large to send.
|
|
1456
|
+
*/
|
|
1457
|
+
memoryBytes?: number;
|
|
1458
|
+
/**
|
|
1459
|
+
* Present when the run registered background work with `waitUntil`:
|
|
1460
|
+
* the value in this result was delivered early, the work keeps running
|
|
1461
|
+
* runtime-side, and this promise resolves with the grace phase's outcome
|
|
1462
|
+
* once it ends. `undefined` for every run that registered nothing. Safe to
|
|
1463
|
+
* ignore — it never rejects.
|
|
1464
|
+
*/
|
|
1450
1465
|
waitUntil?: Promise<WaitUntilResult>;
|
|
1451
1466
|
}
|
|
1452
1467
|
interface RunFailure {
|
|
@@ -1457,31 +1472,40 @@ interface RunFailure {
|
|
|
1457
1472
|
stderr: string[];
|
|
1458
1473
|
durationMs: number;
|
|
1459
1474
|
/**
|
|
1460
|
-
|
|
1461
|
-
|
|
1475
|
+
* See {@link RunSuccess.wallTimeMs}.
|
|
1476
|
+
*/
|
|
1462
1477
|
wallTimeMs: number;
|
|
1463
1478
|
cpuTimeMs: number;
|
|
1464
1479
|
/**
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1480
|
+
* See {@link RunSuccess.queueWaitMs}. Absent on a run that failed before
|
|
1481
|
+
* admission — `ERR_QUEUE_FULL` is refused at the bound without waiting.
|
|
1482
|
+
*/
|
|
1468
1483
|
queueWaitMs?: number;
|
|
1469
1484
|
bridgeCalls: BridgeCallEntry[];
|
|
1470
1485
|
/**
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1486
|
+
* Memory of the isolate that served this run, in bytes: the JS heap plus
|
|
1487
|
+
* buffer memory (ArrayBuffers and typed arrays, including memory grown with
|
|
1488
|
+
* `resize()`, and stream chunks). Read once when the run ends, on prefix
|
|
1489
|
+
* and one-off runs alike; on a prefix it feeds eviction, since warm
|
|
1490
|
+
* instances keep their memory between calls. It includes garbage not yet
|
|
1491
|
+
* collected (a warm instance pushed over its soft line by buffers, with
|
|
1492
|
+
* objects alone under it, is collected once first). A run that sends its
|
|
1493
|
+
* result early (`waitUntil` work, open stream bodies) reports a reading
|
|
1494
|
+
* taken at that moment, before any run-end collection, so it can differ
|
|
1495
|
+
* from the figure the soft line uses. `0` on a prefix run whose instance
|
|
1496
|
+
* could not be measured (failed warm-up, dead instance). `undefined` when
|
|
1497
|
+
* the run was answered without a reading: refused before it started, or a
|
|
1498
|
+
* result too large to send.
|
|
1499
|
+
*/
|
|
1500
|
+
memoryBytes?: number;
|
|
1477
1501
|
}
|
|
1478
1502
|
/**
|
|
1479
|
-
* The run was stopped by an abort — a pre-aborted or mid-run `signal` passed to
|
|
1480
|
-
* `run()`, or a host handler that aborted the run. A deliberate outcome, not a
|
|
1481
|
-
* crash: `error` is retained (its `code` is always `ERR_ABORTED`) so existing
|
|
1482
|
-
* `!result.ok` / `error.code` checks keep working, and `reason` carries the
|
|
1483
|
-
* value handed to `abort(reason)`.
|
|
1484
|
-
*/
|
|
1503
|
+
* The run was stopped by an abort — a pre-aborted or mid-run `signal` passed to
|
|
1504
|
+
* `run()`, or a host handler that aborted the run. A deliberate outcome, not a
|
|
1505
|
+
* crash: `error` is retained (its `code` is always `ERR_ABORTED`) so existing
|
|
1506
|
+
* `!result.ok` / `error.code` checks keep working, and `reason` carries the
|
|
1507
|
+
* value handed to `abort(reason)`.
|
|
1508
|
+
*/
|
|
1485
1509
|
interface RunAborted {
|
|
1486
1510
|
status: "aborted";
|
|
1487
1511
|
ok: false;
|
|
@@ -1491,29 +1515,29 @@ interface RunAborted {
|
|
|
1491
1515
|
stderr: string[];
|
|
1492
1516
|
durationMs: number;
|
|
1493
1517
|
/**
|
|
1494
|
-
|
|
1495
|
-
|
|
1518
|
+
* See {@link RunSuccess.wallTimeMs}.
|
|
1519
|
+
*/
|
|
1496
1520
|
wallTimeMs: number;
|
|
1497
1521
|
cpuTimeMs: number;
|
|
1498
1522
|
/**
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1523
|
+
* Bridge calls recorded before the abort landed. Populated when the runtime
|
|
1524
|
+
* terminates gracefully — the common case, where the run was suspended
|
|
1525
|
+
* awaiting a bridge response. Empty (and `durationMs`/`cpuTimeMs` zero) only
|
|
1526
|
+
* when graceful termination falls back to socket teardown — e.g. a run
|
|
1527
|
+
* aborted while stuck in a tight synchronous loop, or a signal already
|
|
1528
|
+
* aborted at run entry.
|
|
1529
|
+
*/
|
|
1506
1530
|
bridgeCalls: BridgeCallEntry[];
|
|
1507
1531
|
}
|
|
1508
1532
|
type SandboxExports = {
|
|
1509
1533
|
/**
|
|
1510
|
-
|
|
1511
|
-
|
|
1534
|
+
* Value of `export default` in user code, or `undefined`.
|
|
1535
|
+
*/
|
|
1512
1536
|
default: unknown;
|
|
1513
1537
|
} & {
|
|
1514
1538
|
/**
|
|
1515
|
-
|
|
1516
|
-
|
|
1539
|
+
* Named exports live directly on the export object.
|
|
1540
|
+
*/
|
|
1517
1541
|
[name: string]: unknown;
|
|
1518
1542
|
};
|
|
1519
1543
|
interface RunError {
|
|
@@ -1521,69 +1545,66 @@ interface RunError {
|
|
|
1521
1545
|
name: string;
|
|
1522
1546
|
message: string;
|
|
1523
1547
|
/**
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1548
|
+
* Stack trace of the thrown error. `name`/`message`/`stack` are reserved:
|
|
1549
|
+
* they are always read from the error's dedicated properties and never
|
|
1550
|
+
* appear inside `fields`, so a thrown object cannot spoof them there.
|
|
1551
|
+
*/
|
|
1528
1552
|
stack?: string;
|
|
1529
1553
|
/**
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1554
|
+
* All other own enumerable properties of the thrown error (everything
|
|
1555
|
+
* except name/message/stack), e.g. `status`, `reason`, or a `data` payload
|
|
1556
|
+
* from a custom error class — nested here so nothing can collide with
|
|
1557
|
+
* `code`. Present for ERR_USER_CODE and ERR_HOST_BRIDGE when the thrown
|
|
1558
|
+
* value is an object with serializable own properties.
|
|
1559
|
+
*/
|
|
1536
1560
|
fields?: Record<string, unknown>;
|
|
1537
1561
|
/**
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1562
|
+
* Present only for `ERR_INSTANCE_RESET`: what interrupted the co-resident
|
|
1563
|
+
* run that took the shared instance down with it.
|
|
1564
|
+
*/
|
|
1541
1565
|
resetCause?: ResetCause;
|
|
1542
1566
|
/**
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1567
|
+
* Present only for `ERR_INSTANCE_RESET`: the wire run id of the run whose
|
|
1568
|
+
* interruption reset the instance — an opaque number, but stable across
|
|
1569
|
+
* the victims of one reset, so they can be correlated.
|
|
1570
|
+
*/
|
|
1547
1571
|
culpritRunId?: number;
|
|
1548
1572
|
/**
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1573
|
+
* `'prefix'` when the cold start this run paid for failed with one of the
|
|
1574
|
+
* prefix's own, repeatable errors (`ERR_USER_CODE`, `ERR_MODULE_NOT_FOUND`,
|
|
1575
|
+
* `ERR_IMPORT`, `ERR_PREFIX_BRIDGE_CALL`, `ERR_PREFIX_DID_NOT_SETTLE`), so
|
|
1576
|
+
* the failure is the prefix's, not the run code's. Absent for run-code
|
|
1577
|
+
* failures and for load-dependent cold-start failures (`ERR_WARMUP_LIMIT`,
|
|
1578
|
+
* `ERR_MEMORY_LIMIT`), which do not break the prefix. The same run moves
|
|
1579
|
+
* {@link Prefix.status} to `'broken'` (DESIGN.md §16.4).
|
|
1580
|
+
*/
|
|
1557
1581
|
stage?: "prefix";
|
|
1558
1582
|
}
|
|
1559
1583
|
/**
|
|
1560
|
-
* Why a shared instance was reset under an innocent victim — see
|
|
1561
|
-
* `RunError.resetCause`. Every cause describes what the culprit run
|
|
1562
|
-
* (`culpritRunId`) did, never this run.
|
|
1563
|
-
*
|
|
1564
|
-
* `'unsupported'`: the culprit used something the runtime does not support
|
|
1565
|
-
* (today: dynamic `import()`), so the runtime ended it.
|
|
1566
|
-
*/
|
|
1584
|
+
* Why a shared instance was reset under an innocent victim — see
|
|
1585
|
+
* `RunError.resetCause`. Every cause describes what the culprit run
|
|
1586
|
+
* (`culpritRunId`) did, never this run.
|
|
1587
|
+
*
|
|
1588
|
+
* `'unsupported'`: the culprit used something the runtime does not support
|
|
1589
|
+
* (today: dynamic `import()`), so the runtime ended it.
|
|
1590
|
+
*/
|
|
1567
1591
|
type ResetCause = "cpu" | "memory" | "wall" | "abort" | "internal" | "unsupported";
|
|
1568
1592
|
/**
|
|
1569
|
-
* Why a run's options were refused before anything ran. `sandbox.run()`,
|
|
1570
|
-
* `prefix.run()`, `prefix.call()` and `prepare()` reject with an `Error`
|
|
1571
|
-
* whose `code` is one of these; a run never returns them as a result. The
|
|
1572
|
-
* client checks first and throws without a runtime round trip; the runtime
|
|
1573
|
-
* repeats every check (except `ERR_INVALID_REBIND`) and the client throws its
|
|
1574
|
-
* answer the same way. `ERR_PREFIX_DISPOSED`: the prefix was disposed, or the
|
|
1575
|
-
* runtime no longer holds it.
|
|
1576
|
-
*/
|
|
1593
|
+
* Why a run's options were refused before anything ran. `sandbox.run()`,
|
|
1594
|
+
* `prefix.run()`, `prefix.call()` and `prepare()` reject with an `Error`
|
|
1595
|
+
* whose `code` is one of these; a run never returns them as a result. The
|
|
1596
|
+
* client checks first and throws without a runtime round trip; the runtime
|
|
1597
|
+
* repeats every check (except `ERR_INVALID_REBIND`) and the client throws its
|
|
1598
|
+
* answer the same way. `ERR_PREFIX_DISPOSED`: the prefix was disposed, or the
|
|
1599
|
+
* runtime no longer holds it.
|
|
1600
|
+
*/
|
|
1577
1601
|
type SetupErrorCode = "ERR_PREFIX_DISPOSED" | "ERR_PREFIX_BROKEN" | "ERR_UNDECLARED_BINDING" | "ERR_RESERVED_NAME" | "ERR_FROZEN_BINDING" | "ERR_INVALID_REBIND";
|
|
1578
1602
|
/**
|
|
1579
|
-
* Why a run failed, in a `{ ok: false }` result. Setup refusals are thrown
|
|
1580
|
-
* instead and carry a {@link SetupErrorCode}. `readExports()` rejects on
|
|
1581
|
-
* any failure; `prepare()` never runs anything, so a broken prefix reports
|
|
1582
|
-
* these on its first run.
|
|
1583
|
-
*/
|
|
1584
|
-
type RunErrorCode = "ERR_USER_CODE" | "ERR_MEMORY_LIMIT" | "ERR_CPU_TIMEOUT" | "ERR_WALL_TIMEOUT" | "ERR_ABORTED" | "ERR_MODULE_NOT_FOUND" | "ERR_IMPORT" | "ERR_FUNCTION_ARGUMENT_NOT_SUPPORTED" | "ERR_EXPORT_NOT_SERIALIZABLE" | "ERR_TYPE_NOT_SERIALIZABLE" | "ERR_EXPORT_TOO_LARGE" | "ERR_CALL_TARGET_NOT_FOUND" | "ERR_HOST_BRIDGE" | "ERR_BRIDGE_PAYLOAD_TOO_LARGE" | "ERR_BRIDGE_CALL_LIMIT_EXCEEDED" | "ERR_INSTANCE_RESET" | "ERR_CAPACITY_MEMORY" | "ERR_QUEUE_FULL" | "ERR_PREFIX_DID_NOT_SETTLE" | "ERR_PREFIX_BRIDGE_CALL" | "ERR_WARMUP_LIMIT" | "ERR_PROTOCOL_DESYNC" | "ERR_INTERNAL";
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
declare function createSandbox(options?: SandboxOptions): Promise<Sandbox>;
|
|
1588
|
-
//#endregion
|
|
1589
|
-
export { type BridgeCallEntry, type BridgeCallEntryBase, type BridgeCallFailureReason, type BridgeGlobal, type BridgeWithShim, type CallResult, type CallSuccess, type CallTarget, type CreateSandbox, type DataGlobal, type GlobalOptions, type HostExportData, type HostExportFunction, type HostGlobalValue, type HostGlobals, type HostModuleObject, type HostModuleValue, type ImportValue, type Imports, type MemoryLimit, type OneOffResourceLimits, type PrecompileOptions, type Prefix, type PrefixCallOptions, type PrefixRunOptions, type PrefixStatus, type ReadExportsOptions, type ReadExportsResult, type RebindGlobals, type RebindHostModule, type RebindImports, type RebindValue, type ResetCause, type ResourceLimits, type RunCallOptions, type RunError, type RunErrorCode, type RunFailure, type RunOptions, type RunResult, type RunSuccess, type Sandbox, type SandboxExports, type SandboxOptions, type SandboxStats, type SetupErrorCode, type StringGlobal, type WaitUntilResult, createSandbox };
|
|
1603
|
+
* Why a run failed, in a `{ ok: false }` result. Setup refusals are thrown
|
|
1604
|
+
* instead and carry a {@link SetupErrorCode}. `readExports()` rejects on
|
|
1605
|
+
* any failure; `prepare()` never runs anything, so a broken prefix reports
|
|
1606
|
+
* these on its first run.
|
|
1607
|
+
*/
|
|
1608
|
+
type RunErrorCode = "ERR_USER_CODE" | "ERR_MEMORY_LIMIT" | "ERR_CPU_TIMEOUT" | "ERR_WALL_TIMEOUT" | "ERR_ABORTED" | "ERR_MODULE_NOT_FOUND" | "ERR_IMPORT" | "ERR_FUNCTION_ARGUMENT_NOT_SUPPORTED" | "ERR_EXPORT_NOT_SERIALIZABLE" | "ERR_TYPE_NOT_SERIALIZABLE" | "ERR_EXPORT_TOO_LARGE" | "ERR_CALL_TARGET_NOT_FOUND" | "ERR_HOST_BRIDGE" | "ERR_BRIDGE_PAYLOAD_TOO_LARGE" | "ERR_BRIDGE_CALL_LIMIT_EXCEEDED" | "ERR_INSTANCE_RESET" | "ERR_CAPACITY_MEMORY" | "ERR_CAPACITY_CPU" | "ERR_QUEUE_FULL" | "ERR_PREFIX_DID_NOT_SETTLE" | "ERR_PREFIX_BRIDGE_CALL" | "ERR_WARMUP_LIMIT" | "ERR_PROTOCOL_DESYNC" | "ERR_INTERNAL";
|
|
1609
|
+
export declare function createSandbox(options?: SandboxOptions): Promise<Sandbox>;
|
|
1610
|
+
export type { BridgeCallEntry, BridgeCallEntryBase, BridgeCallFailureReason, BridgeGlobal, BridgeWithShim, CallResult, CallSuccess, CallTarget, CreateSandbox, DataGlobal, GlobalOptions, HostExportData, HostExportFunction, HostGlobalValue, HostGlobals, HostModuleObject, HostModuleValue, ImportValue, Imports, MemoryLimit, OneOffResourceLimits, PrecompileOptions, Prefix, PrefixCallOptions, PrefixRunOptions, PrefixStatus, ReadExportsOptions, ReadExportsResult, RebindGlobals, RebindHostModule, RebindImports, RebindValue, ResetCause, ResourceLimits, RunCallOptions, RunError, RunErrorCode, RunFailure, RunOptions, RunResult, RunSuccess, Sandbox, SandboxExports, SandboxOptions, SandboxStats, SetupErrorCode, StringGlobal, WaitUntilResult };
|