@vercel/sandbox 2.0.0-beta.11 → 2.0.0-beta.12

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.
Files changed (196) hide show
  1. package/README.md +8 -0
  2. package/dist/_virtual/rolldown_runtime.cjs +29 -0
  3. package/dist/api-client/api-client.cjs +456 -0
  4. package/dist/api-client/api-client.cjs.map +1 -0
  5. package/dist/api-client/api-client.d.cts +788 -0
  6. package/dist/api-client/api-client.d.ts +782 -777
  7. package/dist/api-client/api-client.js +445 -471
  8. package/dist/api-client/api-client.js.map +1 -1
  9. package/dist/api-client/api-error.cjs +32 -0
  10. package/dist/api-client/api-error.cjs.map +1 -0
  11. package/dist/api-client/api-error.d.cts +29 -0
  12. package/dist/api-client/api-error.d.ts +21 -18
  13. package/dist/api-client/api-error.js +28 -33
  14. package/dist/api-client/api-error.js.map +1 -1
  15. package/dist/api-client/base-client.cjs +125 -0
  16. package/dist/api-client/base-client.cjs.map +1 -0
  17. package/dist/api-client/base-client.d.cts +38 -0
  18. package/dist/api-client/base-client.d.ts +31 -36
  19. package/dist/api-client/base-client.js +110 -130
  20. package/dist/api-client/base-client.js.map +1 -1
  21. package/dist/api-client/file-writer.cjs +62 -0
  22. package/dist/api-client/file-writer.cjs.map +1 -0
  23. package/dist/api-client/file-writer.d.cts +66 -0
  24. package/dist/api-client/file-writer.d.ts +56 -52
  25. package/dist/api-client/file-writer.js +57 -61
  26. package/dist/api-client/file-writer.js.map +1 -1
  27. package/dist/api-client/index.cjs +2 -0
  28. package/dist/api-client/index.d.ts +2 -2
  29. package/dist/api-client/index.js +4 -21
  30. package/dist/api-client/validators.cjs +181 -0
  31. package/dist/api-client/validators.cjs.map +1 -0
  32. package/dist/api-client/validators.d.cts +1469 -0
  33. package/dist/api-client/validators.d.ts +1282 -3699
  34. package/dist/api-client/validators.js +154 -170
  35. package/dist/api-client/validators.js.map +1 -1
  36. package/dist/api-client/with-retry.cjs +89 -0
  37. package/dist/api-client/with-retry.cjs.map +1 -0
  38. package/dist/api-client/with-retry.d.cts +10 -0
  39. package/dist/api-client/with-retry.d.ts +9 -13
  40. package/dist/api-client/with-retry.js +81 -102
  41. package/dist/api-client/with-retry.js.map +1 -1
  42. package/dist/auth/api.cjs +29 -0
  43. package/dist/auth/api.cjs.map +1 -0
  44. package/dist/auth/api.js +26 -25
  45. package/dist/auth/api.js.map +1 -1
  46. package/dist/auth/error.cjs +13 -0
  47. package/dist/auth/error.cjs.map +1 -0
  48. package/dist/auth/error.js +11 -11
  49. package/dist/auth/error.js.map +1 -1
  50. package/dist/auth/file.cjs +64 -0
  51. package/dist/auth/file.cjs.map +1 -0
  52. package/dist/auth/file.d.cts +26 -0
  53. package/dist/auth/file.d.ts +19 -15
  54. package/dist/auth/file.js +49 -64
  55. package/dist/auth/file.js.map +1 -1
  56. package/dist/auth/index.cjs +12 -0
  57. package/dist/auth/index.d.cts +5 -0
  58. package/dist/auth/index.d.ts +5 -6
  59. package/dist/auth/index.js +6 -27
  60. package/dist/auth/linked-project.cjs +38 -0
  61. package/dist/auth/linked-project.cjs.map +1 -0
  62. package/dist/auth/linked-project.js +30 -64
  63. package/dist/auth/linked-project.js.map +1 -1
  64. package/dist/auth/oauth.cjs +205 -0
  65. package/dist/auth/oauth.cjs.map +1 -0
  66. package/dist/auth/oauth.d.cts +135 -0
  67. package/dist/auth/oauth.d.ts +113 -109
  68. package/dist/auth/oauth.js +185 -252
  69. package/dist/auth/oauth.js.map +1 -1
  70. package/dist/auth/poll-for-token.cjs +82 -0
  71. package/dist/auth/poll-for-token.cjs.map +1 -0
  72. package/dist/auth/poll-for-token.d.cts +28 -0
  73. package/dist/auth/poll-for-token.d.ts +23 -15
  74. package/dist/auth/poll-for-token.js +79 -64
  75. package/dist/auth/poll-for-token.js.map +1 -1
  76. package/dist/auth/project.cjs +80 -0
  77. package/dist/auth/project.cjs.map +1 -0
  78. package/dist/auth/project.d.cts +44 -0
  79. package/dist/auth/project.d.ts +12 -8
  80. package/dist/auth/project.js +70 -72
  81. package/dist/auth/project.js.map +1 -1
  82. package/dist/auth/zod.cjs +22 -0
  83. package/dist/auth/zod.cjs.map +1 -0
  84. package/dist/auth/zod.js +18 -17
  85. package/dist/auth/zod.js.map +1 -1
  86. package/dist/command.cjs +328 -0
  87. package/dist/command.cjs.map +1 -0
  88. package/dist/command.d.cts +289 -0
  89. package/dist/command.d.ts +265 -171
  90. package/dist/command.js +323 -226
  91. package/dist/command.js.map +1 -1
  92. package/dist/constants.d.cts +5 -0
  93. package/dist/constants.d.ts +5 -1
  94. package/dist/index.cjs +13 -0
  95. package/dist/index.d.cts +7 -0
  96. package/dist/index.d.ts +7 -6
  97. package/dist/index.js +7 -17
  98. package/dist/network-policy.d.cts +100 -0
  99. package/dist/network-policy.d.ts +32 -28
  100. package/dist/sandbox.cjs +688 -0
  101. package/dist/sandbox.cjs.map +1 -0
  102. package/dist/sandbox.d.cts +780 -0
  103. package/dist/sandbox.d.ts +767 -711
  104. package/dist/sandbox.js +678 -615
  105. package/dist/sandbox.js.map +1 -1
  106. package/dist/session.cjs +520 -0
  107. package/dist/session.cjs.map +1 -0
  108. package/dist/session.d.cts +406 -0
  109. package/dist/session.d.ts +398 -367
  110. package/dist/session.js +517 -505
  111. package/dist/session.js.map +1 -1
  112. package/dist/snapshot.cjs +116 -0
  113. package/dist/snapshot.cjs.map +1 -0
  114. package/dist/snapshot.d.cts +107 -0
  115. package/dist/snapshot.d.ts +98 -91
  116. package/dist/snapshot.js +114 -115
  117. package/dist/snapshot.js.map +1 -1
  118. package/dist/utils/array.cjs +17 -0
  119. package/dist/utils/array.cjs.map +1 -0
  120. package/dist/utils/array.js +12 -15
  121. package/dist/utils/array.js.map +1 -1
  122. package/dist/utils/consume-readable.cjs +18 -0
  123. package/dist/utils/consume-readable.cjs.map +1 -0
  124. package/dist/utils/consume-readable.js +13 -12
  125. package/dist/utils/consume-readable.js.map +1 -1
  126. package/dist/utils/decode-base64-url.cjs +15 -0
  127. package/dist/utils/decode-base64-url.cjs.map +1 -0
  128. package/dist/utils/decode-base64-url.js +10 -9
  129. package/dist/utils/decode-base64-url.js.map +1 -1
  130. package/dist/utils/dev-credentials.cjs +142 -0
  131. package/dist/utils/dev-credentials.cjs.map +1 -0
  132. package/dist/utils/dev-credentials.js +126 -184
  133. package/dist/utils/dev-credentials.js.map +1 -1
  134. package/dist/utils/get-credentials.cjs +123 -0
  135. package/dist/utils/get-credentials.cjs.map +1 -0
  136. package/dist/utils/get-credentials.d.cts +21 -0
  137. package/dist/utils/get-credentials.d.ts +19 -61
  138. package/dist/utils/get-credentials.js +106 -140
  139. package/dist/utils/get-credentials.js.map +1 -1
  140. package/dist/utils/log.cjs +25 -0
  141. package/dist/utils/log.cjs.map +1 -0
  142. package/dist/utils/log.js +15 -17
  143. package/dist/utils/log.js.map +1 -1
  144. package/dist/utils/network-policy.cjs +65 -0
  145. package/dist/utils/network-policy.cjs.map +1 -0
  146. package/dist/utils/network-policy.js +58 -77
  147. package/dist/utils/network-policy.js.map +1 -1
  148. package/dist/utils/normalizePath.cjs +27 -0
  149. package/dist/utils/normalizePath.cjs.map +1 -0
  150. package/dist/utils/normalizePath.js +21 -28
  151. package/dist/utils/normalizePath.js.map +1 -1
  152. package/dist/utils/resolveSignal.cjs +20 -0
  153. package/dist/utils/resolveSignal.cjs.map +1 -0
  154. package/dist/utils/resolveSignal.d.cts +15 -0
  155. package/dist/utils/resolveSignal.d.ts +12 -10
  156. package/dist/utils/resolveSignal.js +14 -17
  157. package/dist/utils/resolveSignal.js.map +1 -1
  158. package/dist/utils/sandbox-snapshot.cjs +14 -0
  159. package/dist/utils/sandbox-snapshot.cjs.map +1 -0
  160. package/dist/utils/sandbox-snapshot.d.cts +10 -0
  161. package/dist/utils/sandbox-snapshot.d.ts +11 -0
  162. package/dist/utils/sandbox-snapshot.js +14 -0
  163. package/dist/utils/sandbox-snapshot.js.map +1 -0
  164. package/dist/utils/types.cjs +13 -0
  165. package/dist/utils/types.cjs.map +1 -0
  166. package/dist/utils/types.d.cts +11 -0
  167. package/dist/utils/types.d.ts +5 -7
  168. package/dist/utils/types.js +8 -8
  169. package/dist/utils/types.js.map +1 -1
  170. package/dist/version.cjs +7 -0
  171. package/dist/version.cjs.map +1 -0
  172. package/dist/version.js +5 -5
  173. package/dist/version.js.map +1 -1
  174. package/package.json +23 -3
  175. package/dist/api-client/index.js.map +0 -1
  176. package/dist/auth/api.d.ts +0 -6
  177. package/dist/auth/error.d.ts +0 -11
  178. package/dist/auth/index.js.map +0 -1
  179. package/dist/auth/linked-project.d.ts +0 -10
  180. package/dist/auth/zod.d.ts +0 -5
  181. package/dist/constants.js +0 -3
  182. package/dist/constants.js.map +0 -1
  183. package/dist/index.js.map +0 -1
  184. package/dist/network-policy.js +0 -3
  185. package/dist/network-policy.js.map +0 -1
  186. package/dist/utils/array.d.ts +0 -9
  187. package/dist/utils/consume-readable.d.ts +0 -5
  188. package/dist/utils/convert-sandbox.d.ts +0 -6
  189. package/dist/utils/convert-sandbox.js +0 -14
  190. package/dist/utils/convert-sandbox.js.map +0 -1
  191. package/dist/utils/decode-base64-url.d.ts +0 -7
  192. package/dist/utils/dev-credentials.d.ts +0 -37
  193. package/dist/utils/log.d.ts +0 -2
  194. package/dist/utils/network-policy.d.ts +0 -7
  195. package/dist/utils/normalizePath.d.ts +0 -17
  196. package/dist/version.d.ts +0 -1
package/dist/session.js CHANGED
@@ -1,507 +1,519 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Session = void 0;
4
- const promises_1 = require("stream/promises");
5
- const fs_1 = require("fs");
6
- const promises_2 = require("fs/promises");
7
- const path_1 = require("path");
8
- const command_1 = require("./command");
9
- const snapshot_1 = require("./snapshot");
10
- const consume_readable_1 = require("./utils/consume-readable");
11
- const convert_sandbox_1 = require("./utils/convert-sandbox");
1
+ import { consumeReadable } from "./utils/consume-readable.js";
2
+ import { APIClient } from "./api-client/api-client.js";
3
+ import "./api-client/index.js";
4
+ import { getCredentials } from "./utils/get-credentials.js";
5
+ import { Command, CommandFinished } from "./command.js";
6
+ import { Snapshot } from "./snapshot.js";
7
+ import { toSandboxSnapshot } from "./utils/sandbox-snapshot.js";
8
+ import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from "@workflow/serde";
9
+ import { dirname, resolve } from "path";
10
+ import { pipeline } from "stream/promises";
11
+ import { createWriteStream } from "fs";
12
+ import { mkdir } from "fs/promises";
13
+
14
+ //#region src/session.ts
12
15
  /**
13
- * A Session represents a running VM instance within a {@link Sandbox}.
14
- *
15
- * Obtain a session via {@link Sandbox.currentSession}.
16
- */
17
- class Session {
18
- /**
19
- * Unique ID of this session.
20
- */
21
- get sessionId() {
22
- return this.session.id;
23
- }
24
- get interactivePort() {
25
- return this.session.interactivePort ?? undefined;
26
- }
27
- /**
28
- * The status of this session.
29
- */
30
- get status() {
31
- return this.session.status;
32
- }
33
- /**
34
- * The creation date of this session.
35
- */
36
- get createdAt() {
37
- return new Date(this.session.createdAt);
38
- }
39
- /**
40
- * The timeout of this session in milliseconds.
41
- */
42
- get timeout() {
43
- return this.session.timeout;
44
- }
45
- /**
46
- * The network policy of this session.
47
- */
48
- get networkPolicy() {
49
- return this.session.networkPolicy;
50
- }
51
- /**
52
- * If the session was created from a snapshot, the ID of that snapshot.
53
- */
54
- get sourceSnapshotId() {
55
- return this.session.sourceSnapshotId;
56
- }
57
- /**
58
- * Memory allocated to this session in MB.
59
- */
60
- get memory() {
61
- return this.session.memory;
62
- }
63
- /**
64
- * Number of vCPUs allocated to this session.
65
- */
66
- get vcpus() {
67
- return this.session.vcpus;
68
- }
69
- /**
70
- * The region where this session is hosted.
71
- */
72
- get region() {
73
- return this.session.region;
74
- }
75
- /**
76
- * Runtime identifier (e.g. "node24", "python3.13").
77
- */
78
- get runtime() {
79
- return this.session.runtime;
80
- }
81
- /**
82
- * The working directory of this session.
83
- */
84
- get cwd() {
85
- return this.session.cwd;
86
- }
87
- /**
88
- * When this session was requested.
89
- */
90
- get requestedAt() {
91
- return new Date(this.session.requestedAt);
92
- }
93
- /**
94
- * When this session started running.
95
- */
96
- get startedAt() {
97
- return this.session.startedAt != null
98
- ? new Date(this.session.startedAt)
99
- : undefined;
100
- }
101
- /**
102
- * When this session was requested to stop.
103
- */
104
- get requestedStopAt() {
105
- return this.session.requestedStopAt != null
106
- ? new Date(this.session.requestedStopAt)
107
- : undefined;
108
- }
109
- /**
110
- * When this session was stopped.
111
- */
112
- get stoppedAt() {
113
- return this.session.stoppedAt != null
114
- ? new Date(this.session.stoppedAt)
115
- : undefined;
116
- }
117
- /**
118
- * When this session was aborted.
119
- */
120
- get abortedAt() {
121
- return this.session.abortedAt != null
122
- ? new Date(this.session.abortedAt)
123
- : undefined;
124
- }
125
- /**
126
- * The wall-clock duration of this session in milliseconds.
127
- */
128
- get duration() {
129
- return this.session.duration;
130
- }
131
- /**
132
- * When a snapshot was requested for this session.
133
- */
134
- get snapshottedAt() {
135
- return this.session.snapshottedAt != null
136
- ? new Date(this.session.snapshottedAt)
137
- : undefined;
138
- }
139
- /**
140
- * When this session was last updated.
141
- */
142
- get updatedAt() {
143
- return new Date(this.session.updatedAt);
144
- }
145
- /**
146
- * The amount of active CPU used by the session. Only reported once the VM is
147
- * stopped.
148
- */
149
- get activeCpuUsageMs() {
150
- return this.session.activeCpuDurationMs;
151
- }
152
- /**
153
- * The amount of network data used by the session. Only reported once the VM
154
- * is stopped.
155
- */
156
- get networkTransfer() {
157
- return this.session.networkTransfer;
158
- }
159
- constructor({ client, routes, session, }) {
160
- this.client = client;
161
- this.routes = routes;
162
- this.session = (0, convert_sandbox_1.convertSession)(session);
163
- }
164
- /**
165
- * Get a previously run command by its ID.
166
- *
167
- * @param cmdId - ID of the command to retrieve
168
- * @param opts - Optional parameters.
169
- * @param opts.signal - An AbortSignal to cancel the operation.
170
- * @returns A {@link Command} instance representing the command
171
- */
172
- async getCommand(cmdId, opts) {
173
- const command = await this.client.getCommand({
174
- sessionId: this.session.id,
175
- cmdId,
176
- signal: opts?.signal,
177
- });
178
- return new command_1.Command({
179
- client: this.client,
180
- sessionId: this.session.id,
181
- cmd: command.json.command,
182
- });
183
- }
184
- async runCommand(commandOrParams, args, opts) {
185
- return typeof commandOrParams === "string"
186
- ? this._runCommand({ cmd: commandOrParams, args, signal: opts?.signal })
187
- : this._runCommand(commandOrParams);
188
- }
189
- /**
190
- * Internal helper to start a command in the session.
191
- *
192
- * @param params - Command execution parameters.
193
- * @returns A {@link Command} or {@link CommandFinished}, depending on `detached`.
194
- * @internal
195
- */
196
- async _runCommand(params) {
197
- const wait = params.detached ? false : true;
198
- const pipeLogs = async (command) => {
199
- if (!params.stdout && !params.stderr) {
200
- return;
201
- }
202
- try {
203
- for await (const log of command.logs({ signal: params.signal })) {
204
- if (log.stream === "stdout") {
205
- params.stdout?.write(log.data);
206
- }
207
- else if (log.stream === "stderr") {
208
- params.stderr?.write(log.data);
209
- }
210
- }
211
- }
212
- catch (err) {
213
- if (params.signal?.aborted) {
214
- return;
215
- }
216
- throw err;
217
- }
218
- };
219
- if (wait) {
220
- const commandStream = await this.client.runCommand({
221
- sessionId: this.session.id,
222
- command: params.cmd,
223
- args: params.args ?? [],
224
- cwd: params.cwd,
225
- env: params.env ?? {},
226
- sudo: params.sudo ?? false,
227
- wait: true,
228
- signal: params.signal,
229
- });
230
- const command = new command_1.Command({
231
- client: this.client,
232
- sessionId: this.session.id,
233
- cmd: commandStream.command,
234
- });
235
- const [finished] = await Promise.all([
236
- commandStream.finished,
237
- pipeLogs(command),
238
- ]);
239
- return new command_1.CommandFinished({
240
- client: this.client,
241
- sessionId: this.session.id,
242
- cmd: finished,
243
- exitCode: finished.exitCode ?? 0,
244
- });
245
- }
246
- const commandResponse = await this.client.runCommand({
247
- sessionId: this.session.id,
248
- command: params.cmd,
249
- args: params.args ?? [],
250
- cwd: params.cwd,
251
- env: params.env ?? {},
252
- sudo: params.sudo ?? false,
253
- signal: params.signal,
254
- });
255
- const command = new command_1.Command({
256
- client: this.client,
257
- sessionId: this.session.id,
258
- cmd: commandResponse.json.command,
259
- });
260
- void pipeLogs(command).catch((err) => {
261
- if (params.signal?.aborted) {
262
- return;
263
- }
264
- (params.stderr ?? params.stdout)?.emit("error", err);
265
- });
266
- return command;
267
- }
268
- /**
269
- * Create a directory in the filesystem of this session.
270
- *
271
- * @param path - Path of the directory to create
272
- * @param opts - Optional parameters.
273
- * @param opts.signal - An AbortSignal to cancel the operation.
274
- */
275
- async mkDir(path, opts) {
276
- await this.client.mkDir({
277
- sessionId: this.session.id,
278
- path: path,
279
- signal: opts?.signal,
280
- });
281
- }
282
- /**
283
- * Read a file from the filesystem of this session as a stream.
284
- *
285
- * @param file - File to read, with path and optional cwd
286
- * @param opts - Optional parameters.
287
- * @param opts.signal - An AbortSignal to cancel the operation.
288
- * @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found
289
- */
290
- async readFile(file, opts) {
291
- return this.client.readFile({
292
- sessionId: this.session.id,
293
- path: file.path,
294
- cwd: file.cwd,
295
- signal: opts?.signal,
296
- });
297
- }
298
- /**
299
- * Read a file from the filesystem of this session as a Buffer.
300
- *
301
- * @param file - File to read, with path and optional cwd
302
- * @param opts - Optional parameters.
303
- * @param opts.signal - An AbortSignal to cancel the operation.
304
- * @returns A promise that resolves to the file contents as a Buffer, or null if file not found
305
- */
306
- async readFileToBuffer(file, opts) {
307
- const stream = await this.client.readFile({
308
- sessionId: this.session.id,
309
- path: file.path,
310
- cwd: file.cwd,
311
- signal: opts?.signal,
312
- });
313
- if (stream === null) {
314
- return null;
315
- }
316
- return (0, consume_readable_1.consumeReadable)(stream);
317
- }
318
- /**
319
- * Download a file from the session to the local filesystem.
320
- *
321
- * @param src - Source file on the session, with path and optional cwd
322
- * @param dst - Destination file on the local machine, with path and optional cwd
323
- * @param opts - Optional parameters.
324
- * @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.
325
- * @param opts.signal - An AbortSignal to cancel the operation.
326
- * @returns The absolute path to the written file, or null if the source file was not found
327
- */
328
- async downloadFile(src, dst, opts) {
329
- if (!src?.path) {
330
- throw new Error("downloadFile: source path is required");
331
- }
332
- if (!dst?.path) {
333
- throw new Error("downloadFile: destination path is required");
334
- }
335
- const stream = await this.client.readFile({
336
- sessionId: this.session.id,
337
- path: src.path,
338
- cwd: src.cwd,
339
- signal: opts?.signal,
340
- });
341
- if (stream === null) {
342
- return null;
343
- }
344
- try {
345
- const dstPath = (0, path_1.resolve)(dst.cwd ?? "", dst.path);
346
- if (opts?.mkdirRecursive) {
347
- await (0, promises_2.mkdir)((0, path_1.dirname)(dstPath), { recursive: true });
348
- }
349
- await (0, promises_1.pipeline)(stream, (0, fs_1.createWriteStream)(dstPath), {
350
- signal: opts?.signal,
351
- });
352
- return dstPath;
353
- }
354
- finally {
355
- stream.destroy();
356
- }
357
- }
358
- /**
359
- * Write files to the filesystem of this session.
360
- * Defaults to writing to /vercel/sandbox unless an absolute path is specified.
361
- * Writes files using the `vercel-sandbox` user.
362
- *
363
- * @param files - Array of files with path and stream/buffer contents
364
- * @param opts - Optional parameters.
365
- * @param opts.signal - An AbortSignal to cancel the operation.
366
- * @returns A promise that resolves when the files are written
367
- */
368
- async writeFiles(files, opts) {
369
- return this.client.writeFiles({
370
- sessionId: this.session.id,
371
- cwd: this.session.cwd,
372
- extractDir: "/",
373
- files: files,
374
- signal: opts?.signal,
375
- });
376
- }
377
- /**
378
- * Get the public domain of a port of this session.
379
- *
380
- * @param p - Port number to resolve
381
- * @returns A full domain (e.g. `https://subdomain.vercel.run`)
382
- * @throws If the port has no associated route
383
- */
384
- domain(p) {
385
- const route = this.routes.find(({ port }) => port == p);
386
- if (route) {
387
- return `https://${route.subdomain}.vercel.run`;
388
- }
389
- else {
390
- throw new Error(`No route for port ${p}`);
391
- }
392
- }
393
- /**
394
- * Stop this session.
395
- *
396
- * @param opts - Optional parameters.
397
- * @param opts.signal - An AbortSignal to cancel the operation.
398
- * @param opts.blocking - If true, poll until the session has fully stopped and return the final state.
399
- * @returns The session at the time the stop was acknowledged, or after fully stopped if `blocking` is true.
400
- */
401
- async stop(opts) {
402
- const response = await this.client.stopSession({
403
- sessionId: this.session.id,
404
- signal: opts?.signal,
405
- blocking: opts?.blocking,
406
- });
407
- this.session = (0, convert_sandbox_1.convertSession)(response.json.session);
408
- return this.session;
409
- }
410
- /**
411
- * Update the current session's settings.
412
- *
413
- * @param params - Fields to update.
414
- * @param params.networkPolicy - The new network policy to apply.
415
- * @param opts - Optional parameters.
416
- * @param opts.signal - An AbortSignal to cancel the operation.
417
- *
418
- * @example
419
- * // Restrict to specific domains
420
- * await session.update({
421
- * networkPolicy: {
422
- * allow: ["*.npmjs.org", "github.com"],
423
- * }
424
- * });
425
- *
426
- * @example
427
- * // Inject credentials with per-domain transformers
428
- * await session.update({
429
- * networkPolicy: {
430
- * allow: {
431
- * "ai-gateway.vercel.sh": [{
432
- * transform: [{
433
- * headers: { authorization: "Bearer ..." }
434
- * }]
435
- * }],
436
- * "*": []
437
- * }
438
- * }
439
- * });
440
- *
441
- * @example
442
- * // Deny all network access
443
- * await session.update({ networkPolicy: "deny-all" });
444
- */
445
- async update(params, opts) {
446
- if (params.networkPolicy !== undefined) {
447
- const response = await this.client.updateNetworkPolicy({
448
- sessionId: this.session.id,
449
- networkPolicy: params.networkPolicy,
450
- signal: opts?.signal,
451
- });
452
- // Update the internal session with the new network policy
453
- this.session = (0, convert_sandbox_1.convertSession)(response.json.session);
454
- }
455
- }
456
- /**
457
- * Extend the timeout of the session by the specified duration.
458
- *
459
- * This allows you to extend the lifetime of a session up until the maximum
460
- * execution timeout for your plan.
461
- *
462
- * @param duration - The duration in milliseconds to extend the timeout by
463
- * @param opts - Optional parameters.
464
- * @param opts.signal - An AbortSignal to cancel the operation.
465
- * @returns A promise that resolves when the timeout is extended
466
- *
467
- * @example
468
- * const sandbox = await Sandbox.create({ timeout: ms('10m') });
469
- * const session = sandbox.currentSession();
470
- * // Extends timeout by 5 minutes, to a total of 15 minutes.
471
- * await session.extendTimeout(ms('5m'));
472
- */
473
- async extendTimeout(duration, opts) {
474
- const response = await this.client.extendTimeout({
475
- sessionId: this.session.id,
476
- duration,
477
- signal: opts?.signal,
478
- });
479
- // Update the internal session with the new timeout value
480
- this.session = (0, convert_sandbox_1.convertSession)(response.json.session);
481
- }
482
- /**
483
- * Create a snapshot from this currently running session. New sandboxes can
484
- * then be created from this snapshot using {@link Sandbox.create}.
485
- *
486
- * Note: this session will be stopped as part of the snapshot creation process.
487
- *
488
- * @param opts - Optional parameters.
489
- * @param opts.expiration - Optional expiration time in milliseconds. Use 0 for no expiration at all.
490
- * @param opts.signal - An AbortSignal to cancel the operation.
491
- * @returns A promise that resolves to the Snapshot instance
492
- */
493
- async snapshot(opts) {
494
- const response = await this.client.createSnapshot({
495
- sessionId: this.session.id,
496
- expiration: opts?.expiration,
497
- signal: opts?.signal,
498
- });
499
- this.session = (0, convert_sandbox_1.convertSession)(response.json.session);
500
- return new snapshot_1.Snapshot({
501
- client: this.client,
502
- snapshot: response.json.snapshot,
503
- });
504
- }
505
- }
506
- exports.Session = Session;
16
+ * A Session represents a running VM instance within a {@link Sandbox}.
17
+ *
18
+ * Obtain a session via {@link Sandbox.currentSession}.
19
+ */
20
+ var Session = class Session {
21
+ /**
22
+ * Lazily resolve credentials and construct an API client.
23
+ * This is used in step contexts where the Sandbox was deserialized
24
+ * without a client (e.g. when crossing workflow/step boundaries).
25
+ * Uses getCredentials() which resolves from OIDC or env vars.
26
+ * @internal
27
+ */
28
+ async ensureClient() {
29
+ "use step";
30
+ if (this._client) return this._client;
31
+ const credentials = await getCredentials();
32
+ this._client = new APIClient({
33
+ teamId: credentials.teamId,
34
+ token: credentials.token
35
+ });
36
+ return this._client;
37
+ }
38
+ get client() {
39
+ if (!this._client) throw new Error("API client not initialized");
40
+ return this._client;
41
+ }
42
+ /** @internal */
43
+ get _sessionSnapshot() {
44
+ return this.session;
45
+ }
46
+ /**
47
+ * Unique ID of this session.
48
+ */
49
+ get sessionId() {
50
+ return this.session.id;
51
+ }
52
+ get interactivePort() {
53
+ return this.session.interactivePort ?? void 0;
54
+ }
55
+ /**
56
+ * The status of this session.
57
+ */
58
+ get status() {
59
+ return this.session.status;
60
+ }
61
+ /**
62
+ * The creation date of this session.
63
+ */
64
+ get createdAt() {
65
+ return new Date(this.session.createdAt);
66
+ }
67
+ /**
68
+ * The timeout of this session in milliseconds.
69
+ */
70
+ get timeout() {
71
+ return this.session.timeout;
72
+ }
73
+ /**
74
+ * The network policy of this session.
75
+ */
76
+ get networkPolicy() {
77
+ return this.session.networkPolicy;
78
+ }
79
+ /**
80
+ * If the session was created from a snapshot, the ID of that snapshot.
81
+ */
82
+ get sourceSnapshotId() {
83
+ return this.session.sourceSnapshotId;
84
+ }
85
+ /**
86
+ * Memory allocated to this session in MB.
87
+ */
88
+ get memory() {
89
+ return this.session.memory;
90
+ }
91
+ /**
92
+ * Number of vCPUs allocated to this session.
93
+ */
94
+ get vcpus() {
95
+ return this.session.vcpus;
96
+ }
97
+ /**
98
+ * The region where this session is hosted.
99
+ */
100
+ get region() {
101
+ return this.session.region;
102
+ }
103
+ /**
104
+ * Runtime identifier (e.g. "node24", "python3.13").
105
+ */
106
+ get runtime() {
107
+ return this.session.runtime;
108
+ }
109
+ /**
110
+ * The working directory of this session.
111
+ */
112
+ get cwd() {
113
+ return this.session.cwd;
114
+ }
115
+ /**
116
+ * When this session was requested.
117
+ */
118
+ get requestedAt() {
119
+ return new Date(this.session.requestedAt);
120
+ }
121
+ /**
122
+ * When this session started running.
123
+ */
124
+ get startedAt() {
125
+ return this.session.startedAt != null ? new Date(this.session.startedAt) : void 0;
126
+ }
127
+ /**
128
+ * When this session was requested to stop.
129
+ */
130
+ get requestedStopAt() {
131
+ return this.session.requestedStopAt != null ? new Date(this.session.requestedStopAt) : void 0;
132
+ }
133
+ /**
134
+ * When this session was stopped.
135
+ */
136
+ get stoppedAt() {
137
+ return this.session.stoppedAt != null ? new Date(this.session.stoppedAt) : void 0;
138
+ }
139
+ /**
140
+ * When this session was aborted.
141
+ */
142
+ get abortedAt() {
143
+ return this.session.abortedAt != null ? new Date(this.session.abortedAt) : void 0;
144
+ }
145
+ /**
146
+ * The wall-clock duration of this session in milliseconds.
147
+ */
148
+ get duration() {
149
+ return this.session.duration;
150
+ }
151
+ /**
152
+ * When a snapshot was requested for this session.
153
+ */
154
+ get snapshottedAt() {
155
+ return this.session.snapshottedAt != null ? new Date(this.session.snapshottedAt) : void 0;
156
+ }
157
+ /**
158
+ * When this session was last updated.
159
+ */
160
+ get updatedAt() {
161
+ return new Date(this.session.updatedAt);
162
+ }
163
+ /**
164
+ * The amount of active CPU used by the session. Only reported once the VM is
165
+ * stopped.
166
+ */
167
+ get activeCpuUsageMs() {
168
+ return this.session.activeCpuDurationMs;
169
+ }
170
+ /**
171
+ * The amount of network data used by the session. Only reported once the VM
172
+ * is stopped.
173
+ */
174
+ get networkTransfer() {
175
+ return this.session.networkTransfer;
176
+ }
177
+ /**
178
+ * Serialize a Session instance to plain data for @workflow/serde.
179
+ *
180
+ * Although Sandbox handles top-level serialization, Session needs these
181
+ * methods so the Workflow SWC compiler can resolve the class by name.
182
+ * The `new Session(...)` self-reference in WORKFLOW_DESERIALIZE forces
183
+ * rolldown to preserve the class name in the compiled output.
184
+ */
185
+ static [WORKFLOW_SERIALIZE](instance) {
186
+ return {
187
+ session: instance.session,
188
+ routes: instance.routes
189
+ };
190
+ }
191
+ static [WORKFLOW_DESERIALIZE](data) {
192
+ return new Session({
193
+ routes: data.routes,
194
+ snapshot: data.session
195
+ });
196
+ }
197
+ constructor(params) {
198
+ this._client = null;
199
+ this.routes = params.routes;
200
+ if ("snapshot" in params) this.session = params.snapshot;
201
+ else {
202
+ this._client = params.client;
203
+ this.session = toSandboxSnapshot(params.session);
204
+ }
205
+ }
206
+ /**
207
+ * Get a previously run command by its ID.
208
+ *
209
+ * @param cmdId - ID of the command to retrieve
210
+ * @param opts - Optional parameters.
211
+ * @param opts.signal - An AbortSignal to cancel the operation.
212
+ * @returns A {@link Command} instance representing the command
213
+ */
214
+ async getCommand(cmdId, opts) {
215
+ "use step";
216
+ const client = await this.ensureClient();
217
+ const command = await client.getCommand({
218
+ sessionId: this.session.id,
219
+ cmdId,
220
+ signal: opts?.signal
221
+ });
222
+ return new Command({
223
+ client,
224
+ sessionId: this.session.id,
225
+ cmd: command.json.command
226
+ });
227
+ }
228
+ async runCommand(commandOrParams, args, opts) {
229
+ "use step";
230
+ const client = await this.ensureClient();
231
+ const params = typeof commandOrParams === "string" ? {
232
+ cmd: commandOrParams,
233
+ args,
234
+ signal: opts?.signal
235
+ } : commandOrParams;
236
+ const wait = params.detached ? false : true;
237
+ const pipeLogs = async (command$1) => {
238
+ if (!params.stdout && !params.stderr) return;
239
+ try {
240
+ for await (const log of command$1.logs({ signal: params.signal })) if (log.stream === "stdout") params.stdout?.write(log.data);
241
+ else if (log.stream === "stderr") params.stderr?.write(log.data);
242
+ } catch (err) {
243
+ if (params.signal?.aborted) return;
244
+ throw err;
245
+ }
246
+ };
247
+ if (wait) {
248
+ const commandStream = await client.runCommand({
249
+ sessionId: this.session.id,
250
+ command: params.cmd,
251
+ args: params.args ?? [],
252
+ cwd: params.cwd,
253
+ env: params.env ?? {},
254
+ sudo: params.sudo ?? false,
255
+ wait: true,
256
+ signal: params.signal
257
+ });
258
+ const command$1 = new Command({
259
+ client,
260
+ sessionId: this.session.id,
261
+ cmd: commandStream.command
262
+ });
263
+ const [finished] = await Promise.all([commandStream.finished, pipeLogs(command$1)]);
264
+ return new CommandFinished({
265
+ client,
266
+ sessionId: this.session.id,
267
+ cmd: finished,
268
+ exitCode: finished.exitCode ?? 0
269
+ });
270
+ }
271
+ const commandResponse = await client.runCommand({
272
+ sessionId: this.session.id,
273
+ command: params.cmd,
274
+ args: params.args ?? [],
275
+ cwd: params.cwd,
276
+ env: params.env ?? {},
277
+ sudo: params.sudo ?? false,
278
+ signal: params.signal
279
+ });
280
+ const command = new Command({
281
+ client,
282
+ sessionId: this.session.id,
283
+ cmd: commandResponse.json.command
284
+ });
285
+ pipeLogs(command).catch((err) => {
286
+ if (params.signal?.aborted) return;
287
+ (params.stderr ?? params.stdout)?.emit("error", err);
288
+ });
289
+ return command;
290
+ }
291
+ /**
292
+ * Create a directory in the filesystem of this session.
293
+ *
294
+ * @param path - Path of the directory to create
295
+ * @param opts - Optional parameters.
296
+ * @param opts.signal - An AbortSignal to cancel the operation.
297
+ */
298
+ async mkDir(path$1, opts) {
299
+ "use step";
300
+ await (await this.ensureClient()).mkDir({
301
+ sessionId: this.session.id,
302
+ path: path$1,
303
+ signal: opts?.signal
304
+ });
305
+ }
306
+ /**
307
+ * Read a file from the filesystem of this session as a stream.
308
+ *
309
+ * @param file - File to read, with path and optional cwd
310
+ * @param opts - Optional parameters.
311
+ * @param opts.signal - An AbortSignal to cancel the operation.
312
+ * @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found
313
+ */
314
+ async readFile(file, opts) {
315
+ "use step";
316
+ return (await this.ensureClient()).readFile({
317
+ sessionId: this.session.id,
318
+ path: file.path,
319
+ cwd: file.cwd,
320
+ signal: opts?.signal
321
+ });
322
+ }
323
+ /**
324
+ * Read a file from the filesystem of this session as a Buffer.
325
+ *
326
+ * @param file - File to read, with path and optional cwd
327
+ * @param opts - Optional parameters.
328
+ * @param opts.signal - An AbortSignal to cancel the operation.
329
+ * @returns A promise that resolves to the file contents as a Buffer, or null if file not found
330
+ */
331
+ async readFileToBuffer(file, opts) {
332
+ "use step";
333
+ const stream = await (await this.ensureClient()).readFile({
334
+ sessionId: this.session.id,
335
+ path: file.path,
336
+ cwd: file.cwd,
337
+ signal: opts?.signal
338
+ });
339
+ if (stream === null) return null;
340
+ return consumeReadable(stream);
341
+ }
342
+ /**
343
+ * Download a file from the session to the local filesystem.
344
+ *
345
+ * @param src - Source file on the session, with path and optional cwd
346
+ * @param dst - Destination file on the local machine, with path and optional cwd
347
+ * @param opts - Optional parameters.
348
+ * @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.
349
+ * @param opts.signal - An AbortSignal to cancel the operation.
350
+ * @returns The absolute path to the written file, or null if the source file was not found
351
+ */
352
+ async downloadFile(src, dst, opts) {
353
+ "use step";
354
+ const client = await this.ensureClient();
355
+ if (!src?.path) throw new Error("downloadFile: source path is required");
356
+ if (!dst?.path) throw new Error("downloadFile: destination path is required");
357
+ const stream = await client.readFile({
358
+ sessionId: this.session.id,
359
+ path: src.path,
360
+ cwd: src.cwd,
361
+ signal: opts?.signal
362
+ });
363
+ if (stream === null) return null;
364
+ try {
365
+ const dstPath = resolve(dst.cwd ?? "", dst.path);
366
+ if (opts?.mkdirRecursive) await mkdir(dirname(dstPath), { recursive: true });
367
+ await pipeline(stream, createWriteStream(dstPath), { signal: opts?.signal });
368
+ return dstPath;
369
+ } finally {
370
+ stream.destroy();
371
+ }
372
+ }
373
+ /**
374
+ * Write files to the filesystem of this session.
375
+ * Defaults to writing to /vercel/sandbox unless an absolute path is specified.
376
+ * Writes files using the `vercel-sandbox` user.
377
+ *
378
+ * @param files - Array of files with path and stream/buffer contents
379
+ * @param opts - Optional parameters.
380
+ * @param opts.signal - An AbortSignal to cancel the operation.
381
+ * @returns A promise that resolves when the files are written
382
+ */
383
+ async writeFiles(files, opts) {
384
+ "use step";
385
+ return (await this.ensureClient()).writeFiles({
386
+ sessionId: this.session.id,
387
+ cwd: this.session.cwd,
388
+ extractDir: "/",
389
+ files,
390
+ signal: opts?.signal
391
+ });
392
+ }
393
+ /**
394
+ * Get the public domain of a port of this session.
395
+ *
396
+ * @param p - Port number to resolve
397
+ * @returns A full domain (e.g. `https://subdomain.vercel.run`)
398
+ * @throws If the port has no associated route
399
+ */
400
+ domain(p) {
401
+ const route = this.routes.find(({ port }) => port == p);
402
+ if (route) return `https://${route.subdomain}.vercel.run`;
403
+ else throw new Error(`No route for port ${p}`);
404
+ }
405
+ /**
406
+ * Stop this session.
407
+ *
408
+ * @param opts - Optional parameters.
409
+ * @param opts.signal - An AbortSignal to cancel the operation.
410
+ * @param opts.blocking - If true, poll until the session has fully stopped and return the final state.
411
+ * @returns The session at the time the stop was acknowledged, or after fully stopped if `blocking` is true.
412
+ */
413
+ async stop(opts) {
414
+ "use step";
415
+ this.session = toSandboxSnapshot((await (await this.ensureClient()).stopSession({
416
+ sessionId: this.session.id,
417
+ signal: opts?.signal,
418
+ blocking: opts?.blocking
419
+ })).json.session);
420
+ return this.session;
421
+ }
422
+ /**
423
+ * Update the current session's settings.
424
+ *
425
+ * @param params - Fields to update.
426
+ * @param params.networkPolicy - The new network policy to apply.
427
+ * @param opts - Optional parameters.
428
+ * @param opts.signal - An AbortSignal to cancel the operation.
429
+ *
430
+ * @example
431
+ * // Restrict to specific domains
432
+ * await session.update({
433
+ * networkPolicy: {
434
+ * allow: ["*.npmjs.org", "github.com"],
435
+ * }
436
+ * });
437
+ *
438
+ * @example
439
+ * // Inject credentials with per-domain transformers
440
+ * await session.update({
441
+ * networkPolicy: {
442
+ * allow: {
443
+ * "ai-gateway.vercel.sh": [{
444
+ * transform: [{
445
+ * headers: { authorization: "Bearer ..." }
446
+ * }]
447
+ * }],
448
+ * "*": []
449
+ * }
450
+ * }
451
+ * });
452
+ *
453
+ * @example
454
+ * // Deny all network access
455
+ * await session.update({ networkPolicy: "deny-all" });
456
+ */
457
+ async update(params, opts) {
458
+ "use step";
459
+ if (params.networkPolicy !== void 0) this.session = toSandboxSnapshot((await (await this.ensureClient()).updateNetworkPolicy({
460
+ sessionId: this.session.id,
461
+ networkPolicy: params.networkPolicy,
462
+ signal: opts?.signal
463
+ })).json.session);
464
+ }
465
+ /**
466
+ * Extend the timeout of the session by the specified duration.
467
+ *
468
+ * This allows you to extend the lifetime of a session up until the maximum
469
+ * execution timeout for your plan.
470
+ *
471
+ * @param duration - The duration in milliseconds to extend the timeout by
472
+ * @param opts - Optional parameters.
473
+ * @param opts.signal - An AbortSignal to cancel the operation.
474
+ * @returns A promise that resolves when the timeout is extended
475
+ *
476
+ * @example
477
+ * const sandbox = await Sandbox.create({ timeout: ms('10m') });
478
+ * const session = sandbox.currentSession();
479
+ * // Extends timeout by 5 minutes, to a total of 15 minutes.
480
+ * await session.extendTimeout(ms('5m'));
481
+ */
482
+ async extendTimeout(duration, opts) {
483
+ "use step";
484
+ this.session = toSandboxSnapshot((await (await this.ensureClient()).extendTimeout({
485
+ sessionId: this.session.id,
486
+ duration,
487
+ signal: opts?.signal
488
+ })).json.session);
489
+ }
490
+ /**
491
+ * Create a snapshot from this currently running session. New sandboxes can
492
+ * then be created from this snapshot using {@link Sandbox.create}.
493
+ *
494
+ * Note: this session will be stopped as part of the snapshot creation process.
495
+ *
496
+ * @param opts - Optional parameters.
497
+ * @param opts.expiration - Optional expiration time in milliseconds. Use 0 for no expiration at all.
498
+ * @param opts.signal - An AbortSignal to cancel the operation.
499
+ * @returns A promise that resolves to the Snapshot instance
500
+ */
501
+ async snapshot(opts) {
502
+ "use step";
503
+ const client = await this.ensureClient();
504
+ const response = await client.createSnapshot({
505
+ sessionId: this.session.id,
506
+ expiration: opts?.expiration,
507
+ signal: opts?.signal
508
+ });
509
+ this.session = toSandboxSnapshot(response.json.session);
510
+ return new Snapshot({
511
+ client,
512
+ snapshot: response.json.snapshot
513
+ });
514
+ }
515
+ };
516
+
517
+ //#endregion
518
+ export { Session };
507
519
  //# sourceMappingURL=session.js.map