@iso4/sandbox 0.7.0 → 0.8.1
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 +72 -5
- package/dist/index.d.mts +66 -32
- package/dist/index.mjs +458 -497
- package/dist/names-BY-6pKkh.mjs +199 -0
- package/dist/utils.d.mts +45 -0
- package/dist/utils.mjs +2 -0
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -88,8 +88,43 @@ const options = {
|
|
|
88
88
|
Functions in bridge return values are currently dropped — return plain data,
|
|
89
89
|
not class instances with methods.
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
A global name can be any well-formed string the runtime does not reserve.
|
|
92
|
+
One that is not a JavaScript identifier
|
|
93
|
+
(`my-tool`, `user.lookup`) or is a reserved word (`class`) is reachable only
|
|
94
|
+
as `globalThis['my-tool']`, not by bare name.
|
|
95
|
+
|
|
96
|
+
`@iso4/sandbox/utils` exports the name rules. `SANDBOX_GLOBALS` is a `Set`
|
|
97
|
+
of every name a fresh sandbox already defines. Check generated names against
|
|
98
|
+
it before installing them. `RESERVED_WORDS` is a `Set` of names that cannot be
|
|
99
|
+
a binding in module source, such as `class` or `eval`; iso4 refuses
|
|
100
|
+
host-module export names in it, except `default`.
|
|
101
|
+
|
|
102
|
+
To check generated names before `prepare()` instead of getting a failed
|
|
103
|
+
setup back, import the predicates from `@iso4/sandbox/utils`. That entry
|
|
104
|
+
never loads the client or the native binary:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { isValidExportName, isValidGlobalName } from '@iso4/sandbox/utils'
|
|
108
|
+
|
|
109
|
+
for (const name of Object.keys(tools)) {
|
|
110
|
+
if (!isValidExportName(name))
|
|
111
|
+
throw new Error(`'${name}' cannot be a host-module export`)
|
|
112
|
+
}
|
|
113
|
+
if (!isValidGlobalName(globalName))
|
|
114
|
+
throw new Error(`'${globalName}' cannot be a host global`)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`isValidExportName` answers whether a string can be a key of a host-module
|
|
118
|
+
object in `imports`: an identifier per `IDENTIFIER_NAME` (Unicode included,
|
|
119
|
+
so `café` is fine) that is not in `RESERVED_WORDS`, or `default`, which maps
|
|
120
|
+
to the module's default export. `default` cannot be written as
|
|
121
|
+
`export const default` in generated source. `isValidGlobalName` is the check `prepare()` and
|
|
122
|
+
`run()` apply to host global names: any well-formed string except the
|
|
123
|
+
runtime-owned names (`console`, `Response`, `NaN`, …), a copy of the
|
|
124
|
+
runtime's list that a test checks against the runtime. Names starting with
|
|
125
|
+
`__iso4_` are iso4's internal namespace (the dispatcher, shim handlers): a
|
|
126
|
+
host global there may be silently overwritten or dropped, and
|
|
127
|
+
`isValidGlobalName` does not flag it.
|
|
93
128
|
|
|
94
129
|
## TypeScript-checked rebinding
|
|
95
130
|
|
|
@@ -383,9 +418,41 @@ interface RunError {
|
|
|
383
418
|
}
|
|
384
419
|
```
|
|
385
420
|
|
|
386
|
-
`run()` never throws for sandboxed failures
|
|
387
|
-
|
|
388
|
-
|
|
421
|
+
`run()` never throws for sandboxed failures. `ok: false` with an error code is
|
|
422
|
+
the normal failure path. It throws only for infrastructure errors (subprocess
|
|
423
|
+
crashed, binary not found) and for invalid options, the same way `prepare()`
|
|
424
|
+
does. `sandbox.run()`, `prefix.run()` and `prefix.call()` reject with an
|
|
425
|
+
`Error` whose `code` is a `SetupErrorCode`: `ERR_RESERVED_NAME` (a global
|
|
426
|
+
name the runtime owns), `ERR_UNDECLARED_BINDING`, `ERR_FROZEN_BINDING` or
|
|
427
|
+
`ERR_INVALID_REBIND` (a rebind the prefix does not allow), or
|
|
428
|
+
`ERR_PREFIX_DISPOSED`. The client checks these before anything is sent, and
|
|
429
|
+
the runtime repeats the checks. A malformed option, such as a limit out of range or a
|
|
430
|
+
global name that is not a well-formed string, rejects with a plain
|
|
431
|
+
`TypeError`. Nothing ran, so retrying with the same options can never
|
|
432
|
+
succeed. Every other code, including `ERR_COMPILE`, is a
|
|
433
|
+
result.
|
|
434
|
+
|
|
435
|
+
```ts
|
|
436
|
+
import type { SetupErrorCode } from '@iso4/sandbox'
|
|
437
|
+
|
|
438
|
+
const setupCodes: readonly string[] = [
|
|
439
|
+
'ERR_RESERVED_NAME',
|
|
440
|
+
'ERR_UNDECLARED_BINDING',
|
|
441
|
+
'ERR_FROZEN_BINDING',
|
|
442
|
+
'ERR_INVALID_REBIND',
|
|
443
|
+
'ERR_PREFIX_DISPOSED',
|
|
444
|
+
] satisfies SetupErrorCode[]
|
|
445
|
+
|
|
446
|
+
try {
|
|
447
|
+
const result = await sandbox.run({ code, globals })
|
|
448
|
+
if (!result.ok)
|
|
449
|
+
console.log('the program failed:', result.error.code)
|
|
450
|
+
} catch (error) {
|
|
451
|
+
if (!(error instanceof Error && 'code' in error && setupCodes.includes(error.code as string)))
|
|
452
|
+
throw error
|
|
453
|
+
console.log('the options are wrong:', error.code as SetupErrorCode)
|
|
454
|
+
}
|
|
455
|
+
```
|
|
389
456
|
|
|
390
457
|
One code is about a _neighbor_, not your own run: `ERR_INSTANCE_RESET` means a
|
|
391
458
|
run sharing the same warm instance had to be terminated mid-execution
|
package/dist/index.d.mts
CHANGED
|
@@ -12,18 +12,6 @@
|
|
|
12
12
|
* parallel. See DESIGN.md §1 and §13.
|
|
13
13
|
*/
|
|
14
14
|
/**
|
|
15
|
-
* Per-run resource caps. Every field is optional at the call site
|
|
16
|
-
* (`limits?: ResourceLimits`): pass only the limits you want to
|
|
17
|
-
* override. The client sends just those over the wire and the **runtime owns
|
|
18
|
-
* the defaults** — it fills in any field you leave unset. The `@default` values
|
|
19
|
-
* documented below mirror the runtime's defaults (defined in
|
|
20
|
-
* `native/v8-runtime/src/ipc.rs`, the source of truth); if they ever drift, the
|
|
21
|
-
* runtime wins.
|
|
22
|
-
*
|
|
23
|
-
* A field set to `0` is not the same as leaving it unset: `0` explicitly
|
|
24
|
-
* disables that limit, whereas unset yields the runtime default.
|
|
25
|
-
*/
|
|
26
|
-
/**
|
|
27
15
|
* The two heap lines a REUSED isolate is held to, when the managed default
|
|
28
16
|
* is not what you want. One-off `run()` isolates are never reused and take a
|
|
29
17
|
* plain `memoryMb` number, enforced exactly.
|
|
@@ -78,6 +66,22 @@ interface OneOffResourceLimits extends ResourceLimits {
|
|
|
78
66
|
*/
|
|
79
67
|
memoryMb?: number;
|
|
80
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Per-run resource caps. Every field is optional at the call site
|
|
71
|
+
* (`limits?: ResourceLimits`): pass only the limits you want to
|
|
72
|
+
* override. The client sends just those over the wire and the **runtime owns
|
|
73
|
+
* the defaults** — it fills in any field you leave unset. The `@default` values
|
|
74
|
+
* documented below mirror the runtime's defaults (defined in
|
|
75
|
+
* `native/v8-runtime/src/ipc.rs`, the source of truth); if they ever drift, the
|
|
76
|
+
* runtime wins.
|
|
77
|
+
*
|
|
78
|
+
* A field set to `0` is not the same as leaving it unset: `0` explicitly
|
|
79
|
+
* disables that limit, whereas unset yields the runtime default.
|
|
80
|
+
*
|
|
81
|
+
* Every field must be an integer from `0` to `2³² − 1`. Anything else
|
|
82
|
+
* (negative, fractional, `NaN`, `Infinity`, a non-number) rejects the call
|
|
83
|
+
* with a `TypeError` before anything runs.
|
|
84
|
+
*/
|
|
81
85
|
interface ResourceLimits {
|
|
82
86
|
/**
|
|
83
87
|
* Maximum CPU time in milliseconds — what the run's own thread actually
|
|
@@ -161,17 +165,29 @@ interface ResourceLimits {
|
|
|
161
165
|
graceMs?: number;
|
|
162
166
|
}
|
|
163
167
|
/**
|
|
164
|
-
* Host-provided globals. Any
|
|
165
|
-
*
|
|
168
|
+
* Host-provided globals. Any string not reserved by the runtime is permitted
|
|
169
|
+
* as a name. Each value becomes a property of the sandbox's `globalThis`.
|
|
166
170
|
*
|
|
167
171
|
* Reserved names (rejected with `ERR_RESERVED_NAME`): `console`,
|
|
168
172
|
* `waitUntil`, `setTimeout`, `clearTimeout`, `Headers`, `Request`,
|
|
169
|
-
* `Response`, `TextEncoder`, `TextDecoder`, `URL`, `URLSearchParams
|
|
170
|
-
*
|
|
171
|
-
* serialization.
|
|
173
|
+
* `Response`, `TextEncoder`, `TextDecoder`, `URL`, `URLSearchParams`, plus
|
|
174
|
+
* `NaN`, `undefined`, `Infinity` and `__proto__`. The first are owned by the
|
|
175
|
+
* runtime; shadowing `Response` would silently break serialization. `NaN`,
|
|
176
|
+
* `undefined` and `Infinity` are non-writable on the global object, so a host
|
|
177
|
+
* global could never take effect; assigning `__proto__` replaces a prototype
|
|
178
|
+
* instead of creating a property.
|
|
172
179
|
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
180
|
+
* A name that is not a JavaScript identifier (`my-tool`, `user.lookup`), or
|
|
181
|
+
* is in `RESERVED_WORDS` other than `eval` and `arguments`, is
|
|
182
|
+
* reachable only as `globalThis[name]`, not by bare name. A name that is
|
|
183
|
+
* not a well-formed string (`'y\uD800'`) throws a `TypeError` before
|
|
184
|
+
* anything is sent. Names starting with `__iso4_` are iso4's internal
|
|
185
|
+
* namespace (the dispatcher, shim handlers): a host global there may be
|
|
186
|
+
* silently overwritten or dropped, and `isValidGlobalName` does not flag it.
|
|
187
|
+
*
|
|
188
|
+
* `SANDBOX_GLOBALS` holds every name a fresh sandbox defines, V8 built-ins
|
|
189
|
+
* included. Both sets and `isValidGlobalName`, which checks a name before
|
|
190
|
+
* `prepare()`, come from `@iso4/sandbox/utils`.
|
|
175
191
|
*
|
|
176
192
|
* Common usage:
|
|
177
193
|
* ```ts
|
|
@@ -350,7 +366,9 @@ type RebindValue<V extends HostGlobalValue> = V extends BridgeWithShim<infer H>
|
|
|
350
366
|
type RebindGlobals<G extends HostGlobals> = { [K in keyof G as G[K] extends string | DataGlobal | StringGlobal ? never : K]?: RebindValue<G[K]> };
|
|
351
367
|
/**
|
|
352
368
|
* Top-level `imports` shape. Keys are import specifiers; values are either
|
|
353
|
-
* a source string or a host-module description object.
|
|
369
|
+
* a source string or a host-module description object. A specifier that is
|
|
370
|
+
* not a well-formed string (`'x\uD800'`) throws a `TypeError` before
|
|
371
|
+
* anything is sent.
|
|
354
372
|
*
|
|
355
373
|
* `M` is the literal shape of the map. `precompile()` infers it from the
|
|
356
374
|
* object literal so the declared specifiers and their value shapes flow
|
|
@@ -696,6 +714,14 @@ interface Sandbox {
|
|
|
696
714
|
* call: { export: 'default.fetch', args: [request] },
|
|
697
715
|
* })
|
|
698
716
|
* ```
|
|
717
|
+
*
|
|
718
|
+
* Invalid options reject, like `prepare()`: a global name the runtime owns
|
|
719
|
+
* rejects with an `Error` whose `code` is `ERR_RESERVED_NAME`; a malformed
|
|
720
|
+
* option (a bad limit or `memoryMb`, a global name that is not a
|
|
721
|
+
* well-formed string, an invalid host-module shape) rejects with a plain
|
|
722
|
+
* `Error`/`TypeError`.
|
|
723
|
+
* Nothing ran. Everything that happens once the run starts resolves as
|
|
724
|
+
* `{ ok: false, error }` — see {@link RunErrorCode}.
|
|
699
725
|
*/
|
|
700
726
|
run: {
|
|
701
727
|
(options: RunCallOptions): Promise<CallResult>;
|
|
@@ -901,7 +927,8 @@ interface Prefix<G extends HostGlobals = {}, M extends Imports = {}> {
|
|
|
901
927
|
* `G` is the declared-globals shape from the matching `precompile()` call.
|
|
902
928
|
* The `globals` field is typed as `Partial<G>` — only names present in `G`
|
|
903
929
|
* may be overridden. Passing any other name is a TypeScript error at compile
|
|
904
|
-
* time and `ERR_UNDECLARED_BINDING` at runtime.
|
|
930
|
+
* time and rejects with `ERR_UNDECLARED_BINDING` at runtime. Every rebind
|
|
931
|
+
* refusal rejects; nothing runs.
|
|
905
932
|
*/
|
|
906
933
|
interface PrefixRunOptions<G extends HostGlobals, M extends Imports> {
|
|
907
934
|
/**
|
|
@@ -911,7 +938,8 @@ interface PrefixRunOptions<G extends HostGlobals, M extends Imports> {
|
|
|
911
938
|
/**
|
|
912
939
|
* Override implementations for a **subset** of globals declared at
|
|
913
940
|
* precompile time. Only names present in `G` are accepted (TypeScript
|
|
914
|
-
* error + runtime `ERR_UNDECLARED_BINDING` for anything
|
|
941
|
+
* error + a runtime rejection with `ERR_UNDECLARED_BINDING` for anything
|
|
942
|
+
* else), but the
|
|
915
943
|
* function signature does not need to match the precompile-time handler
|
|
916
944
|
* exactly — any `HostExportFunction`-compatible value is accepted.
|
|
917
945
|
*/
|
|
@@ -921,7 +949,7 @@ interface PrefixRunOptions<G extends HostGlobals, M extends Imports> {
|
|
|
921
949
|
* Source module imports are frozen at prepare() time and cannot be rebound;
|
|
922
950
|
* data exports are baked into the synthetic module. TypeScript enforces
|
|
923
951
|
* this at the type level via `RebindImports<M>`; anything that slips
|
|
924
|
-
* through
|
|
952
|
+
* through rejects the run at runtime — `ERR_UNDECLARED_BINDING` for locations
|
|
925
953
|
* never declared, `ERR_FROZEN_BINDING` for source modules and data leaves,
|
|
926
954
|
* `ERR_INVALID_REBIND` for a non-function override value.
|
|
927
955
|
*/
|
|
@@ -1464,18 +1492,24 @@ interface RunError {
|
|
|
1464
1492
|
* `RunError.resetCause`.
|
|
1465
1493
|
*/
|
|
1466
1494
|
type ResetCause = "cpu" | "memory" | "wall" | "abort" | "internal";
|
|
1467
|
-
type RunErrorCode = "ERR_USER_CODE" | "ERR_MEMORY_LIMIT" | "ERR_CPU_TIMEOUT" | "ERR_WALL_TIMEOUT" | "ERR_ABORTED" | "ERR_MODULE_NOT_FOUND" | "ERR_COMPILE" | "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_UNDECLARED_BINDING" | "ERR_RESERVED_NAME" | "ERR_FROZEN_BINDING" | "ERR_INVALID_REBIND" | "ERR_PREFIX_DID_NOT_SETTLE" | "ERR_PREFIX_BRIDGE_CALL" | "ERR_WARMUP_LIMIT" | "ERR_PREFIX_DISPOSED" | "ERR_PROTOCOL_DESYNC" | "ERR_INTERNAL";
|
|
1468
|
-
//#endregion
|
|
1469
|
-
//#region src/globals.d.ts
|
|
1470
1495
|
/**
|
|
1471
|
-
*
|
|
1472
|
-
*
|
|
1473
|
-
*
|
|
1474
|
-
*
|
|
1496
|
+
* Why a run's options were refused before anything ran. `sandbox.run()`,
|
|
1497
|
+
* `prefix.run()`, `prefix.call()` and `prepare()` reject with an `Error`
|
|
1498
|
+
* whose `code` is one of these; a run never returns them as a result. The
|
|
1499
|
+
* client checks first and throws without a runtime round trip; the runtime
|
|
1500
|
+
* repeats every check (except `ERR_INVALID_REBIND`) and the client throws its
|
|
1501
|
+
* answer the same way. `ERR_PREFIX_DISPOSED`: the prefix was disposed, or the
|
|
1502
|
+
* runtime no longer holds it.
|
|
1503
|
+
*/
|
|
1504
|
+
type SetupErrorCode = "ERR_PREFIX_DISPOSED" | "ERR_UNDECLARED_BINDING" | "ERR_RESERVED_NAME" | "ERR_FROZEN_BINDING" | "ERR_INVALID_REBIND";
|
|
1505
|
+
/**
|
|
1506
|
+
* Why a run failed, in a `{ ok: false }` result. Setup refusals are thrown
|
|
1507
|
+
* instead and carry a {@link SetupErrorCode}. `prepare()` and
|
|
1508
|
+
* `readExports()` reject on any failure.
|
|
1475
1509
|
*/
|
|
1476
|
-
|
|
1510
|
+
type RunErrorCode = "ERR_USER_CODE" | "ERR_MEMORY_LIMIT" | "ERR_CPU_TIMEOUT" | "ERR_WALL_TIMEOUT" | "ERR_ABORTED" | "ERR_MODULE_NOT_FOUND" | "ERR_COMPILE" | "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";
|
|
1477
1511
|
//#endregion
|
|
1478
1512
|
//#region src/index.d.ts
|
|
1479
1513
|
declare function createSandbox(options?: SandboxOptions): Promise<Sandbox>;
|
|
1480
1514
|
//#endregion
|
|
1481
|
-
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 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,
|
|
1515
|
+
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 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 };
|