@iso4/sandbox 0.6.2 → 0.6.3

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/dist/index.d.mts CHANGED
@@ -356,7 +356,7 @@ type RebindGlobals<G extends HostGlobals> = { [K in keyof G as G[K] extends stri
356
356
  type Imports<M extends Record<string, ImportValue> = Record<string, ImportValue>> = M;
357
357
  /**
358
358
  * A single import value. String = source module. Object = host module
359
- * description (the recursive walker handles all nesting).
359
+ * description (nested up to 64 levels, see {@link HostModuleObject}).
360
360
  */
361
361
  type ImportValue = string | HostModuleObject;
362
362
  /**
@@ -373,6 +373,11 @@ type ImportValue = string | HostModuleObject;
373
373
  * path: V8's format would carry them, but only as their own enumerable
374
374
  * properties, so the methods you meant to send would vanish silently. Copy
375
375
  * what you mean to send into a plain object.
376
+ *
377
+ * Shapes may nest at most 64 levels (a node's path length from its top-level
378
+ * export). A deeper shape is rejected at registration, naming the path. Plain
379
+ * objects always describe shape, so this cap also applies to deep plain-object
380
+ * data; data in any other container is bounded only by V8's deserializer.
376
381
  */
377
382
  interface HostModuleObject {
378
383
  [name: string]: HostModuleValue;
package/dist/index.mjs CHANGED
@@ -9,6 +9,7 @@ import { fileURLToPath } from "node:url";
9
9
  import { createConnection } from "node:net";
10
10
  import { Buffer } from "node:buffer";
11
11
  import v8 from "node:v8";
12
+ import { types } from "node:util";
12
13
  //#region src/binary.ts
13
14
  function resolveRuntimeBinary(options = {}) {
14
15
  if (options.binaryPath) return options.binaryPath;
@@ -310,11 +311,6 @@ async function toDescriptor(value, brandKey, streams) {
310
311
  };
311
312
  }
312
313
  /**
313
- * Depth cap, matching `MAX_DEPTH` in `webcodec.rs`. Cycles are handled by the
314
- * `seen` map, so this only bounds pathologically deep graphs.
315
- */
316
- const MAX_DEPTH = 32;
317
- /**
318
314
  * Replace every `Request`/`Response`/`Headers` anywhere in `value` with a
319
315
  * branded plain object, returning a graph ordinary serialization can write.
320
316
  *
@@ -324,18 +320,43 @@ const MAX_DEPTH = 32;
324
320
  * instance and do not reuse it afterwards (streams cannot cross the boundary,
325
321
  * so the body must be buffered rather than forwarded). Object identity and
326
322
  * cycles are preserved through `seen`. `Map`/`Set` are rebuilt because a host
327
- * type can hide in either.
323
+ * type can hide in either. A value with no host type (and no Proxy) anywhere
324
+ * is returned as is, so the common case costs one scan and no copy. Depth is not capped:
325
+ * the scan is iterative and every level of the rebuild suspends on an
326
+ * `await`, so neither deepens the call stack.
328
327
  * @param value the value to transform
329
328
  * @param brandKey the sandbox's session brand key ({@link brandKeyForToken});
330
329
  * the runtime rehydrates only descriptors stamped with it
331
330
  * @param streams
332
331
  */
333
332
  async function materializeHostTypes(value, brandKey, streams) {
334
- return transform(value, brandKey, streams, /* @__PURE__ */ new Map(), 0);
333
+ if (!hasHostType(value)) return value;
334
+ return transform(value, brandKey, streams, /* @__PURE__ */ new Map());
335
335
  }
336
- async function transform(value, brandKey, streams, seen, depth) {
336
+ function hasHostType(value) {
337
+ const seen = /* @__PURE__ */ new Set();
338
+ const stack = [];
339
+ const push = (v) => {
340
+ if (v !== null && typeof v === "object") stack.push(v);
341
+ };
342
+ push(value);
343
+ for (let v = stack.pop(); v !== void 0; v = stack.pop()) {
344
+ if (seen.has(v)) continue;
345
+ seen.add(v);
346
+ if (v instanceof globalThis.Response || v instanceof globalThis.Request || v instanceof globalThis.Headers || types.isProxy(v)) return true;
347
+ if (ArrayBuffer.isView(v) || v instanceof ArrayBuffer || v instanceof Date || v instanceof RegExp || v instanceof Error) continue;
348
+ if (Array.isArray(v)) for (const item of v) push(item);
349
+ else if (v instanceof Map) for (const [k, item] of v) {
350
+ push(k);
351
+ push(item);
352
+ }
353
+ else if (v instanceof Set) for (const item of v) push(item);
354
+ else for (const item of Object.values(v)) push(item);
355
+ }
356
+ return false;
357
+ }
358
+ async function transform(value, brandKey, streams, seen) {
337
359
  if (value === null || typeof value !== "object") return value;
338
- if (depth > MAX_DEPTH) throw new HostTypeError(`[iso4] value nests deeper than ${MAX_DEPTH} levels; cannot scan it for host types`);
339
360
  const existing = seen.get(value);
340
361
  if (existing !== void 0) return existing;
341
362
  const descriptor = await toDescriptor(value, brandKey, streams);
@@ -347,24 +368,24 @@ async function transform(value, brandKey, streams, seen, depth) {
347
368
  if (Array.isArray(value)) {
348
369
  const out = [];
349
370
  seen.set(value, out);
350
- for (const item of value) out.push(await transform(item, brandKey, streams, seen, depth + 1));
371
+ for (const item of value) out.push(await transform(item, brandKey, streams, seen));
351
372
  return out;
352
373
  }
353
374
  if (value instanceof Map) {
354
375
  const out = /* @__PURE__ */ new Map();
355
376
  seen.set(value, out);
356
- for (const [k, v] of value) out.set(await transform(k, brandKey, streams, seen, depth + 1), await transform(v, brandKey, streams, seen, depth + 1));
377
+ for (const [k, v] of value) out.set(await transform(k, brandKey, streams, seen), await transform(v, brandKey, streams, seen));
357
378
  return out;
358
379
  }
359
380
  if (value instanceof Set) {
360
381
  const out = /* @__PURE__ */ new Set();
361
382
  seen.set(value, out);
362
- for (const v of value) out.add(await transform(v, brandKey, streams, seen, depth + 1));
383
+ for (const v of value) out.add(await transform(v, brandKey, streams, seen));
363
384
  return out;
364
385
  }
365
386
  const out = Object.create(null);
366
387
  seen.set(value, out);
367
- for (const [k, v] of Object.entries(value)) out[k] = await transform(v, brandKey, streams, seen, depth + 1);
388
+ for (const [k, v] of Object.entries(value)) out[k] = await transform(v, brandKey, streams, seen);
368
389
  return out;
369
390
  }
370
391
  function readStr(des) {
@@ -1857,8 +1878,6 @@ function parseRustToTsMessageType(byte) {
1857
1878
  function formatByte(byte) {
1858
1879
  return `0x${byte.toString(16).padStart(2, "0")}`;
1859
1880
  }
1860
- //#endregion
1861
- //#region src/imports.ts
1862
1881
  /**
1863
1882
  * The dispatch-map key for one host-module function leaf. The NUL separator
1864
1883
  * cannot collide with anything a specifier or dot-joined path can contain in
@@ -1925,6 +1944,7 @@ function processImports(imports) {
1925
1944
  * @param seen
1926
1945
  */
1927
1946
  function lowerNode(specifier, value, path, handlers, seen = /* @__PURE__ */ new Set()) {
1947
+ if (path.length > 64) throw new Error(`[@iso4/sandbox] imports['${specifier}'].${path.join(".")}: host-module shapes may nest at most 64 levels deep`);
1928
1948
  if (typeof value === "function") {
1929
1949
  handlers.set(importHandlerKey(specifier, path.join(".")), value);
1930
1950
  return { kind: "function" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
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": [
@@ -49,10 +49,10 @@
49
49
  "vitest": "^4.1.5"
50
50
  },
51
51
  "optionalDependencies": {
52
- "@iso4/v8-darwin-arm64": "^0.6.2",
53
- "@iso4/v8-darwin-x64": "^0.6.2",
54
- "@iso4/v8-linux-arm64-gnu": "^0.6.2",
55
- "@iso4/v8-linux-x64-gnu": "^0.6.2"
52
+ "@iso4/v8-darwin-arm64": "^0.6.3",
53
+ "@iso4/v8-darwin-x64": "^0.6.3",
54
+ "@iso4/v8-linux-arm64-gnu": "^0.6.3",
55
+ "@iso4/v8-linux-x64-gnu": "^0.6.3"
56
56
  },
57
57
  "engines": {
58
58
  "node": ">=24.0.0"