@vite-hub/sandbox 0.0.0 → 0.0.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.
Files changed (35) hide show
  1. package/README.md +56 -1
  2. package/dist/_shared-CJ2_pS4E.js +53 -0
  3. package/dist/cloudflare-retry-Cfra7MvS.js +21 -0
  4. package/dist/common-CS4FcWrh.d.ts +369 -0
  5. package/dist/index-B4j3Mvzr.d.ts +146 -0
  6. package/dist/index.d.ts +74 -0
  7. package/dist/index.js +785 -0
  8. package/dist/integration-wkNE8jMO.js +2032 -0
  9. package/dist/module-types-BomzaqnM.js +7 -0
  10. package/dist/module-types-CUcY8iuI.d.ts +88 -0
  11. package/dist/nitro.d.ts +19 -0
  12. package/dist/nitro.js +488 -0
  13. package/dist/provider-detection-IbezCeoR.js +25 -0
  14. package/dist/runtime/empty-registry.d.ts +6 -0
  15. package/dist/runtime/empty-registry.js +20 -0
  16. package/dist/runtime/nitro-plugin.d.ts +4 -0
  17. package/dist/runtime/nitro-plugin.js +8 -0
  18. package/dist/runtime/provider-loader.d.ts +17 -0
  19. package/dist/runtime/provider-loader.js +21 -0
  20. package/dist/runtime/providers/cloudflare.d.ts +35 -0
  21. package/dist/runtime/providers/cloudflare.js +22 -0
  22. package/dist/runtime/providers/vercel.d.ts +21 -0
  23. package/dist/runtime/providers/vercel.js +65 -0
  24. package/dist/runtime/state.d.ts +17 -0
  25. package/dist/runtime/state.js +21 -0
  26. package/dist/sandbox/providers/cloudflare.d.ts +7 -0
  27. package/dist/sandbox/providers/cloudflare.js +339 -0
  28. package/dist/sandbox/providers/vercel.d.ts +17 -0
  29. package/dist/sandbox/providers/vercel.js +538 -0
  30. package/dist/shared-pqTHYWg1.d.ts +8 -0
  31. package/dist/utils-V7ODsDW_.js +44 -0
  32. package/dist/vercel-runtime-BPzvh_6V.js +11 -0
  33. package/dist/vite.d.ts +40 -0
  34. package/dist/vite.js +441 -0
  35. package/package.json +103 -5
package/README.md CHANGED
@@ -1,3 +1,58 @@
1
1
  # @vite-hub/sandbox
2
2
 
3
- Reserved package name for ViteHub.
3
+ <p>
4
+ <a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
5
+ <img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
6
+ <img alt="Vite" src="https://img.shields.io/badge/Vite-discovery-646cff?style=flat-square">
7
+ <img alt="Sandbox" src="https://img.shields.io/badge/Sandbox-isolated%20runs-b45309?style=flat-square">
8
+ </p>
9
+
10
+ `@vite-hub/sandbox` runs typed work in an isolated provider runtime without coupling callers to that provider.
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ pnpm add @vite-hub/sandbox
16
+ ```
17
+
18
+ Add `@cloudflare/sandbox` or `@vercel/sandbox` for the provider you use.
19
+
20
+ ## Minimal API
21
+
22
+ ```ts
23
+ // server/sandboxes/release-notes.ts
24
+ import { defineSandbox } from "@vite-hub/sandbox"
25
+
26
+ export default defineSandbox(async (payload: { notes?: string } = {}) => {
27
+ return {
28
+ summary: payload.notes?.split("\n")[0] ?? "",
29
+ }
30
+ })
31
+ ```
32
+
33
+ ```ts
34
+ // server/api/release-notes.post.ts
35
+ import { runSandbox } from "@vite-hub/sandbox"
36
+ import { defineEventHandler, readBody } from "h3"
37
+
38
+ export default defineEventHandler(async (event) => {
39
+ const result = await runSandbox("release-notes", await readBody(event))
40
+ return result.isOk() ? result.value : { error: result.error.message }
41
+ })
42
+ ```
43
+
44
+ ```ts
45
+ // nitro.config.ts
46
+ import { defineNitroConfig } from "nitro/config"
47
+
48
+ export default defineNitroConfig({
49
+ modules: ["@vite-hub/sandbox/nitro"],
50
+ sandbox: { provider: "cloudflare" },
51
+ })
52
+ ```
53
+
54
+ ## Vite and Nitro
55
+
56
+ Nitro discovers `server/sandboxes/<name>.ts`. Vite also supports `src/<name>.sandbox.ts` through `hubSandbox()`. Provider config selects [Cloudflare Sandbox SDK](https://developers.cloudflare.com/sandbox/) or [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox/).
57
+
58
+ Learn more at [vitehub.dev](https://vitehub.dev).
@@ -0,0 +1,53 @@
1
+ import { n as SandboxError, r as NotSupportedError, t as sleep } from "./utils-V7ODsDW_.js";
2
+ import { hasProtocol } from "ufo";
3
+ //#region src/sandbox/adapters/base.ts
4
+ var BaseSandboxAdapter = class {
5
+ async listFiles(_path, _opts) {
6
+ throw new NotSupportedError("listFiles", this.provider);
7
+ }
8
+ async exists(_path) {
9
+ throw new NotSupportedError("exists", this.provider);
10
+ }
11
+ async deleteFile(_path) {
12
+ throw new NotSupportedError("deleteFile", this.provider);
13
+ }
14
+ async moveFile(_src, _dst) {
15
+ throw new NotSupportedError("moveFile", this.provider);
16
+ }
17
+ get vercel() {
18
+ throw new NotSupportedError("vercel namespace", this.provider);
19
+ }
20
+ get cloudflare() {
21
+ throw new NotSupportedError("cloudflare namespace", this.provider);
22
+ }
23
+ };
24
+ //#endregion
25
+ //#region src/sandbox/adapters/_shared.ts
26
+ function normalizeLogPattern(pattern) {
27
+ return typeof pattern === "string" ? new RegExp(pattern) : new RegExp(pattern.source, pattern.flags.replace(/g/g, ""));
28
+ }
29
+ function resolvePortProbeUrl(port, opts, resolvePortUrl) {
30
+ if (opts?.hostname) return hasProtocol(opts.hostname, { acceptRelative: false }) ? opts.hostname : `http://${opts.hostname}:${port}`;
31
+ if (resolvePortUrl) try {
32
+ return resolvePortUrl();
33
+ } catch {
34
+ return `http://localhost:${port}`;
35
+ }
36
+ return `http://localhost:${port}`;
37
+ }
38
+ async function waitForPortProbe(fetcher, port, opts, resolvePortUrl) {
39
+ const timeout = opts?.timeout ?? 3e4;
40
+ const startTime = Date.now();
41
+ while (Date.now() - startTime < timeout) try {
42
+ const controller = new AbortController();
43
+ const timeoutId = setTimeout(() => controller.abort(), 1e3);
44
+ await fetcher(resolvePortProbeUrl(port, opts, resolvePortUrl), { signal: controller.signal });
45
+ clearTimeout(timeoutId);
46
+ return;
47
+ } catch {
48
+ await sleep(100);
49
+ }
50
+ throw new SandboxError(`Timeout waiting for port ${port}`, "TIMEOUT");
51
+ }
52
+ //#endregion
53
+ export { waitForPortProbe as n, BaseSandboxAdapter as r, normalizeLogPattern as t };
@@ -0,0 +1,21 @@
1
+ import { n as SandboxError } from "./utils-V7ODsDW_.js";
2
+ //#region src/internal/shared/cloudflare-retry.ts
3
+ const CLOUDFLARE_SANDBOX_RETRY_DELAYS_MS = [
4
+ 1e3,
5
+ 2e3,
6
+ 5e3,
7
+ 1e4,
8
+ 15e3
9
+ ];
10
+ const CLOUDFLARE_RETRIABLE_STARTUP_ERROR_RE = /container is starting|currently provisioning|retry in a moment|network connection lost|not listening in the tcp address|durable object reset|code was updated|aborterror|aborted|maximum number of running container instances exceeded|there is no container instance that can be provided to this durable object/i;
11
+ function collectCloudflareErrorMessages(error, ...extraMessages) {
12
+ const sandboxError = error instanceof SandboxError ? error : void 0;
13
+ return [
14
+ error instanceof Error ? error.message : String(error),
15
+ error instanceof Error && error.cause instanceof Error ? error.cause.message : "",
16
+ sandboxError?.cause instanceof Error ? sandboxError.cause.message : "",
17
+ ...extraMessages
18
+ ].filter(Boolean).join("\n");
19
+ }
20
+ //#endregion
21
+ export { CLOUDFLARE_SANDBOX_RETRY_DELAYS_MS as n, collectCloudflareErrorMessages as r, CLOUDFLARE_RETRIABLE_STARTUP_ERROR_RE as t };
@@ -0,0 +1,369 @@
1
+ //#region src/sandbox/types/vercel.d.ts
2
+ interface VercelSandboxRunCommandParams {
3
+ cmd: string;
4
+ args?: string[];
5
+ cwd?: string;
6
+ env?: Record<string, string>;
7
+ sudo?: boolean;
8
+ detached?: boolean;
9
+ stdout?: NodeJS.WritableStream;
10
+ stderr?: NodeJS.WritableStream;
11
+ signal?: AbortSignal;
12
+ }
13
+ interface VercelSandboxInstance {
14
+ fs?: {
15
+ readFile: {
16
+ (path: string, options?: {
17
+ encoding?: null;
18
+ signal?: AbortSignal;
19
+ } | null): Promise<Uint8Array>;
20
+ (path: string, options: {
21
+ encoding: BufferEncoding;
22
+ signal?: AbortSignal;
23
+ } | BufferEncoding): Promise<string>;
24
+ };
25
+ writeFile: (path: string, data: string | Uint8Array, options?: {
26
+ encoding?: BufferEncoding;
27
+ signal?: AbortSignal;
28
+ } | BufferEncoding) => Promise<void>;
29
+ mkdir: (path: string, options?: {
30
+ recursive?: boolean;
31
+ signal?: AbortSignal;
32
+ } | number) => Promise<string | undefined>;
33
+ readdir: (path: string, options?: {
34
+ signal?: AbortSignal;
35
+ withFileTypes?: false;
36
+ }) => Promise<string[]>;
37
+ stat: (path: string, options?: {
38
+ signal?: AbortSignal;
39
+ }) => Promise<{
40
+ isFile(): boolean;
41
+ isDirectory(): boolean;
42
+ isSymbolicLink(): boolean;
43
+ size: number;
44
+ mtime?: Date;
45
+ mtimeMs?: number;
46
+ }>;
47
+ rm: (path: string, options?: {
48
+ recursive?: boolean;
49
+ force?: boolean;
50
+ signal?: AbortSignal;
51
+ }) => Promise<void>;
52
+ rename: (oldPath: string, newPath: string, options?: {
53
+ signal?: AbortSignal;
54
+ }) => Promise<void>;
55
+ exists?: (path: string, options?: {
56
+ signal?: AbortSignal;
57
+ }) => Promise<boolean>;
58
+ access?: (path: string, options?: {
59
+ signal?: AbortSignal;
60
+ }) => Promise<void>;
61
+ };
62
+ runCommand: {
63
+ (cmd: string, args?: string[], opts?: {
64
+ signal?: AbortSignal;
65
+ }): Promise<VercelSandboxCommandResult>;
66
+ (params: VercelSandboxRunCommandParams & {
67
+ detached: true;
68
+ }): Promise<VercelSandboxCommandResult>;
69
+ (params: VercelSandboxRunCommandParams): Promise<VercelSandboxCommandResult>;
70
+ };
71
+ writeFiles: (files: Array<{
72
+ path: string;
73
+ content: ArrayBufferView<ArrayBufferLike>;
74
+ }>, opts?: {
75
+ signal?: AbortSignal;
76
+ }) => Promise<void>;
77
+ readFileToBuffer: (opts: {
78
+ path: string;
79
+ cwd?: string;
80
+ }, opts2?: {
81
+ signal?: AbortSignal;
82
+ }) => Promise<Uint8Array | null>;
83
+ readFile: (opts: {
84
+ path: string;
85
+ cwd?: string;
86
+ }, opts2?: {
87
+ signal?: AbortSignal;
88
+ }) => Promise<NodeJS.ReadableStream | null>;
89
+ mkDir: (path: string, opts?: {
90
+ signal?: AbortSignal;
91
+ }) => Promise<void>;
92
+ domain: (port: number) => string;
93
+ stop?: (opts?: {
94
+ signal?: AbortSignal;
95
+ blocking?: boolean;
96
+ }) => Promise<unknown>;
97
+ [Symbol.asyncDispose]?: () => Promise<void>;
98
+ }
99
+ interface VercelSandboxCommandResult {
100
+ exitCode: number;
101
+ stdout: () => Promise<string>;
102
+ stderr: () => Promise<string>;
103
+ logs: () => AsyncGenerator<{
104
+ stream: 'stdout' | 'stderr';
105
+ data: string;
106
+ }>;
107
+ kill: () => Promise<void>;
108
+ wait: () => Promise<{
109
+ exitCode: number;
110
+ }>;
111
+ }
112
+ type VercelSandboxSource = {
113
+ type: 'git';
114
+ url: string;
115
+ depth?: number;
116
+ revision?: string;
117
+ } | {
118
+ type: 'git';
119
+ url: string;
120
+ username: string;
121
+ password: string;
122
+ depth?: number;
123
+ revision?: string;
124
+ } | {
125
+ type: 'tarball';
126
+ url: string;
127
+ } | {
128
+ type: 'snapshot';
129
+ snapshotId: string;
130
+ };
131
+ interface VercelSandboxListItem {
132
+ id: string;
133
+ status: string;
134
+ createdAt: string | number;
135
+ }
136
+ interface VercelSandboxSnapshot {
137
+ id: string;
138
+ sandboxId: string;
139
+ createdAt: string;
140
+ }
141
+ type SandboxNetworkPolicy = 'allow-all' | 'deny-all' | {
142
+ allow?: string[] | Record<string, Array<{
143
+ transform?: Array<{
144
+ headers?: Record<string, string>;
145
+ }>;
146
+ }>>;
147
+ subnets?: {
148
+ allow?: string[];
149
+ deny?: string[];
150
+ };
151
+ };
152
+ interface VercelSandboxMetadata {
153
+ id: string;
154
+ runtime: string;
155
+ status: string;
156
+ createdAt: string;
157
+ }
158
+ interface VercelSandboxNamespace {
159
+ readonly native: VercelSandboxInstance;
160
+ snapshot: () => Promise<VercelSandboxSnapshot>;
161
+ getSnapshot: (id: string) => Promise<VercelSandboxSnapshot>;
162
+ listSnapshots: () => Promise<{
163
+ snapshots: VercelSandboxSnapshot[];
164
+ }>;
165
+ deleteSnapshot: (id: string) => Promise<void>;
166
+ domain: (port: number) => string;
167
+ extendTimeout: (durationMs: number) => Promise<void>;
168
+ updateNetworkPolicy: (policy: SandboxNetworkPolicy) => Promise<void>;
169
+ getMetadata: () => VercelSandboxMetadata;
170
+ }
171
+ //#endregion
172
+ //#region src/sandbox/types/common.d.ts
173
+ type SandboxProvider = 'vercel' | 'cloudflare';
174
+ type SandboxFileContent = string | Uint8Array;
175
+ interface SandboxReadFileOptions {
176
+ encoding?: 'utf8' | 'binary';
177
+ }
178
+ interface SandboxExecOptions {
179
+ cwd?: string;
180
+ env?: Record<string, string>;
181
+ timeout?: number;
182
+ sudo?: boolean;
183
+ onStdout?: (data: string) => void;
184
+ onStderr?: (data: string) => void;
185
+ }
186
+ interface SandboxExecResult {
187
+ ok: boolean;
188
+ stdout: string;
189
+ stderr: string;
190
+ code: number | null;
191
+ meta?: Record<string, unknown>;
192
+ }
193
+ interface SandboxCapabilities {
194
+ execEnv: boolean;
195
+ execCwd: boolean;
196
+ execSudo: boolean;
197
+ listFiles: boolean;
198
+ exists: boolean;
199
+ deleteFile: boolean;
200
+ moveFile: boolean;
201
+ readFileStream: boolean;
202
+ startProcess: boolean;
203
+ }
204
+ interface SandboxFileEntry {
205
+ name: string;
206
+ path: string;
207
+ type: 'file' | 'directory';
208
+ size?: number;
209
+ mtime?: string;
210
+ }
211
+ interface SandboxListFilesOptions {
212
+ recursive?: boolean;
213
+ }
214
+ interface SandboxProcessOptions {
215
+ cwd?: string;
216
+ env?: Record<string, string>;
217
+ }
218
+ interface SandboxWaitForPortOptions {
219
+ timeout?: number;
220
+ hostname?: string;
221
+ }
222
+ interface SandboxProcess {
223
+ readonly id: string;
224
+ readonly command: string;
225
+ kill: (signal?: string) => Promise<void>;
226
+ logs: () => Promise<{
227
+ stdout: string;
228
+ stderr: string;
229
+ }>;
230
+ wait: (timeout?: number) => Promise<{
231
+ exitCode: number;
232
+ }>;
233
+ waitForLog: (pattern: string | RegExp, timeout?: number) => Promise<{
234
+ line: string;
235
+ }>;
236
+ waitForPort: (port: number, opts?: SandboxWaitForPortOptions) => Promise<void>;
237
+ }
238
+ interface CloudflareSandboxOptions {
239
+ sleepAfter?: string | number;
240
+ keepAlive?: boolean;
241
+ normalizeId?: boolean;
242
+ }
243
+ interface DurableObjectNamespaceLike {
244
+ idFromName: (name: string) => unknown;
245
+ get: (id: unknown) => unknown;
246
+ }
247
+ interface VercelSandboxProviderOptions {
248
+ provider: 'vercel';
249
+ /**
250
+ * Canonical Vercel sandbox image name.
251
+ * Use `node22` or `node24`.
252
+ */
253
+ runtime?: string;
254
+ timeout?: number;
255
+ cpu?: number;
256
+ ports?: number[];
257
+ source?: VercelSandboxSource;
258
+ networkPolicy?: SandboxNetworkPolicy;
259
+ token?: string;
260
+ teamId?: string;
261
+ projectId?: string;
262
+ }
263
+ interface CloudflareSandboxProviderOptions {
264
+ provider: 'cloudflare';
265
+ namespace: DurableObjectNamespaceLike;
266
+ sandboxId?: string;
267
+ cloudflare?: CloudflareSandboxOptions;
268
+ getSandbox?: (ns: DurableObjectNamespaceLike, id: string, opts?: CloudflareSandboxOptions) => CloudflareSandboxStub;
269
+ }
270
+ type SandboxProviderOptions = VercelSandboxProviderOptions | CloudflareSandboxProviderOptions;
271
+ interface CloudflareSandboxExecResult {
272
+ success: boolean;
273
+ exitCode: number;
274
+ stdout: string;
275
+ stderr: string;
276
+ command: string;
277
+ duration: number;
278
+ timestamp: string;
279
+ sessionId?: string;
280
+ }
281
+ interface CloudflareSandboxWriteFileResult {
282
+ success: boolean;
283
+ path: string;
284
+ timestamp: string;
285
+ exitCode?: number;
286
+ }
287
+ interface CloudflareSandboxReadFileResult {
288
+ success: boolean;
289
+ path: string;
290
+ content: string;
291
+ timestamp: string;
292
+ exitCode?: number;
293
+ encoding?: 'utf-8' | 'base64';
294
+ isBinary?: boolean;
295
+ mimeType?: string;
296
+ size?: number;
297
+ }
298
+ interface CloudflareSandboxStub {
299
+ exec: (cmd: string, opts?: {
300
+ timeout?: number;
301
+ env?: Record<string, string | undefined>;
302
+ cwd?: string;
303
+ stream?: boolean;
304
+ onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
305
+ }) => Promise<CloudflareSandboxExecResult>;
306
+ writeFile: (path: string, content: string, opts?: {
307
+ encoding?: string;
308
+ }) => Promise<CloudflareSandboxWriteFileResult>;
309
+ readFile: (path: string, opts?: {
310
+ encoding?: string;
311
+ }) => Promise<CloudflareSandboxReadFileResult>;
312
+ readFileStream?: (path: string) => Promise<ReadableStream<Uint8Array>>;
313
+ mkdir?: (path: string, opts?: {
314
+ recursive?: boolean;
315
+ }) => Promise<{
316
+ success: boolean;
317
+ }>;
318
+ listFiles?: (path: string, opts?: {
319
+ recursive?: boolean;
320
+ }) => Promise<{
321
+ files: Array<{
322
+ name: string;
323
+ absolutePath: string;
324
+ relativePath: string;
325
+ type: 'file' | 'directory' | 'symlink' | 'other';
326
+ size: number;
327
+ modifiedAt: string;
328
+ }>;
329
+ }>;
330
+ exists?: (path: string) => Promise<{
331
+ exists: boolean;
332
+ success?: boolean;
333
+ path?: string;
334
+ timestamp?: string;
335
+ }>;
336
+ deleteFile?: (path: string) => Promise<{
337
+ success: boolean;
338
+ }>;
339
+ moveFile?: (src: string, dst: string) => Promise<{
340
+ success: boolean;
341
+ }>;
342
+ startProcess?: (cmd: string, opts?: {
343
+ cwd?: string;
344
+ env?: Record<string, string>;
345
+ onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
346
+ }) => Promise<CloudflareSandboxProcessInfo>;
347
+ destroy: () => Promise<void>;
348
+ }
349
+ interface CloudflareSandboxProcessInfo {
350
+ id: string;
351
+ command: string;
352
+ kill: (signal?: string) => Promise<void>;
353
+ getLogs: () => Promise<{
354
+ stdout: string;
355
+ stderr: string;
356
+ }>;
357
+ waitForExit: (timeout?: number) => Promise<{
358
+ exitCode: number;
359
+ }>;
360
+ waitForLog: (pattern: string | RegExp, timeout?: number) => Promise<{
361
+ line: string;
362
+ }>;
363
+ waitForPort: (port: number, opts?: {
364
+ timeout?: number;
365
+ hostname?: string;
366
+ }) => Promise<void>;
367
+ }
368
+ //#endregion
369
+ export { SandboxNetworkPolicy as _, SandboxCapabilities as a, VercelSandboxNamespace as b, SandboxFileContent as c, SandboxProcess as d, SandboxProcessOptions as f, VercelSandboxProviderOptions as g, SandboxReadFileOptions as h, DurableObjectNamespaceLike as i, SandboxFileEntry as l, SandboxProviderOptions as m, CloudflareSandboxProviderOptions as n, SandboxExecOptions as o, SandboxProvider as p, CloudflareSandboxStub as r, SandboxExecResult as s, CloudflareSandboxOptions as t, SandboxListFilesOptions as u, VercelSandboxInstance as v, VercelSandboxSource as x, VercelSandboxListItem as y };
@@ -0,0 +1,146 @@
1
+ import { a as SandboxCapabilities, b as VercelSandboxNamespace, c as SandboxFileContent, d as SandboxProcess, f as SandboxProcessOptions, h as SandboxReadFileOptions, l as SandboxFileEntry, o as SandboxExecOptions, p as SandboxProvider, r as CloudflareSandboxStub, s as SandboxExecResult, u as SandboxListFilesOptions, v as VercelSandboxInstance } from "./common-CS4FcWrh.js";
2
+
3
+ //#region src/sandbox/types/cloudflare.d.ts
4
+ interface SandboxGitCheckoutOptions {
5
+ branch?: string;
6
+ depth?: number;
7
+ sparse?: string[];
8
+ }
9
+ interface SandboxGitCheckoutResult {
10
+ success: boolean;
11
+ path: string;
12
+ branch: string;
13
+ commit: string;
14
+ }
15
+ interface CloudflareSandboxSessionOptions {
16
+ cwd?: string;
17
+ env?: Record<string, string>;
18
+ timeout?: number;
19
+ }
20
+ interface CloudflareSandboxSession {
21
+ id: string;
22
+ exec: (cmd: string, opts?: {
23
+ timeout?: number;
24
+ env?: Record<string, string>;
25
+ cwd?: string;
26
+ }) => Promise<{
27
+ stdout: string;
28
+ stderr: string;
29
+ exitCode: number;
30
+ }>;
31
+ startProcess: (cmd: string, args?: string[], opts?: {
32
+ cwd?: string;
33
+ env?: Record<string, string>;
34
+ }) => Promise<CloudflareSandboxSessionProcess>;
35
+ destroy: () => Promise<void>;
36
+ }
37
+ interface CloudflareSandboxSessionProcess {
38
+ id: string;
39
+ kill: (signal?: string) => Promise<void>;
40
+ logs: () => Promise<{
41
+ stdout: string;
42
+ stderr: string;
43
+ }>;
44
+ wait: (timeout?: number) => Promise<{
45
+ exitCode: number;
46
+ }>;
47
+ }
48
+ interface SandboxCodeContextOptions {
49
+ language?: 'python' | 'javascript' | 'typescript';
50
+ timeout?: number;
51
+ }
52
+ interface SandboxRunCodeOptions {
53
+ context?: SandboxCodeContext;
54
+ timeout?: number;
55
+ language?: 'python' | 'javascript' | 'typescript';
56
+ }
57
+ interface SandboxCodeExecutionResult {
58
+ success: boolean;
59
+ output: string;
60
+ error?: string;
61
+ executionTime: number;
62
+ }
63
+ interface SandboxCodeContext {
64
+ id: string;
65
+ language: 'python' | 'javascript' | 'typescript';
66
+ createdAt: string;
67
+ }
68
+ interface SandboxExposePortOptions {
69
+ hostname?: string;
70
+ protocol?: 'http' | 'https';
71
+ }
72
+ interface SandboxExposedPort {
73
+ port: number;
74
+ url: string;
75
+ hostname: string;
76
+ }
77
+ interface SandboxMountBucketOptions {
78
+ readonly?: boolean;
79
+ }
80
+ interface CloudflareSandboxNamespace {
81
+ readonly native: CloudflareSandboxStub;
82
+ gitCheckout: (url: string, opts?: SandboxGitCheckoutOptions) => Promise<SandboxGitCheckoutResult>;
83
+ createSession: (opts?: CloudflareSandboxSessionOptions) => Promise<CloudflareSandboxSession>;
84
+ getSession: (id: string) => Promise<CloudflareSandboxSession>;
85
+ deleteSession: (id: string) => Promise<void>;
86
+ createCodeContext: (opts?: SandboxCodeContextOptions) => Promise<SandboxCodeContext>;
87
+ runCode: (code: string, opts?: SandboxRunCodeOptions) => Promise<SandboxCodeExecutionResult>;
88
+ listCodeContexts: () => Promise<SandboxCodeContext[]>;
89
+ deleteCodeContext: (id: string) => Promise<void>;
90
+ exposePort: (port: number, opts?: SandboxExposePortOptions) => Promise<{
91
+ url: string;
92
+ }>;
93
+ unexposePort: (port: number) => Promise<void>;
94
+ getExposedPorts: (hostname?: string) => Promise<SandboxExposedPort[]>;
95
+ mountBucket: (bucket: string, path: string, opts?: SandboxMountBucketOptions) => Promise<void>;
96
+ unmountBucket: (path: string) => Promise<void>;
97
+ setEnvVars: (vars: Record<string, string | undefined>) => Promise<void>;
98
+ wsConnect: (request: Request, port: number) => Promise<Response>;
99
+ }
100
+ //#endregion
101
+ //#region src/sandbox/types/index.d.ts
102
+ interface SandboxClientNativeMap {
103
+ vercel: VercelSandboxInstance;
104
+ cloudflare: CloudflareSandboxStub;
105
+ }
106
+ interface SandboxClientBase<P extends SandboxProvider = SandboxProvider> {
107
+ readonly id: string;
108
+ readonly provider: P;
109
+ readonly supports: SandboxCapabilities;
110
+ readonly native: SandboxClientNativeMap[P];
111
+ exec: (cmd: string, args?: string[], opts?: SandboxExecOptions) => Promise<SandboxExecResult>;
112
+ writeFile: (path: string, content: SandboxFileContent) => Promise<void>;
113
+ readFile: {
114
+ (path: string, opts: SandboxReadFileOptions & {
115
+ encoding: 'binary';
116
+ }): Promise<Uint8Array>;
117
+ (path: string, opts?: SandboxReadFileOptions): Promise<string>;
118
+ };
119
+ stop: () => Promise<void>;
120
+ mkdir: (path: string, opts?: {
121
+ recursive?: boolean;
122
+ }) => Promise<void>;
123
+ readFileStream: (path: string) => Promise<ReadableStream<Uint8Array>>;
124
+ startProcess: (cmd: string, args?: string[], opts?: SandboxProcessOptions) => Promise<SandboxProcess>;
125
+ listFiles: (path: string, opts?: SandboxListFilesOptions) => Promise<SandboxFileEntry[]>;
126
+ exists: (path: string) => Promise<boolean>;
127
+ deleteFile: (path: string) => Promise<void>;
128
+ moveFile: (src: string, dst: string) => Promise<void>;
129
+ readonly vercel: P extends 'vercel' ? VercelSandboxNamespace : never;
130
+ readonly cloudflare: P extends 'cloudflare' ? CloudflareSandboxNamespace : never;
131
+ }
132
+ interface SandboxClientNamespaceMap {
133
+ vercel: {
134
+ readonly vercel: VercelSandboxNamespace;
135
+ readonly native: VercelSandboxInstance;
136
+ };
137
+ cloudflare: {
138
+ readonly cloudflare: CloudflareSandboxNamespace;
139
+ readonly native: CloudflareSandboxStub;
140
+ };
141
+ }
142
+ type SandboxClient<P extends SandboxProvider = SandboxProvider> = SandboxClientBase<P> & SandboxClientNamespaceMap[P];
143
+ type VercelSandboxClient = SandboxClient<'vercel'>;
144
+ type CloudflareSandboxClient = SandboxClient<'cloudflare'>;
145
+ //#endregion
146
+ export { SandboxClient as n, VercelSandboxClient as r, CloudflareSandboxClient as t };