@jini-ai/sandbox 0.4.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/CHANGELOG.md +16 -0
- package/LICENSE +201 -0
- package/README.md +209 -0
- package/dist/cjs/core/errors.d.ts +56 -0
- package/dist/cjs/core/errors.js +40 -0
- package/dist/cjs/core/index.d.ts +20 -0
- package/dist/cjs/core/index.js +22 -0
- package/dist/cjs/core/ports.d.ts +232 -0
- package/dist/cjs/core/ports.js +55 -0
- package/dist/cjs/e2b/categorize-e2b-error.d.ts +21 -0
- package/dist/cjs/e2b/categorize-e2b-error.js +27 -0
- package/dist/cjs/e2b/default-vite-react-template.d.ts +26 -0
- package/dist/cjs/e2b/default-vite-react-template.js +122 -0
- package/dist/cjs/e2b/e2b-sandbox-handle.d.ts +131 -0
- package/dist/cjs/e2b/e2b-sandbox-handle.js +28 -0
- package/dist/cjs/e2b/index.d.ts +18 -0
- package/dist/cjs/e2b/index.js +25 -0
- package/dist/cjs/e2b/provider.d.ts +63 -0
- package/dist/cjs/e2b/provider.js +92 -0
- package/dist/cjs/e2b/shell-quote.d.ts +21 -0
- package/dist/cjs/e2b/shell-quote.js +24 -0
- package/dist/cjs/e2b/to-array-buffer.d.ts +18 -0
- package/dist/cjs/e2b/to-array-buffer.js +21 -0
- package/dist/cjs/e2b/wrap-e2b-sandbox.d.ts +31 -0
- package/dist/cjs/e2b/wrap-e2b-sandbox.js +347 -0
- package/dist/cjs/node-worker.d.ts +111 -0
- package/dist/cjs/node-worker.js +157 -0
- package/dist/cjs/package.json +1 -0
- package/dist/core/errors.d.ts +56 -0
- package/dist/core/errors.js +36 -0
- package/dist/core/index.d.ts +20 -0
- package/dist/core/index.js +18 -0
- package/dist/core/ports.d.ts +232 -0
- package/dist/core/ports.js +54 -0
- package/dist/e2b/categorize-e2b-error.d.ts +21 -0
- package/dist/e2b/categorize-e2b-error.js +24 -0
- package/dist/e2b/default-vite-react-template.d.ts +26 -0
- package/dist/e2b/default-vite-react-template.js +119 -0
- package/dist/e2b/e2b-sandbox-handle.d.ts +131 -0
- package/dist/e2b/e2b-sandbox-handle.js +27 -0
- package/dist/e2b/index.d.ts +18 -0
- package/dist/e2b/index.js +15 -0
- package/dist/e2b/provider.d.ts +63 -0
- package/dist/e2b/provider.js +88 -0
- package/dist/e2b/shell-quote.d.ts +21 -0
- package/dist/e2b/shell-quote.js +21 -0
- package/dist/e2b/to-array-buffer.d.ts +18 -0
- package/dist/e2b/to-array-buffer.js +18 -0
- package/dist/e2b/wrap-e2b-sandbox.d.ts +31 -0
- package/dist/e2b/wrap-e2b-sandbox.js +343 -0
- package/dist/node-worker.d.ts +111 -0
- package/dist/node-worker.js +150 -0
- package/package.json +106 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The narrow slice of the E2B SDK this adapter actually calls.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* `@e2b/code-interpreter`'s real `Sandbox` class is much bigger than what this adapter needs
|
|
6
|
+
* (code-execution contexts, chart rendering, Jupyter — none of it relevant to running `npm`
|
|
7
|
+
* commands and a dev server). Depending on this narrower shape instead of the SDK's own type
|
|
8
|
+
* keeps the wrapping logic in `wrap-e2b-sandbox.ts` testable with a plain object literal: no
|
|
9
|
+
* network call, no API key, and no risk of a test silently depending on SDK internals this
|
|
10
|
+
* adapter never touches.
|
|
11
|
+
*
|
|
12
|
+
* Most of this interface is structurally satisfied by a real `Sandbox` instance directly.
|
|
13
|
+
* `files.write` is the one exception: E2B's real `write` is overloaded (a single-file form plus
|
|
14
|
+
* this interface's batch form), and TypeScript's assignability check between an overloaded
|
|
15
|
+
* source and a single-signature target does not reliably pick the matching overload, producing
|
|
16
|
+
* a confusing arity error rather than a real incompatibility. `provider.ts`'s `toE2bHandle`
|
|
17
|
+
* adapts the real `Sandbox` into this interface with one explicit wrapper function for that
|
|
18
|
+
* reason, rather than passing the SDK object straight through.
|
|
19
|
+
*
|
|
20
|
+
* Architectural role:
|
|
21
|
+
* The one place this package's `/e2b` subpath writes down what it assumes E2B's API looks like.
|
|
22
|
+
* If a real `@e2b/code-interpreter` upgrade changes these members, `provider.ts` (which is typed
|
|
23
|
+
* against both this interface and the SDK's real `Sandbox` type) fails to compile — a fast,
|
|
24
|
+
* local signal, rather than a runtime surprise inside `wrap-e2b-sandbox.ts`.
|
|
25
|
+
*/
|
|
26
|
+
/** The subset of E2B's `CommandResult` this adapter reads. */
|
|
27
|
+
export interface E2bCommandResult {
|
|
28
|
+
readonly stdout: string;
|
|
29
|
+
readonly stderr: string;
|
|
30
|
+
readonly exitCode: number;
|
|
31
|
+
}
|
|
32
|
+
/** The subset of E2B's background `CommandHandle` this adapter reads. */
|
|
33
|
+
export interface E2bCommandHandle {
|
|
34
|
+
kill(): Promise<boolean>;
|
|
35
|
+
}
|
|
36
|
+
export interface E2bRunOptions {
|
|
37
|
+
readonly background?: boolean;
|
|
38
|
+
readonly cwd?: string;
|
|
39
|
+
readonly onStdout?: (requiredArgs: {
|
|
40
|
+
data: string;
|
|
41
|
+
}) => void;
|
|
42
|
+
readonly onStderr?: (requiredArgs: {
|
|
43
|
+
data: string;
|
|
44
|
+
}) => void;
|
|
45
|
+
}
|
|
46
|
+
export interface E2bCommands {
|
|
47
|
+
/** Foreground: resolves once the command exits. Background: resolves once it has started. */
|
|
48
|
+
run(requiredArgs: {
|
|
49
|
+
command: string;
|
|
50
|
+
}, optionalArgs?: Omit<E2bRunOptions, 'background'>): Promise<E2bCommandResult>;
|
|
51
|
+
run(requiredArgs: {
|
|
52
|
+
command: string;
|
|
53
|
+
background: true;
|
|
54
|
+
}, optionalArgs?: Omit<E2bRunOptions, 'background'>): Promise<E2bCommandHandle>;
|
|
55
|
+
}
|
|
56
|
+
/** E2B's filesystem event kinds (real values, lowercase — confirmed against the installed
|
|
57
|
+
* `e2b` package's own `FilesystemEventType` enum, not assumed). `'chmod'` is a permissions
|
|
58
|
+
* change, not a content change — this adapter deliberately does not surface it (see
|
|
59
|
+
* `wrap-e2b-sandbox.ts`). */
|
|
60
|
+
export type E2bFilesystemEventType = 'chmod' | 'create' | 'remove' | 'rename' | 'write';
|
|
61
|
+
export interface E2bFilesystemEvent {
|
|
62
|
+
/** Path relative to the watched directory, per E2B's own `FilesystemEvent.name` doc. */
|
|
63
|
+
readonly name: string;
|
|
64
|
+
readonly type: E2bFilesystemEventType;
|
|
65
|
+
}
|
|
66
|
+
export interface E2bWatchHandle {
|
|
67
|
+
stop(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
/** One file to write, as E2B's own batch `write` overload takes it. `data` is `string |
|
|
70
|
+
* ArrayBuffer` — not `Uint8Array` — because that's the real signature; converting a
|
|
71
|
+
* `Uint8Array` view to a correctly-scoped `ArrayBuffer` is `wrap-e2b-sandbox.ts`'s job, not
|
|
72
|
+
* this port's. */
|
|
73
|
+
export interface E2bWriteEntry {
|
|
74
|
+
readonly path: string;
|
|
75
|
+
readonly data: string | ArrayBuffer;
|
|
76
|
+
}
|
|
77
|
+
/** E2B's real filesystem-entry kind (`FileType` enum values, confirmed against the installed
|
|
78
|
+
* `e2b` package). Only `'file'` entries are files; `'dir'` and `'symlink'` are not. */
|
|
79
|
+
export type E2bFileType = 'file' | 'dir' | 'symlink';
|
|
80
|
+
/** The subset of E2B's `EntryInfo` this adapter reads from `files.list`. */
|
|
81
|
+
export interface E2bEntryInfo {
|
|
82
|
+
readonly path: string;
|
|
83
|
+
readonly type?: E2bFileType;
|
|
84
|
+
}
|
|
85
|
+
export interface E2bFilesystem {
|
|
86
|
+
/** Batched over E2B's own multi-file `write` overload — one round trip for however many
|
|
87
|
+
* files `mountFiles` is given, not one per file. E2B's real `write` is itself overloaded
|
|
88
|
+
* (a single-file form in addition to this batch one); `provider.ts` adapts the real
|
|
89
|
+
* `Sandbox` into this interface with an explicit wrapper function rather than relying on
|
|
90
|
+
* the real object satisfying this shape structurally — TypeScript's assignability check for
|
|
91
|
+
* a multi-overload source against a single-signature target picks the wrong overload to
|
|
92
|
+
* compare against and reports a confusing arity mismatch, so the real SDK is never passed
|
|
93
|
+
* through as-is. See `provider.ts`'s `toE2bHandle`. */
|
|
94
|
+
write(requiredArgs: {
|
|
95
|
+
files: readonly E2bWriteEntry[];
|
|
96
|
+
}): Promise<unknown>;
|
|
97
|
+
/** Always requested as raw bytes (`format: 'bytes'`) — this adapter never asks E2B to decode
|
|
98
|
+
* text on its behalf, matching `SandboxSession.readFile`'s "always raw bytes" contract. */
|
|
99
|
+
read(requiredArgs: {
|
|
100
|
+
path: string;
|
|
101
|
+
format: 'bytes';
|
|
102
|
+
}): Promise<Uint8Array>;
|
|
103
|
+
/** `depth` bounds how deep E2B's own server-side walk goes — see `MAX_LIST_DEPTH` in
|
|
104
|
+
* `wrap-e2b-sandbox.ts` for why a bound exists at all. */
|
|
105
|
+
list(requiredArgs: {
|
|
106
|
+
path: string;
|
|
107
|
+
}, optionalArgs?: {
|
|
108
|
+
readonly depth?: number;
|
|
109
|
+
}): Promise<readonly E2bEntryInfo[]>;
|
|
110
|
+
watchDir(requiredArgs: {
|
|
111
|
+
path: string;
|
|
112
|
+
onEvent: (event: E2bFilesystemEvent) => void;
|
|
113
|
+
}, opts?: {
|
|
114
|
+
readonly recursive?: boolean;
|
|
115
|
+
}): Promise<E2bWatchHandle>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The E2B sandbox surface this adapter depends on. A real `Sandbox` instance from
|
|
119
|
+
* `@e2b/code-interpreter` satisfies this structurally; tests substitute a plain object.
|
|
120
|
+
*/
|
|
121
|
+
export interface E2bSandboxHandle {
|
|
122
|
+
readonly commands: E2bCommands;
|
|
123
|
+
readonly files: E2bFilesystem;
|
|
124
|
+
getHost(requiredArgs: {
|
|
125
|
+
port: number;
|
|
126
|
+
}): string;
|
|
127
|
+
/** `true` if the sandbox was killed, `false` if it was already gone — per the real SDK's
|
|
128
|
+
* `Sandbox.kill()` signature. */
|
|
129
|
+
kill(): Promise<boolean>;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=e2b-sandbox-handle.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The narrow slice of the E2B SDK this adapter actually calls.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* `@e2b/code-interpreter`'s real `Sandbox` class is much bigger than what this adapter needs
|
|
6
|
+
* (code-execution contexts, chart rendering, Jupyter — none of it relevant to running `npm`
|
|
7
|
+
* commands and a dev server). Depending on this narrower shape instead of the SDK's own type
|
|
8
|
+
* keeps the wrapping logic in `wrap-e2b-sandbox.ts` testable with a plain object literal: no
|
|
9
|
+
* network call, no API key, and no risk of a test silently depending on SDK internals this
|
|
10
|
+
* adapter never touches.
|
|
11
|
+
*
|
|
12
|
+
* Most of this interface is structurally satisfied by a real `Sandbox` instance directly.
|
|
13
|
+
* `files.write` is the one exception: E2B's real `write` is overloaded (a single-file form plus
|
|
14
|
+
* this interface's batch form), and TypeScript's assignability check between an overloaded
|
|
15
|
+
* source and a single-signature target does not reliably pick the matching overload, producing
|
|
16
|
+
* a confusing arity error rather than a real incompatibility. `provider.ts`'s `toE2bHandle`
|
|
17
|
+
* adapts the real `Sandbox` into this interface with one explicit wrapper function for that
|
|
18
|
+
* reason, rather than passing the SDK object straight through.
|
|
19
|
+
*
|
|
20
|
+
* Architectural role:
|
|
21
|
+
* The one place this package's `/e2b` subpath writes down what it assumes E2B's API looks like.
|
|
22
|
+
* If a real `@e2b/code-interpreter` upgrade changes these members, `provider.ts` (which is typed
|
|
23
|
+
* against both this interface and the SDK's real `Sandbox` type) fails to compile — a fast,
|
|
24
|
+
* local signal, rather than a runtime surprise inside `wrap-e2b-sandbox.ts`.
|
|
25
|
+
*/
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=e2b-sandbox-handle.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @jini-ai/sandbox/e2b
|
|
3
|
+
*
|
|
4
|
+
* The E2B (Firecracker microVM) adapter: `createE2bSandboxProvider` implements
|
|
5
|
+
* `@jini-ai/sandbox/core`'s `SandboxProviderPort` against `@e2b/code-interpreter`, an optional
|
|
6
|
+
* peer dependency of this package. Importing this entry point is what requires it to be
|
|
7
|
+
* installed.
|
|
8
|
+
*/
|
|
9
|
+
export { createE2bSandboxProvider } from './provider.js';
|
|
10
|
+
export type { E2bProviderConfig } from './provider.js';
|
|
11
|
+
export { DEFAULT_VITE_REACT_TEMPLATE } from './default-vite-react-template.js';
|
|
12
|
+
export { mapE2bFileChangeKind, wrapE2bSandbox } from './wrap-e2b-sandbox.js';
|
|
13
|
+
export type { E2bSessionConfig } from './wrap-e2b-sandbox.js';
|
|
14
|
+
export { categorizeE2bError } from './categorize-e2b-error.js';
|
|
15
|
+
export { toArrayBuffer } from './to-array-buffer.js';
|
|
16
|
+
export { shellQuote } from './shell-quote.js';
|
|
17
|
+
export type { E2bCommandHandle, E2bCommandResult, E2bCommands, E2bEntryInfo, E2bFilesystem, E2bFileType, E2bFilesystemEvent, E2bFilesystemEventType, E2bRunOptions, E2bSandboxHandle, E2bWatchHandle, E2bWriteEntry, } from './e2b-sandbox-handle.js';
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @jini-ai/sandbox/e2b
|
|
3
|
+
*
|
|
4
|
+
* The E2B (Firecracker microVM) adapter: `createE2bSandboxProvider` implements
|
|
5
|
+
* `@jini-ai/sandbox/core`'s `SandboxProviderPort` against `@e2b/code-interpreter`, an optional
|
|
6
|
+
* peer dependency of this package. Importing this entry point is what requires it to be
|
|
7
|
+
* installed.
|
|
8
|
+
*/
|
|
9
|
+
export { createE2bSandboxProvider } from './provider.js';
|
|
10
|
+
export { DEFAULT_VITE_REACT_TEMPLATE } from './default-vite-react-template.js';
|
|
11
|
+
export { mapE2bFileChangeKind, wrapE2bSandbox } from './wrap-e2b-sandbox.js';
|
|
12
|
+
export { categorizeE2bError } from './categorize-e2b-error.js';
|
|
13
|
+
export { toArrayBuffer } from './to-array-buffer.js';
|
|
14
|
+
export { shellQuote } from './shell-quote.js';
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The E2B adapter's `SandboxProviderPort` implementation.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* Turns a `BootOptions` request into a real `@e2b/code-interpreter` `Sandbox` and wraps it into
|
|
6
|
+
* a `SandboxSession` via `wrap-e2b-sandbox.ts`. This is the only file in `./e2b` that imports
|
|
7
|
+
* the SDK itself — everything else depends on the narrow `E2bSandboxHandle` port, which is why
|
|
8
|
+
* only live SDK integration needs a real API key or network access. The dedicated
|
|
9
|
+
* `__tests__/provider.test.ts` exercises provider translation and failures with a mocked SDK.
|
|
10
|
+
*
|
|
11
|
+
* Architectural role:
|
|
12
|
+
* The adapter's public entry point. `@e2b/code-interpreter` is an optional peer dependency of
|
|
13
|
+
* this package (see package.json); importing this module is what actually requires it to be
|
|
14
|
+
* installed. `./core` never imports from here.
|
|
15
|
+
*/
|
|
16
|
+
import { Sandbox } from '@e2b/code-interpreter';
|
|
17
|
+
import type { SandboxProviderPort } from '../core/ports.js';
|
|
18
|
+
import type { E2bSandboxHandle } from './e2b-sandbox-handle.js';
|
|
19
|
+
export interface E2bProviderConfig {
|
|
20
|
+
/** E2B API key. Omit to use the SDK's own `E2B_API_KEY` environment variable fallback — this
|
|
21
|
+
* adapter does not duplicate that lookup. */
|
|
22
|
+
readonly apiKey?: string;
|
|
23
|
+
/** Sandbox lifetime in milliseconds. Omit to use the SDK's own default (5 minutes). */
|
|
24
|
+
readonly timeoutMs?: number;
|
|
25
|
+
/** Absolute path inside the sandbox used as the project root.
|
|
26
|
+
* @default '/home/user/app' */
|
|
27
|
+
readonly projectRoot?: string;
|
|
28
|
+
/** Port `getPreview` asks E2B to forward — the port the caller's dev server binds to.
|
|
29
|
+
* @default 5173 */
|
|
30
|
+
readonly previewPort?: number;
|
|
31
|
+
/** How long `getPreview` waits for a single response before deciding nothing is listening.
|
|
32
|
+
* @default 3000 */
|
|
33
|
+
readonly previewCheckTimeoutMs?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Adapts a real `Sandbox` into the object-argument `E2bSandboxHandle` port.
|
|
37
|
+
* SDK positional arguments are translated only at this boundary. `files` is wrapped with one explicit function per member instead, because
|
|
38
|
+
* TypeScript's assignability check between E2B's overloaded `write` (a single-file form plus a
|
|
39
|
+
* batch form) and this interface's batch-only signature doesn't reliably resolve to the
|
|
40
|
+
* matching overload — seen directly as a real, reproducible compile error, not a
|
|
41
|
+
* theoretical concern — so the real object is never passed through as-is for that one member.
|
|
42
|
+
* Calling `sandbox.files.write(files)` here, with a concrete array argument, invokes real
|
|
43
|
+
* per-call overload resolution correctly; it's only the *object-to-interface* structural check
|
|
44
|
+
* that gets confused.
|
|
45
|
+
*
|
|
46
|
+
* Exported (not part of the package's public `./e2b` barrel) so `__tests__/to-e2b-handle.test.ts`
|
|
47
|
+
* can exercise the translation directly against a fake `Sandbox`-shaped object, rather than only
|
|
48
|
+
* indirectly through `boot`'s mocked-out `Sandbox.create` call.
|
|
49
|
+
*/
|
|
50
|
+
export declare function toE2bHandle({ sandbox }: {
|
|
51
|
+
sandbox: Sandbox;
|
|
52
|
+
}): E2bSandboxHandle;
|
|
53
|
+
export declare function createE2bSandboxProvider(_requiredArgs: Record<string, never>, config?: E2bProviderConfig & {
|
|
54
|
+
createSandbox?: (requiredArgs: {
|
|
55
|
+
options: {
|
|
56
|
+
apiKey?: string;
|
|
57
|
+
timeoutMs?: number;
|
|
58
|
+
template?: string;
|
|
59
|
+
};
|
|
60
|
+
}) => Promise<Sandbox>;
|
|
61
|
+
fetchImpl?: typeof fetch;
|
|
62
|
+
}): SandboxProviderPort;
|
|
63
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The E2B adapter's `SandboxProviderPort` implementation.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* Turns a `BootOptions` request into a real `@e2b/code-interpreter` `Sandbox` and wraps it into
|
|
6
|
+
* a `SandboxSession` via `wrap-e2b-sandbox.ts`. This is the only file in `./e2b` that imports
|
|
7
|
+
* the SDK itself — everything else depends on the narrow `E2bSandboxHandle` port, which is why
|
|
8
|
+
* only live SDK integration needs a real API key or network access. The dedicated
|
|
9
|
+
* `__tests__/provider.test.ts` exercises provider translation and failures with a mocked SDK.
|
|
10
|
+
*
|
|
11
|
+
* Architectural role:
|
|
12
|
+
* The adapter's public entry point. `@e2b/code-interpreter` is an optional peer dependency of
|
|
13
|
+
* this package (see package.json); importing this module is what actually requires it to be
|
|
14
|
+
* installed. `./core` never imports from here.
|
|
15
|
+
*/
|
|
16
|
+
import { Sandbox } from '@e2b/code-interpreter';
|
|
17
|
+
import { SandboxOperationError } from '../core/errors.js';
|
|
18
|
+
import { categorizeE2bError } from './categorize-e2b-error.js';
|
|
19
|
+
import { wrapE2bSandbox } from './wrap-e2b-sandbox.js';
|
|
20
|
+
const DEFAULT_PROJECT_ROOT = '/home/user/app';
|
|
21
|
+
const DEFAULT_PREVIEW_PORT = 5173;
|
|
22
|
+
const DEFAULT_PREVIEW_CHECK_TIMEOUT_MS = 3_000;
|
|
23
|
+
/**
|
|
24
|
+
* Adapts a real `Sandbox` into the object-argument `E2bSandboxHandle` port.
|
|
25
|
+
* SDK positional arguments are translated only at this boundary. `files` is wrapped with one explicit function per member instead, because
|
|
26
|
+
* TypeScript's assignability check between E2B's overloaded `write` (a single-file form plus a
|
|
27
|
+
* batch form) and this interface's batch-only signature doesn't reliably resolve to the
|
|
28
|
+
* matching overload — seen directly as a real, reproducible compile error, not a
|
|
29
|
+
* theoretical concern — so the real object is never passed through as-is for that one member.
|
|
30
|
+
* Calling `sandbox.files.write(files)` here, with a concrete array argument, invokes real
|
|
31
|
+
* per-call overload resolution correctly; it's only the *object-to-interface* structural check
|
|
32
|
+
* that gets confused.
|
|
33
|
+
*
|
|
34
|
+
* Exported (not part of the package's public `./e2b` barrel) so `__tests__/to-e2b-handle.test.ts`
|
|
35
|
+
* can exercise the translation directly against a fake `Sandbox`-shaped object, rather than only
|
|
36
|
+
* indirectly through `boot`'s mocked-out `Sandbox.create` call.
|
|
37
|
+
*/
|
|
38
|
+
export function toE2bHandle({ sandbox }) {
|
|
39
|
+
// Return the port union explicitly so overload compatibility does not require SDK-only members.
|
|
40
|
+
function run(requiredArgs, optionalArgs = {}) {
|
|
41
|
+
const { onStdout, onStderr, ...settings } = optionalArgs;
|
|
42
|
+
const sdkOptions = {
|
|
43
|
+
...settings,
|
|
44
|
+
...(onStdout === undefined ? {} : { onStdout: (data) => onStdout({ data }) }),
|
|
45
|
+
...(onStderr === undefined ? {} : { onStderr: (data) => onStderr({ data }) }),
|
|
46
|
+
};
|
|
47
|
+
return requiredArgs.background === true
|
|
48
|
+
? sandbox.commands.run(requiredArgs.command, { ...sdkOptions, background: true })
|
|
49
|
+
: sandbox.commands.run(requiredArgs.command, sdkOptions);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
commands: { run },
|
|
53
|
+
files: {
|
|
54
|
+
write: ({ files }) => sandbox.files.write([...files]),
|
|
55
|
+
read: ({ path, format }) => sandbox.files.read(path, { format }),
|
|
56
|
+
list: ({ path }, opts) => sandbox.files.list(path, opts),
|
|
57
|
+
watchDir: ({ path, onEvent }, opts) => sandbox.files.watchDir(path, onEvent, opts),
|
|
58
|
+
},
|
|
59
|
+
getHost: ({ port }) => sandbox.getHost(port),
|
|
60
|
+
kill: () => sandbox.kill(),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function createE2bSandboxProvider(_requiredArgs, config = {}) {
|
|
64
|
+
return {
|
|
65
|
+
async boot(_requiredArgs, options) {
|
|
66
|
+
const createOptions = {
|
|
67
|
+
...(config.apiKey !== undefined ? { apiKey: config.apiKey } : {}),
|
|
68
|
+
...(config.timeoutMs !== undefined ? { timeoutMs: config.timeoutMs } : {}),
|
|
69
|
+
...(options?.template !== undefined ? { template: options.template } : {}),
|
|
70
|
+
};
|
|
71
|
+
let sandbox;
|
|
72
|
+
try {
|
|
73
|
+
sandbox = config.createSandbox
|
|
74
|
+
? await config.createSandbox({ options: createOptions })
|
|
75
|
+
: await Sandbox.create(createOptions);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
throw new SandboxOperationError({ category: categorizeE2bError({ error }), message: 'Failed to create the sandbox' }, { cause: error });
|
|
79
|
+
}
|
|
80
|
+
return wrapE2bSandbox({ handle: toE2bHandle({ sandbox }), config: {
|
|
81
|
+
projectRoot: config.projectRoot ?? DEFAULT_PROJECT_ROOT,
|
|
82
|
+
previewPort: config.previewPort ?? DEFAULT_PREVIEW_PORT,
|
|
83
|
+
previewCheckTimeoutMs: config.previewCheckTimeoutMs ?? DEFAULT_PREVIEW_CHECK_TIMEOUT_MS,
|
|
84
|
+
} }, config.fetchImpl === undefined ? {} : { fetchImpl: config.fetchImpl });
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file POSIX single-quote escaping for command arguments.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* E2B's `commands.run` takes one command string, not an argv array, so `runCommand`/
|
|
6
|
+
* `startProcess`/`installDependencies` have to assemble `command + ' ' + args` themselves. A
|
|
7
|
+
* naive join breaks the moment an argument contains a space or a shell metacharacter — exactly
|
|
8
|
+
* the kind of input an AI-generated `npm install <package>` call can produce (scoped package
|
|
9
|
+
* names are fine, but a stray `&&` or unescaped quote in a generated argument is not). Wrapping
|
|
10
|
+
* every argument in single quotes and escaping embedded single quotes is the standard POSIX
|
|
11
|
+
* technique that survives that case: nothing between single quotes is interpreted by the shell
|
|
12
|
+
* except the quote character itself.
|
|
13
|
+
*/
|
|
14
|
+
/** Wraps `value` in single quotes for safe interpolation into a POSIX shell command, escaping
|
|
15
|
+
* any single quotes it already contains. The command name itself is not quoted by callers —
|
|
16
|
+
* only arguments — since it is expected to be a trusted program name, not caller-controlled
|
|
17
|
+
* data. */
|
|
18
|
+
export declare function shellQuote({ value }: {
|
|
19
|
+
value: string;
|
|
20
|
+
}): string;
|
|
21
|
+
//# sourceMappingURL=shell-quote.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file POSIX single-quote escaping for command arguments.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* E2B's `commands.run` takes one command string, not an argv array, so `runCommand`/
|
|
6
|
+
* `startProcess`/`installDependencies` have to assemble `command + ' ' + args` themselves. A
|
|
7
|
+
* naive join breaks the moment an argument contains a space or a shell metacharacter — exactly
|
|
8
|
+
* the kind of input an AI-generated `npm install <package>` call can produce (scoped package
|
|
9
|
+
* names are fine, but a stray `&&` or unescaped quote in a generated argument is not). Wrapping
|
|
10
|
+
* every argument in single quotes and escaping embedded single quotes is the standard POSIX
|
|
11
|
+
* technique that survives that case: nothing between single quotes is interpreted by the shell
|
|
12
|
+
* except the quote character itself.
|
|
13
|
+
*/
|
|
14
|
+
/** Wraps `value` in single quotes for safe interpolation into a POSIX shell command, escaping
|
|
15
|
+
* any single quotes it already contains. The command name itself is not quoted by callers —
|
|
16
|
+
* only arguments — since it is expected to be a trusted program name, not caller-controlled
|
|
17
|
+
* data. */
|
|
18
|
+
export function shellQuote({ value }) {
|
|
19
|
+
return `'${value.replaceAll("'", `'\\''`)}'`;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=shell-quote.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Converts a `Uint8Array` to the `ArrayBuffer` E2B's `files.write` actually accepts.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* E2B's real `write` signature is `string | ArrayBuffer | Blob | ReadableStream` — no
|
|
6
|
+
* `Uint8Array` overload, even though `SandboxFile.content` in `./core` is `string | Uint8Array`
|
|
7
|
+
* specifically because it's what Node and the browser share natively. A naive `.buffer` access
|
|
8
|
+
* is wrong the moment the `Uint8Array` is a *view* over a larger buffer (e.g. produced by
|
|
9
|
+
* `.subarray()`) rather than owning the whole thing — it would silently include bytes outside
|
|
10
|
+
* the view's range, corrupting the write in a different way than the one this type was widened
|
|
11
|
+
* to prevent.
|
|
12
|
+
*/
|
|
13
|
+
/** Returns a fresh `ArrayBuffer` containing exactly `bytes`' own range — safe even when `bytes`
|
|
14
|
+
* is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
|
|
15
|
+
export declare function toArrayBuffer({ bytes }: {
|
|
16
|
+
bytes: Uint8Array;
|
|
17
|
+
}): ArrayBuffer;
|
|
18
|
+
//# sourceMappingURL=to-array-buffer.d.ts.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Converts a `Uint8Array` to the `ArrayBuffer` E2B's `files.write` actually accepts.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* E2B's real `write` signature is `string | ArrayBuffer | Blob | ReadableStream` — no
|
|
6
|
+
* `Uint8Array` overload, even though `SandboxFile.content` in `./core` is `string | Uint8Array`
|
|
7
|
+
* specifically because it's what Node and the browser share natively. A naive `.buffer` access
|
|
8
|
+
* is wrong the moment the `Uint8Array` is a *view* over a larger buffer (e.g. produced by
|
|
9
|
+
* `.subarray()`) rather than owning the whole thing — it would silently include bytes outside
|
|
10
|
+
* the view's range, corrupting the write in a different way than the one this type was widened
|
|
11
|
+
* to prevent.
|
|
12
|
+
*/
|
|
13
|
+
/** Returns a fresh `ArrayBuffer` containing exactly `bytes`' own range — safe even when `bytes`
|
|
14
|
+
* is a view with a non-zero `byteOffset` over a larger, shared `buffer`. */
|
|
15
|
+
export function toArrayBuffer({ bytes }) {
|
|
16
|
+
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=to-array-buffer.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { FileChangeKind, SandboxSession } from '../core/ports.js';
|
|
2
|
+
import type { E2bFilesystemEventType, E2bSandboxHandle } from './e2b-sandbox-handle.js';
|
|
3
|
+
/** Resolved (no optionals) configuration `wrapE2bSandbox` needs. Default-filling happens in
|
|
4
|
+
* `provider.ts`, which owns the public, partially-optional config type. */
|
|
5
|
+
export interface E2bSessionConfig {
|
|
6
|
+
/** Absolute path inside the sandbox that `mountFiles`/`runCommand`/`startProcess` treat as
|
|
7
|
+
* the project root, and that `onFileChange` watches (recursively). */
|
|
8
|
+
readonly projectRoot: string;
|
|
9
|
+
/** Port `getPreview` asks E2B to forward — the port the caller's dev server binds to. */
|
|
10
|
+
readonly previewPort: number;
|
|
11
|
+
/** How long `getPreview` waits for a single response before deciding nothing is listening. */
|
|
12
|
+
readonly previewCheckTimeoutMs: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Maps one E2B filesystem event kind onto core's three-way `FileChangeKind`, or `null` to mean
|
|
16
|
+
* "don't surface this one." `'chmod'` is a permissions change, not a content change, and is
|
|
17
|
+
* dropped for that reason. `'rename'` is mapped to `modified` rather than guessed as
|
|
18
|
+
* create/delete because E2B's event carries only the one path this adapter sees, not an old/new
|
|
19
|
+
* pair — a wrong create-or-delete guess would be more misleading than the conservative
|
|
20
|
+
* "something at this path changed."
|
|
21
|
+
*/
|
|
22
|
+
export declare function mapE2bFileChangeKind({ type }: {
|
|
23
|
+
type: E2bFilesystemEventType;
|
|
24
|
+
}): FileChangeKind | null;
|
|
25
|
+
export declare function wrapE2bSandbox({ handle, config }: {
|
|
26
|
+
handle: E2bSandboxHandle;
|
|
27
|
+
config: E2bSessionConfig;
|
|
28
|
+
}, { fetchImpl }?: {
|
|
29
|
+
fetchImpl?: typeof fetch;
|
|
30
|
+
}): Promise<SandboxSession>;
|
|
31
|
+
//# sourceMappingURL=wrap-e2b-sandbox.d.ts.map
|