@iso4/sandbox 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -10
- package/dist/index.d.mts +11 -1
- package/dist/index.mjs +19 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ import { createSafeFetch } from '@iso4/fetch'
|
|
|
27
27
|
const sandbox = await createSandbox()
|
|
28
28
|
|
|
29
29
|
// Compile host setup once into a V8 snapshot
|
|
30
|
-
const prefix = await sandbox.
|
|
30
|
+
const prefix = await sandbox.prepare({
|
|
31
31
|
code: `
|
|
32
32
|
const config = { apiBase: 'https://api.example.com' }
|
|
33
33
|
globalThis.config = config
|
|
@@ -38,7 +38,7 @@ const prefix = await sandbox.precompile({
|
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
// Run agent-generated code against the snapshot — as many times as needed
|
|
41
|
-
const result = await prefix.
|
|
41
|
+
const result = await prefix.execute({
|
|
42
42
|
code: `
|
|
43
43
|
const res = await fetch(config.apiBase + '/users')
|
|
44
44
|
export default { count: res.length }
|
|
@@ -55,6 +55,10 @@ if (result.ok) {
|
|
|
55
55
|
await sandbox.dispose()
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
+
> `sandbox.prepare()` and `prefix.execute()` are the current names. The former
|
|
59
|
+
> names — `sandbox.precompile()` and `prefix.run()` — remain as deprecated
|
|
60
|
+
> aliases with identical behavior and will be removed in a future major.
|
|
61
|
+
|
|
58
62
|
## How globals work
|
|
59
63
|
|
|
60
64
|
`globals` wires any non-reserved name directly into the sandbox's global
|
|
@@ -77,22 +81,22 @@ not class instances with methods.
|
|
|
77
81
|
|
|
78
82
|
## TypeScript-checked rebinding
|
|
79
83
|
|
|
80
|
-
`
|
|
84
|
+
`prepare()` infers the globals shape `G` from what you pass, and the
|
|
81
85
|
returned `Prefix<G>` only allows rebinding those names at run time:
|
|
82
86
|
|
|
83
87
|
```ts
|
|
84
|
-
const prefix = await sandbox.
|
|
88
|
+
const prefix = await sandbox.prepare({
|
|
85
89
|
globals: { fetch: defaultFetch, myTool: defaultTool },
|
|
86
90
|
})
|
|
87
91
|
|
|
88
|
-
prefix.
|
|
89
|
-
prefix.
|
|
92
|
+
prefix.execute({ globals: { fetch: perUserFetch } }) // ✅ rebind one
|
|
93
|
+
prefix.execute({ globals: { unknown: handler } }) // ❌ TS error
|
|
90
94
|
```
|
|
91
95
|
|
|
92
96
|
## Resource limits
|
|
93
97
|
|
|
94
98
|
```ts
|
|
95
|
-
prefix.
|
|
99
|
+
prefix.execute({
|
|
96
100
|
code: agentCode,
|
|
97
101
|
limits: {
|
|
98
102
|
cpuTimeMs: 200, // active JS execution only (await time excluded)
|
|
@@ -111,7 +115,7 @@ carry an ambient value across `await` points — concurrency-safe, unlike a
|
|
|
111
115
|
module variable:
|
|
112
116
|
|
|
113
117
|
```ts
|
|
114
|
-
prefix.
|
|
118
|
+
prefix.execute({
|
|
115
119
|
code: `
|
|
116
120
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
117
121
|
const als = new AsyncLocalStorage()
|
|
@@ -125,7 +129,7 @@ prefix.run({
|
|
|
125
129
|
|
|
126
130
|
Only `run(store, callback, ...args)` and `getStore()` are provided. Built on
|
|
127
131
|
V8's continuation-preserved embedder data; no promise hooks, so it's free
|
|
128
|
-
unless used. Not available in `
|
|
132
|
+
unless used. Not available in `prepare()` (prefix) code — it's for the
|
|
129
133
|
postfix. See DESIGN.md §16.
|
|
130
134
|
|
|
131
135
|
## Result shape
|
|
@@ -179,7 +183,7 @@ Thrown errors keep their identity across the bridge, in both directions:
|
|
|
179
183
|
|
|
180
184
|
V8 runs in a separate Rust subprocess communicating over a Unix domain socket.
|
|
181
185
|
A pool of connections (one per `maxIsolates` slot) provides concurrency —
|
|
182
|
-
five concurrent `prefix.
|
|
186
|
+
five concurrent `prefix.execute()` calls each get their own slot and execute in
|
|
183
187
|
parallel.
|
|
184
188
|
|
|
185
189
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -351,6 +351,11 @@ interface Sandbox {
|
|
|
351
351
|
/**
|
|
352
352
|
* eslint-disable \@typescript-eslint/no-empty-object-type
|
|
353
353
|
*/
|
|
354
|
+
prepare: <G extends HostGlobals = {}, M extends Imports = {}>(options: PrecompileOptions<G, M>) => Promise<Prefix<G, M>>;
|
|
355
|
+
/**
|
|
356
|
+
* @deprecated Renamed to {@link Sandbox.prepare}. `precompile` stays as an
|
|
357
|
+
* alias (identical behavior) and is slated for removal in a future major.
|
|
358
|
+
*/
|
|
354
359
|
precompile: <G extends HostGlobals = {}, M extends Imports = {}>(options: PrecompileOptions<G, M>) => Promise<Prefix<G, M>>;
|
|
355
360
|
/**
|
|
356
361
|
* Tear down the Rust process. Outstanding prefixes are invalidated.
|
|
@@ -435,10 +440,15 @@ interface Prefix<G extends HostGlobals = {}, M extends Imports = {}> {
|
|
|
435
440
|
* host-import function exports declared in `I`.
|
|
436
441
|
*
|
|
437
442
|
* @example
|
|
438
|
-
* const result = await prefix.
|
|
443
|
+
* const result = await prefix.execute({
|
|
439
444
|
* code: `export default await (${agentFn})()`,
|
|
440
445
|
* })
|
|
441
446
|
*/
|
|
447
|
+
execute: (options: PrefixRunOptions<G, M>) => Promise<RunResult>;
|
|
448
|
+
/**
|
|
449
|
+
* @deprecated Renamed to {@link Prefix.execute}. `run` stays as an alias
|
|
450
|
+
* (identical behavior) and is slated for removal in a future major.
|
|
451
|
+
*/
|
|
442
452
|
run: (options: PrefixRunOptions<G, M>) => Promise<RunResult>;
|
|
443
453
|
/**
|
|
444
454
|
* Release the snapshot. Subsequent run() calls reject. Idempotent.
|
package/dist/index.mjs
CHANGED
|
@@ -1759,9 +1759,18 @@ var SandboxImpl = class {
|
|
|
1759
1759
|
}
|
|
1760
1760
|
}
|
|
1761
1761
|
/**
|
|
1762
|
+
* @deprecated Renamed to {@link SandboxImpl.prepare}; kept as an alias.
|
|
1762
1763
|
* @param options
|
|
1763
1764
|
*/
|
|
1764
1765
|
async precompile(options) {
|
|
1766
|
+
return this.prepare(options);
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* Pre-compile a prefix of code into a V8 startup snapshot. See
|
|
1770
|
+
* {@link Sandbox.prepare}.
|
|
1771
|
+
* @param options
|
|
1772
|
+
*/
|
|
1773
|
+
async prepare(options) {
|
|
1765
1774
|
return this.pool.withClient(async (client) => {
|
|
1766
1775
|
const rawGlobals = options.globals ?? {};
|
|
1767
1776
|
const { defs } = processGlobals(rawGlobals);
|
|
@@ -1826,6 +1835,16 @@ var PrefixImpl = class {
|
|
|
1826
1835
|
get alive() {
|
|
1827
1836
|
return this._alive;
|
|
1828
1837
|
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Execute dynamic code against this prefix's snapshot. See
|
|
1840
|
+
* {@link Prefix.execute}.
|
|
1841
|
+
*/
|
|
1842
|
+
async execute(options) {
|
|
1843
|
+
return this.run(options);
|
|
1844
|
+
}
|
|
1845
|
+
/**
|
|
1846
|
+
* @deprecated Renamed to {@link PrefixImpl.execute}; kept as an alias.
|
|
1847
|
+
*/
|
|
1829
1848
|
async run(options) {
|
|
1830
1849
|
if (!this._alive) return {
|
|
1831
1850
|
status: "failed",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iso4/sandbox",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Fast, sandboxed V8 isolate runtime for agent-generated JavaScript. Two-process architecture for crash isolation.",
|
|
6
6
|
"keywords": [
|
|
@@ -47,10 +47,10 @@
|
|
|
47
47
|
"vitest": "^4.1.5"
|
|
48
48
|
},
|
|
49
49
|
"optionalDependencies": {
|
|
50
|
-
"@iso4/v8-darwin-arm64": "^0.4.
|
|
51
|
-
"@iso4/v8-darwin-x64": "^0.4.
|
|
52
|
-
"@iso4/v8-linux-arm64-gnu": "^0.4.
|
|
53
|
-
"@iso4/v8-linux-x64-gnu": "^0.4.
|
|
50
|
+
"@iso4/v8-darwin-arm64": "^0.4.2",
|
|
51
|
+
"@iso4/v8-darwin-x64": "^0.4.2",
|
|
52
|
+
"@iso4/v8-linux-arm64-gnu": "^0.4.2",
|
|
53
|
+
"@iso4/v8-linux-x64-gnu": "^0.4.2"
|
|
54
54
|
},
|
|
55
55
|
"engines": {
|
|
56
56
|
"node": ">=24.0.0"
|