@iso4/sandbox 0.10.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 +27 -15
- package/dist/index.d.mts +1202 -1218
- package/dist/index.mjs +57 -59
- 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 +9 -9
package/dist/index.d.mts
CHANGED
|
@@ -1,706 +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 memory lines a REUSED isolate is held to, when the managed default
|
|
16
|
-
* is not what you want. One-off `run()` isolates are never reused and take a
|
|
17
|
-
* plain `memoryMb` number with no soft line.
|
|
18
|
-
*
|
|
19
|
-
* - `hard` **terminates**: one cap for the JS heap and buffer memory
|
|
20
|
-
* (ArrayBuffers and typed arrays, including `resize()` growth, and stream
|
|
21
|
-
* chunks) together. Crossing it kills whatever JS is
|
|
22
|
-
* executing, taints the instance, and fails its co-resident runs with
|
|
23
|
-
* `ERR_INSTANCE_RESET`. This is the ceiling the container reserves against
|
|
24
|
-
* its admission line.
|
|
25
|
-
* - `soft` **retires**: a warm instance whose memory (JS heap plus buffers)
|
|
26
|
-
* sits above it at two consecutive finishes takes no further runs and is
|
|
27
|
-
* dropped once the runs it is already serving complete. No run fails and nothing is terminated —
|
|
28
|
-
* it is how a prefix that accumulates memory across reuse gets replaced
|
|
29
|
-
* instead of eventually killing a run. Omit it for a hard line only.
|
|
30
|
-
*
|
|
31
|
-
* A plain `memoryMb: number` is the managed form of this: the number is the
|
|
32
|
-
* soft line and the hard line sits a headroom band above it (`+sqrt(8 × n)`
|
|
33
|
-
* MB, so 128 → 160). Write the object when you want the exact numbers.
|
|
34
|
-
*/
|
|
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
|
+
*/
|
|
35
34
|
interface MemoryLimit {
|
|
36
35
|
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
+
*/
|
|
41
40
|
soft?: number;
|
|
42
41
|
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
+
*/
|
|
48
47
|
hard: number;
|
|
49
48
|
}
|
|
50
49
|
/**
|
|
51
|
-
* Limits for a one-off `sandbox.run()`, which always gets a fresh isolate —
|
|
52
|
-
* so unlike prefix runs (whose isolates are shared and reused, cap set at
|
|
53
|
-
* `prepare({ memoryMb })` or the sandbox default) it may cap its own heap.
|
|
54
|
-
*/
|
|
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
|
+
*/
|
|
55
54
|
interface OneOffResourceLimits extends ResourceLimits {
|
|
56
55
|
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
+
*/
|
|
70
69
|
memoryMb?: number;
|
|
71
70
|
}
|
|
72
71
|
/**
|
|
73
|
-
* Per-run resource caps. Every field is optional at the call site
|
|
74
|
-
* (`limits?: ResourceLimits`): pass only the limits you want to
|
|
75
|
-
* override. The client sends just those over the wire and the **runtime owns
|
|
76
|
-
* the defaults** — it fills in any field you leave unset. The `@default` values
|
|
77
|
-
* documented below mirror the runtime's defaults (defined in
|
|
78
|
-
* `native/v8-runtime/src/ipc.rs`, the source of truth); if they ever drift, the
|
|
79
|
-
* runtime wins.
|
|
80
|
-
*
|
|
81
|
-
* A field set to `0` is not the same as leaving it unset: `0` explicitly
|
|
82
|
-
* disables that limit, whereas unset yields the runtime default.
|
|
83
|
-
*
|
|
84
|
-
* Every field must be an integer from `0` to `2³² − 1`. Anything else
|
|
85
|
-
* (negative, fractional, `NaN`, `Infinity`, a non-number) rejects the call
|
|
86
|
-
* with a `TypeError` before anything runs.
|
|
87
|
-
*/
|
|
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
|
+
*/
|
|
88
87
|
interface ResourceLimits {
|
|
89
88
|
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
+
*/
|
|
97
96
|
cpuTimeMs?: number;
|
|
98
97
|
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
+
*/
|
|
103
102
|
wallTimeMs?: number;
|
|
104
103
|
/**
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
+
*/
|
|
122
121
|
maxBridgeCallBytes?: number;
|
|
123
122
|
/**
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
+
*/
|
|
130
129
|
maxExportBytes?: number;
|
|
131
130
|
/**
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
+
*/
|
|
137
136
|
maxStdoutBytes?: number;
|
|
138
137
|
/**
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
+
*/
|
|
144
143
|
maxStderrBytes?: number;
|
|
145
144
|
/**
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
+
*/
|
|
159
158
|
maxBridgeCalls?: number;
|
|
160
159
|
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
+
*/
|
|
168
167
|
graceMs?: number;
|
|
169
168
|
}
|
|
170
169
|
/**
|
|
171
|
-
* Host-provided globals. Any string not reserved by the runtime is permitted
|
|
172
|
-
* as a name. Each value becomes a property of the sandbox's `globalThis`.
|
|
173
|
-
*
|
|
174
|
-
* Reserved names (rejected with `ERR_RESERVED_NAME`): `console`,
|
|
175
|
-
* `waitUntil`, `setTimeout`, `clearTimeout`, `Headers`, `Request`,
|
|
176
|
-
* `Response`, `TextEncoder`, `TextDecoder`, `URL`, `URLSearchParams`, plus
|
|
177
|
-
* `NaN`, `undefined`, `Infinity` and `__proto__`. The first are owned by the
|
|
178
|
-
* runtime; shadowing `Response` would silently break serialization. `NaN`,
|
|
179
|
-
* `undefined` and `Infinity` are non-writable on the global object, so a host
|
|
180
|
-
* global could never take effect; assigning `__proto__` replaces a prototype
|
|
181
|
-
* instead of creating a property.
|
|
182
|
-
*
|
|
183
|
-
* A name that is not a JavaScript identifier (`my-tool`, `user.lookup`), or
|
|
184
|
-
* is in `RESERVED_WORDS` other than `eval` and `arguments`, is
|
|
185
|
-
* reachable only as `globalThis[name]`, not by bare name. A name that is
|
|
186
|
-
* not a well-formed string (`'y\uD800'`) throws a `TypeError` before
|
|
187
|
-
* anything is sent. Names starting with `__iso4_` are iso4's internal
|
|
188
|
-
* namespace (the dispatcher, shim handlers): a host global there may be
|
|
189
|
-
* silently overwritten or dropped, and `isValidGlobalName` does not flag it.
|
|
190
|
-
*
|
|
191
|
-
* `SANDBOX_GLOBALS` holds every name a fresh sandbox defines, V8 built-ins
|
|
192
|
-
* included. Both sets and `isValidGlobalName`, which checks a name before
|
|
193
|
-
* `prepare()`, come from `@iso4/sandbox/utils`.
|
|
194
|
-
*
|
|
195
|
-
* Common usage:
|
|
196
|
-
* ```ts
|
|
197
|
-
* globals: {
|
|
198
|
-
* fetch: myFetchHandler, // \@iso4/fetch provides createSafeFetch()
|
|
199
|
-
* myTool: async (arg) => doSomething(arg),
|
|
200
|
-
* }
|
|
201
|
-
* ```
|
|
202
|
-
*
|
|
203
|
-
* `fetch` is not special — it goes through the same bridge path as any
|
|
204
|
-
* other entry.
|
|
205
|
-
*
|
|
206
|
-
* Three value shapes are supported — see `HostGlobalValue`.
|
|
207
|
-
*/
|
|
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
|
+
*/
|
|
208
207
|
type HostGlobals = Record<string, HostGlobalValue>;
|
|
209
208
|
/**
|
|
210
|
-
* The value types accepted for a named global.
|
|
211
|
-
*
|
|
212
|
-
* - `HostExportFunction` — bridge call: every sandbox invocation dispatches
|
|
213
|
-
* to the host handler. Shorthand for `BridgeGlobal` with the defaults.
|
|
214
|
-
* - `string` — a JS expression the runtime evaluates once and installs as
|
|
215
|
-
* `globalThis.<name>`. Use for pure sandbox utilities that need no host
|
|
216
|
-
* involvement. The expression is evaluated by Rust as its own script, so it
|
|
217
|
-
* never shifts the line numbers of user code. Shorthand for `StringGlobal`
|
|
218
|
-
* with the defaults.
|
|
219
|
-
* - `BridgeGlobal` / `StringGlobal` — the same two as object forms, for when
|
|
220
|
-
* an option (`enumerable`) is needed.
|
|
221
|
-
* - `DataGlobal` — a plain constant carried as data and installed natively;
|
|
222
|
-
* no code text, no host involvement.
|
|
223
|
-
* - `BridgeWithShim` — bridge call + in-sandbox result wrapper. The handler
|
|
224
|
-
* can be rebound per `prefix.run()`; the shim is part of the prefix state.
|
|
225
|
-
*
|
|
226
|
-
* Every **object** form accepts `enumerable?: boolean` (default `true`).
|
|
227
|
-
* The bare-function and bare-string shorthands are always enumerable.
|
|
228
|
-
*/
|
|
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
|
+
*/
|
|
229
228
|
type HostGlobalValue = HostExportFunction | string | BridgeWithShim<any> | DataGlobal | BridgeGlobal<any> | StringGlobal;
|
|
230
229
|
/**
|
|
231
|
-
* Shared option of every object-form global.
|
|
232
|
-
*
|
|
233
|
-
* `enumerable: false` keeps the name out of `for...in`, `Object.keys`, and
|
|
234
|
-
* spreads of `globalThis`, so enumeration-driven sandbox code (serializers,
|
|
235
|
-
* "copy the environment" patterns) never sweeps it up. This is hygiene, not
|
|
236
|
-
* secrecy: the property still appears in `Object.getOwnPropertyNames` (a
|
|
237
|
-
* language-level fact, identical in every engine), and code that knows the
|
|
238
|
-
* name can still call it. A capability that must be undiscoverable belongs in
|
|
239
|
-
* `imports`, which nothing can enumerate.
|
|
240
|
-
*/
|
|
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
|
+
*/
|
|
241
240
|
interface GlobalOptions {
|
|
242
241
|
/**
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
242
|
+
* Whether the installed global shows up in enumeration. Defaults to `true`,
|
|
243
|
+
* matching how browsers keep `fetch` enumerable on `window`.
|
|
244
|
+
*/
|
|
246
245
|
enumerable?: boolean;
|
|
247
246
|
}
|
|
248
247
|
/**
|
|
249
|
-
* Object form of the bare-function bridge global, for when options are
|
|
250
|
-
* needed. `handler` is exactly the function the shorthand would be.
|
|
251
|
-
*
|
|
252
|
-
* ```ts
|
|
253
|
-
* globals: { myTool: { kind: 'bridge', handler: async (q) => search(q), enumerable: false } }
|
|
254
|
-
* ```
|
|
255
|
-
*/
|
|
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
|
+
*/
|
|
256
255
|
interface BridgeGlobal<H extends HostExportFunction = HostExportFunction> extends GlobalOptions {
|
|
257
256
|
kind: "bridge";
|
|
258
257
|
/**
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
258
|
+
* Bridge handler — every sandbox invocation dispatches to it. Can be
|
|
259
|
+
* rebound per `prefix.run()` like a shorthand bridge global.
|
|
260
|
+
*/
|
|
262
261
|
handler: H;
|
|
263
262
|
}
|
|
264
263
|
/**
|
|
265
|
-
* Object form of the bare-string expression global, for when options are
|
|
266
|
-
* needed. `expr` is exactly the expression the shorthand would be.
|
|
267
|
-
*/
|
|
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
|
+
*/
|
|
268
267
|
interface StringGlobal extends GlobalOptions {
|
|
269
268
|
kind: "string";
|
|
270
269
|
/**
|
|
271
|
-
|
|
272
|
-
|
|
270
|
+
* JS expression the runtime evaluates once and installs under the name.
|
|
271
|
+
*/
|
|
273
272
|
expr: string;
|
|
274
273
|
}
|
|
275
274
|
/**
|
|
276
|
-
* A plain **constant** global — a data value installed directly on the sandbox
|
|
277
|
-
* global object, with no host involvement and no code text anywhere.
|
|
278
|
-
*
|
|
279
|
-
* The value crosses the wire as a V8 serialization blob and Rust materialises
|
|
280
|
-
* it natively with `blob::deserialize_value` (the same path host-module data
|
|
281
|
-
* leaves take):
|
|
282
|
-
*
|
|
283
|
-
* ```ts
|
|
284
|
-
* globals: { config: { kind: 'data', value: userConfig } }
|
|
285
|
-
* ```
|
|
286
|
-
*
|
|
287
|
-
* `value` accepts the same set as host-module data leaves (`HostExportData`):
|
|
288
|
-
* primitives, `bigint`, `string`, `Uint8Array`, and plain objects/arrays.
|
|
289
|
-
* Like string globals, a data global is a constant — it cannot be rebound per
|
|
290
|
-
* `prefix.run()`.
|
|
291
|
-
*/
|
|
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
|
+
*/
|
|
292
291
|
interface DataGlobal extends GlobalOptions {
|
|
293
292
|
kind: "data";
|
|
294
293
|
/**
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
+
*/
|
|
299
298
|
value: HostExportData | Request | Response | Headers;
|
|
300
299
|
}
|
|
301
300
|
/**
|
|
302
|
-
* A global that pairs a bridge handler with an in-sandbox shim that wraps
|
|
303
|
-
* its return value. Typical use: adding `.json()` / `.text()` convenience
|
|
304
|
-
* methods to a fetch response without extra bridge round-trips.
|
|
305
|
-
*
|
|
306
|
-
* The runtime installs this natively:
|
|
307
|
-
* 1. `handler` is registered as a bridge stub under the private dispatch
|
|
308
|
-
* name `__iso4_<name>_h`.
|
|
309
|
-
* 2. Rust evaluates the shim expression and builds the wrapper
|
|
310
|
-
* `async (...args) => await shim(await globalThis["__iso4_<name>_h"](...args))`,
|
|
311
|
-
* then sets it on the global object under `<name>` via the V8 API — the
|
|
312
|
-
* public name is a plain string passed to `object.set`, never code.
|
|
313
|
-
*
|
|
314
|
-
* The shim is a JS function expression `(result) => transformedResult` (sync
|
|
315
|
-
* or async). Sandbox built-ins (TextDecoder, JSON, …) are available in scope.
|
|
316
|
-
* The private stub `__iso4_<name>_h` stays on globalThis so the handler can
|
|
317
|
-
* be rebound per `prefix.run()` while the shim itself stays fixed.
|
|
318
|
-
*/
|
|
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
|
+
*/
|
|
319
318
|
/**
|
|
320
|
-
* `H` is the specific handler function type. Carried through the type system
|
|
321
|
-
* so `RebindGlobals` can enforce that any per-run replacement has the same
|
|
322
|
-
* return type, keeping it compatible with the shim that is already compiled
|
|
323
|
-
* into the prefix state.
|
|
324
|
-
*/
|
|
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
|
+
*/
|
|
325
324
|
interface BridgeWithShim<H extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown> extends GlobalOptions {
|
|
326
325
|
kind: "bridge-with-shim";
|
|
327
326
|
/**
|
|
328
|
-
|
|
329
|
-
|
|
327
|
+
* Bridge handler — registered as `__iso4_<name>_h` for rebinding.
|
|
328
|
+
*/
|
|
330
329
|
handler: H;
|
|
331
330
|
/**
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
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
|
+
*/
|
|
345
344
|
shim: string;
|
|
346
345
|
}
|
|
347
346
|
/**
|
|
348
|
-
* What can be supplied as a per-run override for a global declared at
|
|
349
|
-
* precompile time:
|
|
350
|
-
*
|
|
351
|
-
* - `BridgeWithShim<H>` global — only the handler `H` itself. The shim is
|
|
352
|
-
* fixed with the prefix; rebinding must preserve the return type so the
|
|
353
|
-
* shim keeps working.
|
|
354
|
-
* - `HostExportFunction` global — any compatible `HostExportFunction`.
|
|
355
|
-
* - `string` global — cannot be rebound (fixed at prepare() time).
|
|
356
|
-
*/
|
|
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
|
+
*/
|
|
357
356
|
type RebindValue<V extends HostGlobalValue> = V extends BridgeWithShim<infer H> ? H : V extends BridgeGlobal<infer H> ? H : V extends HostExportFunction ? V : never;
|
|
358
357
|
/**
|
|
359
|
-
* The per-run globals override map for a `Prefix<G>`.
|
|
360
|
-
*
|
|
361
|
-
* String-valued and `DataGlobal` (constant) globals are filtered out entirely
|
|
362
|
-
* (key remapping to `never`) rather than mapped to `?: never` — they cannot be
|
|
363
|
-
* rebound because their value is fixed at prepare() time, and they should
|
|
364
|
-
* not appear as valid keys at all.
|
|
365
|
-
*
|
|
366
|
-
* Each remaining key is optional and typed as `RebindValue<G[K]>`, which
|
|
367
|
-
* enforces the correct constraint per global kind.
|
|
368
|
-
*/
|
|
369
|
-
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]>; };
|
|
370
369
|
/**
|
|
371
|
-
* Top-level `imports` shape. Keys are import specifiers; values are either
|
|
372
|
-
* a source string or a host-module description object. A specifier that is
|
|
373
|
-
* not a well-formed string (`'x\uD800'`) throws a `TypeError` before
|
|
374
|
-
* anything is sent.
|
|
375
|
-
*
|
|
376
|
-
* `M` is the literal shape of the map. `precompile()` infers it from the
|
|
377
|
-
* object literal so the declared specifiers and their value shapes flow
|
|
378
|
-
* into `Prefix<G, M>` and feed `RebindImports<M>`.
|
|
379
|
-
*/
|
|
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
|
+
*/
|
|
380
379
|
type Imports<M extends Record<string, ImportValue> = Record<string, ImportValue>> = M;
|
|
381
380
|
/**
|
|
382
|
-
* A single import value. String = source module. Object = host module
|
|
383
|
-
* description (nested up to 64 levels, see {@link HostModuleObject}).
|
|
384
|
-
*/
|
|
381
|
+
* A single import value. String = source module. Object = host module
|
|
382
|
+
* description (nested up to 64 levels, see {@link HostModuleObject}).
|
|
383
|
+
*/
|
|
385
384
|
type ImportValue = string | HostModuleObject;
|
|
386
385
|
/**
|
|
387
|
-
* A host-module description object. Each property is either:
|
|
388
|
-
*
|
|
389
|
-
* - A function — the runtime builds an async bridge trampoline for it.
|
|
390
|
-
* - A data value (anything in {@link HostExportData}) — materialised in the
|
|
391
|
-
* isolate from its V8 serialization blob.
|
|
392
|
-
* - A nested object that itself contains a mix of functions and data —
|
|
393
|
-
* walked recursively.
|
|
394
|
-
*
|
|
395
|
-
* Class instances with prototype methods and stateful handles (streams,
|
|
396
|
-
* sockets) are rejected at registration with a clear error naming the exact
|
|
397
|
-
* path: V8's format would carry them, but only as their own enumerable
|
|
398
|
-
* properties, so the methods you meant to send would vanish silently. Copy
|
|
399
|
-
* what you mean to send into a plain object.
|
|
400
|
-
*
|
|
401
|
-
* Shapes may nest at most 64 levels (a node's path length from its top-level
|
|
402
|
-
* export). A deeper shape is rejected at registration, naming the path. Plain
|
|
403
|
-
* objects always describe shape, so this cap also applies to deep plain-object
|
|
404
|
-
* data; data in any other container is bounded only by V8's deserializer.
|
|
405
|
-
*/
|
|
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
|
+
*/
|
|
406
405
|
interface HostModuleObject {
|
|
407
406
|
[name: string]: HostModuleValue;
|
|
408
407
|
}
|
|
409
408
|
/**
|
|
410
|
-
* Any node the recursive walker may encounter inside a `HostModuleObject`.
|
|
411
|
-
*/
|
|
409
|
+
* Any node the recursive walker may encounter inside a `HostModuleObject`.
|
|
410
|
+
*/
|
|
412
411
|
type HostModuleValue = HostExportFunction | HostExportData | HostModuleObject;
|
|
413
412
|
/**
|
|
414
|
-
* The set of data values that can cross the sandbox boundary.
|
|
415
|
-
*
|
|
416
|
-
* This mirrors what V8's serialization format can represent, so every value
|
|
417
|
-
* here arrives on the other side as a **real instance** — a `Date` is a `Date`,
|
|
418
|
-
* a `Map` is a `Map`, a `Float64Array` keeps its element type. Cyclic and
|
|
419
|
-
* shared references are preserved, including object identity.
|
|
420
|
-
*
|
|
421
|
-
* What is *not* here cannot cross and is rejected loudly: functions (except as
|
|
422
|
-
* declared function leaves), `Promise`, `Symbol`, `WeakMap`, `WeakSet`, and
|
|
423
|
-
* `Proxy`. Class instances are also excluded — V8 would carry one, but only as
|
|
424
|
-
* its own enumerable properties, silently dropping its prototype and methods.
|
|
425
|
-
*
|
|
426
|
-
* Two notes for hosts:
|
|
427
|
-
*
|
|
428
|
-
* - Node `Buffer` is a `Uint8Array` subclass, so it crosses fine, but it
|
|
429
|
-
* arrives as a plain `Uint8Array` (data preserved, subclass identity not).
|
|
430
|
-
* - A `subarray` window carries only the bytes in its window.
|
|
431
|
-
*
|
|
432
|
-
* @see `docs/protocol.md` §4.2 for the full table, both directions.
|
|
433
|
-
*/
|
|
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
|
+
*/
|
|
434
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[] | {
|
|
435
434
|
[key: string]: HostExportData;
|
|
436
435
|
};
|
|
437
436
|
/**
|
|
438
|
-
* A host-side function exposed as a global or import-tree leaf.
|
|
439
|
-
*
|
|
440
|
-
* Args and return are `unknown` — the bridge serialises/deserialises values
|
|
441
|
-
* with V8 ValueSerializer; we don't try to model that at the type level.
|
|
442
|
-
* The one invariant that matters (functions cannot cross the boundary as
|
|
443
|
-
* argument values) is enforced at runtime by the V8 layer, not here. Arrays
|
|
444
|
-
* with holes are refused as arguments too, host-side, and the sandbox call
|
|
445
|
-
* rejects with a catchable `TypeError` before the handler runs.
|
|
446
|
-
*
|
|
447
|
-
* Use specific function types for your handlers and let TypeScript infer
|
|
448
|
-
* them through `HostGlobals`, `RebindGlobals<G>`, and `RebindImports<M>`.
|
|
449
|
-
*/
|
|
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
|
+
*/
|
|
450
449
|
type HostExportFunction = (...args: unknown[]) => unknown;
|
|
451
450
|
/**
|
|
452
|
-
* The rebindable subset of an `Imports<M>` declared at precompile time.
|
|
453
|
-
*
|
|
454
|
-
* Mirrors `RebindGlobals<G>` over the recursive host-module shape:
|
|
455
|
-
*
|
|
456
|
-
* - **Source imports** (string values) are fixed at prepare() time and
|
|
457
|
-
* cannot be rebound — their keys are excluded from the result.
|
|
458
|
-
* - **Host imports** (object values) are walked recursively. Only function
|
|
459
|
-
* leaves survive; data leaves are excluded because their values are
|
|
460
|
-
* emitted as JS literals into the generated module.
|
|
461
|
-
*
|
|
462
|
-
* Passing a specifier or export name that was not declared at precompile
|
|
463
|
-
* is a TypeScript error at compile time and `ERR_UNDECLARED_BINDING` at
|
|
464
|
-
* runtime.
|
|
465
|
-
*
|
|
466
|
-
* When `M` is left at its wide default (no `imports` declared, or no
|
|
467
|
-
* literal shape inferred), the rebind keyspace collapses to
|
|
468
|
-
* `Record<string, never>` — the walker only kicks in for literal shapes,
|
|
469
|
-
* which have finite depth.
|
|
470
|
-
*/
|
|
471
|
-
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; };
|
|
472
471
|
/**
|
|
473
|
-
* Recursive walker over a `HostModuleObject`. Function leaves keep
|
|
474
|
-
* their declared signature; nested objects recurse; data leaves drop out
|
|
475
|
-
* entirely. Only used against literal shapes inferred by `precompile`, so
|
|
476
|
-
* the recursion is always finite.
|
|
477
|
-
*/
|
|
478
|
-
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;
|
|
479
478
|
interface SandboxOptions {
|
|
480
479
|
/**
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
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
|
+
*/
|
|
500
499
|
maxConcurrentRuns?: number;
|
|
501
500
|
/**
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
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
|
+
*/
|
|
516
515
|
maxQueuedRuns?: number;
|
|
517
516
|
/**
|
|
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
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
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
|
+
*/
|
|
548
547
|
memoryMb?: number | MemoryLimit;
|
|
549
548
|
/**
|
|
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
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
* that the byte math stops being exact) and a negative value to `0`; a
|
|
587
|
-
* non-finite one throws.
|
|
588
|
-
*/
|
|
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
|
+
*/
|
|
589
585
|
memoryBudgetMb?: number;
|
|
590
586
|
/**
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
* @default 128
|
|
605
|
-
*/
|
|
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
|
+
*/
|
|
606
600
|
hostReserveMb?: number;
|
|
607
601
|
/**
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
602
|
+
* Override the path to the Rust V8 binary.
|
|
603
|
+
* Default: auto-detect from \@iso4/v8-* platform packages.
|
|
604
|
+
*/
|
|
611
605
|
binaryPath?: string;
|
|
612
606
|
}
|
|
613
607
|
/**
|
|
614
|
-
* A point-in-time capacity/usage snapshot from `sandbox.stats()`.
|
|
615
|
-
* The runtime-side numbers are mutually consistent (taken under one
|
|
616
|
-
* registry lock); `queueDepth` is a separate host-side read taken after
|
|
617
|
-
* the round trip. All of it is stale the moment it returns —
|
|
618
|
-
* diagnostics, not synchronization.
|
|
619
|
-
*/
|
|
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
|
+
*/
|
|
620
614
|
interface SandboxStats {
|
|
621
615
|
/**
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
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
|
+
*/
|
|
626
620
|
activeRuns: number;
|
|
627
621
|
/**
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
622
|
+
* Callers queued for a free run slot (`maxConcurrentRuns` runs are
|
|
623
|
+
* already executing).
|
|
624
|
+
*/
|
|
631
625
|
queueDepth: number;
|
|
632
626
|
/**
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
627
|
+
* Runs admitted concurrently right now. Follows the runtime's own figure
|
|
628
|
+
* as the workload changes, unless `maxConcurrentRuns` pinned it.
|
|
629
|
+
*/
|
|
636
630
|
slotLimit: number;
|
|
637
631
|
/**
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
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
|
+
*/
|
|
649
643
|
openConnections: number;
|
|
650
644
|
/**
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
645
|
+
* Resident warm instances — busy plus idle. Bounded by memory (the RSS
|
|
646
|
+
* mark), not by a count.
|
|
647
|
+
*/
|
|
654
648
|
warmInstances: number;
|
|
655
649
|
/**
|
|
656
|
-
|
|
657
|
-
|
|
650
|
+
* Idle warm instances ready to serve their prefix without a cold start.
|
|
651
|
+
*/
|
|
658
652
|
idleInstances: number;
|
|
659
653
|
/**
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
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
|
+
*/
|
|
666
660
|
idleMemoryBytes: number;
|
|
667
661
|
/**
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
662
|
+
* The memory mark the runtime sheds against, in bytes —
|
|
663
|
+
* `memoryBudgetMb` as the runtime received it.
|
|
664
|
+
* Utilization is `rssBytes / budgetBytes`.
|
|
665
|
+
*/
|
|
672
666
|
budgetBytes: number;
|
|
673
667
|
/**
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
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
|
+
*/
|
|
678
672
|
rssBytes: number;
|
|
679
673
|
/**
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
usageBytes: number;
|
|
686
|
-
/**
|
|
687
|
-
* The isolate admission line in bytes: 90 % of the container limit minus
|
|
688
|
-
* the host reserve. A run needing a new isolate whose `memoryMb` would
|
|
689
|
-
* not fit under it fails with `ERR_CAPACITY_MEMORY`. 0 = no container limit
|
|
690
|
-
* readable, line disabled.
|
|
691
|
-
*/
|
|
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
|
+
*/
|
|
692
679
|
hardLineBytes: number;
|
|
693
680
|
/**
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
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
|
+
*/
|
|
699
686
|
underPressure: boolean;
|
|
700
687
|
/**
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
688
|
+
* Per-prefix instance counts, keyed by prefix id — saturation of a single
|
|
689
|
+
* hot prefix is diagnosable here.
|
|
690
|
+
*/
|
|
704
691
|
prefixes: Record<string, {
|
|
705
692
|
idle: number;
|
|
706
693
|
busy: number;
|
|
@@ -708,404 +695,404 @@ interface SandboxStats {
|
|
|
708
695
|
}
|
|
709
696
|
interface Sandbox {
|
|
710
697
|
/**
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
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
|
+
*/
|
|
732
719
|
run: {
|
|
733
720
|
(options: RunCallOptions): Promise<CallResult>;
|
|
734
721
|
(options: RunOptions): Promise<RunResult>;
|
|
735
722
|
};
|
|
736
723
|
/**
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
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
|
+
*/
|
|
744
731
|
readExports: (options: ReadExportsOptions) => Promise<ReadExportsResult>;
|
|
745
732
|
/**
|
|
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
|
-
|
|
784
|
-
|
|
785
|
-
/**
|
|
786
|
-
|
|
787
|
-
|
|
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
|
+
*/
|
|
788
775
|
prepare: <G extends HostGlobals = {}, M extends Imports = {}>(options: PrecompileOptions<G, M>) => Promise<Prefix<G, M>>;
|
|
789
776
|
/**
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
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
|
+
*/
|
|
793
780
|
precompile: <G extends HostGlobals = {}, M extends Imports = {}>(options: PrecompileOptions<G, M>) => Promise<Prefix<G, M>>;
|
|
794
781
|
/**
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
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
|
+
*/
|
|
800
787
|
stats: () => Promise<SandboxStats>;
|
|
801
788
|
/**
|
|
802
|
-
|
|
803
|
-
|
|
789
|
+
* Tear down the Rust process. Outstanding prefixes are invalidated.
|
|
790
|
+
*/
|
|
804
791
|
dispose: () => Promise<void>;
|
|
805
792
|
/**
|
|
806
|
-
|
|
807
|
-
|
|
793
|
+
* Enables `await using sandbox = await createSandbox()` syntax.
|
|
794
|
+
*/
|
|
808
795
|
[Symbol.asyncDispose]: () => Promise<void>;
|
|
809
796
|
readonly alive: boolean;
|
|
810
797
|
}
|
|
811
798
|
type CreateSandbox = (options?: SandboxOptions) => Promise<Sandbox>;
|
|
812
799
|
/**
|
|
813
|
-
* Options for `sandbox.precompile()`.
|
|
814
|
-
*
|
|
815
|
-
* `G` is the shape of globals this prefix declares. TypeScript infers it
|
|
816
|
-
* from the `globals` field, so the returned `Prefix<G>` is typed to allow
|
|
817
|
-
* only those names to be rebound in subsequent `prefix.run()` calls.
|
|
818
|
-
*/
|
|
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
|
+
*/
|
|
819
806
|
interface PrecompileOptions<G extends HostGlobals, M extends Imports> {
|
|
820
807
|
/**
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
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
|
+
*/
|
|
827
814
|
code: string;
|
|
828
815
|
/**
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
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
|
+
*/
|
|
843
830
|
memoryMb?: number | MemoryLimit;
|
|
844
831
|
/**
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
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
|
+
*/
|
|
858
845
|
globals?: G;
|
|
859
846
|
/**
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
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
|
+
*/
|
|
865
852
|
imports?: M;
|
|
866
853
|
/**
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
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
|
+
*/
|
|
876
863
|
limits?: ResourceLimits;
|
|
877
864
|
/**
|
|
878
|
-
|
|
879
|
-
|
|
865
|
+
* Optional filename for stack traces. Default: "<prefix>".
|
|
866
|
+
*/
|
|
880
867
|
filename?: string;
|
|
881
868
|
}
|
|
882
869
|
/**
|
|
883
|
-
* Handle to a precompiled prefix.
|
|
884
|
-
*
|
|
885
|
-
* `G` is the shape of globals declared at precompile time. `prefix.run()`
|
|
886
|
-
* accepts only `Partial<G>` for its `globals` field — you may override any
|
|
887
|
-
* declared global's implementation per run, but you cannot add names that
|
|
888
|
-
* were not declared at precompile time.
|
|
889
|
-
*
|
|
890
|
-
* **Warm instances.** Runs on a prefix are served by resident
|
|
891
|
-
* isolates: the first run cold-starts one (prefix evaluated once, under the
|
|
892
|
-
* fixed warm-up budget), later runs reuse it and skip isolate boot and
|
|
893
|
-
* prefix re-evaluation. Warmth is a cache, never a guarantee — module-scope
|
|
894
|
-
* state MAY survive between runs on one instance, but any instance can be
|
|
895
|
-
* evicted at any moment (a CPU/memory limit firing mid-execution, an abort
|
|
896
|
-
* landing on running code, memory pressure,
|
|
897
|
-
* `dispose()`), and concurrent runs of one prefix are served by separate
|
|
898
|
-
* instances that share no state. Do not rely on carryover, and do not rely
|
|
899
|
-
* on per-run isolation within a prefix either: keep durable state outside
|
|
900
|
-
* the sandbox, and do per-request setup inside the handler — lazy init
|
|
901
|
-
* (`conn ??= await setup()`) runs under that call's budget and re-runs
|
|
902
|
-
* correctly after any eviction.
|
|
903
|
-
*/
|
|
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
|
+
*/
|
|
904
891
|
interface Prefix<G extends HostGlobals = {}, M extends Imports = {}> {
|
|
905
892
|
readonly id: string;
|
|
906
893
|
/**
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
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
|
+
*/
|
|
919
906
|
execute: (options: PrefixRunOptions<G, M>) => Promise<RunResult>;
|
|
920
907
|
/**
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
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
|
+
*/
|
|
924
911
|
run: (options: PrefixRunOptions<G, M>) => Promise<RunResult>;
|
|
925
912
|
/**
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
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
|
+
*/
|
|
939
926
|
call: (options: PrefixCallOptions<G, M>) => Promise<CallResult>;
|
|
940
927
|
/**
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
928
|
+
* Release the prepared prefix. Subsequent run() calls reject with
|
|
929
|
+
* `ERR_PREFIX_DISPOSED`. Idempotent, and works on a broken prefix too.
|
|
930
|
+
*/
|
|
944
931
|
dispose: () => Promise<void>;
|
|
945
932
|
/**
|
|
946
|
-
|
|
947
|
-
|
|
933
|
+
* Enables `await using prefix = await sandbox.precompile(...)` syntax.
|
|
934
|
+
*/
|
|
948
935
|
[Symbol.asyncDispose]: () => Promise<void>;
|
|
949
936
|
/**
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
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
|
+
*/
|
|
953
940
|
readonly alive: boolean;
|
|
954
941
|
/**
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
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
|
+
*/
|
|
961
948
|
readonly status: PrefixStatus;
|
|
962
949
|
/**
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
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
|
+
*/
|
|
969
956
|
readonly error: RunError | undefined;
|
|
970
957
|
}
|
|
971
958
|
/**
|
|
972
|
-
* Lifecycle of a {@link Prefix}. `'broken'` means the prefix failed to
|
|
973
|
-
* evaluate with one of its own, repeatable errors; every later run is
|
|
974
|
-
* refused with `ERR_PREFIX_BROKEN` and the only retry is a new `prepare()`.
|
|
975
|
-
* Load-dependent cold-start failures (`ERR_WARMUP_LIMIT`,
|
|
976
|
-
* `ERR_MEMORY_LIMIT`) never break a prefix — the next run cold-starts again.
|
|
977
|
-
*/
|
|
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
|
+
*/
|
|
978
965
|
type PrefixStatus = "ready" | "broken" | "disposed";
|
|
979
966
|
/**
|
|
980
|
-
* Options for `prefix.run()`.
|
|
981
|
-
*
|
|
982
|
-
* `G` is the declared-globals shape from the matching `precompile()` call.
|
|
983
|
-
* The `globals` field is typed as `Partial<G>` — only names present in `G`
|
|
984
|
-
* may be overridden. Passing any other name is a TypeScript error at compile
|
|
985
|
-
* time and rejects with `ERR_UNDECLARED_BINDING` at runtime. Every rebind
|
|
986
|
-
* refusal rejects; nothing runs.
|
|
987
|
-
*/
|
|
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
|
+
*/
|
|
988
975
|
interface PrefixRunOptions<G extends HostGlobals, M extends Imports> {
|
|
989
976
|
/**
|
|
990
|
-
|
|
991
|
-
|
|
977
|
+
* Dynamic ESM source code to compile and run against the prefix state.
|
|
978
|
+
*/
|
|
992
979
|
code: string;
|
|
993
980
|
/**
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
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
|
+
*/
|
|
1001
988
|
globals?: RebindGlobals<G>;
|
|
1002
989
|
/**
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
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
|
+
*/
|
|
1011
998
|
imports?: RebindImports<M>;
|
|
1012
999
|
/**
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
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
|
+
*/
|
|
1016
1003
|
limits?: ResourceLimits;
|
|
1017
1004
|
/**
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
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
|
+
*/
|
|
1028
1015
|
signal?: AbortSignal;
|
|
1029
1016
|
/**
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
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
|
+
*/
|
|
1038
1025
|
hardAbortSignal?: AbortSignal;
|
|
1039
1026
|
filename?: string;
|
|
1040
1027
|
}
|
|
1041
1028
|
/**
|
|
1042
|
-
* Options for `prefix.call()` — a {@link CallTarget} plus the same per-call
|
|
1043
|
-
* rebinding surface as {@link PrefixRunOptions}, minus `code`: a call frame
|
|
1044
|
-
* carries a postfix *or* a call, never both.
|
|
1045
|
-
*/
|
|
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
|
+
*/
|
|
1046
1033
|
interface PrefixCallOptions<G extends HostGlobals, M extends Imports> extends CallTarget {
|
|
1047
1034
|
/**
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1035
|
+
* Override implementations for a subset of globals declared at
|
|
1036
|
+
* prepare() time — same rules as {@link PrefixRunOptions.globals}.
|
|
1037
|
+
*/
|
|
1051
1038
|
globals?: RebindGlobals<G>;
|
|
1052
1039
|
/**
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1040
|
+
* Rebind host-module function exports declared at prepare() time — same
|
|
1041
|
+
* rules as {@link PrefixRunOptions.imports}.
|
|
1042
|
+
*/
|
|
1056
1043
|
imports?: RebindImports<M>;
|
|
1057
1044
|
/**
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
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
|
+
*/
|
|
1061
1048
|
limits?: ResourceLimits;
|
|
1062
1049
|
/**
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
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
|
+
*/
|
|
1073
1060
|
signal?: AbortSignal;
|
|
1074
1061
|
/**
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
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
|
+
*/
|
|
1083
1070
|
hardAbortSignal?: AbortSignal;
|
|
1084
1071
|
}
|
|
1085
1072
|
/**
|
|
1086
|
-
* Addresses a function that lives inside the sandbox, **relative to the
|
|
1087
|
-
* module's exports** — never `globalThis`. Either a top-level exported
|
|
1088
|
-
* function (`"handler"`) or a method on an exported object
|
|
1089
|
-
* (`"default.fetch"`).
|
|
1090
|
-
*
|
|
1091
|
-
* The receiver (`this`) is the object the final path segment was read from —
|
|
1092
|
-
* plain `a.b.c()` semantics — so `export default { fetch() { this.… } }` and
|
|
1093
|
-
* `export default new Worker()` (prototype methods included) both work.
|
|
1094
|
-
* A path that does not resolve, or resolves to something that is not
|
|
1095
|
-
* callable, fails the run with `ERR_CALL_TARGET_NOT_FOUND`.
|
|
1096
|
-
*/
|
|
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
|
+
*/
|
|
1097
1084
|
interface CallTarget {
|
|
1098
1085
|
/**
|
|
1099
|
-
|
|
1100
|
-
|
|
1086
|
+
* Dot-separated export path, e.g. `"default.fetch"` or `"handler"`.
|
|
1087
|
+
*/
|
|
1101
1088
|
export: string;
|
|
1102
1089
|
/**
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
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
|
+
*/
|
|
1109
1096
|
args?: unknown[];
|
|
1110
1097
|
}
|
|
1111
1098
|
interface RunOptions {
|
|
@@ -1114,157 +1101,157 @@ interface RunOptions {
|
|
|
1114
1101
|
globals?: HostGlobals;
|
|
1115
1102
|
imports?: Imports;
|
|
1116
1103
|
/**
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
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
|
+
*/
|
|
1127
1114
|
signal?: AbortSignal;
|
|
1128
1115
|
/**
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
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
|
+
*/
|
|
1137
1124
|
hardAbortSignal?: AbortSignal;
|
|
1138
1125
|
filename?: string;
|
|
1139
1126
|
}
|
|
1140
1127
|
/**
|
|
1141
|
-
* `run()` options with a call: the module is evaluated, then
|
|
1142
|
-
* `call.export` is resolved against its freshly evaluated exports and
|
|
1143
|
-
* invoked. The result carries the function's return {@link CallSuccess.value}
|
|
1144
|
-
* instead of `exports` — never both.
|
|
1145
|
-
*/
|
|
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
|
+
*/
|
|
1146
1133
|
interface RunCallOptions extends RunOptions {
|
|
1147
1134
|
call: CallTarget;
|
|
1148
1135
|
}
|
|
1149
1136
|
/**
|
|
1150
|
-
* Options for `sandbox.readExports()` — the deploy/registration path: load a
|
|
1151
|
-
* module once and read its declaration exports (IaC-style limits, connection
|
|
1152
|
-
* requirements, …). Function-valued exports are absent and reported in
|
|
1153
|
-
* {@link ReadExportsResult.skippedExports}, so reading the declarations of a
|
|
1154
|
-
* module that also exports handlers is not an error.
|
|
1155
|
-
*/
|
|
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
|
+
*/
|
|
1156
1143
|
interface ReadExportsOptions {
|
|
1157
1144
|
code: string;
|
|
1158
1145
|
limits?: ResourceLimits;
|
|
1159
1146
|
filename?: string;
|
|
1160
1147
|
}
|
|
1161
1148
|
/**
|
|
1162
|
-
* What `sandbox.readExports()` resolves to. Unlike `run()`, failures reject
|
|
1163
|
-
* the returned promise, since the deploy path treats a broken module as
|
|
1164
|
-
* exceptional rather than as a result variant.
|
|
1165
|
-
*/
|
|
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
|
+
*/
|
|
1166
1153
|
interface ReadExportsResult {
|
|
1167
1154
|
exports: SandboxExports;
|
|
1168
1155
|
/**
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1156
|
+
* Export names absent from `exports` because their value cannot cross the
|
|
1157
|
+
* boundary — see {@link RunSuccess.skippedExports}.
|
|
1158
|
+
*/
|
|
1172
1159
|
skippedExports: string[];
|
|
1173
1160
|
}
|
|
1174
1161
|
/**
|
|
1175
|
-
* Why a run was aborted — whatever value was passed to the aborting
|
|
1176
|
-
* `AbortController.abort(reason)` (or left `undefined`). Opaque to iso4 and
|
|
1177
|
-
* surfaced verbatim on {@link RunAborted.reason}; a consumer built on top
|
|
1178
|
-
* (e.g. a durable-workflow kernel expressing "suspend") gives it meaning.
|
|
1179
|
-
*/
|
|
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
|
+
*/
|
|
1180
1167
|
type AbortReason = unknown;
|
|
1181
1168
|
type RunResult = RunSuccess | RunFailure | RunAborted;
|
|
1182
1169
|
/**
|
|
1183
|
-
* Result of a run that carried a {@link CallTarget}: on success the called
|
|
1184
|
-
* function's return value replaces `exports` — never both. Failures and
|
|
1185
|
-
* aborts are shared with {@link RunResult}.
|
|
1186
|
-
*/
|
|
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
|
+
*/
|
|
1187
1174
|
type CallResult = CallSuccess | RunFailure | RunAborted;
|
|
1188
1175
|
/**
|
|
1189
|
-
* The success arm of a {@link CallResult}. Logs, timings, and bridge records
|
|
1190
|
-
* are identical to {@link RunSuccess}; only the value slot differs.
|
|
1191
|
-
*/
|
|
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
|
+
*/
|
|
1192
1179
|
interface CallSuccess {
|
|
1193
1180
|
status: "completed";
|
|
1194
1181
|
ok: true;
|
|
1195
1182
|
/**
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
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
|
+
*/
|
|
1210
1197
|
value: unknown;
|
|
1211
1198
|
stdout: string[];
|
|
1212
1199
|
stderr: string[];
|
|
1213
1200
|
/**
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
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
|
+
*/
|
|
1220
1207
|
durationMs: number;
|
|
1221
1208
|
/**
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
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
|
+
*/
|
|
1227
1214
|
wallTimeMs: number;
|
|
1228
1215
|
/**
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
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
|
+
*/
|
|
1234
1221
|
cpuTimeMs: number;
|
|
1235
1222
|
/**
|
|
1236
|
-
|
|
1237
|
-
|
|
1223
|
+
* See {@link RunSuccess.queueWaitMs}.
|
|
1224
|
+
*/
|
|
1238
1225
|
queueWaitMs?: number;
|
|
1239
1226
|
bridgeCalls: BridgeCallEntry[];
|
|
1240
1227
|
/**
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
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
|
+
*/
|
|
1254
1241
|
memoryBytes?: number;
|
|
1255
1242
|
/**
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1243
|
+
* Present when the call registered background work with `waitUntil` —
|
|
1244
|
+
* see {@link RunSuccess.waitUntil}. Never rejects.
|
|
1245
|
+
*/
|
|
1259
1246
|
waitUntil?: Promise<WaitUntilResult>;
|
|
1260
1247
|
}
|
|
1261
1248
|
/**
|
|
1262
|
-
* Metadata for a single bridge call attempt, recorded by the Rust runtime on
|
|
1263
|
-
* the run's own clock. No payloads — names, timing, and sizes only — so
|
|
1264
|
-
* recording is cheap enough to stay always-on. Unbounded: every attempt gets
|
|
1265
|
-
* an entry (~100 bytes); consumers that only care about a subset filter the
|
|
1266
|
-
* list themselves.
|
|
1267
|
-
*/
|
|
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
|
+
*/
|
|
1268
1255
|
type BridgeCallEntry = BridgeCallEntryBase & ({
|
|
1269
1256
|
ok: true;
|
|
1270
1257
|
} | {
|
|
@@ -1272,209 +1259,209 @@ type BridgeCallEntry = BridgeCallEntryBase & ({
|
|
|
1272
1259
|
reason: BridgeCallFailureReason;
|
|
1273
1260
|
});
|
|
1274
1261
|
/**
|
|
1275
|
-
* Why a bridge call did not succeed ({@link BridgeCallEntry} with
|
|
1276
|
-
* `ok: false`).
|
|
1277
|
-
*
|
|
1278
|
-
* - `blocked`: refused inside the runtime (`maxBridgeCalls`, payload over
|
|
1279
|
-
* `maxBridgeCallBytes`, function argument, invalid import handle, failed
|
|
1280
|
-
* write). Never reached the host; the sandbox promise rejected.
|
|
1281
|
-
* - `error`: the host answered with an error — the handler threw or
|
|
1282
|
-
* rejected, the host could not decode the arguments, or the return value
|
|
1283
|
-
* could not be serialized. The sandbox promise rejected.
|
|
1284
|
-
* - `unanswered`: sent, but no answer by the time these records were taken —
|
|
1285
|
-
* abort, a limit, an uncaught throw, or the sandbox never awaited it. The
|
|
1286
|
-
* handler ran (or is still running); its answer is discarded. On a run
|
|
1287
|
-
* with pending `waitUntil` work the Result's records are a snapshot, so a
|
|
1288
|
-
* call still in flight shows here even if it settles during the epilogue.
|
|
1289
|
-
* - `dropped`: the run's signal was already aborted when the call reached
|
|
1290
|
-
* the host, so the handler was never invoked.
|
|
1291
|
-
*/
|
|
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
|
+
*/
|
|
1292
1279
|
type BridgeCallFailureReason = "blocked" | "error" | "unanswered" | "dropped";
|
|
1293
1280
|
/**
|
|
1294
|
-
* Fields shared by every {@link BridgeCallEntry}.
|
|
1295
|
-
*/
|
|
1281
|
+
* Fields shared by every {@link BridgeCallEntry}.
|
|
1282
|
+
*/
|
|
1296
1283
|
interface BridgeCallEntryBase {
|
|
1297
1284
|
/**
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
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
|
+
*/
|
|
1303
1290
|
name: string;
|
|
1304
1291
|
/**
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
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
|
+
*/
|
|
1308
1295
|
startMs: number;
|
|
1309
1296
|
/**
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
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
|
+
*/
|
|
1314
1301
|
durationMs: number;
|
|
1315
1302
|
/**
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
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
|
+
*/
|
|
1320
1307
|
argBytes: number;
|
|
1321
1308
|
/**
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1309
|
+
* Serialized size of the successful response value in bytes. `0` when the
|
|
1310
|
+
* handler failed or the call never settled.
|
|
1311
|
+
*/
|
|
1325
1312
|
responseBytes: number;
|
|
1326
1313
|
}
|
|
1327
1314
|
/**
|
|
1328
|
-
* Outcome of a run's `waitUntil` grace phase, delivered through
|
|
1329
|
-
* {@link RunSuccess.waitUntil} / {@link CallSuccess.waitUntil}. The promise
|
|
1330
|
-
* always **resolves** (never rejects), so ignoring it — the intended common
|
|
1331
|
-
* case — produces no unhandled-rejection noise.
|
|
1332
|
-
*/
|
|
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
|
+
*/
|
|
1333
1320
|
interface WaitUntilResult {
|
|
1334
1321
|
/**
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
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
|
+
*/
|
|
1340
1327
|
status: "settled" | "truncated" | "failed";
|
|
1341
1328
|
/**
|
|
1342
|
-
|
|
1343
|
-
|
|
1329
|
+
* Complete wall time of the grace phase (after the result shipped), ms.
|
|
1330
|
+
*/
|
|
1344
1331
|
durationMs: number;
|
|
1345
1332
|
/**
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1333
|
+
* The grace phase's own logic time — same rule as
|
|
1334
|
+
* {@link RunSuccess.wallTimeMs}, metered separately from the run's.
|
|
1335
|
+
*/
|
|
1349
1336
|
wallTimeMs: number;
|
|
1350
1337
|
/**
|
|
1351
|
-
|
|
1352
|
-
|
|
1338
|
+
* CPU burned on the instance thread during the grace phase, ms.
|
|
1339
|
+
*/
|
|
1353
1340
|
cpuTimeMs: number;
|
|
1354
1341
|
/**
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1342
|
+
* Console lines written during the grace phase — the run's own lines
|
|
1343
|
+
* arrived on the result itself.
|
|
1344
|
+
*/
|
|
1358
1345
|
stdout: string[];
|
|
1359
1346
|
stderr: string[];
|
|
1360
1347
|
/**
|
|
1361
|
-
|
|
1362
|
-
|
|
1348
|
+
* Bridge calls attempted during the grace phase.
|
|
1349
|
+
*/
|
|
1363
1350
|
bridgeCalls: BridgeCallEntry[];
|
|
1364
1351
|
/**
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1352
|
+
* The first rejection when `status === 'failed'`, or the fatal error that
|
|
1353
|
+
* ended a truncated phase, when one exists.
|
|
1354
|
+
*/
|
|
1368
1355
|
error?: {
|
|
1369
1356
|
name: string;
|
|
1370
1357
|
message: string;
|
|
1371
1358
|
};
|
|
1372
1359
|
}
|
|
1373
1360
|
/**
|
|
1374
|
-
* `status` is the primary discriminant across all three outcomes; `ok` is kept
|
|
1375
|
-
* as a convenience alias for `status === 'completed'`, so the common guard
|
|
1376
|
-
* stays `if (result.ok)`. Reach for `switch (result.status)` when a deliberate
|
|
1377
|
-
* abort must be told apart from a genuine failure.
|
|
1378
|
-
*/
|
|
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
|
+
*/
|
|
1379
1366
|
interface RunSuccess {
|
|
1380
1367
|
status: "completed";
|
|
1381
1368
|
ok: true;
|
|
1382
1369
|
exports: SandboxExports;
|
|
1383
1370
|
/**
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
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
|
+
*/
|
|
1396
1383
|
skippedExports: string[];
|
|
1397
1384
|
/**
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
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
|
+
*/
|
|
1407
1394
|
stdout: string[];
|
|
1408
1395
|
/**
|
|
1409
|
-
|
|
1410
|
-
|
|
1396
|
+
* Same as {@link RunSuccess.stdout}, for `console.warn` / `.error`.
|
|
1397
|
+
*/
|
|
1411
1398
|
stderr: string[];
|
|
1412
1399
|
/**
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
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
|
+
*/
|
|
1420
1407
|
durationMs: number;
|
|
1421
1408
|
/**
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
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
|
+
*/
|
|
1427
1414
|
wallTimeMs: number;
|
|
1428
1415
|
/**
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
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
|
+
*/
|
|
1433
1420
|
cpuTimeMs: number;
|
|
1434
1421
|
/**
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
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
|
+
*/
|
|
1448
1435
|
queueWaitMs?: number;
|
|
1449
1436
|
/**
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
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
|
+
*/
|
|
1454
1441
|
bridgeCalls: BridgeCallEntry[];
|
|
1455
1442
|
/**
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
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
|
+
*/
|
|
1470
1457
|
memoryBytes?: number;
|
|
1471
1458
|
/**
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
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
|
+
*/
|
|
1478
1465
|
waitUntil?: Promise<WaitUntilResult>;
|
|
1479
1466
|
}
|
|
1480
1467
|
interface RunFailure {
|
|
@@ -1485,40 +1472,40 @@ interface RunFailure {
|
|
|
1485
1472
|
stderr: string[];
|
|
1486
1473
|
durationMs: number;
|
|
1487
1474
|
/**
|
|
1488
|
-
|
|
1489
|
-
|
|
1475
|
+
* See {@link RunSuccess.wallTimeMs}.
|
|
1476
|
+
*/
|
|
1490
1477
|
wallTimeMs: number;
|
|
1491
1478
|
cpuTimeMs: number;
|
|
1492
1479
|
/**
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
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
|
+
*/
|
|
1496
1483
|
queueWaitMs?: number;
|
|
1497
1484
|
bridgeCalls: BridgeCallEntry[];
|
|
1498
1485
|
/**
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
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
|
+
*/
|
|
1513
1500
|
memoryBytes?: number;
|
|
1514
1501
|
}
|
|
1515
1502
|
/**
|
|
1516
|
-
* The run was stopped by an abort — a pre-aborted or mid-run `signal` passed to
|
|
1517
|
-
* `run()`, or a host handler that aborted the run. A deliberate outcome, not a
|
|
1518
|
-
* crash: `error` is retained (its `code` is always `ERR_ABORTED`) so existing
|
|
1519
|
-
* `!result.ok` / `error.code` checks keep working, and `reason` carries the
|
|
1520
|
-
* value handed to `abort(reason)`.
|
|
1521
|
-
*/
|
|
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
|
+
*/
|
|
1522
1509
|
interface RunAborted {
|
|
1523
1510
|
status: "aborted";
|
|
1524
1511
|
ok: false;
|
|
@@ -1528,29 +1515,29 @@ interface RunAborted {
|
|
|
1528
1515
|
stderr: string[];
|
|
1529
1516
|
durationMs: number;
|
|
1530
1517
|
/**
|
|
1531
|
-
|
|
1532
|
-
|
|
1518
|
+
* See {@link RunSuccess.wallTimeMs}.
|
|
1519
|
+
*/
|
|
1533
1520
|
wallTimeMs: number;
|
|
1534
1521
|
cpuTimeMs: number;
|
|
1535
1522
|
/**
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
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
|
+
*/
|
|
1543
1530
|
bridgeCalls: BridgeCallEntry[];
|
|
1544
1531
|
}
|
|
1545
1532
|
type SandboxExports = {
|
|
1546
1533
|
/**
|
|
1547
|
-
|
|
1548
|
-
|
|
1534
|
+
* Value of `export default` in user code, or `undefined`.
|
|
1535
|
+
*/
|
|
1549
1536
|
default: unknown;
|
|
1550
1537
|
} & {
|
|
1551
1538
|
/**
|
|
1552
|
-
|
|
1553
|
-
|
|
1539
|
+
* Named exports live directly on the export object.
|
|
1540
|
+
*/
|
|
1554
1541
|
[name: string]: unknown;
|
|
1555
1542
|
};
|
|
1556
1543
|
interface RunError {
|
|
@@ -1558,69 +1545,66 @@ interface RunError {
|
|
|
1558
1545
|
name: string;
|
|
1559
1546
|
message: string;
|
|
1560
1547
|
/**
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
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
|
+
*/
|
|
1565
1552
|
stack?: string;
|
|
1566
1553
|
/**
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
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
|
+
*/
|
|
1573
1560
|
fields?: Record<string, unknown>;
|
|
1574
1561
|
/**
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1562
|
+
* Present only for `ERR_INSTANCE_RESET`: what interrupted the co-resident
|
|
1563
|
+
* run that took the shared instance down with it.
|
|
1564
|
+
*/
|
|
1578
1565
|
resetCause?: ResetCause;
|
|
1579
1566
|
/**
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
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
|
+
*/
|
|
1584
1571
|
culpritRunId?: number;
|
|
1585
1572
|
/**
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
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
|
+
*/
|
|
1594
1581
|
stage?: "prefix";
|
|
1595
1582
|
}
|
|
1596
1583
|
/**
|
|
1597
|
-
* Why a shared instance was reset under an innocent victim — see
|
|
1598
|
-
* `RunError.resetCause`. Every cause describes what the culprit run
|
|
1599
|
-
* (`culpritRunId`) did, never this run.
|
|
1600
|
-
*
|
|
1601
|
-
* `'unsupported'`: the culprit used something the runtime does not support
|
|
1602
|
-
* (today: dynamic `import()`), so the runtime ended it.
|
|
1603
|
-
*/
|
|
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
|
+
*/
|
|
1604
1591
|
type ResetCause = "cpu" | "memory" | "wall" | "abort" | "internal" | "unsupported";
|
|
1605
1592
|
/**
|
|
1606
|
-
* Why a run's options were refused before anything ran. `sandbox.run()`,
|
|
1607
|
-
* `prefix.run()`, `prefix.call()` and `prepare()` reject with an `Error`
|
|
1608
|
-
* whose `code` is one of these; a run never returns them as a result. The
|
|
1609
|
-
* client checks first and throws without a runtime round trip; the runtime
|
|
1610
|
-
* repeats every check (except `ERR_INVALID_REBIND`) and the client throws its
|
|
1611
|
-
* answer the same way. `ERR_PREFIX_DISPOSED`: the prefix was disposed, or the
|
|
1612
|
-
* runtime no longer holds it.
|
|
1613
|
-
*/
|
|
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
|
+
*/
|
|
1614
1601
|
type SetupErrorCode = "ERR_PREFIX_DISPOSED" | "ERR_PREFIX_BROKEN" | "ERR_UNDECLARED_BINDING" | "ERR_RESERVED_NAME" | "ERR_FROZEN_BINDING" | "ERR_INVALID_REBIND";
|
|
1615
1602
|
/**
|
|
1616
|
-
* Why a run failed, in a `{ ok: false }` result. Setup refusals are thrown
|
|
1617
|
-
* instead and carry a {@link SetupErrorCode}. `readExports()` rejects on
|
|
1618
|
-
* any failure; `prepare()` never runs anything, so a broken prefix reports
|
|
1619
|
-
* these on its first run.
|
|
1620
|
-
*/
|
|
1621
|
-
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";
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
declare function createSandbox(options?: SandboxOptions): Promise<Sandbox>;
|
|
1625
|
-
//#endregion
|
|
1626
|
-
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 };
|