@vercel/sandbox 1.2.0 → 1.3.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/README.md +5 -0
- package/dist/api-client/api-client.js +7 -0
- package/dist/api-client/api-client.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/get-credentials.js +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -1
- package/.turbo/turbo-build.log +0 -4
- package/.turbo/turbo-test.log +0 -24
- package/.turbo/turbo-typecheck.log +0 -4
- package/CHANGELOG.md +0 -277
- package/__mocks__/picocolors.ts +0 -13
- package/scripts/inject-version.ts +0 -11
- package/src/api-client/api-client.test.ts +0 -228
- package/src/api-client/api-client.ts +0 -592
- package/src/api-client/api-error.ts +0 -46
- package/src/api-client/base-client.ts +0 -171
- package/src/api-client/file-writer.ts +0 -90
- package/src/api-client/index.ts +0 -2
- package/src/api-client/validators.ts +0 -146
- package/src/api-client/with-retry.ts +0 -131
- package/src/auth/api.ts +0 -31
- package/src/auth/error.ts +0 -8
- package/src/auth/file.ts +0 -69
- package/src/auth/index.ts +0 -9
- package/src/auth/infer-scope.test.ts +0 -178
- package/src/auth/linked-project.test.ts +0 -86
- package/src/auth/linked-project.ts +0 -40
- package/src/auth/oauth.ts +0 -333
- package/src/auth/poll-for-token.ts +0 -89
- package/src/auth/project.ts +0 -92
- package/src/auth/zod.ts +0 -16
- package/src/command.test.ts +0 -103
- package/src/command.ts +0 -287
- package/src/constants.ts +0 -1
- package/src/index.ts +0 -4
- package/src/sandbox.test.ts +0 -171
- package/src/sandbox.ts +0 -677
- package/src/snapshot.ts +0 -110
- package/src/utils/array.ts +0 -15
- package/src/utils/consume-readable.ts +0 -12
- package/src/utils/decode-base64-url.ts +0 -14
- package/src/utils/dev-credentials.test.ts +0 -217
- package/src/utils/dev-credentials.ts +0 -196
- package/src/utils/get-credentials.test.ts +0 -20
- package/src/utils/get-credentials.ts +0 -183
- package/src/utils/jwt-expiry.test.ts +0 -125
- package/src/utils/jwt-expiry.ts +0 -105
- package/src/utils/log.ts +0 -20
- package/src/utils/normalizePath.test.ts +0 -114
- package/src/utils/normalizePath.ts +0 -33
- package/src/utils/resolveSignal.ts +0 -24
- package/src/utils/types.test.js +0 -7
- package/src/utils/types.ts +0 -23
- package/src/version.ts +0 -2
- package/test-utils/mock-response.ts +0 -12
- package/tsconfig.json +0 -16
- package/turbo.json +0 -9
- package/typedoc.json +0 -13
- package/vercel.json +0 -9
- package/vitest.config.ts +0 -9
- package/vitest.setup.ts +0 -4
package/src/sandbox.ts
DELETED
|
@@ -1,677 +0,0 @@
|
|
|
1
|
-
import type { SandboxMetaData, SandboxRouteData } from "./api-client";
|
|
2
|
-
import { type Writable } from "stream";
|
|
3
|
-
import { pipeline } from "stream/promises";
|
|
4
|
-
import { createWriteStream } from "fs";
|
|
5
|
-
import { mkdir } from "fs/promises";
|
|
6
|
-
import { dirname, join, resolve } from "path";
|
|
7
|
-
import { APIClient } from "./api-client";
|
|
8
|
-
import { Command, CommandFinished } from "./command";
|
|
9
|
-
import { type Credentials, getCredentials } from "./utils/get-credentials";
|
|
10
|
-
import { getPrivateParams, WithPrivate } from "./utils/types";
|
|
11
|
-
import { WithFetchOptions } from "./api-client/api-client";
|
|
12
|
-
import { RUNTIMES } from "./constants";
|
|
13
|
-
import { Snapshot } from "./snapshot";
|
|
14
|
-
import { consumeReadable } from "./utils/consume-readable";
|
|
15
|
-
|
|
16
|
-
/** @inline */
|
|
17
|
-
export interface BaseCreateSandboxParams {
|
|
18
|
-
/**
|
|
19
|
-
* The source of the sandbox.
|
|
20
|
-
*
|
|
21
|
-
* Omit this parameter start a sandbox without a source.
|
|
22
|
-
*
|
|
23
|
-
* For git sources:
|
|
24
|
-
* - `depth`: Creates shallow clones with limited commit history (minimum: 1)
|
|
25
|
-
* - `revision`: Clones and checks out a specific commit, branch, or tag
|
|
26
|
-
*/
|
|
27
|
-
source?:
|
|
28
|
-
| {
|
|
29
|
-
type: "git";
|
|
30
|
-
url: string;
|
|
31
|
-
depth?: number;
|
|
32
|
-
revision?: string;
|
|
33
|
-
}
|
|
34
|
-
| {
|
|
35
|
-
type: "git";
|
|
36
|
-
url: string;
|
|
37
|
-
username: string;
|
|
38
|
-
password: string;
|
|
39
|
-
depth?: number;
|
|
40
|
-
revision?: string;
|
|
41
|
-
}
|
|
42
|
-
| { type: "tarball"; url: string };
|
|
43
|
-
/**
|
|
44
|
-
* Array of port numbers to expose from the sandbox. Sandboxes can
|
|
45
|
-
* expose up to 4 ports.
|
|
46
|
-
*/
|
|
47
|
-
ports?: number[];
|
|
48
|
-
/**
|
|
49
|
-
* Timeout in milliseconds before the sandbox auto-terminates.
|
|
50
|
-
*/
|
|
51
|
-
timeout?: number;
|
|
52
|
-
/**
|
|
53
|
-
* Resources to allocate to the sandbox.
|
|
54
|
-
*
|
|
55
|
-
* Your sandbox will get the amount of vCPUs you specify here and
|
|
56
|
-
* 2048 MB of memory per vCPU.
|
|
57
|
-
*/
|
|
58
|
-
resources?: { vcpus: number };
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The runtime of the sandbox, currently only `node24`, `node22` and `python3.13` are supported.
|
|
62
|
-
* If not specified, the default runtime `node24` will be used.
|
|
63
|
-
*/
|
|
64
|
-
runtime?: RUNTIMES | (string & {});
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* An AbortSignal to cancel sandbox creation.
|
|
68
|
-
*/
|
|
69
|
-
signal?: AbortSignal;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export type CreateSandboxParams =
|
|
73
|
-
| BaseCreateSandboxParams
|
|
74
|
-
| (Omit<BaseCreateSandboxParams, "runtime" | "source"> & {
|
|
75
|
-
source: { type: "snapshot"; snapshotId: string };
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
/** @inline */
|
|
79
|
-
interface GetSandboxParams {
|
|
80
|
-
/**
|
|
81
|
-
* Unique identifier of the sandbox.
|
|
82
|
-
*/
|
|
83
|
-
sandboxId: string;
|
|
84
|
-
/**
|
|
85
|
-
* An AbortSignal to cancel the operation.
|
|
86
|
-
*/
|
|
87
|
-
signal?: AbortSignal;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** @inline */
|
|
91
|
-
interface RunCommandParams {
|
|
92
|
-
/**
|
|
93
|
-
* The command to execute
|
|
94
|
-
*/
|
|
95
|
-
cmd: string;
|
|
96
|
-
/**
|
|
97
|
-
* Arguments to pass to the command
|
|
98
|
-
*/
|
|
99
|
-
args?: string[];
|
|
100
|
-
/**
|
|
101
|
-
* Working directory to execute the command in
|
|
102
|
-
*/
|
|
103
|
-
cwd?: string;
|
|
104
|
-
/**
|
|
105
|
-
* Environment variables to set for this command
|
|
106
|
-
*/
|
|
107
|
-
env?: Record<string, string>;
|
|
108
|
-
/**
|
|
109
|
-
* If true, execute this command with root privileges. Defaults to false.
|
|
110
|
-
*/
|
|
111
|
-
sudo?: boolean;
|
|
112
|
-
/**
|
|
113
|
-
* If true, the command will return without waiting for `exitCode`
|
|
114
|
-
*/
|
|
115
|
-
detached?: boolean;
|
|
116
|
-
/**
|
|
117
|
-
* A `Writable` stream where `stdout` from the command will be piped
|
|
118
|
-
*/
|
|
119
|
-
stdout?: Writable;
|
|
120
|
-
/**
|
|
121
|
-
* A `Writable` stream where `stderr` from the command will be piped
|
|
122
|
-
*/
|
|
123
|
-
stderr?: Writable;
|
|
124
|
-
/**
|
|
125
|
-
* An AbortSignal to cancel the command execution
|
|
126
|
-
*/
|
|
127
|
-
signal?: AbortSignal;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* A Sandbox is an isolated Linux MicroVM to run commands in.
|
|
132
|
-
*
|
|
133
|
-
* Use {@link Sandbox.create} or {@link Sandbox.get} to construct.
|
|
134
|
-
* @hideconstructor
|
|
135
|
-
*/
|
|
136
|
-
export class Sandbox {
|
|
137
|
-
private readonly client: APIClient;
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Routes from ports to subdomains.
|
|
141
|
-
/* @hidden
|
|
142
|
-
*/
|
|
143
|
-
public readonly routes: SandboxRouteData[];
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Unique ID of this sandbox.
|
|
147
|
-
*/
|
|
148
|
-
public get sandboxId(): string {
|
|
149
|
-
return this.sandbox.id;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public get interactivePort(): number | undefined {
|
|
153
|
-
return this.sandbox.interactivePort ?? undefined;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* The status of the sandbox.
|
|
158
|
-
*/
|
|
159
|
-
public get status(): SandboxMetaData["status"] {
|
|
160
|
-
return this.sandbox.status;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* The creation date of the sandbox.
|
|
165
|
-
*/
|
|
166
|
-
public get createdAt(): Date {
|
|
167
|
-
return new Date(this.sandbox.createdAt);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* The timeout of the sandbox in milliseconds.
|
|
172
|
-
*/
|
|
173
|
-
public get timeout(): number {
|
|
174
|
-
return this.sandbox.timeout;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* If the sandbox was created from a snapshot, the ID of that snapshot.
|
|
179
|
-
*/
|
|
180
|
-
public get sourceSnapshotId(): string | undefined {
|
|
181
|
-
return this.sandbox.sourceSnapshotId;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Internal metadata about this sandbox.
|
|
186
|
-
*/
|
|
187
|
-
private sandbox: SandboxMetaData;
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Allow to get a list of sandboxes for a team narrowed to the given params.
|
|
191
|
-
* It returns both the sandboxes and the pagination metadata to allow getting
|
|
192
|
-
* the next page of results.
|
|
193
|
-
*/
|
|
194
|
-
static async list(
|
|
195
|
-
params?: Partial<Parameters<APIClient["listSandboxes"]>[0]> &
|
|
196
|
-
Partial<Credentials> &
|
|
197
|
-
WithFetchOptions,
|
|
198
|
-
) {
|
|
199
|
-
const credentials = await getCredentials(params);
|
|
200
|
-
const client = new APIClient({
|
|
201
|
-
teamId: credentials.teamId,
|
|
202
|
-
token: credentials.token,
|
|
203
|
-
fetch: params?.fetch,
|
|
204
|
-
});
|
|
205
|
-
return client.listSandboxes({
|
|
206
|
-
...credentials,
|
|
207
|
-
...params,
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Create a new sandbox.
|
|
213
|
-
*
|
|
214
|
-
* @param params - Creation parameters and optional credentials.
|
|
215
|
-
* @returns A promise resolving to the created {@link Sandbox}.
|
|
216
|
-
* @example
|
|
217
|
-
* <caption>Create a sandbox and drop it in the end of the block</caption>
|
|
218
|
-
* async function fn() {
|
|
219
|
-
* await using const sandbox = await Sandbox.create();
|
|
220
|
-
* // Sandbox automatically stopped at the end of the lexical scope
|
|
221
|
-
* }
|
|
222
|
-
*/
|
|
223
|
-
static async create(
|
|
224
|
-
params?: WithPrivate<
|
|
225
|
-
CreateSandboxParams | (CreateSandboxParams & Credentials)
|
|
226
|
-
> &
|
|
227
|
-
WithFetchOptions,
|
|
228
|
-
): Promise<Sandbox & AsyncDisposable> {
|
|
229
|
-
const credentials = await getCredentials(params);
|
|
230
|
-
const client = new APIClient({
|
|
231
|
-
teamId: credentials.teamId,
|
|
232
|
-
token: credentials.token,
|
|
233
|
-
fetch: params?.fetch,
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
const privateParams = getPrivateParams(params);
|
|
237
|
-
const sandbox = await client.createSandbox({
|
|
238
|
-
source: params?.source,
|
|
239
|
-
projectId: credentials.projectId,
|
|
240
|
-
ports: params?.ports ?? [],
|
|
241
|
-
timeout: params?.timeout,
|
|
242
|
-
resources: params?.resources,
|
|
243
|
-
runtime: params && "runtime" in params ? params?.runtime : undefined,
|
|
244
|
-
signal: params?.signal,
|
|
245
|
-
...privateParams,
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
return new DisposableSandbox({
|
|
249
|
-
client,
|
|
250
|
-
sandbox: sandbox.json.sandbox,
|
|
251
|
-
routes: sandbox.json.routes,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Retrieve an existing sandbox.
|
|
257
|
-
*
|
|
258
|
-
* @param params - Get parameters and optional credentials.
|
|
259
|
-
* @returns A promise resolving to the {@link Sandbox}.
|
|
260
|
-
*/
|
|
261
|
-
static async get(
|
|
262
|
-
params: WithPrivate<GetSandboxParams | (GetSandboxParams & Credentials)> &
|
|
263
|
-
WithFetchOptions,
|
|
264
|
-
): Promise<Sandbox> {
|
|
265
|
-
const credentials = await getCredentials(params);
|
|
266
|
-
const client = new APIClient({
|
|
267
|
-
teamId: credentials.teamId,
|
|
268
|
-
token: credentials.token,
|
|
269
|
-
fetch: params.fetch,
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
const privateParams = getPrivateParams(params);
|
|
273
|
-
const sandbox = await client.getSandbox({
|
|
274
|
-
sandboxId: params.sandboxId,
|
|
275
|
-
signal: params.signal,
|
|
276
|
-
...privateParams,
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
return new Sandbox({
|
|
280
|
-
client,
|
|
281
|
-
sandbox: sandbox.json.sandbox,
|
|
282
|
-
routes: sandbox.json.routes,
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Create a new Sandbox instance.
|
|
288
|
-
*
|
|
289
|
-
* @param client - API client used to communicate with the backend
|
|
290
|
-
* @param routes - Port-to-subdomain mappings for exposed ports
|
|
291
|
-
* @param sandboxId - Unique identifier for the sandbox
|
|
292
|
-
*/
|
|
293
|
-
constructor({
|
|
294
|
-
client,
|
|
295
|
-
routes,
|
|
296
|
-
sandbox,
|
|
297
|
-
}: {
|
|
298
|
-
client: APIClient;
|
|
299
|
-
routes: SandboxRouteData[];
|
|
300
|
-
sandbox: SandboxMetaData;
|
|
301
|
-
}) {
|
|
302
|
-
this.client = client;
|
|
303
|
-
this.routes = routes;
|
|
304
|
-
this.sandbox = sandbox;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Get a previously run command by its ID.
|
|
309
|
-
*
|
|
310
|
-
* @param cmdId - ID of the command to retrieve
|
|
311
|
-
* @param opts - Optional parameters.
|
|
312
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
313
|
-
* @returns A {@link Command} instance representing the command
|
|
314
|
-
*/
|
|
315
|
-
async getCommand(
|
|
316
|
-
cmdId: string,
|
|
317
|
-
opts?: { signal?: AbortSignal },
|
|
318
|
-
): Promise<Command> {
|
|
319
|
-
const command = await this.client.getCommand({
|
|
320
|
-
sandboxId: this.sandbox.id,
|
|
321
|
-
cmdId,
|
|
322
|
-
signal: opts?.signal,
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
return new Command({
|
|
326
|
-
client: this.client,
|
|
327
|
-
sandboxId: this.sandbox.id,
|
|
328
|
-
cmd: command.json.command,
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Start executing a command in this sandbox.
|
|
334
|
-
*
|
|
335
|
-
* @param command - The command to execute.
|
|
336
|
-
* @param args - Arguments to pass to the command.
|
|
337
|
-
* @param opts - Optional parameters.
|
|
338
|
-
* @param opts.signal - An AbortSignal to cancel the command execution.
|
|
339
|
-
* @returns A {@link CommandFinished} result once execution is done.
|
|
340
|
-
*/
|
|
341
|
-
async runCommand(
|
|
342
|
-
command: string,
|
|
343
|
-
args?: string[],
|
|
344
|
-
opts?: { signal?: AbortSignal },
|
|
345
|
-
): Promise<CommandFinished>;
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Start executing a command in detached mode.
|
|
349
|
-
*
|
|
350
|
-
* @param params - The command parameters.
|
|
351
|
-
* @returns A {@link Command} instance for the running command.
|
|
352
|
-
*/
|
|
353
|
-
async runCommand(
|
|
354
|
-
params: RunCommandParams & { detached: true },
|
|
355
|
-
): Promise<Command>;
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Start executing a command in this sandbox.
|
|
359
|
-
*
|
|
360
|
-
* @param params - The command parameters.
|
|
361
|
-
* @returns A {@link CommandFinished} result once execution is done.
|
|
362
|
-
*/
|
|
363
|
-
async runCommand(params: RunCommandParams): Promise<CommandFinished>;
|
|
364
|
-
|
|
365
|
-
async runCommand(
|
|
366
|
-
commandOrParams: string | RunCommandParams,
|
|
367
|
-
args?: string[],
|
|
368
|
-
opts?: { signal?: AbortSignal },
|
|
369
|
-
): Promise<Command | CommandFinished> {
|
|
370
|
-
return typeof commandOrParams === "string"
|
|
371
|
-
? this._runCommand({ cmd: commandOrParams, args, signal: opts?.signal })
|
|
372
|
-
: this._runCommand(commandOrParams);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Internal helper to start a command in the sandbox.
|
|
377
|
-
*
|
|
378
|
-
* @param params - Command execution parameters.
|
|
379
|
-
* @returns A {@link Command} or {@link CommandFinished}, depending on `detached`.
|
|
380
|
-
* @internal
|
|
381
|
-
*/
|
|
382
|
-
async _runCommand(params: RunCommandParams) {
|
|
383
|
-
const wait = params.detached ? false : true;
|
|
384
|
-
const getLogs = (command: Command) => {
|
|
385
|
-
if (params.stdout || params.stderr) {
|
|
386
|
-
(async () => {
|
|
387
|
-
try {
|
|
388
|
-
for await (const log of command.logs({ signal: params.signal })) {
|
|
389
|
-
if (log.stream === "stdout") {
|
|
390
|
-
params.stdout?.write(log.data);
|
|
391
|
-
} else if (log.stream === "stderr") {
|
|
392
|
-
params.stderr?.write(log.data);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
} catch (err) {
|
|
396
|
-
if (params.signal?.aborted) {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
throw err;
|
|
400
|
-
}
|
|
401
|
-
})();
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
if (wait) {
|
|
406
|
-
const commandStream = await this.client.runCommand({
|
|
407
|
-
sandboxId: this.sandbox.id,
|
|
408
|
-
command: params.cmd,
|
|
409
|
-
args: params.args ?? [],
|
|
410
|
-
cwd: params.cwd,
|
|
411
|
-
env: params.env ?? {},
|
|
412
|
-
sudo: params.sudo ?? false,
|
|
413
|
-
wait: true,
|
|
414
|
-
signal: params.signal,
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
const command = new Command({
|
|
418
|
-
client: this.client,
|
|
419
|
-
sandboxId: this.sandbox.id,
|
|
420
|
-
cmd: commandStream.command,
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
getLogs(command);
|
|
424
|
-
|
|
425
|
-
const finished = await commandStream.finished;
|
|
426
|
-
return new CommandFinished({
|
|
427
|
-
client: this.client,
|
|
428
|
-
sandboxId: this.sandbox.id,
|
|
429
|
-
cmd: finished,
|
|
430
|
-
exitCode: finished.exitCode ?? 0,
|
|
431
|
-
});
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
const commandResponse = await this.client.runCommand({
|
|
435
|
-
sandboxId: this.sandbox.id,
|
|
436
|
-
command: params.cmd,
|
|
437
|
-
args: params.args ?? [],
|
|
438
|
-
cwd: params.cwd,
|
|
439
|
-
env: params.env ?? {},
|
|
440
|
-
sudo: params.sudo ?? false,
|
|
441
|
-
signal: params.signal,
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
const command = new Command({
|
|
445
|
-
client: this.client,
|
|
446
|
-
sandboxId: this.sandbox.id,
|
|
447
|
-
cmd: commandResponse.json.command,
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
getLogs(command);
|
|
451
|
-
|
|
452
|
-
return command;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* Create a directory in the filesystem of this sandbox.
|
|
457
|
-
*
|
|
458
|
-
* @param path - Path of the directory to create
|
|
459
|
-
* @param opts - Optional parameters.
|
|
460
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
461
|
-
*/
|
|
462
|
-
async mkDir(path: string, opts?: { signal?: AbortSignal }): Promise<void> {
|
|
463
|
-
await this.client.mkDir({
|
|
464
|
-
sandboxId: this.sandbox.id,
|
|
465
|
-
path: path,
|
|
466
|
-
signal: opts?.signal,
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* Read a file from the filesystem of this sandbox as a stream.
|
|
472
|
-
*
|
|
473
|
-
* @param file - File to read, with path and optional cwd
|
|
474
|
-
* @param opts - Optional parameters.
|
|
475
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
476
|
-
* @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found
|
|
477
|
-
*/
|
|
478
|
-
async readFile(
|
|
479
|
-
file: { path: string; cwd?: string },
|
|
480
|
-
opts?: { signal?: AbortSignal },
|
|
481
|
-
): Promise<NodeJS.ReadableStream | null> {
|
|
482
|
-
return this.client.readFile({
|
|
483
|
-
sandboxId: this.sandbox.id,
|
|
484
|
-
path: file.path,
|
|
485
|
-
cwd: file.cwd,
|
|
486
|
-
signal: opts?.signal,
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
/**
|
|
491
|
-
* Read a file from the filesystem of this sandbox as a Buffer.
|
|
492
|
-
*
|
|
493
|
-
* @param file - File to read, with path and optional cwd
|
|
494
|
-
* @param opts - Optional parameters.
|
|
495
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
496
|
-
* @returns A promise that resolves to the file contents as a Buffer, or null if file not found
|
|
497
|
-
*/
|
|
498
|
-
async readFileToBuffer(
|
|
499
|
-
file: { path: string; cwd?: string },
|
|
500
|
-
opts?: { signal?: AbortSignal },
|
|
501
|
-
): Promise<Buffer | null> {
|
|
502
|
-
const stream = await this.client.readFile({
|
|
503
|
-
sandboxId: this.sandbox.id,
|
|
504
|
-
path: file.path,
|
|
505
|
-
cwd: file.cwd,
|
|
506
|
-
signal: opts?.signal,
|
|
507
|
-
});
|
|
508
|
-
|
|
509
|
-
if (stream === null) {
|
|
510
|
-
return null;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
return consumeReadable(stream);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
/**
|
|
517
|
-
* Download a file from the sandbox to the local filesystem.
|
|
518
|
-
*
|
|
519
|
-
* @param src - Source file on the sandbox, with path and optional cwd
|
|
520
|
-
* @param dst - Destination file on the local machine, with path and optional cwd
|
|
521
|
-
* @param opts - Optional parameters.
|
|
522
|
-
* @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.
|
|
523
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
524
|
-
* @returns The absolute path to the written file, or null if the source file was not found
|
|
525
|
-
*/
|
|
526
|
-
async downloadFile(
|
|
527
|
-
src: { path: string; cwd?: string },
|
|
528
|
-
dst: { path: string; cwd?: string },
|
|
529
|
-
opts?: { mkdirRecursive?: boolean; signal?: AbortSignal },
|
|
530
|
-
): Promise<string | null> {
|
|
531
|
-
const stream = await this.client.readFile({
|
|
532
|
-
sandboxId: this.sandbox.id,
|
|
533
|
-
path: src.path,
|
|
534
|
-
cwd: src.cwd,
|
|
535
|
-
signal: opts?.signal,
|
|
536
|
-
});
|
|
537
|
-
|
|
538
|
-
if (stream === null) {
|
|
539
|
-
return null;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
try {
|
|
543
|
-
const dstPath = resolve(dst.cwd ?? "", dst.path);
|
|
544
|
-
if (opts?.mkdirRecursive) {
|
|
545
|
-
await mkdir(dirname(dstPath), { recursive: true });
|
|
546
|
-
}
|
|
547
|
-
await pipeline(stream, createWriteStream(dstPath), {
|
|
548
|
-
signal: opts?.signal,
|
|
549
|
-
});
|
|
550
|
-
return dstPath;
|
|
551
|
-
} finally {
|
|
552
|
-
stream.destroy()
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* Write files to the filesystem of this sandbox.
|
|
558
|
-
* Defaults to writing to /vercel/sandbox unless an absolute path is specified.
|
|
559
|
-
* Writes files using the `vercel-sandbox` user.
|
|
560
|
-
*
|
|
561
|
-
* @param files - Array of files with path and stream/buffer contents
|
|
562
|
-
* @param opts - Optional parameters.
|
|
563
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
564
|
-
* @returns A promise that resolves when the files are written
|
|
565
|
-
*/
|
|
566
|
-
async writeFiles(
|
|
567
|
-
files: { path: string; content: Buffer }[],
|
|
568
|
-
opts?: { signal?: AbortSignal },
|
|
569
|
-
) {
|
|
570
|
-
return this.client.writeFiles({
|
|
571
|
-
sandboxId: this.sandbox.id,
|
|
572
|
-
cwd: this.sandbox.cwd,
|
|
573
|
-
extractDir: "/",
|
|
574
|
-
files: files,
|
|
575
|
-
signal: opts?.signal,
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
/**
|
|
580
|
-
* Get the public domain of a port of this sandbox.
|
|
581
|
-
*
|
|
582
|
-
* @param p - Port number to resolve
|
|
583
|
-
* @returns A full domain (e.g. `https://subdomain.vercel.run`)
|
|
584
|
-
* @throws If the port has no associated route
|
|
585
|
-
*/
|
|
586
|
-
domain(p: number): string {
|
|
587
|
-
const route = this.routes.find(({ port }) => port == p);
|
|
588
|
-
if (route) {
|
|
589
|
-
return `https://${route.subdomain}.vercel.run`;
|
|
590
|
-
} else {
|
|
591
|
-
throw new Error(`No route for port ${p}`);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
/**
|
|
596
|
-
* Stop the sandbox.
|
|
597
|
-
*
|
|
598
|
-
* @param opts - Optional parameters.
|
|
599
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
600
|
-
* @returns A promise that resolves when the sandbox is stopped
|
|
601
|
-
*/
|
|
602
|
-
async stop(opts?: { signal?: AbortSignal }) {
|
|
603
|
-
await this.client.stopSandbox({
|
|
604
|
-
sandboxId: this.sandbox.id,
|
|
605
|
-
signal: opts?.signal,
|
|
606
|
-
});
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
/**
|
|
610
|
-
* Extend the timeout of the sandbox by the specified duration.
|
|
611
|
-
*
|
|
612
|
-
* This allows you to extend the lifetime of a sandbox up until the maximum
|
|
613
|
-
* execution timeout for your plan.
|
|
614
|
-
*
|
|
615
|
-
* @param duration - The duration in milliseconds to extend the timeout by
|
|
616
|
-
* @param opts - Optional parameters.
|
|
617
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
618
|
-
* @returns A promise that resolves when the timeout is extended
|
|
619
|
-
*
|
|
620
|
-
* @example
|
|
621
|
-
* const sandbox = await Sandbox.create({ timeout: ms('10m') });
|
|
622
|
-
* // Extends timeout by 5 minutes, to a total of 15 minutes.
|
|
623
|
-
* await sandbox.extendTimeout(ms('5m'));
|
|
624
|
-
*/
|
|
625
|
-
async extendTimeout(
|
|
626
|
-
duration: number,
|
|
627
|
-
opts?: { signal?: AbortSignal },
|
|
628
|
-
): Promise<void> {
|
|
629
|
-
const response = await this.client.extendTimeout({
|
|
630
|
-
sandboxId: this.sandbox.id,
|
|
631
|
-
duration,
|
|
632
|
-
signal: opts?.signal,
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
// Update the internal sandbox metadata with the new timeout value
|
|
636
|
-
this.sandbox = response.json.sandbox;
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Create a snapshot from this currently running sandbox. New sandboxes can
|
|
641
|
-
* then be created from this snapshot using {@link Sandbox.createFromSnapshot}.
|
|
642
|
-
*
|
|
643
|
-
* Note: this sandbox will be stopped as part of the snapshot creation process.
|
|
644
|
-
*
|
|
645
|
-
* @param opts - Optional parameters.
|
|
646
|
-
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
647
|
-
* @returns A promise that resolves to the Snapshot instance
|
|
648
|
-
*/
|
|
649
|
-
async snapshot(opts?: { signal?: AbortSignal }): Promise<Snapshot> {
|
|
650
|
-
const response = await this.client.createSnapshot({
|
|
651
|
-
sandboxId: this.sandbox.id,
|
|
652
|
-
signal: opts?.signal,
|
|
653
|
-
});
|
|
654
|
-
|
|
655
|
-
this.sandbox = response.json.sandbox;
|
|
656
|
-
|
|
657
|
-
return new Snapshot({
|
|
658
|
-
client: this.client,
|
|
659
|
-
snapshot: response.json.snapshot,
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
/**
|
|
665
|
-
* A {@link Sandbox} that can automatically be disposed using a `await using` statement.
|
|
666
|
-
*
|
|
667
|
-
* @example
|
|
668
|
-
* {
|
|
669
|
-
* await using const sandbox = await Sandbox.create();
|
|
670
|
-
* }
|
|
671
|
-
* // Sandbox is automatically stopped here
|
|
672
|
-
*/
|
|
673
|
-
class DisposableSandbox extends Sandbox implements AsyncDisposable {
|
|
674
|
-
async [Symbol.asyncDispose]() {
|
|
675
|
-
await this.stop();
|
|
676
|
-
}
|
|
677
|
-
}
|