@vercel/sandbox 0.0.21 → 0.0.22

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.
@@ -1,4 +1,4 @@
1
1
 
2
- > @vercel/sandbox@0.0.21 build /home/runner/work/sandbox-sdk/sandbox-sdk/packages/sandbox
2
+ > @vercel/sandbox@0.0.22 build /home/runner/work/sandbox-sdk/sandbox-sdk/packages/sandbox
3
3
  > tsc
4
4
 
@@ -1,4 +1,4 @@
1
1
 
2
- > @vercel/sandbox@0.0.21 typecheck /home/runner/work/sandbox-sdk/sandbox-sdk/packages/sandbox
2
+ > @vercel/sandbox@0.0.22 typecheck /home/runner/work/sandbox-sdk/sandbox-sdk/packages/sandbox
3
3
  > tsc --noEmit
4
4
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @vercel/sandbox
2
2
 
3
+ ## 0.0.22
4
+
5
+ ### Patch Changes
6
+
7
+ - add AbortSignal support in public interface ([#137](https://github.com/vercel/sandbox-sdk/pull/137))
8
+
9
+ - add AbortSignal support to command.wait ([#136](https://github.com/vercel/sandbox-sdk/pull/136))
10
+
3
11
  ## 0.0.21
4
12
 
5
13
  ### Patch Changes
@@ -15,6 +15,7 @@ export declare class APIClient extends BaseClient {
15
15
  protected request(path: string, params?: RequestParams): Promise<Response>;
16
16
  getSandbox(params: {
17
17
  sandboxId: string;
18
+ signal?: AbortSignal;
18
19
  }): Promise<Parsed<{
19
20
  sandbox: {
20
21
  region: string;
@@ -58,6 +59,7 @@ export declare class APIClient extends BaseClient {
58
59
  vcpus: number;
59
60
  };
60
61
  runtime?: "node22" | "python3.13" | (string & {});
62
+ signal?: AbortSignal;
61
63
  }>): Promise<Parsed<{
62
64
  sandbox: {
63
65
  region: string;
@@ -89,6 +91,7 @@ export declare class APIClient extends BaseClient {
89
91
  args: string[];
90
92
  env: Record<string, string>;
91
93
  sudo: boolean;
94
+ signal?: AbortSignal;
92
95
  }): Promise<Parsed<{
93
96
  command: {
94
97
  name: string;
@@ -104,20 +107,24 @@ export declare class APIClient extends BaseClient {
104
107
  sandboxId: string;
105
108
  cmdId: string;
106
109
  wait: true;
110
+ signal?: AbortSignal;
107
111
  }): Promise<Parsed<z.infer<typeof CommandFinishedResponse>>>;
108
112
  getCommand(params: {
109
113
  sandboxId: string;
110
114
  cmdId: string;
111
115
  wait?: boolean;
116
+ signal?: AbortSignal;
112
117
  }): Promise<Parsed<z.infer<typeof CommandResponse>>>;
113
118
  mkDir(params: {
114
119
  sandboxId: string;
115
120
  path: string;
116
121
  cwd?: string;
122
+ signal?: AbortSignal;
117
123
  }): Promise<Parsed<{}>>;
118
124
  getFileWriter(params: {
119
125
  sandboxId: string;
120
126
  extractDir: string;
127
+ signal?: AbortSignal;
121
128
  }): {
122
129
  response: Promise<Response>;
123
130
  writer: FileWriter;
@@ -143,6 +150,7 @@ export declare class APIClient extends BaseClient {
143
150
  * @example 1540095775951
144
151
  */
145
152
  until?: number | Date;
153
+ signal?: AbortSignal;
146
154
  }): Promise<Parsed<{
147
155
  sandboxes: {
148
156
  region: string;
@@ -175,16 +183,19 @@ export declare class APIClient extends BaseClient {
175
183
  content: Buffer;
176
184
  }[];
177
185
  extractDir: string;
186
+ signal?: AbortSignal;
178
187
  }): Promise<void>;
179
188
  readFile(params: {
180
189
  sandboxId: string;
181
190
  path: string;
182
191
  cwd?: string;
192
+ signal?: AbortSignal;
183
193
  }): Promise<NodeJS.ReadableStream | null>;
184
194
  killCommand(params: {
185
195
  sandboxId: string;
186
196
  commandId: string;
187
197
  signal: number;
198
+ abortSignal?: AbortSignal;
188
199
  }): Promise<Parsed<{
189
200
  command: {
190
201
  name: string;
@@ -205,5 +216,6 @@ export declare class APIClient extends BaseClient {
205
216
  };
206
217
  stopSandbox(params: {
207
218
  sandboxId: string;
219
+ signal?: AbortSignal;
208
220
  }): Promise<Parsed<z.infer<typeof SandboxResponse>>>;
209
221
  }
@@ -53,7 +53,9 @@ class APIClient extends base_client_1.BaseClient {
53
53
  });
54
54
  }
55
55
  async getSandbox(params) {
56
- return (0, base_client_1.parseOrThrow)(validators_1.SandboxAndRoutesResponse, await this.request(`/v1/sandboxes/${params.sandboxId}`));
56
+ return (0, base_client_1.parseOrThrow)(validators_1.SandboxAndRoutesResponse, await this.request(`/v1/sandboxes/${params.sandboxId}`, {
57
+ signal: params.signal,
58
+ }));
57
59
  }
58
60
  async createSandbox(params) {
59
61
  const privateParams = (0, types_1.getPrivateParams)(params);
@@ -68,6 +70,7 @@ class APIClient extends base_client_1.BaseClient {
68
70
  runtime: params.runtime,
69
71
  ...privateParams,
70
72
  }),
73
+ signal: params.signal,
71
74
  }));
72
75
  }
73
76
  async runCommand(params) {
@@ -80,17 +83,19 @@ class APIClient extends base_client_1.BaseClient {
80
83
  env: params.env,
81
84
  sudo: params.sudo,
82
85
  }),
86
+ signal: params.signal,
83
87
  }));
84
88
  }
85
89
  async getCommand(params) {
86
90
  return params.wait
87
- ? (0, base_client_1.parseOrThrow)(validators_1.CommandFinishedResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`, { query: { wait: "true" } }))
88
- : (0, base_client_1.parseOrThrow)(validators_1.CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`));
91
+ ? (0, base_client_1.parseOrThrow)(validators_1.CommandFinishedResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`, { signal: params.signal, query: { wait: "true" } }))
92
+ : (0, base_client_1.parseOrThrow)(validators_1.CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`, { signal: params.signal }));
89
93
  }
90
94
  async mkDir(params) {
91
95
  return (0, base_client_1.parseOrThrow)(validators_1.EmptyResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/fs/mkdir`, {
92
96
  method: "POST",
93
97
  body: JSON.stringify({ path: params.path, cwd: params.cwd }),
98
+ signal: params.signal,
94
99
  }));
95
100
  }
96
101
  getFileWriter(params) {
@@ -104,6 +109,7 @@ class APIClient extends base_client_1.BaseClient {
104
109
  "x-cwd": params.extractDir,
105
110
  },
106
111
  body: await (0, consume_readable_1.consumeReadable)(writer.readable),
112
+ signal: params.signal,
107
113
  });
108
114
  })(),
109
115
  writer,
@@ -122,12 +128,14 @@ class APIClient extends base_client_1.BaseClient {
122
128
  : params.until?.getTime(),
123
129
  },
124
130
  method: "GET",
131
+ signal: params.signal,
125
132
  }));
126
133
  }
127
134
  async writeFiles(params) {
128
135
  const { writer, response } = this.getFileWriter({
129
136
  sandboxId: params.sandboxId,
130
137
  extractDir: params.extractDir,
138
+ signal: params.signal,
131
139
  });
132
140
  for (const file of params.files) {
133
141
  await writer.addFile({
@@ -146,6 +154,7 @@ class APIClient extends base_client_1.BaseClient {
146
154
  const response = await this.request(`/v1/sandboxes/${params.sandboxId}/fs/read`, {
147
155
  method: "POST",
148
156
  body: JSON.stringify({ path: params.path, cwd: params.cwd }),
157
+ signal: params.signal,
149
158
  });
150
159
  if (response.status === 404) {
151
160
  return null;
@@ -159,6 +168,7 @@ class APIClient extends base_client_1.BaseClient {
159
168
  return (0, base_client_1.parseOrThrow)(validators_1.CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/${params.commandId}/kill`, {
160
169
  method: "POST",
161
170
  body: JSON.stringify({ signal: params.signal }),
171
+ signal: params.abortSignal,
162
172
  }));
163
173
  }
164
174
  getLogs(params) {
@@ -200,7 +210,7 @@ class APIClient extends base_client_1.BaseClient {
200
210
  }
201
211
  async stopSandbox(params) {
202
212
  const url = `/v1/sandboxes/${params.sandboxId}/stop`;
203
- return (0, base_client_1.parseOrThrow)(validators_1.SandboxResponse, await this.request(url, { method: "POST" }));
213
+ return (0, base_client_1.parseOrThrow)(validators_1.SandboxResponse, await this.request(url, { method: "POST", signal: params.signal }));
204
214
  }
205
215
  }
206
216
  exports.APIClient = APIClient;
@@ -41,6 +41,7 @@ class BaseClient {
41
41
  : opts?.headers,
42
42
  // @ts-expect-error Node.js' and undici's Agent have different types
43
43
  dispatcher: this.agent,
44
+ signal: opts?.signal,
44
45
  });
45
46
  if (this.debug) {
46
47
  const duration = Date.now() - start;
package/dist/command.d.ts CHANGED
@@ -88,9 +88,13 @@ export declare class Command {
88
88
  * }
89
89
  * ```
90
90
  *
91
+ * @param params - Optional parameters.
92
+ * @param params.signal - An AbortSignal to cancel waiting.
91
93
  * @returns A {@link CommandFinished} instance with populated exit code.
92
94
  */
93
- wait(): Promise<CommandFinished>;
95
+ wait(params?: {
96
+ signal?: AbortSignal;
97
+ }): Promise<CommandFinished>;
94
98
  /**
95
99
  * Get the output of `stdout`, `stderr`, or both as a string.
96
100
  *
@@ -98,35 +102,50 @@ export declare class Command {
98
102
  * not output valid Unicode.
99
103
  *
100
104
  * @param stream - The output stream to read: "stdout", "stderr", or "both".
105
+ * @param opts - Optional parameters.
106
+ * @param opts.signal - An AbortSignal to cancel output streaming.
101
107
  * @returns The output of the specified stream(s) as a string.
102
108
  */
103
- output(stream?: "stdout" | "stderr" | "both"): Promise<string>;
109
+ output(stream?: "stdout" | "stderr" | "both", opts?: {
110
+ signal?: AbortSignal;
111
+ }): Promise<string>;
104
112
  /**
105
113
  * Get the output of `stdout` as a string.
106
114
  *
107
115
  * NOTE: This may throw string conversion errors if the command does
108
116
  * not output valid Unicode.
109
117
  *
118
+ * @param opts - Optional parameters.
119
+ * @param opts.signal - An AbortSignal to cancel output streaming.
110
120
  * @returns The standard output of the command.
111
121
  */
112
- stdout(): Promise<string>;
122
+ stdout(opts?: {
123
+ signal?: AbortSignal;
124
+ }): Promise<string>;
113
125
  /**
114
126
  * Get the output of `stderr` as a string.
115
127
  *
116
128
  * NOTE: This may throw string conversion errors if the command does
117
129
  * not output valid Unicode.
118
130
  *
131
+ * @param opts - Optional parameters.
132
+ * @param opts.signal - An AbortSignal to cancel output streaming.
119
133
  * @returns The standard error output of the command.
120
134
  */
121
- stderr(): Promise<string>;
135
+ stderr(opts?: {
136
+ signal?: AbortSignal;
137
+ }): Promise<string>;
122
138
  /**
123
139
  * Kill a running command in a sandbox.
124
140
  *
125
- * @param params - commandId and the signal to send the running process.
126
- * Defaults to SIGTERM.
141
+ * @param signal - The signal to send the running process. Defaults to SIGTERM.
142
+ * @param opts - Optional parameters.
143
+ * @param opts.abortSignal - An AbortSignal to cancel the kill operation.
127
144
  * @returns Promise<void>.
128
145
  */
129
- kill(signal?: Signal): Promise<void>;
146
+ kill(signal?: Signal, opts?: {
147
+ abortSignal?: AbortSignal;
148
+ }): Promise<void>;
130
149
  }
131
150
  /**
132
151
  * A command that has finished executing.
package/dist/command.js CHANGED
@@ -82,13 +82,17 @@ class Command {
82
82
  * }
83
83
  * ```
84
84
  *
85
+ * @param params - Optional parameters.
86
+ * @param params.signal - An AbortSignal to cancel waiting.
85
87
  * @returns A {@link CommandFinished} instance with populated exit code.
86
88
  */
87
- async wait() {
89
+ async wait(params) {
90
+ params?.signal?.throwIfAborted();
88
91
  const command = await this.client.getCommand({
89
92
  sandboxId: this.sandboxId,
90
93
  cmdId: this.cmd.id,
91
94
  wait: true,
95
+ signal: params?.signal,
92
96
  });
93
97
  return new CommandFinished({
94
98
  client: this.client,
@@ -104,11 +108,13 @@ class Command {
104
108
  * not output valid Unicode.
105
109
  *
106
110
  * @param stream - The output stream to read: "stdout", "stderr", or "both".
111
+ * @param opts - Optional parameters.
112
+ * @param opts.signal - An AbortSignal to cancel output streaming.
107
113
  * @returns The output of the specified stream(s) as a string.
108
114
  */
109
- async output(stream = "both") {
115
+ async output(stream = "both", opts) {
110
116
  let data = "";
111
- for await (const log of this.logs()) {
117
+ for await (const log of this.logs({ signal: opts?.signal })) {
112
118
  if (stream === "both" || log.stream === stream) {
113
119
  data += log.data;
114
120
  }
@@ -121,10 +127,12 @@ class Command {
121
127
  * NOTE: This may throw string conversion errors if the command does
122
128
  * not output valid Unicode.
123
129
  *
130
+ * @param opts - Optional parameters.
131
+ * @param opts.signal - An AbortSignal to cancel output streaming.
124
132
  * @returns The standard output of the command.
125
133
  */
126
- async stdout() {
127
- return this.output("stdout");
134
+ async stdout(opts) {
135
+ return this.output("stdout", opts);
128
136
  }
129
137
  /**
130
138
  * Get the output of `stderr` as a string.
@@ -132,23 +140,27 @@ class Command {
132
140
  * NOTE: This may throw string conversion errors if the command does
133
141
  * not output valid Unicode.
134
142
  *
143
+ * @param opts - Optional parameters.
144
+ * @param opts.signal - An AbortSignal to cancel output streaming.
135
145
  * @returns The standard error output of the command.
136
146
  */
137
- async stderr() {
138
- return this.output("stderr");
147
+ async stderr(opts) {
148
+ return this.output("stderr", opts);
139
149
  }
140
150
  /**
141
151
  * Kill a running command in a sandbox.
142
152
  *
143
- * @param params - commandId and the signal to send the running process.
144
- * Defaults to SIGTERM.
153
+ * @param signal - The signal to send the running process. Defaults to SIGTERM.
154
+ * @param opts - Optional parameters.
155
+ * @param opts.abortSignal - An AbortSignal to cancel the kill operation.
145
156
  * @returns Promise<void>.
146
157
  */
147
- async kill(signal) {
158
+ async kill(signal, opts) {
148
159
  await this.client.killCommand({
149
160
  sandboxId: this.sandboxId,
150
161
  commandId: this.cmd.id,
151
162
  signal: (0, resolveSignal_1.resolveSignal)(signal ?? "SIGTERM"),
163
+ abortSignal: opts?.abortSignal,
152
164
  });
153
165
  }
154
166
  }
package/dist/sandbox.d.ts CHANGED
@@ -54,6 +54,10 @@ 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
+ * An AbortSignal to cancel sandbox creation.
59
+ */
60
+ signal?: AbortSignal;
57
61
  }
58
62
  /** @inline */
59
63
  interface GetSandboxParams {
@@ -61,6 +65,10 @@ interface GetSandboxParams {
61
65
  * Unique identifier of the sandbox.
62
66
  */
63
67
  sandboxId: string;
68
+ /**
69
+ * An AbortSignal to cancel the operation.
70
+ */
71
+ signal?: AbortSignal;
64
72
  }
65
73
  /** @inline */
66
74
  interface RunCommandParams {
@@ -96,6 +104,10 @@ interface RunCommandParams {
96
104
  * A `Writable` stream where `stderr` from the command will be piped
97
105
  */
98
106
  stderr?: Writable;
107
+ /**
108
+ * An AbortSignal to cancel the command execution
109
+ */
110
+ signal?: AbortSignal;
99
111
  }
100
112
  /**
101
113
  * A Sandbox is an isolated Linux MicroVM to run commands in.
@@ -181,17 +193,25 @@ export declare class Sandbox {
181
193
  * Get a previously run command by its ID.
182
194
  *
183
195
  * @param cmdId - ID of the command to retrieve
196
+ * @param opts - Optional parameters.
197
+ * @param opts.signal - An AbortSignal to cancel the operation.
184
198
  * @returns A {@link Command} instance representing the command
185
199
  */
186
- getCommand(cmdId: string): Promise<Command>;
200
+ getCommand(cmdId: string, opts?: {
201
+ signal?: AbortSignal;
202
+ }): Promise<Command>;
187
203
  /**
188
204
  * Start executing a command in this sandbox.
189
205
  *
190
206
  * @param command - The command to execute.
191
207
  * @param args - Arguments to pass to the command.
208
+ * @param opts - Optional parameters.
209
+ * @param opts.signal - An AbortSignal to cancel the command execution.
192
210
  * @returns A {@link CommandFinished} result once execution is done.
193
211
  */
194
- runCommand(command: string, args?: string[]): Promise<CommandFinished>;
212
+ runCommand(command: string, args?: string[], opts?: {
213
+ signal?: AbortSignal;
214
+ }): Promise<CommandFinished>;
195
215
  /**
196
216
  * Start executing a command in detached mode.
197
217
  *
@@ -220,17 +240,25 @@ export declare class Sandbox {
220
240
  * Create a directory in the filesystem of this sandbox.
221
241
  *
222
242
  * @param path - Path of the directory to create
243
+ * @param opts - Optional parameters.
244
+ * @param opts.signal - An AbortSignal to cancel the operation.
223
245
  */
224
- mkDir(path: string): Promise<void>;
246
+ mkDir(path: string, opts?: {
247
+ signal?: AbortSignal;
248
+ }): Promise<void>;
225
249
  /**
226
250
  * Read a file from the filesystem of this sandbox.
227
251
  *
228
252
  * @param file - File to read, with path and optional cwd
253
+ * @param opts - Optional parameters.
254
+ * @param opts.signal - An AbortSignal to cancel the operation.
229
255
  * @returns A promise that resolves to a ReadableStream containing the file contents
230
256
  */
231
257
  readFile(file: {
232
258
  path: string;
233
259
  cwd?: string;
260
+ }, opts?: {
261
+ signal?: AbortSignal;
234
262
  }): Promise<NodeJS.ReadableStream | null>;
235
263
  /**
236
264
  * Write files to the filesystem of this sandbox.
@@ -238,12 +266,16 @@ export declare class Sandbox {
238
266
  * Writes files using the `vercel-sandbox` user.
239
267
  *
240
268
  * @param files - Array of files with path and stream/buffer contents
269
+ * @param opts - Optional parameters.
270
+ * @param opts.signal - An AbortSignal to cancel the operation.
241
271
  * @returns A promise that resolves when the files are written
242
272
  */
243
273
  writeFiles(files: {
244
274
  path: string;
245
275
  content: Buffer;
246
- }[]): Promise<void>;
276
+ }[], opts?: {
277
+ signal?: AbortSignal;
278
+ }): Promise<void>;
247
279
  /**
248
280
  * Get the public domain of a port of this sandbox.
249
281
  *
@@ -255,8 +287,12 @@ export declare class Sandbox {
255
287
  /**
256
288
  * Stop the sandbox.
257
289
  *
290
+ * @param opts - Optional parameters.
291
+ * @param opts.signal - An AbortSignal to cancel the operation.
258
292
  * @returns A promise that resolves when the sandbox is stopped
259
293
  */
260
- stop(): Promise<void>;
294
+ stop(opts?: {
295
+ signal?: AbortSignal;
296
+ }): Promise<void>;
261
297
  }
262
298
  export {};
package/dist/sandbox.js CHANGED
@@ -35,7 +35,10 @@ class Sandbox {
35
35
  teamId: credentials.teamId,
36
36
  token: credentials.token,
37
37
  });
38
- return client.listSandboxes(params);
38
+ return client.listSandboxes({
39
+ ...params,
40
+ signal: params.signal,
41
+ });
39
42
  }
40
43
  /**
41
44
  * Create a new sandbox.
@@ -57,6 +60,7 @@ class Sandbox {
57
60
  timeout: params?.timeout,
58
61
  resources: params?.resources,
59
62
  runtime: params?.runtime,
63
+ signal: params?.signal,
60
64
  ...privateParams,
61
65
  });
62
66
  return new Sandbox({
@@ -79,6 +83,7 @@ class Sandbox {
79
83
  });
80
84
  const sandbox = await client.getSandbox({
81
85
  sandboxId: params.sandboxId,
86
+ signal: params.signal,
82
87
  });
83
88
  return new Sandbox({
84
89
  client,
@@ -102,12 +107,15 @@ class Sandbox {
102
107
  * Get a previously run command by its ID.
103
108
  *
104
109
  * @param cmdId - ID of the command to retrieve
110
+ * @param opts - Optional parameters.
111
+ * @param opts.signal - An AbortSignal to cancel the operation.
105
112
  * @returns A {@link Command} instance representing the command
106
113
  */
107
- async getCommand(cmdId) {
114
+ async getCommand(cmdId, opts) {
108
115
  const command = await this.client.getCommand({
109
116
  sandboxId: this.sandbox.id,
110
117
  cmdId,
118
+ signal: opts?.signal,
111
119
  });
112
120
  return new command_1.Command({
113
121
  client: this.client,
@@ -115,9 +123,9 @@ class Sandbox {
115
123
  cmd: command.json.command,
116
124
  });
117
125
  }
118
- async runCommand(commandOrParams, args) {
126
+ async runCommand(commandOrParams, args, opts) {
119
127
  return typeof commandOrParams === "string"
120
- ? this._runCommand({ cmd: commandOrParams, args })
128
+ ? this._runCommand({ cmd: commandOrParams, args, signal: opts?.signal })
121
129
  : this._runCommand(commandOrParams);
122
130
  }
123
131
  /**
@@ -135,6 +143,7 @@ class Sandbox {
135
143
  cwd: params.cwd,
136
144
  env: params.env ?? {},
137
145
  sudo: params.sudo ?? false,
146
+ signal: params.signal,
138
147
  });
139
148
  const command = new command_1.Command({
140
149
  client: this.client,
@@ -143,7 +152,7 @@ class Sandbox {
143
152
  });
144
153
  if (params.stdout || params.stderr) {
145
154
  (async () => {
146
- for await (const log of command.logs()) {
155
+ for await (const log of command.logs({ signal: params.signal })) {
147
156
  if (log.stream === "stdout") {
148
157
  params.stdout?.write(log.data);
149
158
  }
@@ -153,30 +162,36 @@ class Sandbox {
153
162
  }
154
163
  })();
155
164
  }
156
- return params.detached ? command : command.wait();
165
+ return params.detached ? command : command.wait({ signal: params.signal });
157
166
  }
158
167
  /**
159
168
  * Create a directory in the filesystem of this sandbox.
160
169
  *
161
170
  * @param path - Path of the directory to create
171
+ * @param opts - Optional parameters.
172
+ * @param opts.signal - An AbortSignal to cancel the operation.
162
173
  */
163
- async mkDir(path) {
174
+ async mkDir(path, opts) {
164
175
  await this.client.mkDir({
165
176
  sandboxId: this.sandbox.id,
166
177
  path: path,
178
+ signal: opts?.signal,
167
179
  });
168
180
  }
169
181
  /**
170
182
  * Read a file from the filesystem of this sandbox.
171
183
  *
172
184
  * @param file - File to read, with path and optional cwd
185
+ * @param opts - Optional parameters.
186
+ * @param opts.signal - An AbortSignal to cancel the operation.
173
187
  * @returns A promise that resolves to a ReadableStream containing the file contents
174
188
  */
175
- async readFile(file) {
189
+ async readFile(file, opts) {
176
190
  return this.client.readFile({
177
191
  sandboxId: this.sandbox.id,
178
192
  path: file.path,
179
193
  cwd: file.cwd,
194
+ signal: opts?.signal,
180
195
  });
181
196
  }
182
197
  /**
@@ -185,14 +200,17 @@ class Sandbox {
185
200
  * Writes files using the `vercel-sandbox` user.
186
201
  *
187
202
  * @param files - Array of files with path and stream/buffer contents
203
+ * @param opts - Optional parameters.
204
+ * @param opts.signal - An AbortSignal to cancel the operation.
188
205
  * @returns A promise that resolves when the files are written
189
206
  */
190
- async writeFiles(files) {
207
+ async writeFiles(files, opts) {
191
208
  return this.client.writeFiles({
192
209
  sandboxId: this.sandbox.id,
193
210
  cwd: this.sandbox.cwd,
194
211
  extractDir: "/",
195
212
  files: files,
213
+ signal: opts?.signal,
196
214
  });
197
215
  }
198
216
  /**
@@ -214,11 +232,14 @@ class Sandbox {
214
232
  /**
215
233
  * Stop the sandbox.
216
234
  *
235
+ * @param opts - Optional parameters.
236
+ * @param opts.signal - An AbortSignal to cancel the operation.
217
237
  * @returns A promise that resolves when the sandbox is stopped
218
238
  */
219
- async stop() {
239
+ async stop(opts) {
220
240
  await this.client.stopSandbox({
221
241
  sandboxId: this.sandbox.id,
242
+ signal: opts?.signal,
222
243
  });
223
244
  }
224
245
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.0.21";
1
+ export declare const VERSION = "0.0.22";
package/dist/version.js CHANGED
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
4
  // Autogenerated by inject-version.ts
5
- exports.VERSION = "0.0.21";
5
+ exports.VERSION = "0.0.22";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/sandbox",
3
- "version": "0.0.21",
3
+ "version": "0.0.22",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -71,10 +71,12 @@ export class APIClient extends BaseClient {
71
71
  });
72
72
  }
73
73
 
74
- async getSandbox(params: { sandboxId: string }) {
74
+ async getSandbox(params: { sandboxId: string; signal?: AbortSignal }) {
75
75
  return parseOrThrow(
76
76
  SandboxAndRoutesResponse,
77
- await this.request(`/v1/sandboxes/${params.sandboxId}`),
77
+ await this.request(`/v1/sandboxes/${params.sandboxId}`, {
78
+ signal: params.signal,
79
+ }),
78
80
  );
79
81
  }
80
82
 
@@ -95,6 +97,7 @@ export class APIClient extends BaseClient {
95
97
  timeout?: number;
96
98
  resources?: { vcpus: number };
97
99
  runtime?: "node22" | "python3.13" | (string & {});
100
+ signal?: AbortSignal;
98
101
  }>,
99
102
  ) {
100
103
  const privateParams = getPrivateParams(params);
@@ -111,6 +114,7 @@ export class APIClient extends BaseClient {
111
114
  runtime: params.runtime,
112
115
  ...privateParams,
113
116
  }),
117
+ signal: params.signal,
114
118
  }),
115
119
  );
116
120
  }
@@ -122,6 +126,7 @@ export class APIClient extends BaseClient {
122
126
  args: string[];
123
127
  env: Record<string, string>;
124
128
  sudo: boolean;
129
+ signal?: AbortSignal;
125
130
  }) {
126
131
  return parseOrThrow(
127
132
  CommandResponse,
@@ -134,6 +139,7 @@ export class APIClient extends BaseClient {
134
139
  env: params.env,
135
140
  sudo: params.sudo,
136
141
  }),
142
+ signal: params.signal,
137
143
  }),
138
144
  );
139
145
  }
@@ -142,44 +148,58 @@ export class APIClient extends BaseClient {
142
148
  sandboxId: string;
143
149
  cmdId: string;
144
150
  wait: true;
151
+ signal?: AbortSignal;
145
152
  }): Promise<Parsed<z.infer<typeof CommandFinishedResponse>>>;
146
153
  async getCommand(params: {
147
154
  sandboxId: string;
148
155
  cmdId: string;
149
156
  wait?: boolean;
157
+ signal?: AbortSignal;
150
158
  }): Promise<Parsed<z.infer<typeof CommandResponse>>>;
151
159
  async getCommand(params: {
152
160
  sandboxId: string;
153
161
  cmdId: string;
154
162
  wait?: boolean;
163
+ signal?: AbortSignal;
155
164
  }) {
156
165
  return params.wait
157
166
  ? parseOrThrow(
158
167
  CommandFinishedResponse,
159
168
  await this.request(
160
169
  `/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`,
161
- { query: { wait: "true" } },
170
+ { signal: params.signal, query: { wait: "true" } },
162
171
  ),
163
172
  )
164
173
  : parseOrThrow(
165
174
  CommandResponse,
166
175
  await this.request(
167
176
  `/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`,
177
+ { signal: params.signal },
168
178
  ),
169
179
  );
170
180
  }
171
181
 
172
- async mkDir(params: { sandboxId: string; path: string; cwd?: string }) {
182
+ async mkDir(params: {
183
+ sandboxId: string;
184
+ path: string;
185
+ cwd?: string;
186
+ signal?: AbortSignal;
187
+ }) {
173
188
  return parseOrThrow(
174
189
  EmptyResponse,
175
190
  await this.request(`/v1/sandboxes/${params.sandboxId}/fs/mkdir`, {
176
191
  method: "POST",
177
192
  body: JSON.stringify({ path: params.path, cwd: params.cwd }),
193
+ signal: params.signal,
178
194
  }),
179
195
  );
180
196
  }
181
197
 
182
- getFileWriter(params: { sandboxId: string; extractDir: string }) {
198
+ getFileWriter(params: {
199
+ sandboxId: string;
200
+ extractDir: string;
201
+ signal?: AbortSignal;
202
+ }) {
183
203
  const writer = new FileWriter();
184
204
  return {
185
205
  response: (async () => {
@@ -190,6 +210,7 @@ export class APIClient extends BaseClient {
190
210
  "x-cwd": params.extractDir,
191
211
  },
192
212
  body: await consumeReadable(writer.readable),
213
+ signal: params.signal,
193
214
  });
194
215
  })(),
195
216
  writer,
@@ -217,6 +238,7 @@ export class APIClient extends BaseClient {
217
238
  * @example 1540095775951
218
239
  */
219
240
  until?: number | Date;
241
+ signal?: AbortSignal;
220
242
  }) {
221
243
  return parseOrThrow(
222
244
  SandboxesResponse,
@@ -234,6 +256,7 @@ export class APIClient extends BaseClient {
234
256
  : params.until?.getTime(),
235
257
  },
236
258
  method: "GET",
259
+ signal: params.signal,
237
260
  }),
238
261
  );
239
262
  }
@@ -243,10 +266,12 @@ export class APIClient extends BaseClient {
243
266
  cwd: string;
244
267
  files: { path: string; content: Buffer }[];
245
268
  extractDir: string;
269
+ signal?: AbortSignal;
246
270
  }) {
247
271
  const { writer, response } = this.getFileWriter({
248
272
  sandboxId: params.sandboxId,
249
273
  extractDir: params.extractDir,
274
+ signal: params.signal,
250
275
  });
251
276
 
252
277
  for (const file of params.files) {
@@ -268,12 +293,14 @@ export class APIClient extends BaseClient {
268
293
  sandboxId: string;
269
294
  path: string;
270
295
  cwd?: string;
296
+ signal?: AbortSignal;
271
297
  }): Promise<NodeJS.ReadableStream | null> {
272
298
  const response = await this.request(
273
299
  `/v1/sandboxes/${params.sandboxId}/fs/read`,
274
300
  {
275
301
  method: "POST",
276
302
  body: JSON.stringify({ path: params.path, cwd: params.cwd }),
303
+ signal: params.signal,
277
304
  },
278
305
  );
279
306
 
@@ -292,6 +319,7 @@ export class APIClient extends BaseClient {
292
319
  sandboxId: string;
293
320
  commandId: string;
294
321
  signal: number;
322
+ abortSignal?: AbortSignal;
295
323
  }) {
296
324
  return parseOrThrow(
297
325
  CommandResponse,
@@ -300,6 +328,7 @@ export class APIClient extends BaseClient {
300
328
  {
301
329
  method: "POST",
302
330
  body: JSON.stringify({ signal: params.signal }),
331
+ signal: params.abortSignal,
303
332
  },
304
333
  ),
305
334
  );
@@ -356,11 +385,12 @@ export class APIClient extends BaseClient {
356
385
 
357
386
  async stopSandbox(params: {
358
387
  sandboxId: string;
388
+ signal?: AbortSignal;
359
389
  }): Promise<Parsed<z.infer<typeof SandboxResponse>>> {
360
390
  const url = `/v1/sandboxes/${params.sandboxId}/stop`;
361
391
  return parseOrThrow(
362
392
  SandboxResponse,
363
- await this.request(url, { method: "POST" }),
393
+ await this.request(url, { method: "POST", signal: params.signal }),
364
394
  );
365
395
  }
366
396
  }
@@ -55,6 +55,7 @@ export class BaseClient {
55
55
  : opts?.headers,
56
56
  // @ts-expect-error Node.js' and undici's Agent have different types
57
57
  dispatcher: this.agent,
58
+ signal: opts?.signal,
58
59
  });
59
60
 
60
61
  if (this.debug) {
package/src/command.ts CHANGED
@@ -112,13 +112,18 @@ export class Command {
112
112
  * }
113
113
  * ```
114
114
  *
115
+ * @param params - Optional parameters.
116
+ * @param params.signal - An AbortSignal to cancel waiting.
115
117
  * @returns A {@link CommandFinished} instance with populated exit code.
116
118
  */
117
- async wait() {
119
+ async wait(params?: { signal?: AbortSignal }) {
120
+ params?.signal?.throwIfAborted();
121
+
118
122
  const command = await this.client.getCommand({
119
123
  sandboxId: this.sandboxId,
120
124
  cmdId: this.cmd.id,
121
125
  wait: true,
126
+ signal: params?.signal,
122
127
  });
123
128
 
124
129
  return new CommandFinished({
@@ -136,11 +141,16 @@ export class Command {
136
141
  * not output valid Unicode.
137
142
  *
138
143
  * @param stream - The output stream to read: "stdout", "stderr", or "both".
144
+ * @param opts - Optional parameters.
145
+ * @param opts.signal - An AbortSignal to cancel output streaming.
139
146
  * @returns The output of the specified stream(s) as a string.
140
147
  */
141
- async output(stream: "stdout" | "stderr" | "both" = "both") {
148
+ async output(
149
+ stream: "stdout" | "stderr" | "both" = "both",
150
+ opts?: { signal?: AbortSignal },
151
+ ) {
142
152
  let data = "";
143
- for await (const log of this.logs()) {
153
+ for await (const log of this.logs({ signal: opts?.signal })) {
144
154
  if (stream === "both" || log.stream === stream) {
145
155
  data += log.data;
146
156
  }
@@ -154,10 +164,12 @@ export class Command {
154
164
  * NOTE: This may throw string conversion errors if the command does
155
165
  * not output valid Unicode.
156
166
  *
167
+ * @param opts - Optional parameters.
168
+ * @param opts.signal - An AbortSignal to cancel output streaming.
157
169
  * @returns The standard output of the command.
158
170
  */
159
- async stdout() {
160
- return this.output("stdout");
171
+ async stdout(opts?: { signal?: AbortSignal }) {
172
+ return this.output("stdout", opts);
161
173
  }
162
174
 
163
175
  /**
@@ -166,24 +178,28 @@ export class Command {
166
178
  * NOTE: This may throw string conversion errors if the command does
167
179
  * not output valid Unicode.
168
180
  *
181
+ * @param opts - Optional parameters.
182
+ * @param opts.signal - An AbortSignal to cancel output streaming.
169
183
  * @returns The standard error output of the command.
170
184
  */
171
- async stderr() {
172
- return this.output("stderr");
185
+ async stderr(opts?: { signal?: AbortSignal }) {
186
+ return this.output("stderr", opts);
173
187
  }
174
188
 
175
189
  /**
176
190
  * Kill a running command in a sandbox.
177
191
  *
178
- * @param params - commandId and the signal to send the running process.
179
- * Defaults to SIGTERM.
192
+ * @param signal - The signal to send the running process. Defaults to SIGTERM.
193
+ * @param opts - Optional parameters.
194
+ * @param opts.abortSignal - An AbortSignal to cancel the kill operation.
180
195
  * @returns Promise<void>.
181
196
  */
182
- async kill(signal?: Signal) {
197
+ async kill(signal?: Signal, opts?: { abortSignal?: AbortSignal }) {
183
198
  await this.client.killCommand({
184
199
  sandboxId: this.sandboxId,
185
200
  commandId: this.cmd.id,
186
201
  signal: resolveSignal(signal ?? "SIGTERM"),
202
+ abortSignal: opts?.abortSignal,
187
203
  });
188
204
  }
189
205
  }
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
  /**
@@ -147,7 +160,10 @@ export class Sandbox {
147
160
  teamId: credentials.teamId,
148
161
  token: credentials.token,
149
162
  });
150
- return client.listSandboxes(params);
163
+ return client.listSandboxes({
164
+ ...params,
165
+ signal: params.signal,
166
+ });
151
167
  }
152
168
 
153
169
  /**
@@ -175,6 +191,7 @@ export class Sandbox {
175
191
  timeout: params?.timeout,
176
192
  resources: params?.resources,
177
193
  runtime: params?.runtime,
194
+ signal: params?.signal,
178
195
  ...privateParams,
179
196
  });
180
197
 
@@ -202,6 +219,7 @@ export class Sandbox {
202
219
 
203
220
  const sandbox = await client.getSandbox({
204
221
  sandboxId: params.sandboxId,
222
+ signal: params.signal,
205
223
  });
206
224
 
207
225
  return new Sandbox({
@@ -236,12 +254,18 @@ export class Sandbox {
236
254
  * Get a previously run command by its ID.
237
255
  *
238
256
  * @param cmdId - ID of the command to retrieve
257
+ * @param opts - Optional parameters.
258
+ * @param opts.signal - An AbortSignal to cancel the operation.
239
259
  * @returns A {@link Command} instance representing the command
240
260
  */
241
- async getCommand(cmdId: string): Promise<Command> {
261
+ async getCommand(
262
+ cmdId: string,
263
+ opts?: { signal?: AbortSignal },
264
+ ): Promise<Command> {
242
265
  const command = await this.client.getCommand({
243
266
  sandboxId: this.sandbox.id,
244
267
  cmdId,
268
+ signal: opts?.signal,
245
269
  });
246
270
 
247
271
  return new Command({
@@ -256,9 +280,15 @@ export class Sandbox {
256
280
  *
257
281
  * @param command - The command to execute.
258
282
  * @param args - Arguments to pass to the command.
283
+ * @param opts - Optional parameters.
284
+ * @param opts.signal - An AbortSignal to cancel the command execution.
259
285
  * @returns A {@link CommandFinished} result once execution is done.
260
286
  */
261
- async runCommand(command: string, args?: string[]): Promise<CommandFinished>;
287
+ async runCommand(
288
+ command: string,
289
+ args?: string[],
290
+ opts?: { signal?: AbortSignal },
291
+ ): Promise<CommandFinished>;
262
292
 
263
293
  /**
264
294
  * Start executing a command in detached mode.
@@ -281,9 +311,10 @@ export class Sandbox {
281
311
  async runCommand(
282
312
  commandOrParams: string | RunCommandParams,
283
313
  args?: string[],
314
+ opts?: { signal?: AbortSignal },
284
315
  ): Promise<Command | CommandFinished> {
285
316
  return typeof commandOrParams === "string"
286
- ? this._runCommand({ cmd: commandOrParams, args })
317
+ ? this._runCommand({ cmd: commandOrParams, args, signal: opts?.signal })
287
318
  : this._runCommand(commandOrParams);
288
319
  }
289
320
 
@@ -302,6 +333,7 @@ export class Sandbox {
302
333
  cwd: params.cwd,
303
334
  env: params.env ?? {},
304
335
  sudo: params.sudo ?? false,
336
+ signal: params.signal,
305
337
  });
306
338
 
307
339
  const command = new Command({
@@ -312,7 +344,7 @@ export class Sandbox {
312
344
 
313
345
  if (params.stdout || params.stderr) {
314
346
  (async () => {
315
- for await (const log of command.logs()) {
347
+ for await (const log of command.logs({ signal: params.signal })) {
316
348
  if (log.stream === "stdout") {
317
349
  params.stdout?.write(log.data);
318
350
  } else if (log.stream === "stderr") {
@@ -322,18 +354,21 @@ export class Sandbox {
322
354
  })();
323
355
  }
324
356
 
325
- return params.detached ? command : command.wait();
357
+ return params.detached ? command : command.wait({ signal: params.signal });
326
358
  }
327
359
 
328
360
  /**
329
361
  * Create a directory in the filesystem of this sandbox.
330
362
  *
331
363
  * @param path - Path of the directory to create
364
+ * @param opts - Optional parameters.
365
+ * @param opts.signal - An AbortSignal to cancel the operation.
332
366
  */
333
- async mkDir(path: string): Promise<void> {
367
+ async mkDir(path: string, opts?: { signal?: AbortSignal }): Promise<void> {
334
368
  await this.client.mkDir({
335
369
  sandboxId: this.sandbox.id,
336
370
  path: path,
371
+ signal: opts?.signal,
337
372
  });
338
373
  }
339
374
 
@@ -341,13 +376,19 @@ export class Sandbox {
341
376
  * Read a file from the filesystem of this sandbox.
342
377
  *
343
378
  * @param file - File to read, with path and optional cwd
379
+ * @param opts - Optional parameters.
380
+ * @param opts.signal - An AbortSignal to cancel the operation.
344
381
  * @returns A promise that resolves to a ReadableStream containing the file contents
345
382
  */
346
- async readFile(file: { path: string; cwd?: string }) {
383
+ async readFile(
384
+ file: { path: string; cwd?: string },
385
+ opts?: { signal?: AbortSignal },
386
+ ) {
347
387
  return this.client.readFile({
348
388
  sandboxId: this.sandbox.id,
349
389
  path: file.path,
350
390
  cwd: file.cwd,
391
+ signal: opts?.signal,
351
392
  });
352
393
  }
353
394
 
@@ -357,14 +398,20 @@ export class Sandbox {
357
398
  * Writes files using the `vercel-sandbox` user.
358
399
  *
359
400
  * @param files - Array of files with path and stream/buffer contents
401
+ * @param opts - Optional parameters.
402
+ * @param opts.signal - An AbortSignal to cancel the operation.
360
403
  * @returns A promise that resolves when the files are written
361
404
  */
362
- async writeFiles(files: { path: string; content: Buffer }[]) {
405
+ async writeFiles(
406
+ files: { path: string; content: Buffer }[],
407
+ opts?: { signal?: AbortSignal },
408
+ ) {
363
409
  return this.client.writeFiles({
364
410
  sandboxId: this.sandbox.id,
365
411
  cwd: this.sandbox.cwd,
366
412
  extractDir: "/",
367
413
  files: files,
414
+ signal: opts?.signal,
368
415
  });
369
416
  }
370
417
 
@@ -387,11 +434,14 @@ export class Sandbox {
387
434
  /**
388
435
  * Stop the sandbox.
389
436
  *
437
+ * @param opts - Optional parameters.
438
+ * @param opts.signal - An AbortSignal to cancel the operation.
390
439
  * @returns A promise that resolves when the sandbox is stopped
391
440
  */
392
- async stop() {
441
+ async stop(opts?: { signal?: AbortSignal }) {
393
442
  await this.client.stopSandbox({
394
443
  sandboxId: this.sandbox.id,
444
+ signal: opts?.signal,
395
445
  });
396
446
  }
397
447
  }
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.22";