@iso4/sandbox 0.9.0 → 0.11.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/README.md +38 -23
- package/dist/index.d.mts +1209 -1188
- package/dist/index.mjs +66 -68
- package/dist/{names-BY-6pKkh.mjs → names-BW1pUf2B.mjs} +4 -5
- package/dist/utils.d.mts +36 -39
- package/dist/utils.mjs +1 -2
- package/package.json +13 -13
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { access, mkdtemp, rm } from "node:fs/promises";
|
|
1
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
3
2
|
import { existsSync, rmSync } from "node:fs";
|
|
4
3
|
import { availableParallelism, tmpdir, totalmem } from "node:os";
|
|
5
4
|
import { dirname, join } from "node:path";
|
|
@@ -11,7 +10,8 @@ import { createConnection } from "node:net";
|
|
|
11
10
|
import { Buffer } from "node:buffer";
|
|
12
11
|
import v8 from "node:v8";
|
|
13
12
|
import { types } from "node:util";
|
|
14
|
-
|
|
13
|
+
import { a as isValidExportName, r as RUNTIME_OWNED_GLOBALS } from "./names-BW1pUf2B.mjs";
|
|
14
|
+
// src/binary.ts
|
|
15
15
|
function resolveRuntimeBinary(options = {}) {
|
|
16
16
|
if (options.binaryPath) return options.binaryPath;
|
|
17
17
|
const platformPackage = platformBinaryPackage();
|
|
@@ -244,7 +244,7 @@ function flatHeaders(headers) {
|
|
|
244
244
|
* this exists so an unbounded body cannot exhaust host memory *before* the
|
|
245
245
|
* payload is ever framed.
|
|
246
246
|
*/
|
|
247
|
-
const MAX_BODY_BYTES =
|
|
247
|
+
const MAX_BODY_BYTES = 16777216;
|
|
248
248
|
async function materializeBody(source, streams) {
|
|
249
249
|
if (source.bodyUsed) throw new HostTypeError("[iso4] cannot serialize a Request/Response whose body was already read — pass a fresh instance (cache the bytes, not the object)");
|
|
250
250
|
const stream = source.body;
|
|
@@ -452,7 +452,7 @@ function readExtras(des) {
|
|
|
452
452
|
/**
|
|
453
453
|
* Methods that must not carry a body, per the fetch spec.
|
|
454
454
|
*/
|
|
455
|
-
const BODYLESS_METHODS = new Set(["GET", "HEAD"]);
|
|
455
|
+
const BODYLESS_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD"]);
|
|
456
456
|
/**
|
|
457
457
|
* Read one host-object payload, tag first, and construct the configured class.
|
|
458
458
|
*
|
|
@@ -512,8 +512,7 @@ var HostTypeDeserializer = class extends v8.Deserializer {
|
|
|
512
512
|
return readHostType(this);
|
|
513
513
|
}
|
|
514
514
|
};
|
|
515
|
-
|
|
516
|
-
//#region src/v8-codec.ts
|
|
515
|
+
// src/v8-codec.ts
|
|
517
516
|
/**
|
|
518
517
|
* Thrown when a value cannot be represented in the V8 serialization format.
|
|
519
518
|
*
|
|
@@ -622,9 +621,8 @@ function deserializeValue(bytes) {
|
|
|
622
621
|
function serializationProbe() {
|
|
623
622
|
return serializeValue(null);
|
|
624
623
|
}
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
const DEFAULT_MAX_FRAME_LENGTH = 64 * 1024 * 1024;
|
|
624
|
+
// src/ipc.ts
|
|
625
|
+
const DEFAULT_MAX_FRAME_LENGTH = 67108864;
|
|
628
626
|
const TsToRustMessageTypes = {
|
|
629
627
|
Authenticate: 1,
|
|
630
628
|
Run: 2,
|
|
@@ -999,7 +997,6 @@ var PayloadWriter = class {
|
|
|
999
997
|
case "shim":
|
|
1000
998
|
this.writeString(def.shim);
|
|
1001
999
|
this.writeString(def.handlerName);
|
|
1002
|
-
break;
|
|
1003
1000
|
}
|
|
1004
1001
|
}
|
|
1005
1002
|
return this;
|
|
@@ -1038,7 +1035,6 @@ var PayloadWriter = class {
|
|
|
1038
1035
|
this.writeString(key);
|
|
1039
1036
|
this.writeHostModuleNode(child);
|
|
1040
1037
|
}
|
|
1041
|
-
break;
|
|
1042
1038
|
}
|
|
1043
1039
|
return this;
|
|
1044
1040
|
}
|
|
@@ -1492,13 +1488,13 @@ function decodeRunCompletePayload(buf, droppedCallIds) {
|
|
|
1492
1488
|
/**
|
|
1493
1489
|
* Streaming bodies — wire constants mirroring `ipc.rs`.
|
|
1494
1490
|
*/
|
|
1495
|
-
const STREAM_CHUNK_MAX_BYTES =
|
|
1496
|
-
const STREAM_CREDIT_WINDOW_BYTES =
|
|
1491
|
+
const STREAM_CHUNK_MAX_BYTES = 65536;
|
|
1492
|
+
const STREAM_CREDIT_WINDOW_BYTES = 262144;
|
|
1497
1493
|
/**
|
|
1498
1494
|
* Bodies that end within this many bytes take the buffered path unchanged;
|
|
1499
1495
|
* larger ones stream. Host-side policy, not wire contract.
|
|
1500
1496
|
*/
|
|
1501
|
-
const STREAM_PROBE_BYTES =
|
|
1497
|
+
const STREAM_PROBE_BYTES = 65536;
|
|
1502
1498
|
/**
|
|
1503
1499
|
* Encode a `StreamChunk` payload: `u32 runId, u32 streamId, Bytes data`.
|
|
1504
1500
|
* @param runId the run the stream belongs to
|
|
@@ -1682,7 +1678,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
|
|
|
1682
1678
|
const wallTimeMs = reader.readF64();
|
|
1683
1679
|
const cpuTimeMs = reader.readF64();
|
|
1684
1680
|
const bridgeCalls = readBridgeCallRecords(reader, droppedCallIds);
|
|
1685
|
-
const
|
|
1681
|
+
const memoryBytes = reader.readOptionalU64();
|
|
1686
1682
|
const backgroundFlags = reader.readU8();
|
|
1687
1683
|
reader.readU8();
|
|
1688
1684
|
reader.assertDone();
|
|
@@ -1700,7 +1696,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
|
|
|
1700
1696
|
wallTimeMs,
|
|
1701
1697
|
cpuTimeMs,
|
|
1702
1698
|
bridgeCalls,
|
|
1703
|
-
|
|
1699
|
+
memoryBytes
|
|
1704
1700
|
}
|
|
1705
1701
|
};
|
|
1706
1702
|
return {
|
|
@@ -1718,7 +1714,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
|
|
|
1718
1714
|
wallTimeMs,
|
|
1719
1715
|
cpuTimeMs,
|
|
1720
1716
|
bridgeCalls,
|
|
1721
|
-
|
|
1717
|
+
memoryBytes
|
|
1722
1718
|
}
|
|
1723
1719
|
};
|
|
1724
1720
|
}
|
|
@@ -1738,7 +1734,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
|
|
|
1738
1734
|
const wallTimeMs = reader.readF64();
|
|
1739
1735
|
const cpuTimeMs = reader.readF64();
|
|
1740
1736
|
const bridgeCalls = readBridgeCallRecords(reader, droppedCallIds);
|
|
1741
|
-
const
|
|
1737
|
+
const memoryBytes = reader.readOptionalU64();
|
|
1742
1738
|
reader.assertDone();
|
|
1743
1739
|
return {
|
|
1744
1740
|
runId,
|
|
@@ -1762,7 +1758,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
|
|
|
1762
1758
|
wallTimeMs,
|
|
1763
1759
|
cpuTimeMs,
|
|
1764
1760
|
bridgeCalls,
|
|
1765
|
-
|
|
1761
|
+
memoryBytes
|
|
1766
1762
|
},
|
|
1767
1763
|
...prefixCause === void 0 ? {} : { prefixCause }
|
|
1768
1764
|
};
|
|
@@ -1954,7 +1950,7 @@ function decodePrecompileResultPayload(buf) {
|
|
|
1954
1950
|
* u32 oneoffRunning
|
|
1955
1951
|
* u32 warmBusy
|
|
1956
1952
|
* u32 warmIdle
|
|
1957
|
-
* u64
|
|
1953
|
+
* u64 idleMemoryBytes
|
|
1958
1954
|
* u64 warmBudgetBytes
|
|
1959
1955
|
* u64 rssBytes
|
|
1960
1956
|
* u8 underPressure
|
|
@@ -1971,10 +1967,9 @@ function decodeStatsPayload(buf) {
|
|
|
1971
1967
|
const warmBusy = reader.readU32();
|
|
1972
1968
|
const warmRuns = reader.readU32();
|
|
1973
1969
|
const warmIdle = reader.readU32();
|
|
1974
|
-
const
|
|
1970
|
+
const idleMemoryBytes = reader.readU64();
|
|
1975
1971
|
const warmBudgetBytes = reader.readU64();
|
|
1976
1972
|
const rssBytes = reader.readU64();
|
|
1977
|
-
const usageBytes = reader.readU64();
|
|
1978
1973
|
const hardLineBytes = reader.readU64();
|
|
1979
1974
|
const underPressure = reader.readBool();
|
|
1980
1975
|
const prefixCount = reader.readU32();
|
|
@@ -1995,10 +1990,9 @@ function decodeStatsPayload(buf) {
|
|
|
1995
1990
|
warmBusy,
|
|
1996
1991
|
warmRuns,
|
|
1997
1992
|
warmIdle,
|
|
1998
|
-
|
|
1993
|
+
idleMemoryBytes,
|
|
1999
1994
|
warmBudgetBytes,
|
|
2000
1995
|
rssBytes,
|
|
2001
|
-
usageBytes,
|
|
2002
1996
|
hardLineBytes,
|
|
2003
1997
|
underPressure,
|
|
2004
1998
|
prefixes
|
|
@@ -2023,8 +2017,7 @@ function parseRustToTsMessageType(byte) {
|
|
|
2023
2017
|
function formatByte(byte) {
|
|
2024
2018
|
return `0x${byte.toString(16).padStart(2, "0")}`;
|
|
2025
2019
|
}
|
|
2026
|
-
|
|
2027
|
-
//#region src/client.ts
|
|
2020
|
+
// src/client.ts
|
|
2028
2021
|
/**
|
|
2029
2022
|
* How long TS waits for Rust's `ERR_ABORTED` Result after sending a HARD
|
|
2030
2023
|
* `Terminate`, before falling back to tearing the connection down.
|
|
@@ -2211,7 +2204,8 @@ var RuntimeIpcClient = class RuntimeIpcClient {
|
|
|
2211
2204
|
* @param options
|
|
2212
2205
|
*/
|
|
2213
2206
|
static async connect(options) {
|
|
2214
|
-
const
|
|
2207
|
+
const socket = await connectSocket(options.socketPath);
|
|
2208
|
+
const client = new RuntimeIpcClient(socket, brandKeyForToken(options.descriptorToken));
|
|
2215
2209
|
client.onSlotAllowance = options.onSlotAllowance;
|
|
2216
2210
|
try {
|
|
2217
2211
|
await client.write(encodeTsToRustFrame(TsToRustMessageTypes.Authenticate, encodeAuthenticatePayload({
|
|
@@ -2432,7 +2426,7 @@ var RuntimeIpcClient = class RuntimeIpcClient {
|
|
|
2432
2426
|
*/
|
|
2433
2427
|
async stats() {
|
|
2434
2428
|
if (this.disposed) throw new Error("runtime IPC client is disposed");
|
|
2435
|
-
return decodeStatsPayload(await this.controlRequest(encodeTsToRustFrame(TsToRustMessageTypes.Stats, new Uint8Array(0)), RustToTsMessageTypes.StatsResult));
|
|
2429
|
+
return decodeStatsPayload(await this.controlRequest(encodeTsToRustFrame(TsToRustMessageTypes.Stats, /* @__PURE__ */ new Uint8Array(0)), RustToTsMessageTypes.StatsResult));
|
|
2436
2430
|
}
|
|
2437
2431
|
/**
|
|
2438
2432
|
* Write a request answered by exactly one payload-only reply frame
|
|
@@ -2470,7 +2464,7 @@ var RuntimeIpcClient = class RuntimeIpcClient {
|
|
|
2470
2464
|
async dispose() {
|
|
2471
2465
|
if (this.disposed) return;
|
|
2472
2466
|
this.disposed = true;
|
|
2473
|
-
if (!this.broken && !this.socket.destroyed) this.socket.write(encodeTsToRustFrame(TsToRustMessageTypes.Close, new Uint8Array(0)));
|
|
2467
|
+
if (!this.broken && !this.socket.destroyed) this.socket.write(encodeTsToRustFrame(TsToRustMessageTypes.Close, /* @__PURE__ */ new Uint8Array(0)));
|
|
2474
2468
|
this.socket.end();
|
|
2475
2469
|
await new Promise((resolve) => {
|
|
2476
2470
|
if (this.socket.destroyed) {
|
|
@@ -3229,8 +3223,7 @@ var RunPool = class {
|
|
|
3229
3223
|
await this.connections.dispose();
|
|
3230
3224
|
}
|
|
3231
3225
|
};
|
|
3232
|
-
|
|
3233
|
-
//#region src/globals.ts
|
|
3226
|
+
// src/globals.ts
|
|
3234
3227
|
/**
|
|
3235
3228
|
* The error every setup refusal is thrown as, whichever side detects it: a
|
|
3236
3229
|
* plain `Error` carrying the runtime's code.
|
|
@@ -3568,8 +3561,7 @@ function describeKind(value) {
|
|
|
3568
3561
|
if (Array.isArray(value)) return "array";
|
|
3569
3562
|
return typeof value;
|
|
3570
3563
|
}
|
|
3571
|
-
|
|
3572
|
-
//#region src/index.ts
|
|
3564
|
+
// src/index.ts
|
|
3573
3565
|
/**
|
|
3574
3566
|
* \@iso4/sandbox - public entry point.
|
|
3575
3567
|
*
|
|
@@ -3584,6 +3576,13 @@ function describeKind(value) {
|
|
|
3584
3576
|
* shedding work the sandbox could still have taken.
|
|
3585
3577
|
*/
|
|
3586
3578
|
const DEFAULT_MAX_QUEUED_RUNS = 1e4;
|
|
3579
|
+
const CHILD_ENV = [
|
|
3580
|
+
"TZ",
|
|
3581
|
+
"LANG",
|
|
3582
|
+
"RUST_BACKTRACE",
|
|
3583
|
+
"LC_*",
|
|
3584
|
+
"ISO4_*"
|
|
3585
|
+
];
|
|
3587
3586
|
async function createSandbox(options) {
|
|
3588
3587
|
if (options !== void 0 && "maxIsolates" in options) throw new TypeError("[@iso4/sandbox] maxIsolates was removed: capacity admits runs, not connections — set maxConcurrentRuns instead (same default: os.availableParallelism())");
|
|
3589
3588
|
const binaryPath = resolveRuntimeBinary(options);
|
|
@@ -3607,16 +3606,18 @@ async function createSandbox(options) {
|
|
|
3607
3606
|
String(warmBudgetBytes),
|
|
3608
3607
|
"--host-reserve-bytes",
|
|
3609
3608
|
String(hostReserveMb * 1024 * 1024)
|
|
3610
|
-
], {
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3609
|
+
], {
|
|
3610
|
+
stdio: [
|
|
3611
|
+
"ignore",
|
|
3612
|
+
"ignore",
|
|
3613
|
+
"inherit"
|
|
3614
|
+
],
|
|
3615
|
+
env: Object.fromEntries(Object.entries(process.env).filter(([name]) => CHILD_ENV.some((entry) => entry.endsWith("*") ? name.startsWith(entry.slice(0, -1)) : name === entry)))
|
|
3616
|
+
});
|
|
3615
3617
|
proc.once("error", (err) => {
|
|
3616
3618
|
process.stderr.write(`[@iso4/sandbox] failed to start V8 process: ${err.message}\n`);
|
|
3617
3619
|
});
|
|
3618
3620
|
try {
|
|
3619
|
-
await waitForSocket(socketPath, proc);
|
|
3620
3621
|
let pool;
|
|
3621
3622
|
const connect = () => RuntimeIpcClient.connect({
|
|
3622
3623
|
socketPath,
|
|
@@ -3624,7 +3625,7 @@ async function createSandbox(options) {
|
|
|
3624
3625
|
onSlotAllowance: pinnedConcurrency === void 0 ? (allowance) => pool.setSlotLimit(allowance) : void 0
|
|
3625
3626
|
});
|
|
3626
3627
|
pool = new RunPool(maxConcurrentRuns, maxQueuedRuns, connect);
|
|
3627
|
-
const statsClient = await connect
|
|
3628
|
+
const statsClient = await connectAtStartup(connect, socketPath, proc);
|
|
3628
3629
|
return new SandboxImpl(proc, pool, statsClient, socketPath, brandKey, options?.memoryMb);
|
|
3629
3630
|
} catch (error) {
|
|
3630
3631
|
proc.kill();
|
|
@@ -3637,22 +3638,22 @@ async function createSandbox(options) {
|
|
|
3637
3638
|
}
|
|
3638
3639
|
/**
|
|
3639
3640
|
* The warm budget in bytes — the ONE capacity fact the runtime needs
|
|
3640
|
-
* (celld's model): the RSS mark it sheds against
|
|
3641
|
+
* (celld's model): the RSS mark it sheds against.
|
|
3641
3642
|
* Independent of `memoryMb`: RSS is measured, not derived from per-isolate
|
|
3642
3643
|
* caps, so an uncapped-heap sandbox is budgeted all the same.
|
|
3643
|
-
* @param memoryBudgetMb the explicit budget knob (
|
|
3644
|
-
*
|
|
3645
|
-
* for the container-aware default
|
|
3644
|
+
* @param memoryBudgetMb the explicit budget knob (> 0), or undefined for
|
|
3645
|
+
* the container-aware default
|
|
3646
3646
|
* @param hostReserveMb what the default subtracts from the container limit
|
|
3647
3647
|
*/
|
|
3648
3648
|
function resolveWarmBudgetBytes(memoryBudgetMb, hostReserveMb) {
|
|
3649
3649
|
if (memoryBudgetMb !== void 0 && !Number.isFinite(memoryBudgetMb)) throw new TypeError("[@iso4/sandbox] memoryBudgetMb must be a finite number of megabytes");
|
|
3650
|
+
if (memoryBudgetMb !== void 0 && Math.floor(memoryBudgetMb * 1024 * 1024) < 1) throw new TypeError("[@iso4/sandbox] memoryBudgetMb must be a number of megabytes > 0");
|
|
3650
3651
|
const budgetMb = memoryBudgetMb ?? defaultMemoryBudgetMb(hostReserveMb);
|
|
3651
|
-
return Math.min(Math.
|
|
3652
|
+
return Math.min(Math.floor(budgetMb * 1024 * 1024), Number.MAX_SAFE_INTEGER);
|
|
3652
3653
|
}
|
|
3653
3654
|
/**
|
|
3654
|
-
* Default Node-host reserve
|
|
3655
|
-
* capacity line is drawn. Mirrors the runtime's `container.rs`
|
|
3655
|
+
* Default Node-host reserve (what the Node process uses) subtracted from the
|
|
3656
|
+
* container limit before any capacity line is drawn. Mirrors the runtime's `container.rs`
|
|
3656
3657
|
* `DEFAULT_HOST_RESERVE_BYTES` — change both together. Sized for a typical
|
|
3657
3658
|
* Node host (~80 MB measured) with margin; `hostReserveMb` tunes it.
|
|
3658
3659
|
*/
|
|
@@ -3665,30 +3666,28 @@ const DEFAULT_HOST_RESERVE_MB = 128;
|
|
|
3665
3666
|
function resolveHostReserveMb(hostReserveMb) {
|
|
3666
3667
|
if (hostReserveMb === void 0) return DEFAULT_HOST_RESERVE_MB;
|
|
3667
3668
|
if (!Number.isInteger(hostReserveMb) || hostReserveMb < 0) throw new TypeError("[@iso4/sandbox] hostReserveMb must be an integer >= 0 (megabytes; 0 = no reserve)");
|
|
3668
|
-
return Math.min(hostReserveMb, Math.floor(Number.MAX_SAFE_INTEGER /
|
|
3669
|
+
return Math.min(hostReserveMb, Math.floor(Number.MAX_SAFE_INTEGER / 1048576));
|
|
3669
3670
|
}
|
|
3670
3671
|
/**
|
|
3671
3672
|
* Default memory budget (the 80% shedding mark): 80% of what remains of the
|
|
3672
3673
|
* container limit after the Node-host reserve — container/cgroup-aware via
|
|
3673
3674
|
* `process.constrainedMemory()` (`os.totalmem()` lies inside containers;
|
|
3674
3675
|
* the fallback covers bare metal, where constrainedMemory reports 0). The
|
|
3675
|
-
* runtime compares
|
|
3676
|
-
*
|
|
3677
|
-
* identical base.
|
|
3676
|
+
* runtime compares its own RSS against it, and derives its hard admission
|
|
3677
|
+
* line at 90% of the identical base.
|
|
3678
3678
|
*
|
|
3679
3679
|
* Floored at 64 MB: on a host at or below the reserve the subtraction goes
|
|
3680
3680
|
* to zero or negative, and a zero DEFAULT would silently disable the
|
|
3681
|
-
* watermarks on exactly the memory-starved machines that need them most
|
|
3682
|
-
*
|
|
3683
|
-
*
|
|
3684
|
-
* unprotected — and says so on stderr.
|
|
3681
|
+
* watermarks on exactly the memory-starved machines that need them most.
|
|
3682
|
+
* The floor makes such a host shed warmth aggressively instead — degraded,
|
|
3683
|
+
* never unprotected — and says so on stderr.
|
|
3685
3684
|
* @param hostReserveMb the reserve both lines are drawn from
|
|
3686
3685
|
*/
|
|
3687
3686
|
function defaultMemoryBudgetMb(hostReserveMb) {
|
|
3688
3687
|
const totalMb = containerLimitMb();
|
|
3689
3688
|
const budgetMb = Math.floor((totalMb - hostReserveMb) * .8);
|
|
3690
3689
|
if (budgetMb < 64) {
|
|
3691
|
-
process.stderr.write(`[@iso4/sandbox] host memory (${Math.floor(totalMb)} MB) leaves no room for a warm budget after the ${hostReserveMb} MB host reserve — flooring the budget at 64 MB (expect aggressive eviction); set memoryBudgetMb explicitly to tune
|
|
3690
|
+
process.stderr.write(`[@iso4/sandbox] host memory (${Math.floor(totalMb)} MB) leaves no room for a warm budget after the ${hostReserveMb} MB host reserve — flooring the budget at 64 MB (expect aggressive eviction); set memoryBudgetMb explicitly to tune\n`);
|
|
3692
3691
|
return 64;
|
|
3693
3692
|
}
|
|
3694
3693
|
return budgetMb;
|
|
@@ -3703,7 +3702,7 @@ function defaultMemoryBudgetMb(hostReserveMb) {
|
|
|
3703
3702
|
*/
|
|
3704
3703
|
function containerLimitMb() {
|
|
3705
3704
|
const constrained = process.constrainedMemory?.() || Number.POSITIVE_INFINITY;
|
|
3706
|
-
return Math.min(constrained, totalmem()) /
|
|
3705
|
+
return Math.min(constrained, totalmem()) / 1048576;
|
|
3707
3706
|
}
|
|
3708
3707
|
/**
|
|
3709
3708
|
* The memory one run may reach, in megabytes — what capacity planning has to
|
|
@@ -3727,7 +3726,7 @@ function runCeilingMb(memoryMb) {
|
|
|
3727
3726
|
*/
|
|
3728
3727
|
function warnIfSlotsOutrunMemory(maxConcurrentRuns, memoryMb, warmBudgetBytes, hostReserveMb) {
|
|
3729
3728
|
const ceilingMb = runCeilingMb(memoryMb);
|
|
3730
|
-
const budgetMb = Math.floor(warmBudgetBytes /
|
|
3729
|
+
const budgetMb = Math.floor(warmBudgetBytes / 1048576);
|
|
3731
3730
|
if (ceilingMb <= 0 || budgetMb <= 0) return;
|
|
3732
3731
|
const wantMb = maxConcurrentRuns * ceilingMb;
|
|
3733
3732
|
if (wantMb <= budgetMb) return;
|
|
@@ -3736,12 +3735,13 @@ function warnIfSlotsOutrunMemory(maxConcurrentRuns, memoryMb, warmBudgetBytes, h
|
|
|
3736
3735
|
if (wantMb > lineMb) consequence = `over the runtime's ${lineMb} MB admission line — runs that need a new isolate fail with ERR_CAPACITY_MEMORY; lower maxConcurrentRuns or memoryMb`;
|
|
3737
3736
|
process.stderr.write(`[@iso4/sandbox] maxConcurrentRuns ${maxConcurrentRuns} × a ${ceilingMb} MB heap ceiling = ${wantMb} MB, ${consequence}\n`);
|
|
3738
3737
|
}
|
|
3739
|
-
async function
|
|
3738
|
+
async function connectAtStartup(connect, socketPath, proc, timeoutMs = 5e3) {
|
|
3740
3739
|
const deadline = Date.now() + timeoutMs;
|
|
3741
3740
|
while (Date.now() < deadline) try {
|
|
3742
|
-
await
|
|
3743
|
-
|
|
3744
|
-
|
|
3741
|
+
return await connect();
|
|
3742
|
+
} catch (error) {
|
|
3743
|
+
const code = error.code;
|
|
3744
|
+
if (code !== "ENOENT" && code !== "ECONNREFUSED") throw error;
|
|
3745
3745
|
if (proc.exitCode !== null || proc.signalCode !== null) throw new Error(`[@iso4/sandbox] the V8 process exited before its socket appeared (exit code ${proc.exitCode}, signal ${proc.signalCode}). Its own diagnostics are on this process's stderr.`);
|
|
3746
3746
|
await new Promise((resolve) => {
|
|
3747
3747
|
setTimeout(resolve, 50);
|
|
@@ -3814,7 +3814,7 @@ const U32_LIMIT_FIELDS = [
|
|
|
3814
3814
|
"maxBridgeCalls",
|
|
3815
3815
|
"graceMs"
|
|
3816
3816
|
];
|
|
3817
|
-
const U32_MAX =
|
|
3817
|
+
const U32_MAX = 0xffffffff;
|
|
3818
3818
|
/**
|
|
3819
3819
|
* Refuse a limit the wire cannot carry as a `u32` instead of letting the
|
|
3820
3820
|
* encoder throw a `RangeError` or silently truncate a fraction.
|
|
@@ -3949,7 +3949,7 @@ function desyncResult(error) {
|
|
|
3949
3949
|
* checks first; this catches what it cannot know, such as a prefix the
|
|
3950
3950
|
* runtime dropped. Runs throw these, like `prepare()`, instead of returning them.
|
|
3951
3951
|
*/
|
|
3952
|
-
const SETUP_ERROR_CODES = new Set([
|
|
3952
|
+
const SETUP_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
3953
3953
|
"ERR_RESERVED_NAME",
|
|
3954
3954
|
"ERR_UNDECLARED_BINDING",
|
|
3955
3955
|
"ERR_FROZEN_BINDING",
|
|
@@ -4105,10 +4105,9 @@ var SandboxImpl = class {
|
|
|
4105
4105
|
openConnections: this.pool.openConnections,
|
|
4106
4106
|
warmInstances: raw.warmBusy + raw.warmIdle,
|
|
4107
4107
|
idleInstances: raw.warmIdle,
|
|
4108
|
-
|
|
4108
|
+
idleMemoryBytes: raw.idleMemoryBytes,
|
|
4109
4109
|
budgetBytes: raw.warmBudgetBytes,
|
|
4110
4110
|
rssBytes: raw.rssBytes,
|
|
4111
|
-
usageBytes: raw.usageBytes,
|
|
4112
4111
|
hardLineBytes: raw.hardLineBytes,
|
|
4113
4112
|
underPressure: raw.underPressure,
|
|
4114
4113
|
prefixes: Object.fromEntries(raw.prefixes.map((p) => [p.prefixId, {
|
|
@@ -4381,5 +4380,4 @@ var PrefixImpl = class {
|
|
|
4381
4380
|
await this.dispose();
|
|
4382
4381
|
}
|
|
4383
4382
|
};
|
|
4384
|
-
//#endregion
|
|
4385
4383
|
export { createSandbox };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/names.ts
|
|
2
2
|
/**
|
|
3
3
|
* Name rules behind the public `@iso4/sandbox/utils` entry. Imports nothing,
|
|
4
4
|
* so loading it never touches the client or the native binary.
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* runtime reserves the name. Names the grammar
|
|
11
11
|
* forbids outright are in {@link RESERVED_WORDS}.
|
|
12
12
|
*/
|
|
13
|
-
const SANDBOX_GLOBALS = new Set([
|
|
13
|
+
const SANDBOX_GLOBALS = /* @__PURE__ */ new Set([
|
|
14
14
|
"AggregateError",
|
|
15
15
|
"Array",
|
|
16
16
|
"ArrayBuffer",
|
|
@@ -92,7 +92,7 @@ const SANDBOX_GLOBALS = new Set([
|
|
|
92
92
|
* Names that cannot be a binding in module source. A host-module key may not
|
|
93
93
|
* use one, except `default`.
|
|
94
94
|
*/
|
|
95
|
-
const RESERVED_WORDS = new Set([
|
|
95
|
+
const RESERVED_WORDS = /* @__PURE__ */ new Set([
|
|
96
96
|
"break",
|
|
97
97
|
"case",
|
|
98
98
|
"catch",
|
|
@@ -156,7 +156,7 @@ const IDENTIFIER_NAME = /^[\p{ID_Start}$_][\p{ID_Continue}$\u200C\u200D]*$/u;
|
|
|
156
156
|
* checks every name here and every {@link SANDBOX_GLOBALS} name against the
|
|
157
157
|
* runtime.
|
|
158
158
|
*/
|
|
159
|
-
const RUNTIME_OWNED_GLOBALS = new Set([
|
|
159
|
+
const RUNTIME_OWNED_GLOBALS = /* @__PURE__ */ new Set([
|
|
160
160
|
"console",
|
|
161
161
|
"waitUntil",
|
|
162
162
|
"setTimeout",
|
|
@@ -195,5 +195,4 @@ function isValidExportName(name) {
|
|
|
195
195
|
function isValidGlobalName(name) {
|
|
196
196
|
return name.isWellFormed() && !RUNTIME_OWNED_GLOBALS.has(name);
|
|
197
197
|
}
|
|
198
|
-
//#endregion
|
|
199
198
|
export { isValidExportName as a, SANDBOX_GLOBALS as i, RESERVED_WORDS as n, isValidGlobalName as o, RUNTIME_OWNED_GLOBALS as r, IDENTIFIER_NAME as t };
|
package/dist/utils.d.mts
CHANGED
|
@@ -1,45 +1,42 @@
|
|
|
1
|
-
//#region src/names.d.ts
|
|
2
1
|
/**
|
|
3
|
-
* Name rules behind the public `@iso4/sandbox/utils` entry. Imports nothing,
|
|
4
|
-
* so loading it never touches the client or the native binary.
|
|
5
|
-
*/
|
|
2
|
+
* Name rules behind the public `@iso4/sandbox/utils` entry. Imports nothing,
|
|
3
|
+
* so loading it never touches the client or the native binary.
|
|
4
|
+
*/
|
|
6
5
|
/**
|
|
7
|
-
* Every global name a fresh sandbox defines: the JS built-ins of the bundled
|
|
8
|
-
* V8 plus the runtime's own (`console`, `waitUntil`, timers, web classes).
|
|
9
|
-
* A host global under one of these names replaces the built-in, unless the
|
|
10
|
-
* runtime reserves the name. Names the grammar
|
|
11
|
-
* forbids outright are in {@link RESERVED_WORDS}.
|
|
12
|
-
*/
|
|
13
|
-
declare const SANDBOX_GLOBALS: ReadonlySet<string>;
|
|
6
|
+
* Every global name a fresh sandbox defines: the JS built-ins of the bundled
|
|
7
|
+
* V8 plus the runtime's own (`console`, `waitUntil`, timers, web classes).
|
|
8
|
+
* A host global under one of these names replaces the built-in, unless the
|
|
9
|
+
* runtime reserves the name. Names the grammar
|
|
10
|
+
* forbids outright are in {@link RESERVED_WORDS}.
|
|
11
|
+
*/
|
|
12
|
+
export declare const SANDBOX_GLOBALS: ReadonlySet<string>;
|
|
14
13
|
/**
|
|
15
|
-
* Names that cannot be a binding in module source. A host-module key may not
|
|
16
|
-
* use one, except `default`.
|
|
17
|
-
*/
|
|
18
|
-
declare const RESERVED_WORDS: ReadonlySet<string>;
|
|
14
|
+
* Names that cannot be a binding in module source. A host-module key may not
|
|
15
|
+
* use one, except `default`.
|
|
16
|
+
*/
|
|
17
|
+
export declare const RESERVED_WORDS: ReadonlySet<string>;
|
|
19
18
|
/**
|
|
20
|
-
* A JavaScript identifier name, Unicode included: ID_Start/ID_Continue plus
|
|
21
|
-
* `$`, `_`, ZWNJ and ZWJ. A name that matches and is not in
|
|
22
|
-
* {@link RESERVED_WORDS} can be a binding in module source, so it is a valid
|
|
23
|
-
* host-module export name.
|
|
24
|
-
*/
|
|
25
|
-
declare const IDENTIFIER_NAME: RegExp;
|
|
19
|
+
* A JavaScript identifier name, Unicode included: ID_Start/ID_Continue plus
|
|
20
|
+
* `$`, `_`, ZWNJ and ZWJ. A name that matches and is not in
|
|
21
|
+
* {@link RESERVED_WORDS} can be a binding in module source, so it is a valid
|
|
22
|
+
* host-module export name.
|
|
23
|
+
*/
|
|
24
|
+
export declare const IDENTIFIER_NAME: RegExp;
|
|
26
25
|
/**
|
|
27
|
-
* Whether `name` can be a key of a host-module object in `imports`: an
|
|
28
|
-
* identifier per {@link IDENTIFIER_NAME} that is not in
|
|
29
|
-
* {@link RESERVED_WORDS}, or `default`, which maps to the module's default
|
|
30
|
-
* export. `default` cannot be written as `export const default` in generated source.
|
|
31
|
-
* @param name candidate name
|
|
32
|
-
*/
|
|
33
|
-
declare function isValidExportName(name: string): boolean;
|
|
26
|
+
* Whether `name` can be a key of a host-module object in `imports`: an
|
|
27
|
+
* identifier per {@link IDENTIFIER_NAME} that is not in
|
|
28
|
+
* {@link RESERVED_WORDS}, or `default`, which maps to the module's default
|
|
29
|
+
* export. `default` cannot be written as `export const default` in generated source.
|
|
30
|
+
* @param name candidate name
|
|
31
|
+
*/
|
|
32
|
+
export declare function isValidExportName(name: string): boolean;
|
|
34
33
|
/**
|
|
35
|
-
* Whether `prepare()` and `run()` accept `name` as a host global name: any
|
|
36
|
-
* well-formed string that is not a runtime-owned name such as `console` or
|
|
37
|
-
* `Response`. A name that is not an identifier is reachable only as
|
|
38
|
-
* `globalThis[name]`. Names starting with `__iso4_` are iso4's internal
|
|
39
|
-
* namespace (the dispatcher, shim handlers): a host global there may be
|
|
40
|
-
* silently overwritten or dropped, and this does not flag it.
|
|
41
|
-
* @param name candidate host global name
|
|
42
|
-
*/
|
|
43
|
-
declare function isValidGlobalName(name: string): boolean;
|
|
44
|
-
//#endregion
|
|
45
|
-
export { IDENTIFIER_NAME, RESERVED_WORDS, SANDBOX_GLOBALS, isValidExportName, isValidGlobalName };
|
|
34
|
+
* Whether `prepare()` and `run()` accept `name` as a host global name: any
|
|
35
|
+
* well-formed string that is not a runtime-owned name such as `console` or
|
|
36
|
+
* `Response`. A name that is not an identifier is reachable only as
|
|
37
|
+
* `globalThis[name]`. Names starting with `__iso4_` are iso4's internal
|
|
38
|
+
* namespace (the dispatcher, shim handlers): a host global there may be
|
|
39
|
+
* silently overwritten or dropped, and this does not flag it.
|
|
40
|
+
* @param name candidate host global name
|
|
41
|
+
*/
|
|
42
|
+
export declare function isValidGlobalName(name: string): boolean;
|
package/dist/utils.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { IDENTIFIER_NAME, RESERVED_WORDS, SANDBOX_GLOBALS, isValidExportName, isValidGlobalName };
|
|
1
|
+
export { t as IDENTIFIER_NAME, n as RESERVED_WORDS, i as SANDBOX_GLOBALS, a as isValidExportName, o as isValidGlobalName } from "./names-BW1pUf2B.mjs";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iso4/sandbox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.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": [
|
|
@@ -42,21 +42,21 @@
|
|
|
42
42
|
"dist"
|
|
43
43
|
],
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@changesets/cli": "^2.
|
|
46
|
-
"@codspeed/vitest-plugin": "^
|
|
47
|
-
"@schplitt/eslint-config": "^1.
|
|
48
|
-
"@types/node": "^24.
|
|
49
|
-
"eslint": "^10.
|
|
50
|
-
"tsdown": "^0.
|
|
51
|
-
"tsnapi": "^1.
|
|
45
|
+
"@changesets/cli": "^2.31.1",
|
|
46
|
+
"@codspeed/vitest-plugin": "^6.1.1",
|
|
47
|
+
"@schplitt/eslint-config": "^1.6.0",
|
|
48
|
+
"@types/node": "^24.19.4",
|
|
49
|
+
"eslint": "^10.16.0",
|
|
50
|
+
"tsdown": "^0.23.7",
|
|
51
|
+
"tsnapi": "^1.6.1",
|
|
52
52
|
"typescript": "^6.0.3",
|
|
53
|
-
"vitest": "^4.1.
|
|
53
|
+
"vitest": "^4.1.12"
|
|
54
54
|
},
|
|
55
55
|
"optionalDependencies": {
|
|
56
|
-
"@iso4/v8-darwin-arm64": "^0.
|
|
57
|
-
"@iso4/v8-darwin-x64": "^0.
|
|
58
|
-
"@iso4/v8-linux-arm64-gnu": "^0.
|
|
59
|
-
"@iso4/v8-linux-x64-gnu": "^0.
|
|
56
|
+
"@iso4/v8-darwin-arm64": "^0.11.0",
|
|
57
|
+
"@iso4/v8-darwin-x64": "^0.11.0",
|
|
58
|
+
"@iso4/v8-linux-arm64-gnu": "^0.11.0",
|
|
59
|
+
"@iso4/v8-linux-x64-gnu": "^0.11.0"
|
|
60
60
|
},
|
|
61
61
|
"engines": {
|
|
62
62
|
"node": ">=24.0.0"
|