@jini-ai/sandbox 0.7.1 → 0.7.3-next.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/CHANGELOG.md +1 -1
- package/dist/cjs/e2b/categorize-e2b-error.d.ts +1 -1
- package/dist/cjs/e2b/categorize-e2b-error.js +1 -1
- package/dist/cjs/e2b/shell-quote.d.ts +1 -1
- package/dist/cjs/e2b/shell-quote.js +1 -1
- package/dist/cjs/e2b/to-array-buffer.d.ts +1 -1
- package/dist/cjs/e2b/to-array-buffer.js +1 -1
- package/dist/cjs/e2b/wrap-e2b-sandbox.d.ts +1 -1
- package/dist/cjs/e2b/wrap-e2b-sandbox.js +97 -53
- package/dist/cjs/node-worker.d.ts +2 -2
- package/dist/cjs/node-worker.js +2 -2
- package/dist/e2b/categorize-e2b-error.d.ts +1 -1
- package/dist/e2b/categorize-e2b-error.js +1 -1
- package/dist/e2b/shell-quote.d.ts +1 -1
- package/dist/e2b/shell-quote.js +1 -1
- package/dist/e2b/to-array-buffer.d.ts +1 -1
- package/dist/e2b/to-array-buffer.js +1 -1
- package/dist/e2b/wrap-e2b-sandbox.d.ts +1 -1
- package/dist/e2b/wrap-e2b-sandbox.js +97 -53
- package/dist/node-worker.d.ts +2 -2
- package/dist/node-worker.js +2 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
|
|
27
27
|
### Patch Changes
|
|
28
28
|
|
|
29
|
-
-
|
|
29
|
+
- Host clean-up release (2026-10-08): code the first host moved into Jini, plus the Jini clean-up (see the commit log since 0.4.0).
|
|
30
30
|
|
|
31
31
|
## 0.4.0 — 2026-10-02
|
|
32
32
|
|
|
@@ -17,5 +17,5 @@
|
|
|
17
17
|
import type { SandboxErrorCategory } from '../core/errors.js';
|
|
18
18
|
export declare function categorizeE2bError({ error }: {
|
|
19
19
|
error: unknown;
|
|
20
|
-
}): SandboxErrorCategory;
|
|
20
|
+
}, _optional?: Record<string, never>): SandboxErrorCategory;
|
|
21
21
|
//# sourceMappingURL=categorize-e2b-error.d.ts.map
|
|
@@ -18,7 +18,7 @@ const KNOWN_ERROR_NAMES = {
|
|
|
18
18
|
function nameOf(error) {
|
|
19
19
|
return error instanceof Error ? error.name : undefined;
|
|
20
20
|
}
|
|
21
|
-
function categorizeE2bError({ error }) {
|
|
21
|
+
function categorizeE2bError({ error }, _optional = {}) {
|
|
22
22
|
const name = nameOf(error);
|
|
23
23
|
if (name === undefined)
|
|
24
24
|
return 'unknown';
|
|
@@ -18,7 +18,7 @@ exports.shellQuote = shellQuote;
|
|
|
18
18
|
* any single quotes it already contains. The command name itself is not quoted by callers —
|
|
19
19
|
* only arguments — since it is expected to be a trusted program name, not caller-controlled
|
|
20
20
|
* data. */
|
|
21
|
-
function shellQuote({ value }) {
|
|
21
|
+
function shellQuote({ value }, _optional = {}) {
|
|
22
22
|
const escapedQuote = "'\\''";
|
|
23
23
|
return `'${value.replaceAll("'", escapedQuote)}'`;
|
|
24
24
|
}
|
|
@@ -14,5 +14,5 @@
|
|
|
14
14
|
* is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
|
|
15
15
|
export declare function toArrayBuffer({ bytes }: {
|
|
16
16
|
bytes: Uint8Array;
|
|
17
|
-
}): ArrayBuffer;
|
|
17
|
+
}, _optional?: Record<string, never>): ArrayBuffer;
|
|
18
18
|
//# sourceMappingURL=to-array-buffer.d.ts.map
|
|
@@ -15,7 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.toArrayBuffer = toArrayBuffer;
|
|
16
16
|
/** Returns a fresh `ArrayBuffer` containing exactly `bytes`' own range — safe even when `bytes`
|
|
17
17
|
* is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
|
|
18
|
-
function toArrayBuffer({ bytes }) {
|
|
18
|
+
function toArrayBuffer({ bytes }, _optional = {}) {
|
|
19
19
|
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
20
20
|
}
|
|
21
21
|
//# sourceMappingURL=to-array-buffer.js.map
|
|
@@ -21,7 +21,7 @@ export interface E2bSessionConfig {
|
|
|
21
21
|
*/
|
|
22
22
|
export declare function mapE2bFileChangeKind({ type }: {
|
|
23
23
|
type: E2bFilesystemEventType;
|
|
24
|
-
}): FileChangeKind | null;
|
|
24
|
+
}, _optional?: Record<string, never>): FileChangeKind | null;
|
|
25
25
|
export declare function wrapE2bSandbox({ handle, config }: {
|
|
26
26
|
handle: E2bSandboxHandle;
|
|
27
27
|
config: E2bSessionConfig;
|
|
@@ -78,7 +78,7 @@ function streamingOptsFor(options) {
|
|
|
78
78
|
* pair — a wrong create-or-delete guess would be more misleading than the conservative
|
|
79
79
|
* "something at this path changed."
|
|
80
80
|
*/
|
|
81
|
-
function mapE2bFileChangeKind({ type }) {
|
|
81
|
+
function mapE2bFileChangeKind({ type }, _optional = {}) {
|
|
82
82
|
switch (type) {
|
|
83
83
|
case 'create':
|
|
84
84
|
return 'created';
|
|
@@ -128,63 +128,43 @@ function rollbackMount({ mounted, mountedFiles }, _options = {}) {
|
|
|
128
128
|
mountedFiles.delete(path);
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (kind === null)
|
|
131
|
+
function fingerprint(content) {
|
|
132
|
+
return (0, node_crypto_1.createHash)('sha256').update(content).digest('hex');
|
|
133
|
+
}
|
|
134
|
+
function createFileWatchListener({ handle, projectRoot, fileChangeListeners, mountedFiles, mountingPaths }) {
|
|
135
|
+
return (event) => {
|
|
136
|
+
const kind = mapE2bFileChangeKind({ type: event.type });
|
|
137
|
+
if (kind === null)
|
|
138
|
+
return;
|
|
139
|
+
const path = node_path_1.posix.normalize(event.name);
|
|
140
|
+
const deliver = () => {
|
|
141
|
+
const changeEvent = { path: event.name, kind };
|
|
142
|
+
for (const listener of fileChangeListeners)
|
|
143
|
+
listener(changeEvent);
|
|
144
|
+
};
|
|
145
|
+
if (mountingPaths.has(path) && (event.type === 'create' || event.type === 'write'))
|
|
146
|
+
return;
|
|
147
|
+
if (mountedFiles.has(path) && (event.type === 'create' || event.type === 'write')) {
|
|
148
|
+
void handle.files.read({ path: node_path_1.posix.join(projectRoot, path), format: 'bytes' }).then(bytes => {
|
|
149
|
+
if (mountingPaths.has(path))
|
|
151
150
|
return;
|
|
152
|
-
|
|
153
|
-
const deliver = () => {
|
|
154
|
-
const changeEvent = { path: event.name, kind };
|
|
155
|
-
for (const listener of fileChangeListeners)
|
|
156
|
-
listener(changeEvent);
|
|
157
|
-
};
|
|
158
|
-
if (mountingPaths.has(path) && (event.type === 'create' || event.type === 'write'))
|
|
151
|
+
if (mountedFiles.get(path)?.fingerprint === fingerprint(bytes))
|
|
159
152
|
return;
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (mountedFiles.get(path)?.fingerprint === fingerprint(bytes))
|
|
165
|
-
return;
|
|
166
|
-
mountedFiles.delete(path);
|
|
167
|
-
deliver();
|
|
168
|
-
}, () => {
|
|
169
|
-
if (mountingPaths.has(path))
|
|
170
|
-
return;
|
|
171
|
-
mountedFiles.delete(path);
|
|
172
|
-
deliver();
|
|
173
|
-
});
|
|
153
|
+
mountedFiles.delete(path);
|
|
154
|
+
deliver();
|
|
155
|
+
}, () => {
|
|
156
|
+
if (mountingPaths.has(path))
|
|
174
157
|
return;
|
|
175
|
-
}
|
|
176
158
|
mountedFiles.delete(path);
|
|
177
159
|
deliver();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
* so `teardown` doesn't redundantly re-kill it; failed kills stay tracked for a retry. */
|
|
187
|
-
const trackedProcesses = new Set();
|
|
160
|
+
});
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
mountedFiles.delete(path);
|
|
164
|
+
deliver();
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function createFileMounter({ handle, projectRoot, mountedFiles, mountingPaths }) {
|
|
188
168
|
return {
|
|
189
169
|
async mountFiles({ files }) {
|
|
190
170
|
if (files.length === 0)
|
|
@@ -224,6 +204,10 @@ async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
|
224
204
|
}
|
|
225
205
|
}
|
|
226
206
|
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function createFileReaders({ handle, projectRoot }) {
|
|
210
|
+
return {
|
|
227
211
|
async readFile({ path }) {
|
|
228
212
|
const resolved = resolveProjectPath({ projectRoot, path });
|
|
229
213
|
try {
|
|
@@ -247,6 +231,10 @@ async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
|
247
231
|
.map((entry) => relativeToRoot(entry.path, projectRoot))
|
|
248
232
|
.filter((relativePath) => !isExcludedPath(relativePath));
|
|
249
233
|
},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function createCommandRunners({ handle, projectRoot }) {
|
|
237
|
+
return {
|
|
250
238
|
async runCommand({ command }, options = {}) {
|
|
251
239
|
try {
|
|
252
240
|
const result = await handle.commands.run({ command: buildCommand(command, options.args ?? []) }, {
|
|
@@ -275,6 +263,10 @@ async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
|
275
263
|
throw wrapE2bError(error, 'npm install failed');
|
|
276
264
|
}
|
|
277
265
|
},
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function createProcessStarter({ handle, projectRoot, trackedProcesses }) {
|
|
269
|
+
return {
|
|
278
270
|
async startProcess({ command }, { args = [] } = {}) {
|
|
279
271
|
const outputListeners = new Set();
|
|
280
272
|
let e2bHandle;
|
|
@@ -312,6 +304,10 @@ async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
|
312
304
|
trackedProcesses.add(processHandle);
|
|
313
305
|
return processHandle;
|
|
314
306
|
},
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
function createPreviewReader({ handle, previewPort, previewCheckTimeoutMs, fetchImpl }) {
|
|
310
|
+
return {
|
|
315
311
|
async getPreview() {
|
|
316
312
|
const url = `https://${handle.getHost({ port: previewPort })}`;
|
|
317
313
|
const controller = new AbortController();
|
|
@@ -329,10 +325,18 @@ async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
|
329
325
|
}
|
|
330
326
|
return { url };
|
|
331
327
|
},
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function createFileChangeSubscription({ fileChangeListeners }) {
|
|
331
|
+
return {
|
|
332
332
|
onFileChange({ listener }) {
|
|
333
333
|
fileChangeListeners.add(listener);
|
|
334
334
|
return () => fileChangeListeners.delete(listener);
|
|
335
335
|
},
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
function createSessionTeardown({ handle, watchHandle, trackedProcesses, mountedFiles }) {
|
|
339
|
+
return {
|
|
336
340
|
async teardown() {
|
|
337
341
|
// Best-effort: every tracked process gets a kill attempt regardless of whether another
|
|
338
342
|
// one fails. A process that refuses to die is a different problem than this adapter can
|
|
@@ -363,4 +367,44 @@ async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
|
363
367
|
},
|
|
364
368
|
};
|
|
365
369
|
}
|
|
370
|
+
async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
371
|
+
const { projectRoot, previewPort, previewCheckTimeoutMs } = config;
|
|
372
|
+
try {
|
|
373
|
+
await handle.commands.run({ command: `mkdir -p ${(0, shell_quote_js_1.shellQuote)({ value: projectRoot })}` });
|
|
374
|
+
}
|
|
375
|
+
catch (error) {
|
|
376
|
+
throw wrapE2bError(error, 'Failed to create the sandbox project root');
|
|
377
|
+
}
|
|
378
|
+
const fileChangeListeners = new Set();
|
|
379
|
+
// The watcher carries no writer identity and can deliver echoes after write() resolves.
|
|
380
|
+
// Keep content fingerprints, not file bodies, to suppress those delayed host echoes while
|
|
381
|
+
// still reporting subsequent sandbox edits. Same-content rewrites are indistinguishable.
|
|
382
|
+
const mountedFiles = new Map();
|
|
383
|
+
const mountingPaths = new Map();
|
|
384
|
+
let watchHandle;
|
|
385
|
+
try {
|
|
386
|
+
watchHandle = await handle.files.watchDir({ path: projectRoot,
|
|
387
|
+
onEvent: createFileWatchListener({ handle, projectRoot, fileChangeListeners, mountedFiles, mountingPaths }),
|
|
388
|
+
}, { recursive: true });
|
|
389
|
+
}
|
|
390
|
+
catch (error) {
|
|
391
|
+
throw wrapE2bError(error, 'Failed to start watching the sandbox project root');
|
|
392
|
+
}
|
|
393
|
+
/** Every process this session has started via `startProcess`, so `teardown` can kill them
|
|
394
|
+
* all — see `SandboxSession.teardown`'s doc for why an orphaned dev server on `local` is a
|
|
395
|
+
* real liability, not just untidy. A process removes itself once its own `kill()` succeeds,
|
|
396
|
+
* so `teardown` doesn't redundantly re-kill it; failed kills stay tracked for a retry. */
|
|
397
|
+
const trackedProcesses = new Set();
|
|
398
|
+
const state = { handle, projectRoot, previewPort, previewCheckTimeoutMs, fetchImpl,
|
|
399
|
+
fileChangeListeners, mountedFiles, mountingPaths, watchHandle, trackedProcesses };
|
|
400
|
+
return {
|
|
401
|
+
...createFileMounter(state),
|
|
402
|
+
...createFileReaders(state),
|
|
403
|
+
...createCommandRunners(state),
|
|
404
|
+
...createProcessStarter(state),
|
|
405
|
+
...createPreviewReader(state),
|
|
406
|
+
...createFileChangeSubscription(state),
|
|
407
|
+
...createSessionTeardown(state),
|
|
408
|
+
};
|
|
409
|
+
}
|
|
366
410
|
//# sourceMappingURL=wrap-e2b-sandbox.js.map
|
|
@@ -90,7 +90,7 @@ export declare function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, ma
|
|
|
90
90
|
rawValue: string | undefined;
|
|
91
91
|
defaultTimeoutMs: number;
|
|
92
92
|
maxTimeoutMs: number;
|
|
93
|
-
}): number;
|
|
93
|
+
}, _optional?: Record<string, never>): number;
|
|
94
94
|
/**
|
|
95
95
|
* Run structured-cloneable payloads in a fresh bounded worker using a caller-owned result codec.
|
|
96
96
|
* @param required Entry, payload, budgets and worker/timer/decoder ports.
|
|
@@ -141,5 +141,5 @@ export declare function createNodeWorkerFactory({ env }: {
|
|
|
141
141
|
* @returns WorkerScheduler using native timers.
|
|
142
142
|
* @complexity O(1) per schedule/cancel.
|
|
143
143
|
*/
|
|
144
|
-
export declare function createNodeWorkerScheduler(_required: Record<string, never>): WorkerScheduler;
|
|
144
|
+
export declare function createNodeWorkerScheduler(_required: Record<string, never>, _optional?: Record<string, never>): WorkerScheduler;
|
|
145
145
|
//# sourceMappingURL=node-worker.d.ts.map
|
package/dist/cjs/node-worker.js
CHANGED
|
@@ -57,7 +57,7 @@ function createWorkerCapacity({ maxConcurrent }, _options = {}) {
|
|
|
57
57
|
*/
|
|
58
58
|
// Do not use parseInt: "5e3" becomes 5 milliseconds and "60000ms" silently becomes
|
|
59
59
|
// 60000. A malformed configuration must fall back, never masquerade as a deliberate budget.
|
|
60
|
-
function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, maxTimeoutMs }) {
|
|
60
|
+
function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, maxTimeoutMs }, _optional = {}) {
|
|
61
61
|
if (rawValue === undefined || !/^\d+$/.test(rawValue.trim()))
|
|
62
62
|
return defaultTimeoutMs;
|
|
63
63
|
const parsed = Number(rawValue.trim());
|
|
@@ -212,7 +212,7 @@ function createNodeWorkerFactory({ env }, options = {}) {
|
|
|
212
212
|
* @returns WorkerScheduler using native timers.
|
|
213
213
|
* @complexity O(1) per schedule/cancel.
|
|
214
214
|
*/
|
|
215
|
-
function createNodeWorkerScheduler(_required) {
|
|
215
|
+
function createNodeWorkerScheduler(_required, _optional = {}) {
|
|
216
216
|
return { schedule: ({ delayMs, run }) => { const timer = setTimeout(run, delayMs); return () => clearTimeout(timer); } };
|
|
217
217
|
}
|
|
218
218
|
//# sourceMappingURL=node-worker.js.map
|
|
@@ -17,5 +17,5 @@
|
|
|
17
17
|
import type { SandboxErrorCategory } from '../core/errors.js';
|
|
18
18
|
export declare function categorizeE2bError({ error }: {
|
|
19
19
|
error: unknown;
|
|
20
|
-
}): SandboxErrorCategory;
|
|
20
|
+
}, _optional?: Record<string, never>): SandboxErrorCategory;
|
|
21
21
|
//# sourceMappingURL=categorize-e2b-error.d.ts.map
|
|
@@ -15,7 +15,7 @@ const KNOWN_ERROR_NAMES = {
|
|
|
15
15
|
function nameOf(error) {
|
|
16
16
|
return error instanceof Error ? error.name : undefined;
|
|
17
17
|
}
|
|
18
|
-
export function categorizeE2bError({ error }) {
|
|
18
|
+
export function categorizeE2bError({ error }, _optional = {}) {
|
|
19
19
|
const name = nameOf(error);
|
|
20
20
|
if (name === undefined)
|
|
21
21
|
return 'unknown';
|
package/dist/e2b/shell-quote.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* any single quotes it already contains. The command name itself is not quoted by callers —
|
|
16
16
|
* only arguments — since it is expected to be a trusted program name, not caller-controlled
|
|
17
17
|
* data. */
|
|
18
|
-
export function shellQuote({ value }) {
|
|
18
|
+
export function shellQuote({ value }, _optional = {}) {
|
|
19
19
|
const escapedQuote = "'\\''";
|
|
20
20
|
return `'${value.replaceAll("'", escapedQuote)}'`;
|
|
21
21
|
}
|
|
@@ -14,5 +14,5 @@
|
|
|
14
14
|
* is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
|
|
15
15
|
export declare function toArrayBuffer({ bytes }: {
|
|
16
16
|
bytes: Uint8Array;
|
|
17
|
-
}): ArrayBuffer;
|
|
17
|
+
}, _optional?: Record<string, never>): ArrayBuffer;
|
|
18
18
|
//# sourceMappingURL=to-array-buffer.d.ts.map
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
/** Returns a fresh `ArrayBuffer` containing exactly `bytes`' own range — safe even when `bytes`
|
|
14
14
|
* is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
|
|
15
|
-
export function toArrayBuffer({ bytes }) {
|
|
15
|
+
export function toArrayBuffer({ bytes }, _optional = {}) {
|
|
16
16
|
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
17
17
|
}
|
|
18
18
|
//# sourceMappingURL=to-array-buffer.js.map
|
|
@@ -21,7 +21,7 @@ export interface E2bSessionConfig {
|
|
|
21
21
|
*/
|
|
22
22
|
export declare function mapE2bFileChangeKind({ type }: {
|
|
23
23
|
type: E2bFilesystemEventType;
|
|
24
|
-
}): FileChangeKind | null;
|
|
24
|
+
}, _optional?: Record<string, never>): FileChangeKind | null;
|
|
25
25
|
export declare function wrapE2bSandbox({ handle, config }: {
|
|
26
26
|
handle: E2bSandboxHandle;
|
|
27
27
|
config: E2bSessionConfig;
|
|
@@ -74,7 +74,7 @@ function streamingOptsFor(options) {
|
|
|
74
74
|
* pair — a wrong create-or-delete guess would be more misleading than the conservative
|
|
75
75
|
* "something at this path changed."
|
|
76
76
|
*/
|
|
77
|
-
export function mapE2bFileChangeKind({ type }) {
|
|
77
|
+
export function mapE2bFileChangeKind({ type }, _optional = {}) {
|
|
78
78
|
switch (type) {
|
|
79
79
|
case 'create':
|
|
80
80
|
return 'created';
|
|
@@ -124,63 +124,43 @@ function rollbackMount({ mounted, mountedFiles }, _options = {}) {
|
|
|
124
124
|
mountedFiles.delete(path);
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (kind === null)
|
|
127
|
+
function fingerprint(content) {
|
|
128
|
+
return createHash('sha256').update(content).digest('hex');
|
|
129
|
+
}
|
|
130
|
+
function createFileWatchListener({ handle, projectRoot, fileChangeListeners, mountedFiles, mountingPaths }) {
|
|
131
|
+
return (event) => {
|
|
132
|
+
const kind = mapE2bFileChangeKind({ type: event.type });
|
|
133
|
+
if (kind === null)
|
|
134
|
+
return;
|
|
135
|
+
const path = posix.normalize(event.name);
|
|
136
|
+
const deliver = () => {
|
|
137
|
+
const changeEvent = { path: event.name, kind };
|
|
138
|
+
for (const listener of fileChangeListeners)
|
|
139
|
+
listener(changeEvent);
|
|
140
|
+
};
|
|
141
|
+
if (mountingPaths.has(path) && (event.type === 'create' || event.type === 'write'))
|
|
142
|
+
return;
|
|
143
|
+
if (mountedFiles.has(path) && (event.type === 'create' || event.type === 'write')) {
|
|
144
|
+
void handle.files.read({ path: posix.join(projectRoot, path), format: 'bytes' }).then(bytes => {
|
|
145
|
+
if (mountingPaths.has(path))
|
|
147
146
|
return;
|
|
148
|
-
|
|
149
|
-
const deliver = () => {
|
|
150
|
-
const changeEvent = { path: event.name, kind };
|
|
151
|
-
for (const listener of fileChangeListeners)
|
|
152
|
-
listener(changeEvent);
|
|
153
|
-
};
|
|
154
|
-
if (mountingPaths.has(path) && (event.type === 'create' || event.type === 'write'))
|
|
147
|
+
if (mountedFiles.get(path)?.fingerprint === fingerprint(bytes))
|
|
155
148
|
return;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (mountedFiles.get(path)?.fingerprint === fingerprint(bytes))
|
|
161
|
-
return;
|
|
162
|
-
mountedFiles.delete(path);
|
|
163
|
-
deliver();
|
|
164
|
-
}, () => {
|
|
165
|
-
if (mountingPaths.has(path))
|
|
166
|
-
return;
|
|
167
|
-
mountedFiles.delete(path);
|
|
168
|
-
deliver();
|
|
169
|
-
});
|
|
149
|
+
mountedFiles.delete(path);
|
|
150
|
+
deliver();
|
|
151
|
+
}, () => {
|
|
152
|
+
if (mountingPaths.has(path))
|
|
170
153
|
return;
|
|
171
|
-
}
|
|
172
154
|
mountedFiles.delete(path);
|
|
173
155
|
deliver();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
* so `teardown` doesn't redundantly re-kill it; failed kills stay tracked for a retry. */
|
|
183
|
-
const trackedProcesses = new Set();
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
mountedFiles.delete(path);
|
|
160
|
+
deliver();
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function createFileMounter({ handle, projectRoot, mountedFiles, mountingPaths }) {
|
|
184
164
|
return {
|
|
185
165
|
async mountFiles({ files }) {
|
|
186
166
|
if (files.length === 0)
|
|
@@ -220,6 +200,10 @@ export async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } =
|
|
|
220
200
|
}
|
|
221
201
|
}
|
|
222
202
|
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function createFileReaders({ handle, projectRoot }) {
|
|
206
|
+
return {
|
|
223
207
|
async readFile({ path }) {
|
|
224
208
|
const resolved = resolveProjectPath({ projectRoot, path });
|
|
225
209
|
try {
|
|
@@ -243,6 +227,10 @@ export async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } =
|
|
|
243
227
|
.map((entry) => relativeToRoot(entry.path, projectRoot))
|
|
244
228
|
.filter((relativePath) => !isExcludedPath(relativePath));
|
|
245
229
|
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function createCommandRunners({ handle, projectRoot }) {
|
|
233
|
+
return {
|
|
246
234
|
async runCommand({ command }, options = {}) {
|
|
247
235
|
try {
|
|
248
236
|
const result = await handle.commands.run({ command: buildCommand(command, options.args ?? []) }, {
|
|
@@ -271,6 +259,10 @@ export async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } =
|
|
|
271
259
|
throw wrapE2bError(error, 'npm install failed');
|
|
272
260
|
}
|
|
273
261
|
},
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function createProcessStarter({ handle, projectRoot, trackedProcesses }) {
|
|
265
|
+
return {
|
|
274
266
|
async startProcess({ command }, { args = [] } = {}) {
|
|
275
267
|
const outputListeners = new Set();
|
|
276
268
|
let e2bHandle;
|
|
@@ -308,6 +300,10 @@ export async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } =
|
|
|
308
300
|
trackedProcesses.add(processHandle);
|
|
309
301
|
return processHandle;
|
|
310
302
|
},
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
function createPreviewReader({ handle, previewPort, previewCheckTimeoutMs, fetchImpl }) {
|
|
306
|
+
return {
|
|
311
307
|
async getPreview() {
|
|
312
308
|
const url = `https://${handle.getHost({ port: previewPort })}`;
|
|
313
309
|
const controller = new AbortController();
|
|
@@ -325,10 +321,18 @@ export async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } =
|
|
|
325
321
|
}
|
|
326
322
|
return { url };
|
|
327
323
|
},
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function createFileChangeSubscription({ fileChangeListeners }) {
|
|
327
|
+
return {
|
|
328
328
|
onFileChange({ listener }) {
|
|
329
329
|
fileChangeListeners.add(listener);
|
|
330
330
|
return () => fileChangeListeners.delete(listener);
|
|
331
331
|
},
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function createSessionTeardown({ handle, watchHandle, trackedProcesses, mountedFiles }) {
|
|
335
|
+
return {
|
|
332
336
|
async teardown() {
|
|
333
337
|
// Best-effort: every tracked process gets a kill attempt regardless of whether another
|
|
334
338
|
// one fails. A process that refuses to die is a different problem than this adapter can
|
|
@@ -359,4 +363,44 @@ export async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } =
|
|
|
359
363
|
},
|
|
360
364
|
};
|
|
361
365
|
}
|
|
366
|
+
export async function wrapE2bSandbox({ handle, config }, { fetchImpl = fetch } = {}) {
|
|
367
|
+
const { projectRoot, previewPort, previewCheckTimeoutMs } = config;
|
|
368
|
+
try {
|
|
369
|
+
await handle.commands.run({ command: `mkdir -p ${shellQuote({ value: projectRoot })}` });
|
|
370
|
+
}
|
|
371
|
+
catch (error) {
|
|
372
|
+
throw wrapE2bError(error, 'Failed to create the sandbox project root');
|
|
373
|
+
}
|
|
374
|
+
const fileChangeListeners = new Set();
|
|
375
|
+
// The watcher carries no writer identity and can deliver echoes after write() resolves.
|
|
376
|
+
// Keep content fingerprints, not file bodies, to suppress those delayed host echoes while
|
|
377
|
+
// still reporting subsequent sandbox edits. Same-content rewrites are indistinguishable.
|
|
378
|
+
const mountedFiles = new Map();
|
|
379
|
+
const mountingPaths = new Map();
|
|
380
|
+
let watchHandle;
|
|
381
|
+
try {
|
|
382
|
+
watchHandle = await handle.files.watchDir({ path: projectRoot,
|
|
383
|
+
onEvent: createFileWatchListener({ handle, projectRoot, fileChangeListeners, mountedFiles, mountingPaths }),
|
|
384
|
+
}, { recursive: true });
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
throw wrapE2bError(error, 'Failed to start watching the sandbox project root');
|
|
388
|
+
}
|
|
389
|
+
/** Every process this session has started via `startProcess`, so `teardown` can kill them
|
|
390
|
+
* all — see `SandboxSession.teardown`'s doc for why an orphaned dev server on `local` is a
|
|
391
|
+
* real liability, not just untidy. A process removes itself once its own `kill()` succeeds,
|
|
392
|
+
* so `teardown` doesn't redundantly re-kill it; failed kills stay tracked for a retry. */
|
|
393
|
+
const trackedProcesses = new Set();
|
|
394
|
+
const state = { handle, projectRoot, previewPort, previewCheckTimeoutMs, fetchImpl,
|
|
395
|
+
fileChangeListeners, mountedFiles, mountingPaths, watchHandle, trackedProcesses };
|
|
396
|
+
return {
|
|
397
|
+
...createFileMounter(state),
|
|
398
|
+
...createFileReaders(state),
|
|
399
|
+
...createCommandRunners(state),
|
|
400
|
+
...createProcessStarter(state),
|
|
401
|
+
...createPreviewReader(state),
|
|
402
|
+
...createFileChangeSubscription(state),
|
|
403
|
+
...createSessionTeardown(state),
|
|
404
|
+
};
|
|
405
|
+
}
|
|
362
406
|
//# sourceMappingURL=wrap-e2b-sandbox.js.map
|
package/dist/node-worker.d.ts
CHANGED
|
@@ -90,7 +90,7 @@ export declare function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, ma
|
|
|
90
90
|
rawValue: string | undefined;
|
|
91
91
|
defaultTimeoutMs: number;
|
|
92
92
|
maxTimeoutMs: number;
|
|
93
|
-
}): number;
|
|
93
|
+
}, _optional?: Record<string, never>): number;
|
|
94
94
|
/**
|
|
95
95
|
* Run structured-cloneable payloads in a fresh bounded worker using a caller-owned result codec.
|
|
96
96
|
* @param required Entry, payload, budgets and worker/timer/decoder ports.
|
|
@@ -141,5 +141,5 @@ export declare function createNodeWorkerFactory({ env }: {
|
|
|
141
141
|
* @returns WorkerScheduler using native timers.
|
|
142
142
|
* @complexity O(1) per schedule/cancel.
|
|
143
143
|
*/
|
|
144
|
-
export declare function createNodeWorkerScheduler(_required: Record<string, never>): WorkerScheduler;
|
|
144
|
+
export declare function createNodeWorkerScheduler(_required: Record<string, never>, _optional?: Record<string, never>): WorkerScheduler;
|
|
145
145
|
//# sourceMappingURL=node-worker.d.ts.map
|
package/dist/node-worker.js
CHANGED
|
@@ -47,7 +47,7 @@ export function createWorkerCapacity({ maxConcurrent }, _options = {}) {
|
|
|
47
47
|
*/
|
|
48
48
|
// Do not use parseInt: "5e3" becomes 5 milliseconds and "60000ms" silently becomes
|
|
49
49
|
// 60000. A malformed configuration must fall back, never masquerade as a deliberate budget.
|
|
50
|
-
export function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, maxTimeoutMs }) {
|
|
50
|
+
export function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, maxTimeoutMs }, _optional = {}) {
|
|
51
51
|
if (rawValue === undefined || !/^\d+$/.test(rawValue.trim()))
|
|
52
52
|
return defaultTimeoutMs;
|
|
53
53
|
const parsed = Number(rawValue.trim());
|
|
@@ -202,7 +202,7 @@ export function createNodeWorkerFactory({ env }, options = {}) {
|
|
|
202
202
|
* @returns WorkerScheduler using native timers.
|
|
203
203
|
* @complexity O(1) per schedule/cancel.
|
|
204
204
|
*/
|
|
205
|
-
export function createNodeWorkerScheduler(_required) {
|
|
205
|
+
export function createNodeWorkerScheduler(_required, _optional = {}) {
|
|
206
206
|
return { schedule: ({ delayMs, run }) => { const timer = setTimeout(run, delayMs); return () => clearTimeout(timer); } };
|
|
207
207
|
}
|
|
208
208
|
//# sourceMappingURL=node-worker.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/sandbox",
|
|
3
|
-
"version": "0.7.1",
|
|
3
|
+
"version": "0.7.3-next.1",
|
|
4
4
|
"description": "Execution/sandbox sibling to @jini-ai/vibecoding's edit loop: boot an environment, mount files, install deps, run commands, preview, and tear down — behind one interface with a driver-neutral core. Import `./core` with no adapter installed; add `./e2b` for the E2B (Firecracker microVM) backend.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|