@iso4/sandbox 0.2.2 → 0.3.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 CHANGED
@@ -104,6 +104,30 @@ prefix.run({
104
104
  })
105
105
  ```
106
106
 
107
+ ## Async context (`AsyncLocalStorage`)
108
+
109
+ Run/postfix code can import a minimal, Node-compatible `AsyncLocalStorage` to
110
+ carry an ambient value across `await` points — concurrency-safe, unlike a
111
+ module variable:
112
+
113
+ ```ts
114
+ prefix.run({
115
+ code: `
116
+ import { AsyncLocalStorage } from 'node:async_hooks'
117
+ const als = new AsyncLocalStorage()
118
+ export default await als.run('trace-42', async () => {
119
+ await somethingAsync()
120
+ return als.getStore() // 'trace-42', even several awaits deep
121
+ })
122
+ `,
123
+ })
124
+ ```
125
+
126
+ Only `run(store, callback, ...args)` and `getStore()` are provided. Built on
127
+ V8's continuation-preserved embedder data; no promise hooks, so it's free
128
+ unless used. Not available in `precompile()` (prefix) code — it's for the
129
+ postfix. See DESIGN.md §16.
130
+
107
131
  ## Result shape
108
132
 
109
133
  ```ts
package/dist/index.d.mts CHANGED
@@ -198,14 +198,15 @@ type ImportValue = string | HostModuleObject;
198
198
  * A host-module description object. Each property is either:
199
199
  *
200
200
  * - A function — becomes an async bridge stub in the generated module.
201
- * - A plain data value (primitive, plain object/array, `Date`, `BigInt`,
201
+ * - A plain data value (primitive, plain object/array, `BigInt`,
202
202
  * `Uint8Array`) — becomes a JS literal in the generated module.
203
203
  * - A nested object that itself contains a mix of functions and data —
204
204
  * walked recursively.
205
205
  *
206
- * `Map`, `Set`, circular references, class instances with prototype
207
- * methods, and stateful handles (streams, sockets) are rejected at
208
- * registration with a clear error.
206
+ * `Date`, `Map`, `Set`, circular references, class instances with
207
+ * prototype methods, and stateful handles (streams, sockets) are rejected
208
+ * at registration with a clear error. The supported set deliberately
209
+ * matches what the wire codec can carry back out of the sandbox.
209
210
  */
210
211
  interface HostModuleObject {
211
212
  [name: string]: HostModuleValue;
@@ -218,7 +219,7 @@ type HostModuleValue = HostExportFunction | HostExportData | HostModuleObject;
218
219
  * The set of plain-data values supported as data leaves in the host-module
219
220
  * shape. The JS-literal emitter knows how to emit each of these.
220
221
  */
221
- type HostExportData = null | undefined | boolean | number | bigint | string | Uint8Array | Date | HostExportData[] | {
222
+ type HostExportData = null | undefined | boolean | number | bigint | string | Uint8Array | HostExportData[] | {
222
223
  [key: string]: HostExportData;
223
224
  };
224
225
  /**
package/dist/index.mjs CHANGED
@@ -398,6 +398,11 @@ var WireWriter = class {
398
398
  return this;
399
399
  }
400
400
  if (typeof value === "object") {
401
+ const proto = Object.getPrototypeOf(value);
402
+ if (proto !== null && proto !== Object.prototype) {
403
+ const name = value.constructor?.name ?? "(unknown)";
404
+ throw new TypeError(`[iso4] encodeWireValue: cannot encode ${name} instance; only primitives, bigint, string, Uint8Array, and plain objects/arrays cross the boundary — copy the data into a plain object`);
405
+ }
401
406
  if (this.visiting.has(value)) throw new TypeError("[iso4] encodeWireValue: cyclic or self-referential structure");
402
407
  this.visiting.add(value);
403
408
  const entries = Object.entries(value).filter(([k]) => k !== "__proto__");
@@ -1353,8 +1358,9 @@ function emitValue(specifier, value, path, registry, allocId, fnIds, dataPaths,
1353
1358
  /**
1354
1359
  * Emit a `HostExportData` value as a JS expression that reproduces the
1355
1360
  * original value in the sandbox. Supports primitives, plain objects/arrays,
1356
- * `Date`, `BigInt`, `Uint8Array`. `Map`, `Set`, circular refs, and class
1357
- * instances throw with a clear pointer.
1361
+ * `BigInt`, `Uint8Array` — the same set the wire codec can carry back out.
1362
+ * `Date`, `Map`, `Set`, circular refs, and class instances throw with a
1363
+ * clear pointer.
1358
1364
  *
1359
1365
  * @param specifier
1360
1366
  * @param value
@@ -1374,8 +1380,7 @@ function emitDataLiteral(specifier, value, path, seen = /* @__PURE__ */ new Set(
1374
1380
  case "string": return JSON.stringify(value);
1375
1381
  }
1376
1382
  if (value instanceof Uint8Array) return `new Uint8Array([${Array.from(value).join(", ")}])`;
1377
- if (value instanceof Date) return `new Date(${value.getTime()})`;
1378
- if (value instanceof Map || value instanceof Set) throw new Error(`[@iso4/sandbox] imports['${specifier}'].${path.join(".")}: Map / Set values are not yet supported as data leaves`);
1383
+ if (value instanceof Date || value instanceof Map || value instanceof Set) throw new Error(`[@iso4/sandbox] imports['${specifier}'].${path.join(".")}: ${value.constructor.name} values are not supported as data leaves; supported: primitives, bigint, string, Uint8Array, plain objects/arrays`);
1379
1384
  if (typeof value === "object") {
1380
1385
  if (seen.has(value)) throw new Error(`[@iso4/sandbox] imports['${specifier}'].${path.join(".")}: circular references in data leaves are not supported`);
1381
1386
  seen.add(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
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.2.2",
51
- "@iso4/v8-darwin-x64": "^0.2.2",
52
- "@iso4/v8-linux-arm64-gnu": "^0.2.2",
53
- "@iso4/v8-linux-x64-gnu": "^0.2.2"
50
+ "@iso4/v8-darwin-arm64": "^0.3.1",
51
+ "@iso4/v8-darwin-x64": "^0.3.1",
52
+ "@iso4/v8-linux-arm64-gnu": "^0.3.1",
53
+ "@iso4/v8-linux-x64-gnu": "^0.3.1"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=24.0.0"