@iso4/sandbox 0.0.4 → 0.1.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/dist/index.d.mts +4 -0
- package/dist/index.mjs +157 -46
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -479,6 +479,10 @@ interface RunError {
|
|
|
479
479
|
name: string;
|
|
480
480
|
message: string;
|
|
481
481
|
stack?: string;
|
|
482
|
+
/**
|
|
483
|
+
* Own enumerable properties of the thrown error, excluding name/message/stack. Present only for ERR_USER_CODE when the thrown value is an object with serializable own properties.
|
|
484
|
+
*/
|
|
485
|
+
data?: unknown;
|
|
482
486
|
}
|
|
483
487
|
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_EXPORT_TOO_LARGE" | "ERR_EXPORT_UNRESOLVED_PROMISE" | "ERR_HOST_BRIDGE" | "ERR_BRIDGE_PAYLOAD_TOO_LARGE" | "ERR_BRIDGE_CALL_LIMIT_EXCEEDED" | "ERR_UNDECLARED_BINDING" | "ERR_PREFIX_DISPOSED" | "ERR_INTERNAL";
|
|
484
488
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -239,6 +239,7 @@ function decodeRunCompletionPayload(buf) {
|
|
|
239
239
|
const name = reader.readString();
|
|
240
240
|
const message = reader.readString();
|
|
241
241
|
const stack = reader.readU8() === 1 ? reader.readString() : void 0;
|
|
242
|
+
const data = reader.readU8() === 1 ? decodeWireValueFromReader(reader) : void 0;
|
|
242
243
|
const stdout = reader.readStringList();
|
|
243
244
|
const stderr = reader.readStringList();
|
|
244
245
|
const durationMs = reader.readF64();
|
|
@@ -251,7 +252,8 @@ function decodeRunCompletionPayload(buf) {
|
|
|
251
252
|
code,
|
|
252
253
|
name,
|
|
253
254
|
message,
|
|
254
|
-
stack
|
|
255
|
+
stack,
|
|
256
|
+
data
|
|
255
257
|
},
|
|
256
258
|
stdout,
|
|
257
259
|
stderr,
|
|
@@ -290,6 +292,7 @@ function decodePrecompileResultPayload(buf) {
|
|
|
290
292
|
const name = reader.readString();
|
|
291
293
|
const message = reader.readString();
|
|
292
294
|
const stack = reader.readU8() === 1 ? reader.readString() : void 0;
|
|
295
|
+
if (reader.readU8() === 1) decodeWireValueFromReader(reader);
|
|
293
296
|
reader.assertDone();
|
|
294
297
|
return {
|
|
295
298
|
ok: false,
|
|
@@ -732,10 +735,29 @@ function formatByte(byte) {
|
|
|
732
735
|
}
|
|
733
736
|
//#endregion
|
|
734
737
|
//#region src/client.ts
|
|
738
|
+
/**
|
|
739
|
+
* Thrown out of a run when its `AbortSignal` fires mid-flight. The connection
|
|
740
|
+
* is torn down (so the Rust isolate is reclaimed) before this propagates, and
|
|
741
|
+
* the client is marked unusable so the pool replaces it rather than reusing a
|
|
742
|
+
* half-dead slot. `index.ts` catches this and synthesizes the `ERR_ABORTED`
|
|
743
|
+
* `RunResult`.
|
|
744
|
+
*/
|
|
745
|
+
var RunAbortedError = class extends Error {
|
|
746
|
+
constructor(message = "run was aborted") {
|
|
747
|
+
super(message);
|
|
748
|
+
this.name = "RunAbortedError";
|
|
749
|
+
}
|
|
750
|
+
};
|
|
735
751
|
var RuntimeIpcClient = class RuntimeIpcClient {
|
|
736
752
|
socket;
|
|
737
753
|
reader;
|
|
738
754
|
disposed = false;
|
|
755
|
+
/**
|
|
756
|
+
* Set when an in-flight abort tore this connection down. A broken client
|
|
757
|
+
* must not be returned to the pool's free list — `ConnectionPool.release`
|
|
758
|
+
* checks `usable` and replaces it with a fresh connection instead.
|
|
759
|
+
*/
|
|
760
|
+
broken = false;
|
|
739
761
|
constructor(socket) {
|
|
740
762
|
this.socket = socket;
|
|
741
763
|
this.reader = new FrameReader();
|
|
@@ -752,6 +774,14 @@ var RuntimeIpcClient = class RuntimeIpcClient {
|
|
|
752
774
|
this.reader.close(/* @__PURE__ */ new Error("socket closed"));
|
|
753
775
|
});
|
|
754
776
|
}
|
|
777
|
+
/**
|
|
778
|
+
* A client is usable while its connection is intact. Once an in-flight abort
|
|
779
|
+
* (or a dispose) tears the socket down it becomes unusable and the pool must
|
|
780
|
+
* replace it.
|
|
781
|
+
*/
|
|
782
|
+
get usable() {
|
|
783
|
+
return !this.disposed && !this.broken;
|
|
784
|
+
}
|
|
755
785
|
static async connect(options) {
|
|
756
786
|
const client = new RuntimeIpcClient(await connectSocket(options.socketPath));
|
|
757
787
|
await client.write(encodeTsToRustFrame(TsToRustMessageTypes.Authenticate, encodeAuthenticatePayload({
|
|
@@ -777,7 +807,7 @@ var RuntimeIpcClient = class RuntimeIpcClient {
|
|
|
777
807
|
globals: globalNames,
|
|
778
808
|
imports: options?.imports
|
|
779
809
|
})));
|
|
780
|
-
return this.drainUntilResult(makeDispatcher(globals));
|
|
810
|
+
return this.drainUntilResult(makeDispatcher(globals), options?.signal);
|
|
781
811
|
}
|
|
782
812
|
async precompile(options) {
|
|
783
813
|
if (this.disposed) throw new Error("runtime IPC client is disposed");
|
|
@@ -807,7 +837,7 @@ var RuntimeIpcClient = class RuntimeIpcClient {
|
|
|
807
837
|
imports: options.imports,
|
|
808
838
|
runId: this.nextRunIdValue()
|
|
809
839
|
})));
|
|
810
|
-
return this.drainUntilResult(makeDispatcher(globals));
|
|
840
|
+
return this.drainUntilResult(makeDispatcher(globals), options.signal);
|
|
811
841
|
}
|
|
812
842
|
async disposePrefix(prefixId) {
|
|
813
843
|
if (this.disposed) return;
|
|
@@ -839,9 +869,50 @@ var RuntimeIpcClient = class RuntimeIpcClient {
|
|
|
839
869
|
* garbage-collected when it eventually settles. Any in-flight I/O or timers
|
|
840
870
|
* continue to completion. Rust silently ignores any late `BridgeResponse`
|
|
841
871
|
* that arrives after the run has completed (see session.rs).
|
|
872
|
+
*
|
|
873
|
+
* ── In-flight abort ──────────────────────────────────────────────────────
|
|
874
|
+
* When `signal` fires mid-run — including while a bridge call is in flight —
|
|
875
|
+
* we tear the connection down immediately (`abortConnection`): the reader is
|
|
876
|
+
* closed so this loop throws, and the socket is destroyed so the Rust side
|
|
877
|
+
* observes EOF and reclaims the isolate promptly (rather than waiting for
|
|
878
|
+
* `wallTimeMs`). The loop then throws `RunAbortedError`, which `index.ts`
|
|
879
|
+
* maps to an `ERR_ABORTED` `RunResult`. Any late `BridgeResponse` written by
|
|
880
|
+
* an orphaned handler after this point fails silently (the socket is gone),
|
|
881
|
+
* so the sandbox never observes a return value for the in-flight call.
|
|
842
882
|
* @param dispatcher
|
|
883
|
+
* @param signal
|
|
884
|
+
*/
|
|
885
|
+
async drainUntilResult(dispatcher, signal) {
|
|
886
|
+
if (signal?.aborted) {
|
|
887
|
+
this.abortConnection();
|
|
888
|
+
throw new RunAbortedError();
|
|
889
|
+
}
|
|
890
|
+
const onAbort = () => {
|
|
891
|
+
this.abortConnection();
|
|
892
|
+
};
|
|
893
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
894
|
+
try {
|
|
895
|
+
return await this.drainFrames(dispatcher);
|
|
896
|
+
} catch (error) {
|
|
897
|
+
if (signal?.aborted) throw new RunAbortedError();
|
|
898
|
+
throw error;
|
|
899
|
+
} finally {
|
|
900
|
+
signal?.removeEventListener("abort", onAbort);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Tear down the connection in response to an in-flight abort. Idempotent.
|
|
905
|
+
* Marks the client `broken` (so the pool replaces it), closes the frame
|
|
906
|
+
* reader (so `drainFrames` stops awaiting), and destroys the socket (so the
|
|
907
|
+
* Rust session sees EOF and drops the isolate).
|
|
843
908
|
*/
|
|
844
|
-
|
|
909
|
+
abortConnection() {
|
|
910
|
+
if (this.broken) return;
|
|
911
|
+
this.broken = true;
|
|
912
|
+
this.reader.close(new RunAbortedError());
|
|
913
|
+
this.socket.destroy();
|
|
914
|
+
}
|
|
915
|
+
async drainFrames(dispatcher) {
|
|
845
916
|
for await (const frame of this.reader) switch (frame.messageType) {
|
|
846
917
|
case RustToTsMessageTypes.Result: return { result: frame.payload };
|
|
847
918
|
case RustToTsMessageTypes.Log: break;
|
|
@@ -913,9 +984,11 @@ function connectSocket(socketPath) {
|
|
|
913
984
|
var ConnectionPool = class {
|
|
914
985
|
free;
|
|
915
986
|
waiters = [];
|
|
987
|
+
connect;
|
|
916
988
|
disposed = false;
|
|
917
|
-
constructor(clients) {
|
|
989
|
+
constructor(clients, connect) {
|
|
918
990
|
this.free = [...clients];
|
|
991
|
+
this.connect = connect;
|
|
919
992
|
}
|
|
920
993
|
/**
|
|
921
994
|
* Borrow a client, run `fn`, then unconditionally return the client.
|
|
@@ -953,10 +1026,36 @@ var ConnectionPool = class {
|
|
|
953
1026
|
client.dispose();
|
|
954
1027
|
return;
|
|
955
1028
|
}
|
|
1029
|
+
if (!client.usable) {
|
|
1030
|
+
this.replace(client).catch(() => {});
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
956
1033
|
const next = this.waiters.shift();
|
|
957
1034
|
if (next !== void 0) next.resolve(client);
|
|
958
1035
|
else this.free.push(client);
|
|
959
1036
|
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Dispose a dead slot and connect a fresh one in its place. If reconnection
|
|
1039
|
+
* fails (or no factory was supplied) the pool simply runs with one fewer
|
|
1040
|
+
* slot rather than handing back a broken connection; any waiter still gets
|
|
1041
|
+
* served by other slots as they free up.
|
|
1042
|
+
* @param dead
|
|
1043
|
+
*/
|
|
1044
|
+
async replace(dead) {
|
|
1045
|
+
await dead.dispose().catch(() => {});
|
|
1046
|
+
if (this.connect === void 0) return;
|
|
1047
|
+
let fresh;
|
|
1048
|
+
try {
|
|
1049
|
+
fresh = await this.connect();
|
|
1050
|
+
} catch {
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
if (this.disposed) {
|
|
1054
|
+
await fresh.dispose().catch(() => {});
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
this.release(fresh);
|
|
1058
|
+
}
|
|
960
1059
|
};
|
|
961
1060
|
//#endregion
|
|
962
1061
|
//#region src/globals.ts
|
|
@@ -1337,10 +1436,11 @@ async function createSandbox(options) {
|
|
|
1337
1436
|
process.stderr.write(`[@iso4/sandbox] failed to start V8 process: ${err.message}\n`);
|
|
1338
1437
|
});
|
|
1339
1438
|
await waitForSocket(socketPath);
|
|
1340
|
-
|
|
1439
|
+
const connect = () => RuntimeIpcClient.connect({
|
|
1341
1440
|
socketPath,
|
|
1342
1441
|
token
|
|
1343
|
-
})
|
|
1442
|
+
});
|
|
1443
|
+
return new SandboxImpl(proc, new ConnectionPool(await Promise.all(Array.from({ length: maxIsolates }, () => connect())), connect), socketPath);
|
|
1344
1444
|
}
|
|
1345
1445
|
async function waitForSocket(socketPath, timeoutMs = 5e3) {
|
|
1346
1446
|
const deadline = Date.now() + timeoutMs;
|
|
@@ -1370,6 +1470,25 @@ function toWireLimits(limits) {
|
|
|
1370
1470
|
maxBridgeCalls: limits?.maxBridgeCalls ?? 10
|
|
1371
1471
|
};
|
|
1372
1472
|
}
|
|
1473
|
+
/**
|
|
1474
|
+
* The `RunResult` returned when a run is aborted — whether the signal was
|
|
1475
|
+
* already aborted at run entry or fired mid-flight (surfaced as a
|
|
1476
|
+
* `RunAbortedError` from the client). Shared so both paths produce an
|
|
1477
|
+
* identical shape.
|
|
1478
|
+
*/
|
|
1479
|
+
function abortedResult() {
|
|
1480
|
+
return {
|
|
1481
|
+
ok: false,
|
|
1482
|
+
error: {
|
|
1483
|
+
code: "ERR_ABORTED",
|
|
1484
|
+
name: "AbortError",
|
|
1485
|
+
message: "run was aborted"
|
|
1486
|
+
},
|
|
1487
|
+
stdout: [],
|
|
1488
|
+
stderr: [],
|
|
1489
|
+
durationMs: 0
|
|
1490
|
+
};
|
|
1491
|
+
}
|
|
1373
1492
|
var SandboxImpl = class {
|
|
1374
1493
|
proc;
|
|
1375
1494
|
pool;
|
|
@@ -1387,17 +1506,7 @@ var SandboxImpl = class {
|
|
|
1387
1506
|
return this._alive;
|
|
1388
1507
|
}
|
|
1389
1508
|
async run(options) {
|
|
1390
|
-
if (options.signal?.aborted) return
|
|
1391
|
-
ok: false,
|
|
1392
|
-
error: {
|
|
1393
|
-
code: "ERR_ABORTED",
|
|
1394
|
-
name: "AbortError",
|
|
1395
|
-
message: "run was aborted"
|
|
1396
|
-
},
|
|
1397
|
-
stdout: [],
|
|
1398
|
-
stderr: [],
|
|
1399
|
-
durationMs: 0
|
|
1400
|
-
};
|
|
1509
|
+
if (options.signal?.aborted) return abortedResult();
|
|
1401
1510
|
const { bridgeGlobals, preamble } = processGlobals(options.globals ?? {});
|
|
1402
1511
|
const { bindings, registry, shape: _shape } = processImports(options.imports);
|
|
1403
1512
|
const allGlobals = registry.size > 0 ? {
|
|
@@ -1405,14 +1514,20 @@ var SandboxImpl = class {
|
|
|
1405
1514
|
[BRIDGE_DISPATCH_GLOBAL]: createDispatchGlobal(registry)
|
|
1406
1515
|
} : bridgeGlobals;
|
|
1407
1516
|
const code = preamble ? `${preamble}\n${options.code}` : options.code;
|
|
1408
|
-
|
|
1409
|
-
return
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1517
|
+
try {
|
|
1518
|
+
return await this.pool.withClient(async (client) => {
|
|
1519
|
+
return decodeRunCompletionPayload((await client.runRawCode(code, {
|
|
1520
|
+
filename: options.filename,
|
|
1521
|
+
limits: toWireLimits(options.limits),
|
|
1522
|
+
globals: allGlobals,
|
|
1523
|
+
imports: bindings,
|
|
1524
|
+
signal: options.signal
|
|
1525
|
+
})).result).result;
|
|
1526
|
+
});
|
|
1527
|
+
} catch (error) {
|
|
1528
|
+
if (error instanceof RunAbortedError) return abortedResult();
|
|
1529
|
+
throw error;
|
|
1530
|
+
}
|
|
1416
1531
|
}
|
|
1417
1532
|
/**
|
|
1418
1533
|
* @param options
|
|
@@ -1504,17 +1619,7 @@ var PrefixImpl = class {
|
|
|
1504
1619
|
stderr: [],
|
|
1505
1620
|
durationMs: 0
|
|
1506
1621
|
};
|
|
1507
|
-
if (options.signal?.aborted) return
|
|
1508
|
-
ok: false,
|
|
1509
|
-
error: {
|
|
1510
|
-
code: "ERR_ABORTED",
|
|
1511
|
-
name: "AbortError",
|
|
1512
|
-
message: "run was aborted"
|
|
1513
|
-
},
|
|
1514
|
-
stdout: [],
|
|
1515
|
-
stderr: [],
|
|
1516
|
-
durationMs: 0
|
|
1517
|
-
};
|
|
1622
|
+
if (options.signal?.aborted) return abortedResult();
|
|
1518
1623
|
const bridgeGlobals = extractBridgeGlobals(options.globals ?? {}, this.defaultGlobals);
|
|
1519
1624
|
let registry;
|
|
1520
1625
|
try {
|
|
@@ -1537,15 +1642,21 @@ var PrefixImpl = class {
|
|
|
1537
1642
|
...bridgeGlobals,
|
|
1538
1643
|
[BRIDGE_DISPATCH_GLOBAL]: createDispatchGlobal(registry)
|
|
1539
1644
|
} : bridgeGlobals;
|
|
1540
|
-
|
|
1541
|
-
return
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1645
|
+
try {
|
|
1646
|
+
return await this.pool.withClient(async (client) => {
|
|
1647
|
+
return decodeRunCompletionPayload((await client.prefixRun({
|
|
1648
|
+
prefixId: this.id,
|
|
1649
|
+
code: options.code,
|
|
1650
|
+
filename: options.filename,
|
|
1651
|
+
limits: toWireLimits(options.limits),
|
|
1652
|
+
globals: allGlobals,
|
|
1653
|
+
signal: options.signal
|
|
1654
|
+
})).result).result;
|
|
1655
|
+
});
|
|
1656
|
+
} catch (error) {
|
|
1657
|
+
if (error instanceof RunAbortedError) return abortedResult();
|
|
1658
|
+
throw error;
|
|
1659
|
+
}
|
|
1549
1660
|
}
|
|
1550
1661
|
async dispose() {
|
|
1551
1662
|
if (!this._alive) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iso4/sandbox",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
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.0
|
|
51
|
-
"@iso4/v8-darwin-x64": "^0.0
|
|
52
|
-
"@iso4/v8-linux-arm64-gnu": "^0.0
|
|
53
|
-
"@iso4/v8-linux-x64-gnu": "^0.0
|
|
50
|
+
"@iso4/v8-darwin-arm64": "^0.1.0",
|
|
51
|
+
"@iso4/v8-darwin-x64": "^0.1.0",
|
|
52
|
+
"@iso4/v8-linux-arm64-gnu": "^0.1.0",
|
|
53
|
+
"@iso4/v8-linux-x64-gnu": "^0.1.0"
|
|
54
54
|
},
|
|
55
55
|
"engines": {
|
|
56
56
|
"node": ">=24.0.0"
|