Skip to content

Commit 87129c4

Browse files
NathanFlurryclaude
andcommitted
refactor: add createNodeV8Runtime wrapper, remove @secure-exec/v8 from public API
createNodeV8Runtime() in @secure-exec/node bakes in bridge code and warm pool defaults (pool of 3, 128MB heap). Callers no longer need to import @secure-exec/v8 directly. Updated benchmarks, docs, and internal refs to use the new wrapper. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cdc58d0 commit 87129c4

9 files changed

Lines changed: 415 additions & 152 deletions

File tree

docs-internal/specs/v8-runtime.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ await jail.set("_fsReadFile", new ivm.Reference(async (path) => { ... }));
473473
const script = await isolate.compileScript(code);
474474
await script.run(context);
475475

476-
// After (@secure-exec/v8)
477-
import { createV8Runtime } from "@secure-exec/v8";
478-
const runtime = createV8Runtime();
476+
// After (@secure-exec/node)
477+
import { createNodeV8Runtime } from "@secure-exec/node";
478+
const runtime = createNodeV8Runtime();
479479
const session = runtime.createSession({ heapLimitMb: 128 });
480480
const result = await session.execute(code, {
481481
_fsReadFile: async (path: string) => { ... },

docs/process-isolation.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ NodeRuntime C ──── Tenant 2 Process (sessions: C1)
6262
```
6363

6464
```ts
65-
import { createV8Runtime } from "@secure-exec/v8";
65+
import { createNodeV8Runtime } from "@secure-exec/node";
6666
import {
6767
NodeRuntime,
6868
createNodeDriver,
6969
createNodeRuntimeDriverFactory,
7070
} from "@secure-exec/node";
7171

7272
// Create a dedicated process for this tenant
73-
const tenantProcess = await createV8Runtime({ maxSessions: 10 });
73+
const tenantProcess = await createNodeV8Runtime({ maxSessions: 10 });
7474

7575
const rt1 = new NodeRuntime({
7676
systemDriver: createNodeDriver(),
@@ -95,15 +95,15 @@ NodeRuntime B ──── Process B (session: B1)
9595
```
9696

9797
```ts
98-
import { createV8Runtime } from "@secure-exec/v8";
98+
import { createNodeV8Runtime } from "@secure-exec/node";
9999
import {
100100
NodeRuntime,
101101
createNodeDriver,
102102
createNodeRuntimeDriverFactory,
103103
} from "@secure-exec/node";
104104

105-
const processA = await createV8Runtime();
106-
const processB = await createV8Runtime();
105+
const processA = await createNodeV8Runtime();
106+
const processB = await createNodeV8Runtime();
107107

108108
const rtA = new NodeRuntime({
109109
systemDriver: createNodeDriver(),
@@ -132,7 +132,7 @@ Choose based on your isolation requirements. The shared topology is the most mem
132132

133133
```ts
134134
// This process allows up to 5 concurrent sessions
135-
const process = await createV8Runtime({ maxSessions: 5 });
135+
const process = await createNodeV8Runtime({ maxSessions: 5 });
136136

137137
// Both runtimes share the 5-session budget
138138
const rt1 = new NodeRuntime({
@@ -157,7 +157,7 @@ When a V8 process crashes (OOM, segfault, panic):
157157
3. **New sessions cannot be created on the crashed process.** Create a new `V8Runtime` to recover.
158158

159159
```ts
160-
const process = await createV8Runtime();
160+
const process = await createNodeV8Runtime();
161161
const factory = createNodeRuntimeDriverFactory({ v8Runtime: process });
162162

163163
const rt = new NodeRuntime({
@@ -171,14 +171,14 @@ const result = await rt.exec("const a = []; while(true) a.push(new Array(1e6))")
171171
// Host process is still alive
172172
```
173173

174-
Runtimes using the default shared process share crash fate — if the global process dies, all runtimes are affected. Use explicit `createV8Runtime()` handles to control which runtimes share a crash domain.
174+
Runtimes using the default shared process share crash fate — if the global process dies, all runtimes are affected. Use explicit `createNodeV8Runtime()` handles to control which runtimes share a crash domain.
175175

176176
## Warm pool
177177

178178
Cold-starting a new session costs ~6ms (thread spawn + isolate creation from snapshot). The warm pool eliminates this by pre-creating sessions with isolates already initialized.
179179

180180
```ts
181-
const process = await createV8Runtime({
181+
const process = await createNodeV8Runtime({
182182
warmupBridgeCode: bridgeCode,
183183
warmPoolSize: 3, // default: 3 when bridgeCode provided, 0 otherwise
184184
defaultWarmHeapLimitMb: 128,
@@ -199,7 +199,7 @@ Set `warmPoolSize: 0` to disable the pool entirely (falls back to the cold path
199199
The caller owns the `V8Runtime` handle and is responsible for disposing it when done.
200200

201201
```ts
202-
const process = await createV8Runtime();
202+
const process = await createNodeV8Runtime();
203203

204204
// ... use process ...
205205

packages/secure-exec-node/src/bridge-handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
RESOURCE_BUDGET_ERROR_CODE,
3535
} from "./isolate-bootstrap.js";
3636
import type { DriverDeps } from "./isolate-bootstrap.js";
37-
import type { BridgeHandlers } from "@secure-exec/v8";
37+
import type { BridgeHandlers } from "./execution-driver.js";
3838
import type { StdioHook, StdioEvent } from "@secure-exec/core/internal/shared/api-types";
3939

4040
// Estimate serialized size of a network response object for payload limit checks

packages/secure-exec-node/src/driver.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export interface NodeDriverOptions {
4040

4141
export interface NodeRuntimeDriverFactoryOptions {
4242
createIsolate?(memoryLimit: number): unknown;
43+
/** V8 runtime process to use for sessions.
44+
* If omitted, uses the global shared process (current behavior). */
45+
v8Runtime?: import("./execution-driver.js").V8Runtime;
4346
}
4447

4548
/** Thin VFS adapter that delegates directly to `node:fs/promises`. */
@@ -777,6 +780,7 @@ export function createNodeRuntimeDriverFactory(
777780
new NodeExecutionDriver({
778781
...runtimeOptions,
779782
createIsolate: options.createIsolate,
783+
v8Runtime: options.v8Runtime,
780784
}),
781785
};
782786
}

0 commit comments

Comments
 (0)