@vercel/sandbox 0.0.20 → 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.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +14 -0
- package/dist/api-client/api-client.d.ts +22 -10
- package/dist/api-client/api-client.js +14 -4
- package/dist/api-client/base-client.d.ts +1 -0
- package/dist/api-client/base-client.js +7 -0
- package/dist/api-client/validators.d.ts +46 -46
- package/dist/command.d.ts +27 -8
- package/dist/command.js +22 -10
- package/dist/sandbox.d.ts +43 -7
- package/dist/sandbox.js +31 -10
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -1
- package/src/api-client/api-client.ts +36 -6
- package/src/api-client/base-client.ts +8 -0
- package/src/command.ts +26 -10
- package/src/sandbox.ts +60 -10
- package/src/version.ts +1 -1
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(
|
|
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.
|
|
1
|
+
export declare const VERSION = "0.0.22";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercel/sandbox",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"jsonlines": "0.1.1",
|
|
15
15
|
"ms": "2.1.3",
|
|
16
16
|
"tar-stream": "3.1.7",
|
|
17
|
+
"undici": "^7.16.0",
|
|
17
18
|
"zod": "3.24.4"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
@@ -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: {
|
|
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: {
|
|
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
|
}
|
|
@@ -3,6 +3,7 @@ import { APIError } from "./api-error";
|
|
|
3
3
|
import { ZodType } from "zod";
|
|
4
4
|
import { array } from "../utils/array";
|
|
5
5
|
import { withRetry, type RequestOptions } from "./with-retry";
|
|
6
|
+
import { Agent } from "undici";
|
|
6
7
|
|
|
7
8
|
export interface RequestParams extends RequestInit {
|
|
8
9
|
headers?: Record<string, string>;
|
|
@@ -22,12 +23,16 @@ export class BaseClient {
|
|
|
22
23
|
private fetch: ReturnType<typeof withRetry<RequestInit>>;
|
|
23
24
|
private debug: boolean;
|
|
24
25
|
private host: string;
|
|
26
|
+
private agent: Agent;
|
|
25
27
|
|
|
26
28
|
constructor(params: { debug?: boolean; host: string; token?: string }) {
|
|
27
29
|
this.fetch = withRetry(globalThis.fetch);
|
|
28
30
|
this.host = params.host;
|
|
29
31
|
this.debug = params.debug ?? process.env.DEBUG_FETCH === "true";
|
|
30
32
|
this.token = params.token;
|
|
33
|
+
this.agent = new Agent({
|
|
34
|
+
bodyTimeout: 0, // disable body timeout to allow long logs streaming
|
|
35
|
+
});
|
|
31
36
|
}
|
|
32
37
|
|
|
33
38
|
protected async request(path: string, opts?: RequestParams) {
|
|
@@ -48,6 +53,9 @@ export class BaseClient {
|
|
|
48
53
|
headers: this.token
|
|
49
54
|
? { Authorization: `Bearer ${this.token}`, ...opts?.headers }
|
|
50
55
|
: opts?.headers,
|
|
56
|
+
// @ts-expect-error Node.js' and undici's Agent have different types
|
|
57
|
+
dispatcher: this.agent,
|
|
58
|
+
signal: opts?.signal,
|
|
51
59
|
});
|
|
52
60
|
|
|
53
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(
|
|
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
|
|
179
|
-
*
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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.
|
|
2
|
+
export const VERSION = "0.0.22";
|