@vite-hub/sandbox 0.0.1 → 0.0.3

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 +35 -37
  2. package/dist/cloudflare-env-2NctU8i7.js +12 -0
  3. package/dist/index.d.ts +23 -32
  4. package/dist/index.js +688 -265
  5. package/dist/module-types-DfXe133f.d.ts +91 -0
  6. package/dist/provider-loader-hL7uHVWp.d.ts +21 -0
  7. package/dist/runtime/provider-loader.d.ts +2 -17
  8. package/dist/runtime/provider-loader.js +4 -10
  9. package/dist/runtime/providers/cloudflare.d.ts +5 -11
  10. package/dist/runtime/providers/cloudflare.js +14 -11
  11. package/dist/runtime/providers/vercel.d.ts +103 -12
  12. package/dist/runtime/providers/vercel.js +16 -30
  13. package/dist/runtime/state.d.ts +1 -1
  14. package/dist/vite.d.ts +12 -28
  15. package/dist/vite.js +2280 -354
  16. package/package.json +60 -59
  17. package/dist/_shared-CJ2_pS4E.js +0 -53
  18. package/dist/cloudflare-retry-Cfra7MvS.js +0 -21
  19. package/dist/common-CS4FcWrh.d.ts +0 -369
  20. package/dist/index-B4j3Mvzr.d.ts +0 -146
  21. package/dist/integration-wkNE8jMO.js +0 -2032
  22. package/dist/module-types-CUcY8iuI.d.ts +0 -88
  23. package/dist/nitro.d.ts +0 -19
  24. package/dist/nitro.js +0 -488
  25. package/dist/provider-detection-IbezCeoR.js +0 -25
  26. package/dist/runtime/nitro-plugin.d.ts +0 -4
  27. package/dist/runtime/nitro-plugin.js +0 -8
  28. package/dist/sandbox/providers/cloudflare.d.ts +0 -7
  29. package/dist/sandbox/providers/cloudflare.js +0 -339
  30. package/dist/sandbox/providers/vercel.d.ts +0 -17
  31. package/dist/sandbox/providers/vercel.js +0 -538
  32. package/dist/shared-pqTHYWg1.d.ts +0 -8
  33. package/dist/utils-V7ODsDW_.js +0 -44
  34. package/dist/vercel-runtime-BPzvh_6V.js +0 -11
  35. /package/dist/{module-types-BomzaqnM.js → module-types-Dj1MFmHT.js} +0 -0
@@ -1,339 +0,0 @@
1
- import { n as SandboxError, r as NotSupportedError, t as sleep } from "../../utils-V7ODsDW_.js";
2
- import { n as CLOUDFLARE_SANDBOX_RETRY_DELAYS_MS, t as CLOUDFLARE_RETRIABLE_STARTUP_ERROR_RE } from "../../cloudflare-retry-Cfra7MvS.js";
3
- import { r as BaseSandboxAdapter, t as normalizeLogPattern } from "../../_shared-CJ2_pS4E.js";
4
- import { getSandbox } from "@cloudflare/sandbox";
5
- import { Buffer } from "node:buffer";
6
- //#region src/sandbox/utils.ts
7
- function shellQuote(arg) {
8
- if (!/[^\w\-./=]/.test(arg)) return arg;
9
- return `'${arg.replace(/'/g, `'\\''`)}'`;
10
- }
11
- //#endregion
12
- //#region src/sandbox/adapters/cloudflare/process.ts
13
- var CloudflareProcessHandle = class {
14
- id;
15
- command;
16
- processInfo;
17
- constructor(id, command, processInfo) {
18
- this.id = id;
19
- this.command = command;
20
- this.processInfo = processInfo;
21
- }
22
- async kill(signal) {
23
- await this.processInfo.kill(signal);
24
- }
25
- async logs() {
26
- return this.processInfo.getLogs();
27
- }
28
- async wait(timeout) {
29
- return this.processInfo.waitForExit(timeout);
30
- }
31
- async waitForLog(pattern, timeout = 3e4) {
32
- try {
33
- return await this.processInfo.waitForLog(pattern, timeout);
34
- } catch (error) {
35
- const message = error instanceof Error ? error.message : String(error);
36
- if (!/ProcessExitedBeforeReadyError|before becoming ready|exited with code/i.test(message)) throw error;
37
- }
38
- const startTime = Date.now();
39
- const regex = normalizeLogPattern(pattern);
40
- while (Date.now() - startTime < timeout) {
41
- const { stdout, stderr } = await this.logs();
42
- for (const line of `${stdout}${stderr}`.split("\n")) if (regex.test(line)) return { line };
43
- await sleep(100);
44
- }
45
- throw new SandboxError(`Timeout waiting for log pattern: ${pattern}`, "TIMEOUT");
46
- }
47
- async waitForPort(port, opts) {
48
- await this.processInfo.waitForPort(port, opts);
49
- }
50
- };
51
- const CLOUDFLARE_READ_FILE_TIMEOUT_MS = 15e3;
52
- const CLOUDFLARE_STOP_TIMEOUT_MS = 1e4;
53
- const CLOUDFLARE_EXEC_REQUEST_TIMEOUT_MS = 18e4;
54
- function createCloudflareTransportError(operation, error) {
55
- return new SandboxError(error instanceof Error ? error.message : String(error), {
56
- cause: error,
57
- code: "SANDBOX_TRANSPORT_ERROR",
58
- details: { operation },
59
- provider: "cloudflare"
60
- });
61
- }
62
- function isRetriableCloudflareTransportError(error) {
63
- const sandboxError = error instanceof SandboxError ? error : void 0;
64
- const message = error instanceof Error ? error.message : String(error);
65
- if (sandboxError?.code === "TIMEOUT") return true;
66
- return CLOUDFLARE_RETRIABLE_STARTUP_ERROR_RE.test(message);
67
- }
68
- async function withCloudflareDeadline(operation, timeoutMs, run) {
69
- let timeoutId;
70
- try {
71
- return await Promise.race([run(), new Promise((_, reject) => {
72
- timeoutId = setTimeout(() => {
73
- reject(new SandboxError(`Cloudflare sandbox ${operation} timed out after ${timeoutMs}ms.`, {
74
- code: "TIMEOUT",
75
- details: {
76
- operation,
77
- timeout: timeoutMs
78
- },
79
- provider: "cloudflare"
80
- }));
81
- }, timeoutMs);
82
- })]);
83
- } finally {
84
- if (timeoutId) clearTimeout(timeoutId);
85
- }
86
- }
87
- async function withCloudflareTransportRetry(operation, run) {
88
- const attempts = CLOUDFLARE_SANDBOX_RETRY_DELAYS_MS.length + 1;
89
- for (let attempt = 0; attempt < attempts; attempt++) try {
90
- return await run();
91
- } catch (error) {
92
- const sandboxError = error instanceof SandboxError ? error : createCloudflareTransportError(operation, error);
93
- if (!(attempt < CLOUDFLARE_SANDBOX_RETRY_DELAYS_MS.length && isRetriableCloudflareTransportError(sandboxError))) throw sandboxError;
94
- await sleep(CLOUDFLARE_SANDBOX_RETRY_DELAYS_MS[attempt]);
95
- }
96
- throw new SandboxError(`Cloudflare sandbox ${operation} retries exhausted.`, {
97
- code: "SANDBOX_TRANSPORT_ERROR",
98
- details: { operation },
99
- provider: "cloudflare"
100
- });
101
- }
102
- function resolveExecRequestTimeout(timeout) {
103
- if (typeof timeout === "number" && timeout > 0) return Math.min(timeout, CLOUDFLARE_EXEC_REQUEST_TIMEOUT_MS);
104
- return CLOUDFLARE_EXEC_REQUEST_TIMEOUT_MS;
105
- }
106
- //#endregion
107
- //#region src/sandbox/adapters/cloudflare.ts
108
- var CloudflareNamespaceImpl = class {
109
- native;
110
- constructor(stub) {
111
- this.native = stub;
112
- }
113
- get extendedStub() {
114
- return this.native;
115
- }
116
- async gitCheckout(url, opts) {
117
- if (typeof this.extendedStub.gitCheckout === "function") return this.extendedStub.gitCheckout(url, opts);
118
- throw new NotSupportedError("gitCheckout", "cloudflare");
119
- }
120
- async createSession(opts) {
121
- if (typeof this.extendedStub.createSession === "function") return this.extendedStub.createSession(opts);
122
- throw new NotSupportedError("createSession", "cloudflare");
123
- }
124
- async getSession(id) {
125
- if (typeof this.extendedStub.getSession === "function") return this.extendedStub.getSession(id);
126
- throw new NotSupportedError("getSession", "cloudflare");
127
- }
128
- async deleteSession(id) {
129
- if (typeof this.extendedStub.deleteSession === "function") {
130
- await this.extendedStub.deleteSession(id);
131
- return;
132
- }
133
- throw new NotSupportedError("deleteSession", "cloudflare");
134
- }
135
- async createCodeContext(opts) {
136
- if (typeof this.extendedStub.createCodeContext === "function") return this.extendedStub.createCodeContext(opts);
137
- throw new NotSupportedError("createCodeContext", "cloudflare");
138
- }
139
- async runCode(code, opts) {
140
- if (typeof this.extendedStub.runCode === "function") return this.extendedStub.runCode(code, opts);
141
- throw new NotSupportedError("runCode", "cloudflare");
142
- }
143
- async listCodeContexts() {
144
- if (typeof this.extendedStub.listCodeContexts === "function") return this.extendedStub.listCodeContexts();
145
- throw new NotSupportedError("listCodeContexts", "cloudflare");
146
- }
147
- async deleteCodeContext(id) {
148
- if (typeof this.extendedStub.deleteCodeContext === "function") {
149
- await this.extendedStub.deleteCodeContext(id);
150
- return;
151
- }
152
- throw new NotSupportedError("deleteCodeContext", "cloudflare");
153
- }
154
- async exposePort(port, opts) {
155
- if (!opts?.hostname) throw new SandboxError("Cloudflare exposePort() requires opts.hostname. Use a custom domain (not *.workers.dev).", "INVALID_ARGUMENT");
156
- if (typeof this.extendedStub.exposePort === "function") return this.extendedStub.exposePort(port, opts);
157
- throw new NotSupportedError("exposePort", "cloudflare");
158
- }
159
- async unexposePort(port) {
160
- if (typeof this.extendedStub.unexposePort === "function") {
161
- await this.extendedStub.unexposePort(port);
162
- return;
163
- }
164
- throw new NotSupportedError("unexposePort", "cloudflare");
165
- }
166
- async getExposedPorts(hostname) {
167
- if (!hostname) throw new SandboxError("Cloudflare getExposedPorts() requires a hostname argument. Use your custom domain hostname.", "INVALID_ARGUMENT");
168
- if (typeof this.extendedStub.getExposedPorts === "function") return this.extendedStub.getExposedPorts(hostname);
169
- throw new NotSupportedError("getExposedPorts", "cloudflare");
170
- }
171
- async mountBucket(bucket, path, opts) {
172
- if (typeof this.extendedStub.mountBucket === "function") {
173
- await this.extendedStub.mountBucket(bucket, path, opts);
174
- return;
175
- }
176
- throw new NotSupportedError("mountBucket", "cloudflare");
177
- }
178
- async unmountBucket(path) {
179
- if (typeof this.extendedStub.unmountBucket === "function") {
180
- await this.extendedStub.unmountBucket(path);
181
- return;
182
- }
183
- throw new NotSupportedError("unmountBucket", "cloudflare");
184
- }
185
- async setEnvVars(vars) {
186
- if (typeof this.extendedStub.setEnvVars === "function") {
187
- await this.extendedStub.setEnvVars(vars);
188
- return;
189
- }
190
- throw new NotSupportedError("setEnvVars", "cloudflare");
191
- }
192
- async wsConnect(request, port) {
193
- if (typeof this.extendedStub.wsConnect === "function") return this.extendedStub.wsConnect(request, port);
194
- throw new NotSupportedError("wsConnect", "cloudflare");
195
- }
196
- };
197
- var CloudflareSandboxAdapter = class extends BaseSandboxAdapter {
198
- id;
199
- provider = "cloudflare";
200
- native;
201
- _cloudflare;
202
- constructor(id, stub) {
203
- super();
204
- this.id = id;
205
- this.native = stub;
206
- }
207
- get supports() {
208
- return {
209
- execEnv: true,
210
- execCwd: true,
211
- execSudo: false,
212
- listFiles: true,
213
- exists: true,
214
- deleteFile: true,
215
- moveFile: true,
216
- readFileStream: true,
217
- startProcess: !!this.native.startProcess
218
- };
219
- }
220
- get cloudflare() {
221
- if (!this._cloudflare) this._cloudflare = new CloudflareNamespaceImpl(this.native);
222
- return this._cloudflare;
223
- }
224
- async exec(command, args = [], opts) {
225
- const cmd = args.length ? `${shellQuote(command)} ${args.map(shellQuote).join(" ")}` : shellQuote(command);
226
- const result = await withCloudflareTransportRetry("exec", async () => await withCloudflareDeadline("exec", resolveExecRequestTimeout(opts?.timeout), async () => await this.native.exec(cmd, {
227
- timeout: opts?.timeout,
228
- env: opts?.env,
229
- cwd: opts?.cwd,
230
- stream: !!(opts?.onStdout || opts?.onStderr),
231
- onOutput: opts?.onStdout || opts?.onStderr ? (stream, data) => {
232
- if (stream === "stdout") opts?.onStdout?.(data);
233
- if (stream === "stderr") opts?.onStderr?.(data);
234
- } : void 0
235
- })));
236
- return {
237
- ok: result.success,
238
- stdout: result.stdout,
239
- stderr: result.stderr,
240
- code: result.exitCode
241
- };
242
- }
243
- async writeFile(path, content) {
244
- const body = typeof content === "string" ? content : Buffer.from(content).toString("base64");
245
- const options = typeof content === "string" ? void 0 : { encoding: "base64" };
246
- if (!(await withCloudflareTransportRetry("writeFile", async () => await withCloudflareDeadline("writeFile", 15e3, async () => await this.native.writeFile(path, body, options)))).success) throw new SandboxError(`Failed to write file: ${path}`);
247
- }
248
- async readFile(path, opts) {
249
- const result = await withCloudflareTransportRetry("readFile", async () => await withCloudflareDeadline("readFile", CLOUDFLARE_READ_FILE_TIMEOUT_MS, async () => await this.native.readFile(path, opts?.encoding === "binary" ? { encoding: "base64" } : void 0)));
250
- if (!result.success) throw new SandboxError(`Failed to read file: ${path}`);
251
- if (opts?.encoding === "binary") return new Uint8Array(Buffer.from(result.content, "base64"));
252
- return result.content;
253
- }
254
- async stop() {
255
- await withCloudflareTransportRetry("destroy", async () => await withCloudflareDeadline("destroy", CLOUDFLARE_STOP_TIMEOUT_MS, async () => await this.native.destroy()));
256
- }
257
- async mkdir(path, opts) {
258
- if (this.native.mkdir) {
259
- if (!(await withCloudflareTransportRetry("mkdir", async () => await withCloudflareDeadline("mkdir", 15e3, async () => await this.native.mkdir(path, opts)))).success) throw new SandboxError(`Failed to create directory: ${path}`);
260
- return;
261
- }
262
- const result = await this.exec("mkdir", opts?.recursive ? ["-p", path] : [path]);
263
- if (!result.ok) throw new SandboxError(`Failed to create directory: ${path}. ${result.stderr}`);
264
- }
265
- async readFileStream(path) {
266
- if (this.native.readFileStream) return this.native.readFileStream(path);
267
- const content = await this.readFile(path);
268
- const encoder = new TextEncoder();
269
- return new ReadableStream({ start(controller) {
270
- controller.enqueue(encoder.encode(content));
271
- controller.close();
272
- } });
273
- }
274
- async startProcess(cmd, args = [], opts) {
275
- if (!this.native.startProcess) throw new NotSupportedError("startProcess", "cloudflare");
276
- const processInfo = await this.native.startProcess(args.length ? `${shellQuote(cmd)} ${args.map(shellQuote).join(" ")}` : shellQuote(cmd), opts);
277
- if (typeof processInfo.getLogs !== "function" || typeof processInfo.waitForExit !== "function" || typeof processInfo.waitForLog !== "function" || typeof processInfo.waitForPort !== "function") throw new SandboxError("Cloudflare process handles must provide getLogs(), waitForExit(), waitForLog(), and waitForPort().", "NOT_SUPPORTED");
278
- return new CloudflareProcessHandle(processInfo.id, processInfo.command, {
279
- kill: processInfo.kill.bind(processInfo),
280
- getLogs: processInfo.getLogs.bind(processInfo),
281
- waitForExit: processInfo.waitForExit.bind(processInfo),
282
- waitForLog: processInfo.waitForLog.bind(processInfo),
283
- waitForPort: processInfo.waitForPort.bind(processInfo)
284
- });
285
- }
286
- async listFiles(path, opts) {
287
- if (this.native.listFiles) return (await this.native.listFiles(path, opts)).files.filter((file) => file.type === "file" || file.type === "directory").map((file) => ({
288
- name: file.name,
289
- path: file.absolutePath,
290
- type: file.type,
291
- size: file.size,
292
- mtime: file.modifiedAt
293
- }));
294
- const result = await this.exec("ls", [opts?.recursive ? "-laR" : "-la", path]);
295
- if (!result.ok) throw new SandboxError(`Failed to list files: ${path}. ${result.stderr}`);
296
- return result.stdout.split("\n").filter(Boolean).slice(1).map((line) => {
297
- const parts = line.split(/\s+/);
298
- const name = parts[parts.length - 1] || "";
299
- return {
300
- name,
301
- path: `${path}/${name}`,
302
- type: line.startsWith("d") ? "directory" : "file"
303
- };
304
- }).filter((file) => file.name && file.name !== "." && file.name !== "..");
305
- }
306
- async exists(path) {
307
- if (this.native.exists) return (await this.native.exists(path)).exists;
308
- try {
309
- return (await this.exec("test", ["-e", path])).ok;
310
- } catch {
311
- return false;
312
- }
313
- }
314
- async deleteFile(path) {
315
- if (this.native.deleteFile) {
316
- if (!(await this.native.deleteFile(path)).success) throw new SandboxError(`Failed to delete file: ${path}`);
317
- return;
318
- }
319
- const result = await this.exec("rm", ["-f", path]);
320
- if (!result.ok) throw new SandboxError(`Failed to delete file: ${path}. ${result.stderr}`);
321
- }
322
- async moveFile(src, dst) {
323
- if (this.native.moveFile) {
324
- if (!(await this.native.moveFile(src, dst)).success) throw new SandboxError(`Failed to move file: ${src} -> ${dst}`);
325
- return;
326
- }
327
- const result = await this.exec("mv", [src, dst]);
328
- if (!result.ok) throw new SandboxError(`Failed to move file: ${src} -> ${dst}. ${result.stderr}`);
329
- }
330
- };
331
- //#endregion
332
- //#region src/sandbox/providers/cloudflare.ts
333
- async function createCloudflareSandboxClient(provider) {
334
- if (!provider.namespace) throw new SandboxError("Cloudflare sandbox requires a Durable Objects binding namespace.");
335
- const id = provider.sandboxId ?? `cloudflare-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
336
- return new CloudflareSandboxAdapter(id, (provider.getSandbox ?? ((ns, sandboxId, opts) => getSandbox(ns, sandboxId, opts)))(provider.namespace, id, provider.cloudflare));
337
- }
338
- //#endregion
339
- export { createCloudflareSandboxClient };
@@ -1,17 +0,0 @@
1
- import { g as VercelSandboxProviderOptions, y as VercelSandboxListItem } from "../../common-CS4FcWrh.js";
2
- import { r as VercelSandboxClient } from "../../index-B4j3Mvzr.js";
3
- import { t as ResolvedVercelSandboxCredentials } from "../../shared-pqTHYWg1.js";
4
-
5
- //#region src/sandbox/providers/vercel.d.ts
6
- type ResolvedVercelSandboxProvider = VercelSandboxProviderOptions & {
7
- credentials?: ResolvedVercelSandboxCredentials;
8
- };
9
- declare function createVercelSandboxClient(provider: ResolvedVercelSandboxProvider): Promise<VercelSandboxClient>;
10
- declare const VercelSandboxStatic: {
11
- list(): Promise<{
12
- sandboxes: VercelSandboxListItem[];
13
- }>;
14
- get(id: string): Promise<VercelSandboxClient | null>;
15
- };
16
- //#endregion
17
- export { VercelSandboxStatic, createVercelSandboxClient };