@vercel/sandbox 2.0.0-beta.11 → 2.0.0-beta.13
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 +8 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/api-client/api-client.cjs +456 -0
- package/dist/api-client/api-client.cjs.map +1 -0
- package/dist/api-client/api-client.d.cts +790 -0
- package/dist/api-client/api-client.d.ts +784 -777
- package/dist/api-client/api-client.js +445 -471
- package/dist/api-client/api-client.js.map +1 -1
- package/dist/api-client/api-error.cjs +32 -0
- package/dist/api-client/api-error.cjs.map +1 -0
- package/dist/api-client/api-error.d.cts +29 -0
- package/dist/api-client/api-error.d.ts +21 -18
- package/dist/api-client/api-error.js +28 -33
- package/dist/api-client/api-error.js.map +1 -1
- package/dist/api-client/base-client.cjs +125 -0
- package/dist/api-client/base-client.cjs.map +1 -0
- package/dist/api-client/base-client.d.cts +38 -0
- package/dist/api-client/base-client.d.ts +31 -36
- package/dist/api-client/base-client.js +110 -130
- package/dist/api-client/base-client.js.map +1 -1
- package/dist/api-client/file-writer.cjs +62 -0
- package/dist/api-client/file-writer.cjs.map +1 -0
- package/dist/api-client/file-writer.d.cts +66 -0
- package/dist/api-client/file-writer.d.ts +56 -52
- package/dist/api-client/file-writer.js +57 -61
- package/dist/api-client/file-writer.js.map +1 -1
- package/dist/api-client/index.cjs +2 -0
- package/dist/api-client/index.d.ts +2 -2
- package/dist/api-client/index.js +4 -21
- package/dist/api-client/validators.cjs +182 -0
- package/dist/api-client/validators.cjs.map +1 -0
- package/dist/api-client/validators.d.cts +1469 -0
- package/dist/api-client/validators.d.ts +1282 -3699
- package/dist/api-client/validators.js +155 -170
- package/dist/api-client/validators.js.map +1 -1
- package/dist/api-client/with-retry.cjs +89 -0
- package/dist/api-client/with-retry.cjs.map +1 -0
- package/dist/api-client/with-retry.d.cts +10 -0
- package/dist/api-client/with-retry.d.ts +9 -13
- package/dist/api-client/with-retry.js +81 -102
- package/dist/api-client/with-retry.js.map +1 -1
- package/dist/auth/api.cjs +29 -0
- package/dist/auth/api.cjs.map +1 -0
- package/dist/auth/api.js +26 -25
- package/dist/auth/api.js.map +1 -1
- package/dist/auth/error.cjs +13 -0
- package/dist/auth/error.cjs.map +1 -0
- package/dist/auth/error.js +11 -11
- package/dist/auth/error.js.map +1 -1
- package/dist/auth/file.cjs +64 -0
- package/dist/auth/file.cjs.map +1 -0
- package/dist/auth/file.d.cts +26 -0
- package/dist/auth/file.d.ts +19 -15
- package/dist/auth/file.js +49 -64
- package/dist/auth/file.js.map +1 -1
- package/dist/auth/index.cjs +12 -0
- package/dist/auth/index.d.cts +5 -0
- package/dist/auth/index.d.ts +5 -6
- package/dist/auth/index.js +6 -27
- package/dist/auth/linked-project.cjs +38 -0
- package/dist/auth/linked-project.cjs.map +1 -0
- package/dist/auth/linked-project.js +30 -64
- package/dist/auth/linked-project.js.map +1 -1
- package/dist/auth/oauth.cjs +205 -0
- package/dist/auth/oauth.cjs.map +1 -0
- package/dist/auth/oauth.d.cts +135 -0
- package/dist/auth/oauth.d.ts +113 -109
- package/dist/auth/oauth.js +185 -252
- package/dist/auth/oauth.js.map +1 -1
- package/dist/auth/poll-for-token.cjs +82 -0
- package/dist/auth/poll-for-token.cjs.map +1 -0
- package/dist/auth/poll-for-token.d.cts +28 -0
- package/dist/auth/poll-for-token.d.ts +23 -15
- package/dist/auth/poll-for-token.js +79 -64
- package/dist/auth/poll-for-token.js.map +1 -1
- package/dist/auth/project.cjs +80 -0
- package/dist/auth/project.cjs.map +1 -0
- package/dist/auth/project.d.cts +44 -0
- package/dist/auth/project.d.ts +12 -8
- package/dist/auth/project.js +70 -72
- package/dist/auth/project.js.map +1 -1
- package/dist/auth/zod.cjs +22 -0
- package/dist/auth/zod.cjs.map +1 -0
- package/dist/auth/zod.js +18 -17
- package/dist/auth/zod.js.map +1 -1
- package/dist/command.cjs +328 -0
- package/dist/command.cjs.map +1 -0
- package/dist/command.d.cts +289 -0
- package/dist/command.d.ts +265 -171
- package/dist/command.js +323 -226
- package/dist/command.js.map +1 -1
- package/dist/constants.d.cts +5 -0
- package/dist/constants.d.ts +5 -1
- package/dist/index.cjs +13 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -6
- package/dist/index.js +7 -17
- package/dist/network-policy.d.cts +100 -0
- package/dist/network-policy.d.ts +32 -28
- package/dist/sandbox.cjs +694 -0
- package/dist/sandbox.cjs.map +1 -0
- package/dist/sandbox.d.cts +796 -0
- package/dist/sandbox.d.ts +783 -711
- package/dist/sandbox.js +684 -615
- package/dist/sandbox.js.map +1 -1
- package/dist/session.cjs +520 -0
- package/dist/session.cjs.map +1 -0
- package/dist/session.d.cts +406 -0
- package/dist/session.d.ts +398 -367
- package/dist/session.js +517 -505
- package/dist/session.js.map +1 -1
- package/dist/snapshot.cjs +116 -0
- package/dist/snapshot.cjs.map +1 -0
- package/dist/snapshot.d.cts +107 -0
- package/dist/snapshot.d.ts +98 -91
- package/dist/snapshot.js +114 -115
- package/dist/snapshot.js.map +1 -1
- package/dist/utils/array.cjs +17 -0
- package/dist/utils/array.cjs.map +1 -0
- package/dist/utils/array.js +12 -15
- package/dist/utils/array.js.map +1 -1
- package/dist/utils/consume-readable.cjs +18 -0
- package/dist/utils/consume-readable.cjs.map +1 -0
- package/dist/utils/consume-readable.js +13 -12
- package/dist/utils/consume-readable.js.map +1 -1
- package/dist/utils/decode-base64-url.cjs +15 -0
- package/dist/utils/decode-base64-url.cjs.map +1 -0
- package/dist/utils/decode-base64-url.js +10 -9
- package/dist/utils/decode-base64-url.js.map +1 -1
- package/dist/utils/dev-credentials.cjs +142 -0
- package/dist/utils/dev-credentials.cjs.map +1 -0
- package/dist/utils/dev-credentials.js +126 -184
- package/dist/utils/dev-credentials.js.map +1 -1
- package/dist/utils/get-credentials.cjs +123 -0
- package/dist/utils/get-credentials.cjs.map +1 -0
- package/dist/utils/get-credentials.d.cts +21 -0
- package/dist/utils/get-credentials.d.ts +19 -61
- package/dist/utils/get-credentials.js +106 -140
- package/dist/utils/get-credentials.js.map +1 -1
- package/dist/utils/log.cjs +25 -0
- package/dist/utils/log.cjs.map +1 -0
- package/dist/utils/log.js +15 -17
- package/dist/utils/log.js.map +1 -1
- package/dist/utils/network-policy.cjs +65 -0
- package/dist/utils/network-policy.cjs.map +1 -0
- package/dist/utils/network-policy.js +58 -77
- package/dist/utils/network-policy.js.map +1 -1
- package/dist/utils/normalizePath.cjs +27 -0
- package/dist/utils/normalizePath.cjs.map +1 -0
- package/dist/utils/normalizePath.js +21 -28
- package/dist/utils/normalizePath.js.map +1 -1
- package/dist/utils/resolveSignal.cjs +20 -0
- package/dist/utils/resolveSignal.cjs.map +1 -0
- package/dist/utils/resolveSignal.d.cts +15 -0
- package/dist/utils/resolveSignal.d.ts +12 -10
- package/dist/utils/resolveSignal.js +14 -17
- package/dist/utils/resolveSignal.js.map +1 -1
- package/dist/utils/sandbox-snapshot.cjs +14 -0
- package/dist/utils/sandbox-snapshot.cjs.map +1 -0
- package/dist/utils/sandbox-snapshot.d.cts +10 -0
- package/dist/utils/sandbox-snapshot.d.ts +11 -0
- package/dist/utils/sandbox-snapshot.js +14 -0
- package/dist/utils/sandbox-snapshot.js.map +1 -0
- package/dist/utils/types.cjs +13 -0
- package/dist/utils/types.cjs.map +1 -0
- package/dist/utils/types.d.cts +11 -0
- package/dist/utils/types.d.ts +5 -7
- package/dist/utils/types.js +8 -8
- package/dist/utils/types.js.map +1 -1
- package/dist/version.cjs +7 -0
- package/dist/version.cjs.map +1 -0
- package/dist/version.js +5 -5
- package/dist/version.js.map +1 -1
- package/package.json +23 -3
- package/dist/api-client/index.js.map +0 -1
- package/dist/auth/api.d.ts +0 -6
- package/dist/auth/error.d.ts +0 -11
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/linked-project.d.ts +0 -10
- package/dist/auth/zod.d.ts +0 -5
- package/dist/constants.js +0 -3
- package/dist/constants.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/network-policy.js +0 -3
- package/dist/network-policy.js.map +0 -1
- package/dist/utils/array.d.ts +0 -9
- package/dist/utils/consume-readable.d.ts +0 -5
- package/dist/utils/convert-sandbox.d.ts +0 -6
- package/dist/utils/convert-sandbox.js +0 -14
- package/dist/utils/convert-sandbox.js.map +0 -1
- package/dist/utils/decode-base64-url.d.ts +0 -7
- package/dist/utils/dev-credentials.d.ts +0 -37
- package/dist/utils/log.d.ts +0 -2
- package/dist/utils/network-policy.d.ts +0 -7
- package/dist/utils/normalizePath.d.ts +0 -17
- package/dist/version.d.ts +0 -1
package/dist/sandbox.cjs
ADDED
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_api_error = require('./api-client/api-error.cjs');
|
|
3
|
+
const require_network_policy = require('./utils/network-policy.cjs');
|
|
4
|
+
const require_types = require('./utils/types.cjs');
|
|
5
|
+
const require_api_client = require('./api-client/api-client.cjs');
|
|
6
|
+
require('./api-client/index.cjs');
|
|
7
|
+
const require_get_credentials = require('./utils/get-credentials.cjs');
|
|
8
|
+
const require_session = require('./session.cjs');
|
|
9
|
+
let __workflow_serde = require("@workflow/serde");
|
|
10
|
+
let node_timers_promises = require("node:timers/promises");
|
|
11
|
+
|
|
12
|
+
//#region src/sandbox.ts
|
|
13
|
+
function isSandboxStoppedError(err) {
|
|
14
|
+
return err instanceof require_api_error.APIError && err.response.status === 410;
|
|
15
|
+
}
|
|
16
|
+
function isSandboxStoppingError(err) {
|
|
17
|
+
return err instanceof require_api_error.APIError && err.response.status === 422 && err.json?.error?.code === "sandbox_stopping";
|
|
18
|
+
}
|
|
19
|
+
function isSandboxSnapshottingError(err) {
|
|
20
|
+
return err instanceof require_api_error.APIError && err.response.status === 422 && err.json?.error?.code === "sandbox_snapshotting";
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A Sandbox is a persistent, isolated Linux MicroVMs to run commands in.
|
|
24
|
+
* Use {@link Sandbox.create} or {@link Sandbox.get} to construct.
|
|
25
|
+
* @hideconstructor
|
|
26
|
+
*/
|
|
27
|
+
var Sandbox = class Sandbox {
|
|
28
|
+
/**
|
|
29
|
+
* Lazily resolve credentials and construct an API client.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
async ensureClient() {
|
|
33
|
+
"use step";
|
|
34
|
+
if (this._client) return this._client;
|
|
35
|
+
const credentials = await require_get_credentials.getCredentials();
|
|
36
|
+
this._client = new require_api_client.APIClient({
|
|
37
|
+
teamId: credentials.teamId,
|
|
38
|
+
token: credentials.token
|
|
39
|
+
});
|
|
40
|
+
return this._client;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The name of this sandbox.
|
|
44
|
+
*/
|
|
45
|
+
get name() {
|
|
46
|
+
return this.sandbox.name;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Routes from ports to subdomains.
|
|
50
|
+
* @hidden
|
|
51
|
+
*/
|
|
52
|
+
get routes() {
|
|
53
|
+
return this.currentSession().routes;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Whether the sandbox persists the state.
|
|
57
|
+
*/
|
|
58
|
+
get persistent() {
|
|
59
|
+
return this.sandbox.persistent;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The region this sandbox runs in.
|
|
63
|
+
*/
|
|
64
|
+
get region() {
|
|
65
|
+
return this.sandbox.region;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Number of virtual CPUs allocated.
|
|
69
|
+
*/
|
|
70
|
+
get vcpus() {
|
|
71
|
+
return this.sandbox.vcpus;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Memory allocated in MB.
|
|
75
|
+
*/
|
|
76
|
+
get memory() {
|
|
77
|
+
return this.sandbox.memory;
|
|
78
|
+
}
|
|
79
|
+
/** Runtime identifier (e.g. "node24", "python3.13"). */
|
|
80
|
+
get runtime() {
|
|
81
|
+
return this.sandbox.runtime;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Cumulative egress bytes across all sessions.
|
|
85
|
+
*/
|
|
86
|
+
get totalEgressBytes() {
|
|
87
|
+
return this.sandbox.totalEgressBytes;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Cumulative ingress bytes across all sessions.
|
|
91
|
+
*/
|
|
92
|
+
get totalIngressBytes() {
|
|
93
|
+
return this.sandbox.totalIngressBytes;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Cumulative active CPU duration in milliseconds across all sessions.
|
|
97
|
+
*/
|
|
98
|
+
get totalActiveCpuDurationMs() {
|
|
99
|
+
return this.sandbox.totalActiveCpuDurationMs;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Cumulative wall-clock duration in milliseconds across all sessions.
|
|
103
|
+
*/
|
|
104
|
+
get totalDurationMs() {
|
|
105
|
+
return this.sandbox.totalDurationMs;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* When this sandbox was last updated.
|
|
109
|
+
*/
|
|
110
|
+
get updatedAt() {
|
|
111
|
+
return new Date(this.sandbox.updatedAt);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* When the sandbox status was last updated.
|
|
115
|
+
*/
|
|
116
|
+
get statusUpdatedAt() {
|
|
117
|
+
return this.sandbox.statusUpdatedAt ? new Date(this.sandbox.statusUpdatedAt) : void 0;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* When this sandbox was created.
|
|
121
|
+
*/
|
|
122
|
+
get createdAt() {
|
|
123
|
+
return new Date(this.sandbox.createdAt);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Interactive port.
|
|
127
|
+
*/
|
|
128
|
+
get interactivePort() {
|
|
129
|
+
return this.currentSession().interactivePort;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The status of the current session.
|
|
133
|
+
*/
|
|
134
|
+
get status() {
|
|
135
|
+
return this.currentSession().status;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The default timeout of this sandbox in milliseconds.
|
|
139
|
+
*/
|
|
140
|
+
get timeout() {
|
|
141
|
+
return this.sandbox.timeout;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Key-value tags attached to the sandbox.
|
|
145
|
+
*/
|
|
146
|
+
get tags() {
|
|
147
|
+
return this.sandbox.tags;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The default network policy of this sandbox.
|
|
151
|
+
*/
|
|
152
|
+
get networkPolicy() {
|
|
153
|
+
return this.sandbox.networkPolicy ? require_network_policy.fromAPINetworkPolicy(this.sandbox.networkPolicy) : void 0;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* If the session was created from a snapshot, the ID of that snapshot.
|
|
157
|
+
*/
|
|
158
|
+
get sourceSnapshotId() {
|
|
159
|
+
return this.currentSession().sourceSnapshotId;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The current snapshot ID of this sandbox, if any.
|
|
163
|
+
*/
|
|
164
|
+
get currentSnapshotId() {
|
|
165
|
+
return this.sandbox.currentSnapshotId;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The default snapshot expiration in milliseconds, if set.
|
|
169
|
+
*/
|
|
170
|
+
get snapshotExpiration() {
|
|
171
|
+
return this.sandbox.snapshotExpiration;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The amount of CPU used by the session. Only reported once the VM is stopped.
|
|
175
|
+
*/
|
|
176
|
+
get activeCpuUsageMs() {
|
|
177
|
+
return this.currentSession().activeCpuUsageMs;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The amount of network data used by the session. Only reported once the VM is stopped.
|
|
181
|
+
*/
|
|
182
|
+
get networkTransfer() {
|
|
183
|
+
return this.currentSession().networkTransfer;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Allow to get a list of sandboxes for a team narrowed to the given params.
|
|
187
|
+
* It returns both the sandboxes and the pagination metadata to allow getting
|
|
188
|
+
* the next page of results.
|
|
189
|
+
*/
|
|
190
|
+
static async list(params) {
|
|
191
|
+
"use step";
|
|
192
|
+
const credentials = await require_get_credentials.getCredentials(params);
|
|
193
|
+
return (await new require_api_client.APIClient({
|
|
194
|
+
teamId: credentials.teamId,
|
|
195
|
+
token: credentials.token,
|
|
196
|
+
fetch: params?.fetch
|
|
197
|
+
}).listSandboxes({
|
|
198
|
+
...credentials,
|
|
199
|
+
...params
|
|
200
|
+
})).json;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Serialize a Sandbox instance to plain data for @workflow/serde.
|
|
204
|
+
*
|
|
205
|
+
* @param instance - The Sandbox instance to serialize
|
|
206
|
+
* @returns A plain object containing sandbox metadata and routes
|
|
207
|
+
*/
|
|
208
|
+
static [__workflow_serde.WORKFLOW_SERIALIZE](instance) {
|
|
209
|
+
return {
|
|
210
|
+
metadata: instance.session?._sessionSnapshot,
|
|
211
|
+
routes: instance.session?.routes ?? [],
|
|
212
|
+
sandboxMetadata: instance.sandbox,
|
|
213
|
+
projectId: instance.projectId
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Deserialize a Sandbox from serialized snapshot data.
|
|
218
|
+
*
|
|
219
|
+
* The deserialized instance uses the serialized metadata synchronously and
|
|
220
|
+
* lazily creates an API client only when methods perform API requests.
|
|
221
|
+
*
|
|
222
|
+
* @param data - The serialized sandbox data
|
|
223
|
+
* @returns The reconstructed Sandbox instance
|
|
224
|
+
*/
|
|
225
|
+
static [__workflow_serde.WORKFLOW_DESERIALIZE](data) {
|
|
226
|
+
const sandbox = new Sandbox({
|
|
227
|
+
sandbox: data.sandboxMetadata,
|
|
228
|
+
routes: data.routes,
|
|
229
|
+
projectId: data.projectId
|
|
230
|
+
});
|
|
231
|
+
if (data.metadata) sandbox.session = new require_session.Session({
|
|
232
|
+
routes: data.routes,
|
|
233
|
+
snapshot: data.metadata
|
|
234
|
+
});
|
|
235
|
+
return sandbox;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Create a new sandbox.
|
|
239
|
+
*
|
|
240
|
+
* @param params - Creation parameters and optional credentials.
|
|
241
|
+
* @returns A promise resolving to the created {@link Sandbox}.
|
|
242
|
+
* @example
|
|
243
|
+
* <caption>Create a sandbox with default options</caption>
|
|
244
|
+
* const sandbox = await Sandbox.create();
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* <caption>Create a sandbox and drop it in the end of the block</caption>
|
|
248
|
+
* async function fn() {
|
|
249
|
+
* await using const sandbox = await Sandbox.create();
|
|
250
|
+
* // Sandbox automatically stopped at the end of the lexical scope
|
|
251
|
+
* }
|
|
252
|
+
*/
|
|
253
|
+
static async create(params) {
|
|
254
|
+
"use step";
|
|
255
|
+
const credentials = await require_get_credentials.getCredentials(params);
|
|
256
|
+
const client = new require_api_client.APIClient({
|
|
257
|
+
teamId: credentials.teamId,
|
|
258
|
+
token: credentials.token,
|
|
259
|
+
fetch: params?.fetch
|
|
260
|
+
});
|
|
261
|
+
const privateParams = require_types.getPrivateParams(params);
|
|
262
|
+
const response = await client.createSandbox({
|
|
263
|
+
source: params?.source,
|
|
264
|
+
projectId: credentials.projectId,
|
|
265
|
+
ports: params?.ports ?? [],
|
|
266
|
+
timeout: params?.timeout,
|
|
267
|
+
resources: params?.resources,
|
|
268
|
+
runtime: params && "runtime" in params ? params?.runtime : void 0,
|
|
269
|
+
networkPolicy: params?.networkPolicy,
|
|
270
|
+
env: params?.env,
|
|
271
|
+
tags: params?.tags,
|
|
272
|
+
snapshotExpiration: params?.snapshotExpiration,
|
|
273
|
+
signal: params?.signal,
|
|
274
|
+
name: params?.name,
|
|
275
|
+
persistent: params?.persistent,
|
|
276
|
+
...privateParams
|
|
277
|
+
});
|
|
278
|
+
return new DisposableSandbox({
|
|
279
|
+
client,
|
|
280
|
+
session: response.json.session,
|
|
281
|
+
sandbox: response.json.sandbox,
|
|
282
|
+
routes: response.json.routes,
|
|
283
|
+
projectId: credentials.projectId,
|
|
284
|
+
onResume: params?.onResume
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Retrieve an existing sandbox and resume its session.
|
|
289
|
+
*
|
|
290
|
+
* @param params - Get parameters and optional credentials.
|
|
291
|
+
* @returns A promise resolving to the {@link Sandbox}.
|
|
292
|
+
*/
|
|
293
|
+
static async get(params) {
|
|
294
|
+
"use step";
|
|
295
|
+
const credentials = await require_get_credentials.getCredentials(params);
|
|
296
|
+
const client = new require_api_client.APIClient({
|
|
297
|
+
teamId: credentials.teamId,
|
|
298
|
+
token: credentials.token,
|
|
299
|
+
fetch: params.fetch
|
|
300
|
+
});
|
|
301
|
+
const privateParams = require_types.getPrivateParams(params);
|
|
302
|
+
const response = await client.getSandbox({
|
|
303
|
+
name: params.name,
|
|
304
|
+
projectId: credentials.projectId,
|
|
305
|
+
resume: params.resume,
|
|
306
|
+
signal: params.signal,
|
|
307
|
+
...privateParams
|
|
308
|
+
});
|
|
309
|
+
const sandbox = new Sandbox({
|
|
310
|
+
client,
|
|
311
|
+
session: response.json.session,
|
|
312
|
+
sandbox: response.json.sandbox,
|
|
313
|
+
routes: response.json.routes,
|
|
314
|
+
projectId: credentials.projectId,
|
|
315
|
+
onResume: params.onResume
|
|
316
|
+
});
|
|
317
|
+
if (response.json.resumed && params.onResume) await params.onResume(sandbox);
|
|
318
|
+
return sandbox;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Create a new Sandbox instance.
|
|
322
|
+
*
|
|
323
|
+
* @param params.client - Optional API client. If not provided, will be lazily created using global credentials.
|
|
324
|
+
* @param params.routes - Port-to-subdomain mappings for exposed ports
|
|
325
|
+
* @param params.sandbox - Sandbox snapshot metadata
|
|
326
|
+
*/
|
|
327
|
+
constructor({ client, routes, session, sandbox, projectId, onResume }) {
|
|
328
|
+
this._client = null;
|
|
329
|
+
this.resumePromise = null;
|
|
330
|
+
this._client = client ?? null;
|
|
331
|
+
if (session) this.session = new require_session.Session({
|
|
332
|
+
client,
|
|
333
|
+
routes,
|
|
334
|
+
session
|
|
335
|
+
});
|
|
336
|
+
this.sandbox = sandbox;
|
|
337
|
+
this.projectId = projectId ?? "";
|
|
338
|
+
this.onResume = onResume;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Get the current session (the running VM) for this sandbox.
|
|
342
|
+
*
|
|
343
|
+
* @returns The {@link Session} instance.
|
|
344
|
+
*/
|
|
345
|
+
currentSession() {
|
|
346
|
+
if (!this.session) throw new Error("No active session. Run a command or call resume first.");
|
|
347
|
+
return this.session;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Resume this sandbox by creating a new session via `getSandbox`.
|
|
351
|
+
*/
|
|
352
|
+
async resume(signal) {
|
|
353
|
+
if (!this.resumePromise) this.resumePromise = this.doResume(signal).finally(() => {
|
|
354
|
+
this.resumePromise = null;
|
|
355
|
+
});
|
|
356
|
+
return this.resumePromise;
|
|
357
|
+
}
|
|
358
|
+
async doResume(signal) {
|
|
359
|
+
const client = await this.ensureClient();
|
|
360
|
+
const response = await client.getSandbox({
|
|
361
|
+
name: this.sandbox.name,
|
|
362
|
+
projectId: this.projectId,
|
|
363
|
+
resume: true,
|
|
364
|
+
signal
|
|
365
|
+
});
|
|
366
|
+
this.session = new require_session.Session({
|
|
367
|
+
client,
|
|
368
|
+
routes: response.json.routes,
|
|
369
|
+
session: response.json.session
|
|
370
|
+
});
|
|
371
|
+
if (this.onResume && response.json.resumed) await this.onResume(this);
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Poll until the current session reaches a terminal state, then resume.
|
|
375
|
+
*/
|
|
376
|
+
async waitForStopAndResume(signal) {
|
|
377
|
+
"use step";
|
|
378
|
+
const client = await this.ensureClient();
|
|
379
|
+
const pollingInterval = 500;
|
|
380
|
+
let status = this.session.status;
|
|
381
|
+
while (status === "stopping" || status === "snapshotting") {
|
|
382
|
+
await (0, node_timers_promises.setTimeout)(pollingInterval, void 0, { signal });
|
|
383
|
+
const poll = await client.getSession({
|
|
384
|
+
sessionId: this.session.sessionId,
|
|
385
|
+
signal
|
|
386
|
+
});
|
|
387
|
+
this.session = new require_session.Session({
|
|
388
|
+
client,
|
|
389
|
+
routes: poll.json.routes,
|
|
390
|
+
session: poll.json.session
|
|
391
|
+
});
|
|
392
|
+
status = poll.json.session.status;
|
|
393
|
+
}
|
|
394
|
+
await this.resume(signal);
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Execute `fn`, and if the session is stopped/stopping/snapshotting, resume and retry.
|
|
398
|
+
*/
|
|
399
|
+
async withResume(fn, signal) {
|
|
400
|
+
if (!this.session) await this.resume(signal);
|
|
401
|
+
try {
|
|
402
|
+
return await fn();
|
|
403
|
+
} catch (err) {
|
|
404
|
+
if (isSandboxStoppedError(err)) {
|
|
405
|
+
await this.resume(signal);
|
|
406
|
+
return fn();
|
|
407
|
+
}
|
|
408
|
+
if (isSandboxStoppingError(err) || isSandboxSnapshottingError(err)) {
|
|
409
|
+
await this.waitForStopAndResume(signal);
|
|
410
|
+
return fn();
|
|
411
|
+
}
|
|
412
|
+
throw err;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
async runCommand(commandOrParams, args, opts) {
|
|
416
|
+
"use step";
|
|
417
|
+
const signal = typeof commandOrParams === "string" ? opts?.signal : commandOrParams.signal;
|
|
418
|
+
return this.withResume(() => this.session.runCommand(commandOrParams, args, opts), signal);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Internal helper to start a command in the sandbox.
|
|
422
|
+
*
|
|
423
|
+
* @param params - Command execution parameters.
|
|
424
|
+
* @returns A {@link Command} or {@link CommandFinished}, depending on `detached`.
|
|
425
|
+
* @internal
|
|
426
|
+
*/
|
|
427
|
+
async getCommand(cmdId, opts) {
|
|
428
|
+
"use step";
|
|
429
|
+
return this.withResume(() => this.session.getCommand(cmdId, opts), opts?.signal);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Create a directory in the filesystem of this sandbox.
|
|
433
|
+
*
|
|
434
|
+
* @param path - Path of the directory to create
|
|
435
|
+
* @param opts - Optional parameters.
|
|
436
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
437
|
+
*/
|
|
438
|
+
async mkDir(path, opts) {
|
|
439
|
+
"use step";
|
|
440
|
+
return this.withResume(() => this.session.mkDir(path, opts), opts?.signal);
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Read a file from the filesystem of this sandbox as a stream.
|
|
444
|
+
*
|
|
445
|
+
* @param file - File to read, with path and optional cwd
|
|
446
|
+
* @param opts - Optional parameters.
|
|
447
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
448
|
+
* @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found
|
|
449
|
+
*/
|
|
450
|
+
async readFile(file, opts) {
|
|
451
|
+
"use step";
|
|
452
|
+
return this.withResume(() => this.session.readFile(file, opts), opts?.signal);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Read a file from the filesystem of this sandbox as a Buffer.
|
|
456
|
+
*
|
|
457
|
+
* @param file - File to read, with path and optional cwd
|
|
458
|
+
* @param opts - Optional parameters.
|
|
459
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
460
|
+
* @returns A promise that resolves to the file contents as a Buffer, or null if file not found
|
|
461
|
+
*/
|
|
462
|
+
async readFileToBuffer(file, opts) {
|
|
463
|
+
"use step";
|
|
464
|
+
return this.withResume(() => this.session.readFileToBuffer(file, opts), opts?.signal);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Download a file from the sandbox to the local filesystem.
|
|
468
|
+
*
|
|
469
|
+
* @param src - Source file on the sandbox, with path and optional cwd
|
|
470
|
+
* @param dst - Destination file on the local machine, with path and optional cwd
|
|
471
|
+
* @param opts - Optional parameters.
|
|
472
|
+
* @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.
|
|
473
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
474
|
+
* @returns The absolute path to the written file, or null if the source file was not found
|
|
475
|
+
*/
|
|
476
|
+
async downloadFile(src, dst, opts) {
|
|
477
|
+
"use step";
|
|
478
|
+
return this.withResume(() => this.session.downloadFile(src, dst, opts), opts?.signal);
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Write files to the filesystem of this sandbox.
|
|
482
|
+
* Defaults to writing to /vercel/sandbox unless an absolute path is specified.
|
|
483
|
+
* Writes files using the `vercel-sandbox` user.
|
|
484
|
+
*
|
|
485
|
+
* @param files - Array of files with path, content, and optional mode (permissions)
|
|
486
|
+
* @param opts - Optional parameters.
|
|
487
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
488
|
+
* @returns A promise that resolves when the files are written
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* // Write an executable script
|
|
492
|
+
* await sandbox.writeFiles([
|
|
493
|
+
* { path: "/usr/local/bin/myscript", content: "#!/bin/bash\necho hello", mode: 0o755 }
|
|
494
|
+
* ]);
|
|
495
|
+
*/
|
|
496
|
+
async writeFiles(files, opts) {
|
|
497
|
+
"use step";
|
|
498
|
+
return this.withResume(() => this.session.writeFiles(files, opts), opts?.signal);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Get the public domain of a port of this sandbox.
|
|
502
|
+
*
|
|
503
|
+
* @param p - Port number to resolve
|
|
504
|
+
* @returns A full domain (e.g. `https://subdomain.vercel.run`)
|
|
505
|
+
* @throws If the port has no associated route
|
|
506
|
+
*/
|
|
507
|
+
domain(p) {
|
|
508
|
+
return this.currentSession().domain(p);
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Stop the sandbox.
|
|
512
|
+
*
|
|
513
|
+
* @param opts - Optional parameters.
|
|
514
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
515
|
+
* @param opts.blocking - If true, poll until the sandbox has fully stopped and return the final state.
|
|
516
|
+
* @returns The sandbox at the time the stop was acknowledged, or after fully stopped if `blocking` is true.
|
|
517
|
+
*/
|
|
518
|
+
async stop(opts) {
|
|
519
|
+
"use step";
|
|
520
|
+
if (!this.session) throw new Error("No active session to stop.");
|
|
521
|
+
return this.session.stop(opts);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Update the network policy for this sandbox.
|
|
525
|
+
*
|
|
526
|
+
* @deprecated Use {@link Sandbox.update} instead.
|
|
527
|
+
*
|
|
528
|
+
* @param networkPolicy - The new network policy to apply.
|
|
529
|
+
* @param opts - Optional parameters.
|
|
530
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
531
|
+
* @returns A promise that resolves when the network policy is updated.
|
|
532
|
+
*
|
|
533
|
+
* @example
|
|
534
|
+
* // Restrict to specific domains
|
|
535
|
+
* await sandbox.updateNetworkPolicy({
|
|
536
|
+
* allow: ["*.npmjs.org", "github.com"],
|
|
537
|
+
* });
|
|
538
|
+
*
|
|
539
|
+
* @example
|
|
540
|
+
* // Inject credentials with per-domain transformers
|
|
541
|
+
* await sandbox.updateNetworkPolicy({
|
|
542
|
+
* allow: {
|
|
543
|
+
* "ai-gateway.vercel.sh": [{
|
|
544
|
+
* transform: [{
|
|
545
|
+
* headers: { authorization: "Bearer ..." }
|
|
546
|
+
* }]
|
|
547
|
+
* }],
|
|
548
|
+
* "*": []
|
|
549
|
+
* }
|
|
550
|
+
* });
|
|
551
|
+
*
|
|
552
|
+
* @example
|
|
553
|
+
* // Deny all network access
|
|
554
|
+
* await sandbox.updateNetworkPolicy("deny-all");
|
|
555
|
+
*/
|
|
556
|
+
async updateNetworkPolicy(networkPolicy, opts) {
|
|
557
|
+
"use step";
|
|
558
|
+
await this.withResume(() => this.session.update({ networkPolicy }, opts), opts?.signal);
|
|
559
|
+
return this.session.networkPolicy;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Extend the timeout of the sandbox by the specified duration.
|
|
563
|
+
*
|
|
564
|
+
* This allows you to extend the lifetime of a sandbox up until the maximum
|
|
565
|
+
* execution timeout for your plan.
|
|
566
|
+
*
|
|
567
|
+
* @param duration - The duration in milliseconds to extend the timeout by
|
|
568
|
+
* @param opts - Optional parameters.
|
|
569
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
570
|
+
* @returns A promise that resolves when the timeout is extended
|
|
571
|
+
*
|
|
572
|
+
* @example
|
|
573
|
+
* const sandbox = await Sandbox.create({ timeout: ms('10m') });
|
|
574
|
+
* // Extends timeout by 5 minutes, to a total of 15 minutes.
|
|
575
|
+
* await sandbox.extendTimeout(ms('5m'));
|
|
576
|
+
*/
|
|
577
|
+
async extendTimeout(duration, opts) {
|
|
578
|
+
"use step";
|
|
579
|
+
return this.withResume(() => this.session.extendTimeout(duration, opts), opts?.signal);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Create a snapshot from this currently running sandbox. New sandboxes can
|
|
583
|
+
* then be created from this snapshot using {@link Sandbox.createFromSnapshot}.
|
|
584
|
+
*
|
|
585
|
+
* Note: this sandbox will be stopped as part of the snapshot creation process.
|
|
586
|
+
*
|
|
587
|
+
* @param opts - Optional parameters.
|
|
588
|
+
* @param opts.expiration - Optional expiration time in milliseconds. Use 0 for no expiration at all.
|
|
589
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
590
|
+
* @returns A promise that resolves to the Snapshot instance
|
|
591
|
+
*/
|
|
592
|
+
async snapshot(opts) {
|
|
593
|
+
"use step";
|
|
594
|
+
return this.withResume(() => this.session.snapshot(opts), opts?.signal);
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Update the sandbox configuration.
|
|
598
|
+
*
|
|
599
|
+
* @param params - Fields to update.
|
|
600
|
+
* @param opts - Optional abort signal.
|
|
601
|
+
*/
|
|
602
|
+
async update(params, opts) {
|
|
603
|
+
"use step";
|
|
604
|
+
const client = await this.ensureClient();
|
|
605
|
+
let resources;
|
|
606
|
+
if (params.resources?.vcpus) resources = {
|
|
607
|
+
vcpus: params.resources.vcpus,
|
|
608
|
+
memory: params.resources.vcpus * 2048
|
|
609
|
+
};
|
|
610
|
+
this.sandbox = (await client.updateSandbox({
|
|
611
|
+
name: this.sandbox.name,
|
|
612
|
+
projectId: this.projectId,
|
|
613
|
+
persistent: params.persistent,
|
|
614
|
+
resources,
|
|
615
|
+
timeout: params.timeout,
|
|
616
|
+
networkPolicy: params.networkPolicy,
|
|
617
|
+
tags: params.tags,
|
|
618
|
+
snapshotExpiration: params.snapshotExpiration,
|
|
619
|
+
signal: opts?.signal
|
|
620
|
+
})).json.sandbox;
|
|
621
|
+
if (params.networkPolicy) try {
|
|
622
|
+
return await this.session?.update({ networkPolicy: params.networkPolicy }, opts);
|
|
623
|
+
} catch (err) {
|
|
624
|
+
if (isSandboxStoppedError(err) || isSandboxStoppingError(err)) return;
|
|
625
|
+
throw err;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Delete this sandbox.
|
|
630
|
+
*
|
|
631
|
+
* After deletion the instance becomes inert — all further API calls will
|
|
632
|
+
* throw immediately.
|
|
633
|
+
*/
|
|
634
|
+
async delete(opts) {
|
|
635
|
+
"use step";
|
|
636
|
+
await (await this.ensureClient()).deleteSandbox({
|
|
637
|
+
name: this.sandbox.name,
|
|
638
|
+
projectId: this.projectId,
|
|
639
|
+
signal: opts?.signal
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* List sessions (VMs) that have been created for this sandbox.
|
|
644
|
+
*
|
|
645
|
+
* @param params - Optional pagination parameters.
|
|
646
|
+
* @returns The list of sessions and pagination metadata.
|
|
647
|
+
*/
|
|
648
|
+
async listSessions(params) {
|
|
649
|
+
"use step";
|
|
650
|
+
return (await (await this.ensureClient()).listSessions({
|
|
651
|
+
projectId: this.projectId,
|
|
652
|
+
name: this.sandbox.name,
|
|
653
|
+
limit: params?.limit,
|
|
654
|
+
cursor: params?.cursor,
|
|
655
|
+
sortOrder: params?.sortOrder,
|
|
656
|
+
signal: params?.signal
|
|
657
|
+
})).json;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* List snapshots that belong to this sandbox.
|
|
661
|
+
*
|
|
662
|
+
* @param params - Optional pagination parameters.
|
|
663
|
+
* @returns The list of snapshots and pagination metadata.
|
|
664
|
+
*/
|
|
665
|
+
async listSnapshots(params) {
|
|
666
|
+
"use step";
|
|
667
|
+
return (await (await this.ensureClient()).listSnapshots({
|
|
668
|
+
projectId: this.projectId,
|
|
669
|
+
name: this.sandbox.name,
|
|
670
|
+
limit: params?.limit,
|
|
671
|
+
cursor: params?.cursor,
|
|
672
|
+
sortOrder: params?.sortOrder,
|
|
673
|
+
signal: params?.signal
|
|
674
|
+
})).json;
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
/**
|
|
678
|
+
* A {@link Sandbox} that can automatically be disposed using a `await using` statement.
|
|
679
|
+
*
|
|
680
|
+
* @example
|
|
681
|
+
* {
|
|
682
|
+
* await using const sandbox = await Sandbox.create();
|
|
683
|
+
* }
|
|
684
|
+
* // Sandbox is automatically stopped here
|
|
685
|
+
*/
|
|
686
|
+
var DisposableSandbox = class extends Sandbox {
|
|
687
|
+
async [Symbol.asyncDispose]() {
|
|
688
|
+
await this.stop();
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
//#endregion
|
|
693
|
+
exports.Sandbox = Sandbox;
|
|
694
|
+
//# sourceMappingURL=sandbox.cjs.map
|