@vercel/sandbox 0.0.21 → 0.0.23

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/src/sandbox.ts CHANGED
@@ -54,6 +54,11 @@ export interface CreateSandboxParams {
54
54
  * If not specified, the default runtime `node22` will be used.
55
55
  */
56
56
  runtime?: "node22" | "python3.13" | (string & {});
57
+
58
+ /**
59
+ * An AbortSignal to cancel sandbox creation.
60
+ */
61
+ signal?: AbortSignal;
57
62
  }
58
63
 
59
64
  /** @inline */
@@ -62,6 +67,10 @@ interface GetSandboxParams {
62
67
  * Unique identifier of the sandbox.
63
68
  */
64
69
  sandboxId: string;
70
+ /**
71
+ * An AbortSignal to cancel the operation.
72
+ */
73
+ signal?: AbortSignal;
65
74
  }
66
75
 
67
76
  /** @inline */
@@ -98,6 +107,10 @@ interface RunCommandParams {
98
107
  * A `Writable` stream where `stderr` from the command will be piped
99
108
  */
100
109
  stderr?: Writable;
110
+ /**
111
+ * An AbortSignal to cancel the command execution
112
+ */
113
+ signal?: AbortSignal;
101
114
  }
102
115
 
103
116
  /**
@@ -129,10 +142,17 @@ export class Sandbox {
129
142
  return this.sandbox.status;
130
143
  }
131
144
 
145
+ /**
146
+ * The timeout of the sandbox in milliseconds.
147
+ */
148
+ public get timeout(): number {
149
+ return this.sandbox.timeout;
150
+ }
151
+
132
152
  /**
133
153
  * Internal metadata about this sandbox.
134
154
  */
135
- private readonly sandbox: SandboxMetaData;
155
+ private sandbox: SandboxMetaData;
136
156
 
137
157
  /**
138
158
  * Allow to get a list of sandboxes for a team narrowed to the given params.
@@ -147,7 +167,10 @@ export class Sandbox {
147
167
  teamId: credentials.teamId,
148
168
  token: credentials.token,
149
169
  });
150
- return client.listSandboxes(params);
170
+ return client.listSandboxes({
171
+ ...params,
172
+ signal: params.signal,
173
+ });
151
174
  }
152
175
 
153
176
  /**
@@ -175,6 +198,7 @@ export class Sandbox {
175
198
  timeout: params?.timeout,
176
199
  resources: params?.resources,
177
200
  runtime: params?.runtime,
201
+ signal: params?.signal,
178
202
  ...privateParams,
179
203
  });
180
204
 
@@ -202,6 +226,7 @@ export class Sandbox {
202
226
 
203
227
  const sandbox = await client.getSandbox({
204
228
  sandboxId: params.sandboxId,
229
+ signal: params.signal,
205
230
  });
206
231
 
207
232
  return new Sandbox({
@@ -236,12 +261,18 @@ export class Sandbox {
236
261
  * Get a previously run command by its ID.
237
262
  *
238
263
  * @param cmdId - ID of the command to retrieve
264
+ * @param opts - Optional parameters.
265
+ * @param opts.signal - An AbortSignal to cancel the operation.
239
266
  * @returns A {@link Command} instance representing the command
240
267
  */
241
- async getCommand(cmdId: string): Promise<Command> {
268
+ async getCommand(
269
+ cmdId: string,
270
+ opts?: { signal?: AbortSignal },
271
+ ): Promise<Command> {
242
272
  const command = await this.client.getCommand({
243
273
  sandboxId: this.sandbox.id,
244
274
  cmdId,
275
+ signal: opts?.signal,
245
276
  });
246
277
 
247
278
  return new Command({
@@ -256,9 +287,15 @@ export class Sandbox {
256
287
  *
257
288
  * @param command - The command to execute.
258
289
  * @param args - Arguments to pass to the command.
290
+ * @param opts - Optional parameters.
291
+ * @param opts.signal - An AbortSignal to cancel the command execution.
259
292
  * @returns A {@link CommandFinished} result once execution is done.
260
293
  */
261
- async runCommand(command: string, args?: string[]): Promise<CommandFinished>;
294
+ async runCommand(
295
+ command: string,
296
+ args?: string[],
297
+ opts?: { signal?: AbortSignal },
298
+ ): Promise<CommandFinished>;
262
299
 
263
300
  /**
264
301
  * Start executing a command in detached mode.
@@ -281,9 +318,10 @@ export class Sandbox {
281
318
  async runCommand(
282
319
  commandOrParams: string | RunCommandParams,
283
320
  args?: string[],
321
+ opts?: { signal?: AbortSignal },
284
322
  ): Promise<Command | CommandFinished> {
285
323
  return typeof commandOrParams === "string"
286
- ? this._runCommand({ cmd: commandOrParams, args })
324
+ ? this._runCommand({ cmd: commandOrParams, args, signal: opts?.signal })
287
325
  : this._runCommand(commandOrParams);
288
326
  }
289
327
 
@@ -302,6 +340,7 @@ export class Sandbox {
302
340
  cwd: params.cwd,
303
341
  env: params.env ?? {},
304
342
  sudo: params.sudo ?? false,
343
+ signal: params.signal,
305
344
  });
306
345
 
307
346
  const command = new Command({
@@ -312,7 +351,7 @@ export class Sandbox {
312
351
 
313
352
  if (params.stdout || params.stderr) {
314
353
  (async () => {
315
- for await (const log of command.logs()) {
354
+ for await (const log of command.logs({ signal: params.signal })) {
316
355
  if (log.stream === "stdout") {
317
356
  params.stdout?.write(log.data);
318
357
  } else if (log.stream === "stderr") {
@@ -322,18 +361,21 @@ export class Sandbox {
322
361
  })();
323
362
  }
324
363
 
325
- return params.detached ? command : command.wait();
364
+ return params.detached ? command : command.wait({ signal: params.signal });
326
365
  }
327
366
 
328
367
  /**
329
368
  * Create a directory in the filesystem of this sandbox.
330
369
  *
331
370
  * @param path - Path of the directory to create
371
+ * @param opts - Optional parameters.
372
+ * @param opts.signal - An AbortSignal to cancel the operation.
332
373
  */
333
- async mkDir(path: string): Promise<void> {
374
+ async mkDir(path: string, opts?: { signal?: AbortSignal }): Promise<void> {
334
375
  await this.client.mkDir({
335
376
  sandboxId: this.sandbox.id,
336
377
  path: path,
378
+ signal: opts?.signal,
337
379
  });
338
380
  }
339
381
 
@@ -341,13 +383,19 @@ export class Sandbox {
341
383
  * Read a file from the filesystem of this sandbox.
342
384
  *
343
385
  * @param file - File to read, with path and optional cwd
386
+ * @param opts - Optional parameters.
387
+ * @param opts.signal - An AbortSignal to cancel the operation.
344
388
  * @returns A promise that resolves to a ReadableStream containing the file contents
345
389
  */
346
- async readFile(file: { path: string; cwd?: string }) {
390
+ async readFile(
391
+ file: { path: string; cwd?: string },
392
+ opts?: { signal?: AbortSignal },
393
+ ) {
347
394
  return this.client.readFile({
348
395
  sandboxId: this.sandbox.id,
349
396
  path: file.path,
350
397
  cwd: file.cwd,
398
+ signal: opts?.signal,
351
399
  });
352
400
  }
353
401
 
@@ -357,14 +405,20 @@ export class Sandbox {
357
405
  * Writes files using the `vercel-sandbox` user.
358
406
  *
359
407
  * @param files - Array of files with path and stream/buffer contents
408
+ * @param opts - Optional parameters.
409
+ * @param opts.signal - An AbortSignal to cancel the operation.
360
410
  * @returns A promise that resolves when the files are written
361
411
  */
362
- async writeFiles(files: { path: string; content: Buffer }[]) {
412
+ async writeFiles(
413
+ files: { path: string; content: Buffer }[],
414
+ opts?: { signal?: AbortSignal },
415
+ ) {
363
416
  return this.client.writeFiles({
364
417
  sandboxId: this.sandbox.id,
365
418
  cwd: this.sandbox.cwd,
366
419
  extractDir: "/",
367
420
  files: files,
421
+ signal: opts?.signal,
368
422
  });
369
423
  }
370
424
 
@@ -387,11 +441,39 @@ export class Sandbox {
387
441
  /**
388
442
  * Stop the sandbox.
389
443
  *
444
+ * @param opts - Optional parameters.
445
+ * @param opts.signal - An AbortSignal to cancel the operation.
390
446
  * @returns A promise that resolves when the sandbox is stopped
391
447
  */
392
- async stop() {
448
+ async stop(opts?: { signal?: AbortSignal }) {
393
449
  await this.client.stopSandbox({
394
450
  sandboxId: this.sandbox.id,
451
+ signal: opts?.signal,
452
+ });
453
+ }
454
+
455
+ /**
456
+ * Extend the timeout of the sandbox by the specified duration.
457
+ *
458
+ * This allows you to extend the lifetime of a sandbox up until the maximum
459
+ * execution timeout for your plan.
460
+ *
461
+ * @param duration - The duration in milliseconds to extend the timeout by
462
+ * @param opts - Optional parameters.
463
+ * @param opts.signal - An AbortSignal to cancel the operation.
464
+ * @returns A promise that resolves when the timeout is extended
465
+ */
466
+ async extendTimeout(
467
+ duration: number,
468
+ opts?: { signal?: AbortSignal },
469
+ ): Promise<void> {
470
+ const response = await this.client.extendTimeout({
471
+ sandboxId: this.sandbox.id,
472
+ duration,
473
+ signal: opts?.signal,
395
474
  });
475
+
476
+ // Update the internal sandbox metadata with the new timeout value
477
+ this.sandbox = response.json.sandbox;
396
478
  }
397
479
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Autogenerated by inject-version.ts
2
- export const VERSION = "0.0.21";
2
+ export const VERSION = "0.0.23";