@vercel/sandbox 2.0.0-beta.2 → 2.0.0-beta.20

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