@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.
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 +790 -0
  6. package/dist/api-client/api-client.d.ts +784 -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 +182 -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 +155 -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 +694 -0
  101. package/dist/sandbox.cjs.map +1 -0
  102. package/dist/sandbox.d.cts +796 -0
  103. package/dist/sandbox.d.ts +783 -711
  104. package/dist/sandbox.js +684 -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.d.ts CHANGED
@@ -1,375 +1,406 @@
1
- import type { SessionMetaData, SandboxRouteData } from "./api-client";
2
- import type { Writable } from "stream";
3
- import type { APIClient } from "./api-client";
4
- import { Command, CommandFinished } from "./command";
5
- import { Snapshot } from "./snapshot";
6
- import type { NetworkPolicy, NetworkPolicyRule, NetworkTransformer } from "./network-policy";
7
- import { type ConvertedSession } from "./utils/convert-sandbox";
8
- export type { NetworkPolicy, NetworkPolicyRule, NetworkTransformer };
1
+ import { SandboxRouteData, SessionMetaData } from "./api-client/validators.js";
2
+ import { NetworkPolicy, NetworkPolicyRule, NetworkTransformer } from "./network-policy.js";
3
+ import { APIClient } from "./api-client/api-client.js";
4
+ import "./api-client/index.js";
5
+ import { Command, CommandFinished } from "./command.js";
6
+ import { Snapshot } from "./snapshot.js";
7
+ import { SandboxSnapshot } from "./utils/sandbox-snapshot.js";
8
+ import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from "@workflow/serde";
9
+ import { Writable } from "stream";
10
+
11
+ //#region src/session.d.ts
12
+ /**
13
+ * Serialized representation of a Session for @workflow/serde.
14
+ */
15
+ interface SerializedSession {
16
+ session: SandboxSnapshot;
17
+ routes: SandboxRouteData[];
18
+ }
9
19
  /** @inline */
10
- export interface RunCommandParams {
11
- /**
12
- * The command to execute
13
- */
14
- cmd: string;
15
- /**
16
- * Arguments to pass to the command
17
- */
18
- args?: string[];
19
- /**
20
- * Working directory to execute the command in
21
- */
22
- cwd?: string;
23
- /**
24
- * Environment variables to set for this command
25
- */
26
- env?: Record<string, string>;
27
- /**
28
- * If true, execute this command with root privileges. Defaults to false.
29
- */
30
- sudo?: boolean;
31
- /**
32
- * If true, the command will return without waiting for `exitCode`
33
- */
34
- detached?: boolean;
35
- /**
36
- * A `Writable` stream where `stdout` from the command will be piped
37
- */
38
- stdout?: Writable;
39
- /**
40
- * A `Writable` stream where `stderr` from the command will be piped
41
- */
42
- stderr?: Writable;
43
- /**
44
- * An AbortSignal to cancel the command execution
45
- */
46
- signal?: AbortSignal;
20
+ interface RunCommandParams {
21
+ /**
22
+ * The command to execute
23
+ */
24
+ cmd: string;
25
+ /**
26
+ * Arguments to pass to the command
27
+ */
28
+ args?: string[];
29
+ /**
30
+ * Working directory to execute the command in
31
+ */
32
+ cwd?: string;
33
+ /**
34
+ * Environment variables to set for this command
35
+ */
36
+ env?: Record<string, string>;
37
+ /**
38
+ * If true, execute this command with root privileges. Defaults to false.
39
+ */
40
+ sudo?: boolean;
41
+ /**
42
+ * If true, the command will return without waiting for `exitCode`
43
+ */
44
+ detached?: boolean;
45
+ /**
46
+ * A `Writable` stream where `stdout` from the command will be piped
47
+ */
48
+ stdout?: Writable;
49
+ /**
50
+ * A `Writable` stream where `stderr` from the command will be piped
51
+ */
52
+ stderr?: Writable;
53
+ /**
54
+ * An AbortSignal to cancel the command execution
55
+ */
56
+ signal?: AbortSignal;
47
57
  }
48
58
  /**
49
59
  * A Session represents a running VM instance within a {@link Sandbox}.
50
60
  *
51
61
  * Obtain a session via {@link Sandbox.currentSession}.
52
62
  */
53
- export declare class Session {
54
- private readonly client;
55
- /**
56
- * Routes from ports to subdomains.
57
- * @hidden
58
- */
59
- readonly routes: SandboxRouteData[];
60
- /**
61
- * Internal metadata about the current session.
62
- */
63
- private session;
64
- /**
65
- * Unique ID of this session.
66
- */
67
- get sessionId(): string;
68
- get interactivePort(): number | undefined;
69
- /**
70
- * The status of this session.
71
- */
72
- get status(): SessionMetaData["status"];
73
- /**
74
- * The creation date of this session.
75
- */
76
- get createdAt(): Date;
77
- /**
78
- * The timeout of this session in milliseconds.
79
- */
80
- get timeout(): number;
81
- /**
82
- * The network policy of this session.
83
- */
84
- get networkPolicy(): NetworkPolicy | undefined;
85
- /**
86
- * If the session was created from a snapshot, the ID of that snapshot.
87
- */
88
- get sourceSnapshotId(): string | undefined;
89
- /**
90
- * Memory allocated to this session in MB.
91
- */
92
- get memory(): number;
93
- /**
94
- * Number of vCPUs allocated to this session.
95
- */
96
- get vcpus(): number;
97
- /**
98
- * The region where this session is hosted.
99
- */
100
- get region(): string;
101
- /**
102
- * Runtime identifier (e.g. "node24", "python3.13").
103
- */
104
- get runtime(): string;
105
- /**
106
- * The working directory of this session.
107
- */
108
- get cwd(): string;
109
- /**
110
- * When this session was requested.
111
- */
112
- get requestedAt(): Date;
113
- /**
114
- * When this session started running.
115
- */
116
- get startedAt(): Date | undefined;
117
- /**
118
- * When this session was requested to stop.
119
- */
120
- get requestedStopAt(): Date | undefined;
121
- /**
122
- * When this session was stopped.
123
- */
124
- get stoppedAt(): Date | undefined;
125
- /**
126
- * When this session was aborted.
127
- */
128
- get abortedAt(): Date | undefined;
129
- /**
130
- * The wall-clock duration of this session in milliseconds.
131
- */
132
- get duration(): number | undefined;
133
- /**
134
- * When a snapshot was requested for this session.
135
- */
136
- get snapshottedAt(): Date | undefined;
137
- /**
138
- * When this session was last updated.
139
- */
140
- get updatedAt(): Date;
141
- /**
142
- * The amount of active CPU used by the session. Only reported once the VM is
143
- * stopped.
144
- */
145
- get activeCpuUsageMs(): number | undefined;
146
- /**
147
- * The amount of network data used by the session. Only reported once the VM
148
- * is stopped.
149
- */
150
- get networkTransfer(): {
151
- ingress: number;
152
- egress: number;
153
- } | undefined;
154
- constructor({ client, routes, session, }: {
155
- client: APIClient;
156
- routes: SandboxRouteData[];
157
- session: SessionMetaData;
158
- });
159
- /**
160
- * Get a previously run command by its ID.
161
- *
162
- * @param cmdId - ID of the command to retrieve
163
- * @param opts - Optional parameters.
164
- * @param opts.signal - An AbortSignal to cancel the operation.
165
- * @returns A {@link Command} instance representing the command
166
- */
167
- getCommand(cmdId: string, opts?: {
168
- signal?: AbortSignal;
169
- }): Promise<Command>;
170
- /**
171
- * Start executing a command in this session.
172
- *
173
- * @param command - The command to execute.
174
- * @param args - Arguments to pass to the command.
175
- * @param opts - Optional parameters.
176
- * @param opts.signal - An AbortSignal to cancel the command execution.
177
- * @returns A {@link CommandFinished} result once execution is done.
178
- */
179
- runCommand(command: string, args?: string[], opts?: {
180
- signal?: AbortSignal;
181
- }): Promise<CommandFinished>;
182
- /**
183
- * Start executing a command in detached mode.
184
- *
185
- * @param params - The command parameters.
186
- * @returns A {@link Command} instance for the running command.
187
- */
188
- runCommand(params: RunCommandParams & {
189
- detached: true;
190
- }): Promise<Command>;
191
- /**
192
- * Start executing a command in this session.
193
- *
194
- * @param params - The command parameters.
195
- * @returns A {@link CommandFinished} result once execution is done.
196
- */
197
- runCommand(params: RunCommandParams): Promise<CommandFinished>;
198
- /**
199
- * Internal helper to start a command in the session.
200
- *
201
- * @param params - Command execution parameters.
202
- * @returns A {@link Command} or {@link CommandFinished}, depending on `detached`.
203
- * @internal
204
- */
205
- _runCommand(params: RunCommandParams): Promise<Command>;
206
- /**
207
- * Create a directory in the filesystem of this session.
208
- *
209
- * @param path - Path of the directory to create
210
- * @param opts - Optional parameters.
211
- * @param opts.signal - An AbortSignal to cancel the operation.
212
- */
213
- mkDir(path: string, opts?: {
214
- signal?: AbortSignal;
215
- }): Promise<void>;
216
- /**
217
- * Read a file from the filesystem of this session as a stream.
218
- *
219
- * @param file - File to read, with path and optional cwd
220
- * @param opts - Optional parameters.
221
- * @param opts.signal - An AbortSignal to cancel the operation.
222
- * @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found
223
- */
224
- readFile(file: {
225
- path: string;
226
- cwd?: string;
227
- }, opts?: {
228
- signal?: AbortSignal;
229
- }): Promise<NodeJS.ReadableStream | null>;
230
- /**
231
- * Read a file from the filesystem of this session as a Buffer.
232
- *
233
- * @param file - File to read, with path and optional cwd
234
- * @param opts - Optional parameters.
235
- * @param opts.signal - An AbortSignal to cancel the operation.
236
- * @returns A promise that resolves to the file contents as a Buffer, or null if file not found
237
- */
238
- readFileToBuffer(file: {
239
- path: string;
240
- cwd?: string;
241
- }, opts?: {
242
- signal?: AbortSignal;
243
- }): Promise<Buffer | null>;
244
- /**
245
- * Download a file from the session to the local filesystem.
246
- *
247
- * @param src - Source file on the session, with path and optional cwd
248
- * @param dst - Destination file on the local machine, with path and optional cwd
249
- * @param opts - Optional parameters.
250
- * @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.
251
- * @param opts.signal - An AbortSignal to cancel the operation.
252
- * @returns The absolute path to the written file, or null if the source file was not found
253
- */
254
- downloadFile(src: {
255
- path: string;
256
- cwd?: string;
257
- }, dst: {
258
- path: string;
259
- cwd?: string;
260
- }, opts?: {
261
- mkdirRecursive?: boolean;
262
- signal?: AbortSignal;
263
- }): Promise<string | null>;
264
- /**
265
- * Write files to the filesystem of this session.
266
- * Defaults to writing to /vercel/sandbox unless an absolute path is specified.
267
- * Writes files using the `vercel-sandbox` user.
268
- *
269
- * @param files - Array of files with path and stream/buffer contents
270
- * @param opts - Optional parameters.
271
- * @param opts.signal - An AbortSignal to cancel the operation.
272
- * @returns A promise that resolves when the files are written
273
- */
274
- writeFiles(files: {
275
- path: string;
276
- content: Buffer;
277
- }[], opts?: {
278
- signal?: AbortSignal;
279
- }): Promise<void>;
280
- /**
281
- * Get the public domain of a port of this session.
282
- *
283
- * @param p - Port number to resolve
284
- * @returns A full domain (e.g. `https://subdomain.vercel.run`)
285
- * @throws If the port has no associated route
286
- */
287
- domain(p: number): string;
288
- /**
289
- * Stop this session.
290
- *
291
- * @param opts - Optional parameters.
292
- * @param opts.signal - An AbortSignal to cancel the operation.
293
- * @param opts.blocking - If true, poll until the session has fully stopped and return the final state.
294
- * @returns The session at the time the stop was acknowledged, or after fully stopped if `blocking` is true.
295
- */
296
- stop(opts?: {
297
- signal?: AbortSignal;
298
- blocking?: boolean;
299
- }): Promise<ConvertedSession>;
300
- /**
301
- * Update the current session's settings.
302
- *
303
- * @param params - Fields to update.
304
- * @param params.networkPolicy - The new network policy to apply.
305
- * @param opts - Optional parameters.
306
- * @param opts.signal - An AbortSignal to cancel the operation.
307
- *
308
- * @example
309
- * // Restrict to specific domains
310
- * await session.update({
311
- * networkPolicy: {
312
- * allow: ["*.npmjs.org", "github.com"],
313
- * }
314
- * });
315
- *
316
- * @example
317
- * // Inject credentials with per-domain transformers
318
- * await session.update({
319
- * networkPolicy: {
320
- * allow: {
321
- * "ai-gateway.vercel.sh": [{
322
- * transform: [{
323
- * headers: { authorization: "Bearer ..." }
324
- * }]
325
- * }],
326
- * "*": []
327
- * }
328
- * }
329
- * });
330
- *
331
- * @example
332
- * // Deny all network access
333
- * await session.update({ networkPolicy: "deny-all" });
334
- */
335
- update(params: {
336
- networkPolicy?: NetworkPolicy;
337
- }, opts?: {
338
- signal?: AbortSignal;
339
- }): Promise<void>;
340
- /**
341
- * Extend the timeout of the session by the specified duration.
342
- *
343
- * This allows you to extend the lifetime of a session up until the maximum
344
- * execution timeout for your plan.
345
- *
346
- * @param duration - The duration in milliseconds to extend the timeout by
347
- * @param opts - Optional parameters.
348
- * @param opts.signal - An AbortSignal to cancel the operation.
349
- * @returns A promise that resolves when the timeout is extended
350
- *
351
- * @example
352
- * const sandbox = await Sandbox.create({ timeout: ms('10m') });
353
- * const session = sandbox.currentSession();
354
- * // Extends timeout by 5 minutes, to a total of 15 minutes.
355
- * await session.extendTimeout(ms('5m'));
356
- */
357
- extendTimeout(duration: number, opts?: {
358
- signal?: AbortSignal;
359
- }): Promise<void>;
360
- /**
361
- * Create a snapshot from this currently running session. New sandboxes can
362
- * then be created from this snapshot using {@link Sandbox.create}.
363
- *
364
- * Note: this session will be stopped as part of the snapshot creation process.
365
- *
366
- * @param opts - Optional parameters.
367
- * @param opts.expiration - Optional expiration time in milliseconds. Use 0 for no expiration at all.
368
- * @param opts.signal - An AbortSignal to cancel the operation.
369
- * @returns A promise that resolves to the Snapshot instance
370
- */
371
- snapshot(opts?: {
372
- expiration?: number;
373
- signal?: AbortSignal;
374
- }): Promise<Snapshot>;
63
+ declare class Session {
64
+ private _client;
65
+ /**
66
+ * Lazily resolve credentials and construct an API client.
67
+ * This is used in step contexts where the Sandbox was deserialized
68
+ * without a client (e.g. when crossing workflow/step boundaries).
69
+ * Uses getCredentials() which resolves from OIDC or env vars.
70
+ * @internal
71
+ */
72
+ private ensureClient;
73
+ /**
74
+ * Routes from ports to subdomains.
75
+ * @hidden
76
+ */
77
+ readonly routes: SandboxRouteData[];
78
+ /**
79
+ * Internal metadata about the current session.
80
+ */
81
+ private session;
82
+ private get client();
83
+ /** @internal */
84
+ get _sessionSnapshot(): SandboxSnapshot;
85
+ /**
86
+ * Unique ID of this session.
87
+ */
88
+ get sessionId(): string;
89
+ get interactivePort(): number | undefined;
90
+ /**
91
+ * The status of this session.
92
+ */
93
+ get status(): SessionMetaData["status"];
94
+ /**
95
+ * The creation date of this session.
96
+ */
97
+ get createdAt(): Date;
98
+ /**
99
+ * The timeout of this session in milliseconds.
100
+ */
101
+ get timeout(): number;
102
+ /**
103
+ * The network policy of this session.
104
+ */
105
+ get networkPolicy(): NetworkPolicy | undefined;
106
+ /**
107
+ * If the session was created from a snapshot, the ID of that snapshot.
108
+ */
109
+ get sourceSnapshotId(): string | undefined;
110
+ /**
111
+ * Memory allocated to this session in MB.
112
+ */
113
+ get memory(): number;
114
+ /**
115
+ * Number of vCPUs allocated to this session.
116
+ */
117
+ get vcpus(): number;
118
+ /**
119
+ * The region where this session is hosted.
120
+ */
121
+ get region(): string;
122
+ /**
123
+ * Runtime identifier (e.g. "node24", "python3.13").
124
+ */
125
+ get runtime(): string;
126
+ /**
127
+ * The working directory of this session.
128
+ */
129
+ get cwd(): string;
130
+ /**
131
+ * When this session was requested.
132
+ */
133
+ get requestedAt(): Date;
134
+ /**
135
+ * When this session started running.
136
+ */
137
+ get startedAt(): Date | undefined;
138
+ /**
139
+ * When this session was requested to stop.
140
+ */
141
+ get requestedStopAt(): Date | undefined;
142
+ /**
143
+ * When this session was stopped.
144
+ */
145
+ get stoppedAt(): Date | undefined;
146
+ /**
147
+ * When this session was aborted.
148
+ */
149
+ get abortedAt(): Date | undefined;
150
+ /**
151
+ * The wall-clock duration of this session in milliseconds.
152
+ */
153
+ get duration(): number | undefined;
154
+ /**
155
+ * When a snapshot was requested for this session.
156
+ */
157
+ get snapshottedAt(): Date | undefined;
158
+ /**
159
+ * When this session was last updated.
160
+ */
161
+ get updatedAt(): Date;
162
+ /**
163
+ * The amount of active CPU used by the session. Only reported once the VM is
164
+ * stopped.
165
+ */
166
+ get activeCpuUsageMs(): number | undefined;
167
+ /**
168
+ * The amount of network data used by the session. Only reported once the VM
169
+ * is stopped.
170
+ */
171
+ get networkTransfer(): {
172
+ ingress: number;
173
+ egress: number;
174
+ } | undefined;
175
+ /**
176
+ * Serialize a Session instance to plain data for @workflow/serde.
177
+ *
178
+ * Although Sandbox handles top-level serialization, Session needs these
179
+ * methods so the Workflow SWC compiler can resolve the class by name.
180
+ * The `new Session(...)` self-reference in WORKFLOW_DESERIALIZE forces
181
+ * rolldown to preserve the class name in the compiled output.
182
+ */
183
+ static [WORKFLOW_SERIALIZE](instance: Session): SerializedSession;
184
+ static [WORKFLOW_DESERIALIZE](data: SerializedSession): Session;
185
+ constructor(params: {
186
+ client: APIClient;
187
+ routes: SandboxRouteData[];
188
+ session: SessionMetaData;
189
+ } | {
190
+ /** @internal – used during deserialization with an already-converted snapshot */
191
+ routes: SandboxRouteData[];
192
+ snapshot: SandboxSnapshot;
193
+ });
194
+ /**
195
+ * Get a previously run command by its ID.
196
+ *
197
+ * @param cmdId - ID of the command to retrieve
198
+ * @param opts - Optional parameters.
199
+ * @param opts.signal - An AbortSignal to cancel the operation.
200
+ * @returns A {@link Command} instance representing the command
201
+ */
202
+ getCommand(cmdId: string, opts?: {
203
+ signal?: AbortSignal;
204
+ }): Promise<Command>;
205
+ /**
206
+ * Start executing a command in this session.
207
+ *
208
+ * @param command - The command to execute.
209
+ * @param args - Arguments to pass to the command.
210
+ * @param opts - Optional parameters.
211
+ * @param opts.signal - An AbortSignal to cancel the command execution.
212
+ * @returns A {@link CommandFinished} result once execution is done.
213
+ */
214
+ runCommand(command: string, args?: string[], opts?: {
215
+ signal?: AbortSignal;
216
+ }): Promise<CommandFinished>;
217
+ /**
218
+ * Start executing a command in detached mode.
219
+ *
220
+ * @param params - The command parameters.
221
+ * @returns A {@link Command} instance for the running command.
222
+ */
223
+ runCommand(params: RunCommandParams & {
224
+ detached: true;
225
+ }): Promise<Command>;
226
+ /**
227
+ * Start executing a command in this session.
228
+ *
229
+ * @param params - The command parameters.
230
+ * @returns A {@link CommandFinished} result once execution is done.
231
+ */
232
+ runCommand(params: RunCommandParams): Promise<CommandFinished>;
233
+ /**
234
+ * Create a directory in the filesystem of this session.
235
+ *
236
+ * @param path - Path of the directory to create
237
+ * @param opts - Optional parameters.
238
+ * @param opts.signal - An AbortSignal to cancel the operation.
239
+ */
240
+ mkDir(path: string, opts?: {
241
+ signal?: AbortSignal;
242
+ }): Promise<void>;
243
+ /**
244
+ * Read a file from the filesystem of this session as a stream.
245
+ *
246
+ * @param file - File to read, with path and optional cwd
247
+ * @param opts - Optional parameters.
248
+ * @param opts.signal - An AbortSignal to cancel the operation.
249
+ * @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found
250
+ */
251
+ readFile(file: {
252
+ path: string;
253
+ cwd?: string;
254
+ }, opts?: {
255
+ signal?: AbortSignal;
256
+ }): Promise<NodeJS.ReadableStream | null>;
257
+ /**
258
+ * Read a file from the filesystem of this session as a Buffer.
259
+ *
260
+ * @param file - File to read, with path and optional cwd
261
+ * @param opts - Optional parameters.
262
+ * @param opts.signal - An AbortSignal to cancel the operation.
263
+ * @returns A promise that resolves to the file contents as a Buffer, or null if file not found
264
+ */
265
+ readFileToBuffer(file: {
266
+ path: string;
267
+ cwd?: string;
268
+ }, opts?: {
269
+ signal?: AbortSignal;
270
+ }): Promise<Buffer | null>;
271
+ /**
272
+ * Download a file from the session to the local filesystem.
273
+ *
274
+ * @param src - Source file on the session, with path and optional cwd
275
+ * @param dst - Destination file on the local machine, with path and optional cwd
276
+ * @param opts - Optional parameters.
277
+ * @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.
278
+ * @param opts.signal - An AbortSignal to cancel the operation.
279
+ * @returns The absolute path to the written file, or null if the source file was not found
280
+ */
281
+ downloadFile(src: {
282
+ path: string;
283
+ cwd?: string;
284
+ }, dst: {
285
+ path: string;
286
+ cwd?: string;
287
+ }, opts?: {
288
+ mkdirRecursive?: boolean;
289
+ signal?: AbortSignal;
290
+ }): Promise<string | null>;
291
+ /**
292
+ * Write files to the filesystem of this session.
293
+ * Defaults to writing to /vercel/sandbox unless an absolute path is specified.
294
+ * Writes files using the `vercel-sandbox` user.
295
+ *
296
+ * @param files - Array of files with path and stream/buffer contents
297
+ * @param opts - Optional parameters.
298
+ * @param opts.signal - An AbortSignal to cancel the operation.
299
+ * @returns A promise that resolves when the files are written
300
+ */
301
+ writeFiles(files: {
302
+ path: string;
303
+ content: string | Uint8Array;
304
+ mode?: number;
305
+ }[], opts?: {
306
+ signal?: AbortSignal;
307
+ }): Promise<void>;
308
+ /**
309
+ * Get the public domain of a port of this session.
310
+ *
311
+ * @param p - Port number to resolve
312
+ * @returns A full domain (e.g. `https://subdomain.vercel.run`)
313
+ * @throws If the port has no associated route
314
+ */
315
+ domain(p: number): string;
316
+ /**
317
+ * Stop this session.
318
+ *
319
+ * @param opts - Optional parameters.
320
+ * @param opts.signal - An AbortSignal to cancel the operation.
321
+ * @param opts.blocking - If true, poll until the session has fully stopped and return the final state.
322
+ * @returns The session at the time the stop was acknowledged, or after fully stopped if `blocking` is true.
323
+ */
324
+ stop(opts?: {
325
+ signal?: AbortSignal;
326
+ blocking?: boolean;
327
+ }): Promise<SandboxSnapshot>;
328
+ /**
329
+ * Update the current session's settings.
330
+ *
331
+ * @param params - Fields to update.
332
+ * @param params.networkPolicy - The new network policy to apply.
333
+ * @param opts - Optional parameters.
334
+ * @param opts.signal - An AbortSignal to cancel the operation.
335
+ *
336
+ * @example
337
+ * // Restrict to specific domains
338
+ * await session.update({
339
+ * networkPolicy: {
340
+ * allow: ["*.npmjs.org", "github.com"],
341
+ * }
342
+ * });
343
+ *
344
+ * @example
345
+ * // Inject credentials with per-domain transformers
346
+ * await session.update({
347
+ * networkPolicy: {
348
+ * allow: {
349
+ * "ai-gateway.vercel.sh": [{
350
+ * transform: [{
351
+ * headers: { authorization: "Bearer ..." }
352
+ * }]
353
+ * }],
354
+ * "*": []
355
+ * }
356
+ * }
357
+ * });
358
+ *
359
+ * @example
360
+ * // Deny all network access
361
+ * await session.update({ networkPolicy: "deny-all" });
362
+ */
363
+ update(params: {
364
+ networkPolicy?: NetworkPolicy;
365
+ }, opts?: {
366
+ signal?: AbortSignal;
367
+ }): Promise<void>;
368
+ /**
369
+ * Extend the timeout of the session by the specified duration.
370
+ *
371
+ * This allows you to extend the lifetime of a session up until the maximum
372
+ * execution timeout for your plan.
373
+ *
374
+ * @param duration - The duration in milliseconds to extend the timeout by
375
+ * @param opts - Optional parameters.
376
+ * @param opts.signal - An AbortSignal to cancel the operation.
377
+ * @returns A promise that resolves when the timeout is extended
378
+ *
379
+ * @example
380
+ * const sandbox = await Sandbox.create({ timeout: ms('10m') });
381
+ * const session = sandbox.currentSession();
382
+ * // Extends timeout by 5 minutes, to a total of 15 minutes.
383
+ * await session.extendTimeout(ms('5m'));
384
+ */
385
+ extendTimeout(duration: number, opts?: {
386
+ signal?: AbortSignal;
387
+ }): Promise<void>;
388
+ /**
389
+ * Create a snapshot from this currently running session. New sandboxes can
390
+ * then be created from this snapshot using {@link Sandbox.create}.
391
+ *
392
+ * Note: this session will be stopped as part of the snapshot creation process.
393
+ *
394
+ * @param opts - Optional parameters.
395
+ * @param opts.expiration - Optional expiration time in milliseconds. Use 0 for no expiration at all.
396
+ * @param opts.signal - An AbortSignal to cancel the operation.
397
+ * @returns A promise that resolves to the Snapshot instance
398
+ */
399
+ snapshot(opts?: {
400
+ expiration?: number;
401
+ signal?: AbortSignal;
402
+ }): Promise<Snapshot>;
375
403
  }
404
+ //#endregion
405
+ export { RunCommandParams, Session };
406
+ //# sourceMappingURL=session.d.ts.map