@daytonaio/sdk 0.175.0 → 0.178.0

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 (91) hide show
  1. package/cjs/CodeInterpreter.d.ts +3 -2
  2. package/cjs/CodeInterpreter.js.map +1 -1
  3. package/cjs/ComputerUse.d.ts +104 -2
  4. package/cjs/ComputerUse.js +851 -763
  5. package/cjs/ComputerUse.js.map +1 -1
  6. package/cjs/Daytona.d.ts +4 -3
  7. package/cjs/Daytona.js +429 -443
  8. package/cjs/Daytona.js.map +1 -1
  9. package/cjs/FileSystem.d.ts +2 -2
  10. package/cjs/FileSystem.js +491 -521
  11. package/cjs/FileSystem.js.map +1 -1
  12. package/cjs/Git.d.ts +2 -1
  13. package/cjs/Git.js +287 -310
  14. package/cjs/Git.js.map +1 -1
  15. package/cjs/LspServer.d.ts +2 -1
  16. package/cjs/LspServer.js +209 -226
  17. package/cjs/LspServer.js.map +1 -1
  18. package/cjs/ObjectStorage.js +170 -166
  19. package/cjs/ObjectStorage.js.map +1 -1
  20. package/cjs/Process.d.ts +4 -3
  21. package/cjs/Process.js +562 -600
  22. package/cjs/Process.js.map +1 -1
  23. package/cjs/PtyHandle.d.ts +2 -2
  24. package/cjs/PtyHandle.js +327 -338
  25. package/cjs/PtyHandle.js.map +1 -1
  26. package/cjs/Sandbox.d.ts +4 -3
  27. package/cjs/Sandbox.js +756 -821
  28. package/cjs/Sandbox.js.map +1 -1
  29. package/cjs/Snapshot.d.ts +3 -2
  30. package/cjs/Snapshot.js +203 -213
  31. package/cjs/Snapshot.js.map +1 -1
  32. package/cjs/Volume.d.ts +2 -1
  33. package/cjs/Volume.js +90 -92
  34. package/cjs/Volume.js.map +1 -1
  35. package/cjs/errors/DaytonaError.d.ts +2 -1
  36. package/cjs/errors/DaytonaError.js.map +1 -1
  37. package/cjs/index.d.ts +2 -2
  38. package/cjs/index.js +2 -1
  39. package/cjs/index.js.map +1 -1
  40. package/cjs/types/CodeInterpreter.d.ts +1 -1
  41. package/cjs/utils/Binary.js +14 -2
  42. package/cjs/utils/Binary.js.map +1 -1
  43. package/cjs/utils/otel.decorator.d.ts +7 -8
  44. package/cjs/utils/otel.decorator.js +24 -30
  45. package/cjs/utils/otel.decorator.js.map +1 -1
  46. package/esm/CodeInterpreter.d.ts +3 -2
  47. package/esm/CodeInterpreter.js.map +1 -1
  48. package/esm/ComputerUse.d.ts +104 -2
  49. package/esm/ComputerUse.js +857 -763
  50. package/esm/ComputerUse.js.map +1 -1
  51. package/esm/Daytona.d.ts +4 -3
  52. package/esm/Daytona.js +431 -444
  53. package/esm/Daytona.js.map +1 -1
  54. package/esm/FileSystem.d.ts +2 -2
  55. package/esm/FileSystem.js +493 -522
  56. package/esm/FileSystem.js.map +1 -1
  57. package/esm/Git.d.ts +2 -1
  58. package/esm/Git.js +289 -311
  59. package/esm/Git.js.map +1 -1
  60. package/esm/LspServer.d.ts +2 -1
  61. package/esm/LspServer.js +211 -227
  62. package/esm/LspServer.js.map +1 -1
  63. package/esm/ObjectStorage.js +172 -167
  64. package/esm/ObjectStorage.js.map +1 -1
  65. package/esm/Process.d.ts +4 -3
  66. package/esm/Process.js +564 -601
  67. package/esm/Process.js.map +1 -1
  68. package/esm/PtyHandle.d.ts +2 -2
  69. package/esm/PtyHandle.js +329 -339
  70. package/esm/PtyHandle.js.map +1 -1
  71. package/esm/Sandbox.d.ts +4 -3
  72. package/esm/Sandbox.js +759 -823
  73. package/esm/Sandbox.js.map +1 -1
  74. package/esm/Snapshot.d.ts +3 -2
  75. package/esm/Snapshot.js +206 -215
  76. package/esm/Snapshot.js.map +1 -1
  77. package/esm/Volume.d.ts +2 -1
  78. package/esm/Volume.js +92 -93
  79. package/esm/Volume.js.map +1 -1
  80. package/esm/errors/DaytonaError.d.ts +2 -1
  81. package/esm/errors/DaytonaError.js.map +1 -1
  82. package/esm/index.d.ts +2 -2
  83. package/esm/index.js +1 -1
  84. package/esm/index.js.map +1 -1
  85. package/esm/types/CodeInterpreter.d.ts +1 -1
  86. package/esm/utils/Binary.js +14 -2
  87. package/esm/utils/Binary.js.map +1 -1
  88. package/esm/utils/otel.decorator.d.ts +7 -8
  89. package/esm/utils/otel.decorator.js +26 -32
  90. package/esm/utils/otel.decorator.js.map +1 -1
  91. package/package.json +3 -3
package/cjs/Sandbox.js CHANGED
@@ -57,849 +57,784 @@ const otel_decorator_1 = require("./utils/otel.decorator");
57
57
  *
58
58
  * @class
59
59
  */
60
- class Sandbox {
61
- clientConfig;
62
- axiosInstance;
63
- sandboxApi;
64
- fs;
65
- git;
66
- process;
67
- computerUse;
68
- codeInterpreter;
69
- id;
70
- name;
71
- organizationId;
72
- snapshot;
73
- user;
74
- env;
75
- labels;
76
- public;
77
- target;
78
- cpu;
79
- gpu;
80
- memory;
81
- disk;
82
- state;
83
- errorReason;
84
- recoverable;
85
- backupState;
86
- backupCreatedAt;
87
- autoStopInterval;
88
- autoArchiveInterval;
89
- autoDeleteInterval;
90
- volumes;
91
- buildInfo;
92
- createdAt;
93
- updatedAt;
94
- lastActivityAt;
95
- networkBlockAll;
96
- networkAllowList;
97
- toolboxProxyUrl;
98
- infoApi;
99
- /**
100
- * Creates a new Sandbox instance
101
- *
102
- * @param {SandboxDto} sandboxDto - The API Sandbox instance
103
- */
104
- constructor(sandboxDto, clientConfig, axiosInstance, sandboxApi) {
105
- this.clientConfig = clientConfig;
106
- this.axiosInstance = axiosInstance;
107
- this.sandboxApi = sandboxApi;
108
- this.processSandboxDto(sandboxDto);
109
- // Set the toolbox base URL
110
- let baseUrl = this.toolboxProxyUrl;
111
- if (!baseUrl.endsWith('/')) {
112
- baseUrl += '/';
113
- }
114
- this.axiosInstance.defaults.baseURL = baseUrl + this.id;
115
- this.clientConfig.basePath = this.axiosInstance.defaults.baseURL;
116
- // Initialize Services
117
- const getPreviewToken = async () => (await this.getPreviewLink(1)).token;
118
- this.fs = new FileSystem_1.FileSystem(this.clientConfig, new toolbox_api_client_1.FileSystemApi(this.clientConfig, '', this.axiosInstance));
119
- this.git = new Git_1.Git(new toolbox_api_client_1.GitApi(this.clientConfig, '', this.axiosInstance));
120
- const language = sandboxDto.labels?.[Daytona_2.CODE_TOOLBOX_LANGUAGE_LABEL];
121
- this.process = new Process_1.Process(this.clientConfig, new toolbox_api_client_1.ProcessApi(this.clientConfig, '', this.axiosInstance), getPreviewToken, language);
122
- this.codeInterpreter = new CodeInterpreter_1.CodeInterpreter(this.clientConfig, new toolbox_api_client_1.InterpreterApi(this.clientConfig, '', this.axiosInstance), getPreviewToken);
123
- this.computerUse = new ComputerUse_1.ComputerUse(new toolbox_api_client_1.ComputerUseApi(this.clientConfig, '', this.axiosInstance));
124
- this.infoApi = new toolbox_api_client_1.InfoApi(this.clientConfig, '', this.axiosInstance);
125
- }
126
- /**
127
- * Gets the user's home directory path for the logged in user inside the Sandbox.
128
- *
129
- * @returns {Promise<string | undefined>} The absolute path to the Sandbox user's home directory for the logged in user
130
- *
131
- * @example
132
- * const userHomeDir = await sandbox.getUserHomeDir();
133
- * console.log(`Sandbox user home: ${userHomeDir}`);
134
- */
135
- async getUserHomeDir() {
136
- const response = await this.infoApi.getUserHomeDir();
137
- return response.data.dir;
138
- }
139
- /**
140
- * @deprecated Use `getUserHomeDir` instead. This method will be removed in a future version.
141
- */
142
- async getUserRootDir() {
143
- return this.getUserHomeDir();
144
- }
145
- /**
146
- * Gets the working directory path inside the Sandbox.
147
- *
148
- * @returns {Promise<string | undefined>} The absolute path to the Sandbox working directory. Uses the WORKDIR specified
149
- * in the Dockerfile if present, or falling back to the user's home directory if not.
150
- *
151
- * @example
152
- * const workDir = await sandbox.getWorkDir();
153
- * console.log(`Sandbox working directory: ${workDir}`);
154
- */
155
- async getWorkDir() {
156
- const response = await this.infoApi.getWorkDir();
157
- return response.data.dir;
158
- }
159
- /**
160
- * Creates a new Language Server Protocol (LSP) server instance.
161
- *
162
- * The LSP server provides language-specific features like code completion,
163
- * diagnostics, and more.
164
- *
165
- * @param {LspLanguageId} languageId - The language server type (e.g., "typescript")
166
- * @param {string} pathToProject - Path to the project root directory. Relative paths are resolved based on the sandbox working directory.
167
- * @returns {LspServer} A new LSP server instance configured for the specified language
168
- *
169
- * @example
170
- * const lsp = await sandbox.createLspServer('typescript', 'workspace/project');
171
- */
172
- async createLspServer(languageId, pathToProject) {
173
- return new LspServer_1.LspServer(languageId, pathToProject, new toolbox_api_client_1.LspApi(this.clientConfig, '', this.axiosInstance));
174
- }
175
- /**
176
- * Sets labels for the Sandbox.
177
- *
178
- * Labels are key-value pairs that can be used to organize and identify Sandboxes.
179
- *
180
- * @param {Record<string, string>} labels - Dictionary of key-value pairs representing Sandbox labels
181
- * @returns {Promise<void>}
182
- *
183
- * @example
184
- * // Set sandbox labels
185
- * await sandbox.setLabels({
186
- * project: 'my-project',
187
- * environment: 'development',
188
- * team: 'backend'
189
- * });
190
- */
191
- async setLabels(labels) {
192
- this.labels = (await this.sandboxApi.replaceLabels(this.id, { labels })).data.labels;
193
- return this.labels;
194
- }
195
- /**
196
- * Start the Sandbox.
197
- *
198
- * This method starts the Sandbox and waits for it to be ready.
199
- *
200
- * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
201
- * Defaults to 60-second timeout.
202
- * @returns {Promise<void>}
203
- * @throws {DaytonaError} - `DaytonaError` - If Sandbox fails to start or times out
204
- *
205
- * @example
206
- * const sandbox = await daytona.getCurrentSandbox('my-sandbox');
207
- * await sandbox.start(40); // Wait up to 40 seconds
208
- * console.log('Sandbox started successfully');
209
- */
210
- async start(timeout = 60) {
211
- if (timeout < 0) {
212
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
213
- }
214
- const startTime = Date.now();
215
- const response = await this.sandboxApi.startSandbox(this.id, undefined, { timeout: timeout * 1000 });
216
- this.processSandboxDto(response.data);
217
- const timeElapsed = Date.now() - startTime;
218
- await this.waitUntilStarted(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
219
- }
220
- /**
221
- * Recover the Sandbox from a recoverable error and wait for it to be ready.
222
- *
223
- * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
224
- * Defaults to 60-second timeout.
225
- * @returns {Promise<void>}
226
- * @throws {DaytonaError} - `DaytonaError` - If Sandbox fails to recover or times out
227
- *
228
- * @example
229
- * const sandbox = await daytona.get('my-sandbox-id');
230
- * await sandbox.recover();
231
- * console.log('Sandbox recovered successfully');
232
- */
233
- async recover(timeout = 60) {
234
- if (timeout < 0) {
235
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
236
- }
237
- const startTime = Date.now();
238
- const response = await this.sandboxApi.recoverSandbox(this.id, undefined, undefined, { timeout: timeout * 1000 });
239
- this.processSandboxDto(response.data);
240
- const timeElapsed = Date.now() - startTime;
241
- await this.waitUntilStarted(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
242
- }
243
- /**
244
- * Stops the Sandbox.
245
- *
246
- * This method stops the Sandbox and waits for it to be fully stopped.
247
- *
248
- * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
249
- * Defaults to 60-second timeout.
250
- * @param {boolean} [force] - If true, uses SIGKILL instead of SIGTERM. Defaults to false.
251
- * @returns {Promise<void>}
252
- *
253
- * @example
254
- * const sandbox = await daytona.get('my-sandbox-id');
255
- * await sandbox.stop();
256
- * console.log('Sandbox stopped successfully');
257
- */
258
- async stop(timeout = 60, force = false) {
259
- if (timeout < 0) {
260
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
261
- }
262
- const startTime = Date.now();
263
- await this.sandboxApi.stopSandbox(this.id, undefined, force, { timeout: timeout * 1000 });
264
- await this.refreshDataSafe();
265
- const timeElapsed = Date.now() - startTime;
266
- await this.waitUntilStopped(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
267
- }
268
- /**
269
- * Forks the Sandbox, creating a new Sandbox with an identical filesystem.
270
- *
271
- * The forked Sandbox is a copy-on-write clone of the original. It starts
272
- * with the same disk contents but operates independently from that point on.
273
- *
274
- * @param {object} [params] - Fork parameters
275
- * @param {string} [params.name] - Optional name for the forked Sandbox. If not provided, a unique name will be generated.
276
- * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
277
- * Defaults to 60-second timeout.
278
- * @returns {Promise<Sandbox>} The forked Sandbox.
279
- * @throws {DaytonaValidationError} - If timeout is a negative number
280
- * @throws {DaytonaError} - If the fork operation fails or times out
281
- *
282
- * @example
283
- * const sandbox = await daytona.get('my-sandbox');
284
- * const forked = await sandbox._experimental_fork({ name: 'my-fork' });
285
- * console.log(`Forked sandbox: ${forked.id}`);
286
- */
287
- async _experimental_fork(params, timeout = 60) {
288
- if (timeout < 0) {
289
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
290
- }
291
- const startTime = Date.now();
292
- const response = await this.sandboxApi.forkSandbox(this.id, { name: params?.name }, undefined, {
293
- timeout: timeout * 1000,
294
- });
295
- const sandboxDto = response.data;
296
- const forkedSandbox = new Sandbox(sandboxDto, structuredClone(this.clientConfig), Daytona_1.Daytona.createAxiosInstance(), this.sandboxApi);
297
- const timeElapsed = Date.now() - startTime;
298
- await forkedSandbox.waitUntilStarted(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
299
- return forkedSandbox;
300
- }
301
- /**
302
- * Creates a snapshot from the current state of the Sandbox.
303
- *
304
- * This captures the Sandbox's filesystem into a reusable snapshot that can be
305
- * used to create new Sandboxes. The Sandbox will temporarily enter a
306
- * 'snapshotting' state and return to its previous state when complete.
307
- *
308
- * @param {string} name - Name for the new snapshot
309
- * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
310
- * Defaults to 60-second timeout.
311
- * @returns {Promise<void>}
312
- * @throws {DaytonaValidationError} - If timeout is a negative number
313
- * @throws {DaytonaError} - If the snapshot operation fails or times out
314
- *
315
- * @example
316
- * const sandbox = await daytona.get('my-sandbox');
317
- * await sandbox._experimental_createSnapshot('my-snapshot');
318
- * console.log('Snapshot created successfully');
319
- */
320
- async _experimental_createSnapshot(name, timeout = 60) {
321
- if (timeout < 0) {
322
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
323
- }
324
- const startTime = Date.now();
325
- const req = { name };
326
- await this.sandboxApi.createSandboxSnapshot(this.id, req, undefined, {
327
- timeout: timeout * 1000,
328
- });
329
- await this.refreshData();
330
- const timeElapsed = Date.now() - startTime;
331
- const remainingTimeout = timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout;
332
- await this.waitForSnapshotComplete(remainingTimeout);
333
- }
334
- async waitForSnapshotComplete(timeout) {
335
- let checkInterval = 100;
336
- const startTime = Date.now();
337
- while (this.state === api_client_1.SandboxState.SNAPSHOTTING) {
338
- await this.refreshData();
339
- // @ts-expect-error this.refreshData() can modify this.state so this check is fine
340
- if (this.state === api_client_1.SandboxState.ERROR || this.state === api_client_1.SandboxState.BUILD_FAILED) {
341
- throw new DaytonaError_1.DaytonaError(`Sandbox ${this.id} snapshot failed with state: ${this.state}, error reason: ${this.errorReason}`);
342
- }
343
- if (this.state !== api_client_1.SandboxState.SNAPSHOTTING) {
344
- return;
60
+ let Sandbox = (() => {
61
+ let _instanceExtraInitializers = [];
62
+ let _getUserHomeDir_decorators;
63
+ let _getUserRootDir_decorators;
64
+ let _getWorkDir_decorators;
65
+ let _createLspServer_decorators;
66
+ let _setLabels_decorators;
67
+ let _start_decorators;
68
+ let _stop_decorators;
69
+ let __experimental_fork_decorators;
70
+ let __experimental_createSnapshot_decorators;
71
+ let _delete_decorators;
72
+ let _waitUntilStarted_decorators;
73
+ let _waitUntilStopped_decorators;
74
+ let _refreshData_decorators;
75
+ let _setAutostopInterval_decorators;
76
+ let _setAutoArchiveInterval_decorators;
77
+ let _setAutoDeleteInterval_decorators;
78
+ let _updateNetworkSettings_decorators;
79
+ let _getPreviewLink_decorators;
80
+ let _archive_decorators;
81
+ let _resize_decorators;
82
+ let _waitForResizeComplete_decorators;
83
+ let _createSshAccess_decorators;
84
+ let _revokeSshAccess_decorators;
85
+ let _validateSshAccess_decorators;
86
+ return class Sandbox {
87
+ static {
88
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
89
+ _getUserHomeDir_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
90
+ _getUserRootDir_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
91
+ _getWorkDir_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
92
+ _createLspServer_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
93
+ _setLabels_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
94
+ _start_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
95
+ _stop_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
96
+ __experimental_fork_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
97
+ __experimental_createSnapshot_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
98
+ _delete_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
99
+ _waitUntilStarted_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
100
+ _waitUntilStopped_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
101
+ _refreshData_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
102
+ _setAutostopInterval_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
103
+ _setAutoArchiveInterval_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
104
+ _setAutoDeleteInterval_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
105
+ _updateNetworkSettings_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
106
+ _getPreviewLink_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
107
+ _archive_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
108
+ _resize_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
109
+ _waitForResizeComplete_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
110
+ _createSshAccess_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
111
+ _revokeSshAccess_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
112
+ _validateSshAccess_decorators = [(0, otel_decorator_1.WithInstrumentation)()];
113
+ tslib_1.__esDecorate(this, null, _getUserHomeDir_decorators, { kind: "method", name: "getUserHomeDir", static: false, private: false, access: { has: obj => "getUserHomeDir" in obj, get: obj => obj.getUserHomeDir }, metadata: _metadata }, null, _instanceExtraInitializers);
114
+ tslib_1.__esDecorate(this, null, _getUserRootDir_decorators, { kind: "method", name: "getUserRootDir", static: false, private: false, access: { has: obj => "getUserRootDir" in obj, get: obj => obj.getUserRootDir }, metadata: _metadata }, null, _instanceExtraInitializers);
115
+ tslib_1.__esDecorate(this, null, _getWorkDir_decorators, { kind: "method", name: "getWorkDir", static: false, private: false, access: { has: obj => "getWorkDir" in obj, get: obj => obj.getWorkDir }, metadata: _metadata }, null, _instanceExtraInitializers);
116
+ tslib_1.__esDecorate(this, null, _createLspServer_decorators, { kind: "method", name: "createLspServer", static: false, private: false, access: { has: obj => "createLspServer" in obj, get: obj => obj.createLspServer }, metadata: _metadata }, null, _instanceExtraInitializers);
117
+ tslib_1.__esDecorate(this, null, _setLabels_decorators, { kind: "method", name: "setLabels", static: false, private: false, access: { has: obj => "setLabels" in obj, get: obj => obj.setLabels }, metadata: _metadata }, null, _instanceExtraInitializers);
118
+ tslib_1.__esDecorate(this, null, _start_decorators, { kind: "method", name: "start", static: false, private: false, access: { has: obj => "start" in obj, get: obj => obj.start }, metadata: _metadata }, null, _instanceExtraInitializers);
119
+ tslib_1.__esDecorate(this, null, _stop_decorators, { kind: "method", name: "stop", static: false, private: false, access: { has: obj => "stop" in obj, get: obj => obj.stop }, metadata: _metadata }, null, _instanceExtraInitializers);
120
+ tslib_1.__esDecorate(this, null, __experimental_fork_decorators, { kind: "method", name: "_experimental_fork", static: false, private: false, access: { has: obj => "_experimental_fork" in obj, get: obj => obj._experimental_fork }, metadata: _metadata }, null, _instanceExtraInitializers);
121
+ tslib_1.__esDecorate(this, null, __experimental_createSnapshot_decorators, { kind: "method", name: "_experimental_createSnapshot", static: false, private: false, access: { has: obj => "_experimental_createSnapshot" in obj, get: obj => obj._experimental_createSnapshot }, metadata: _metadata }, null, _instanceExtraInitializers);
122
+ tslib_1.__esDecorate(this, null, _delete_decorators, { kind: "method", name: "delete", static: false, private: false, access: { has: obj => "delete" in obj, get: obj => obj.delete }, metadata: _metadata }, null, _instanceExtraInitializers);
123
+ tslib_1.__esDecorate(this, null, _waitUntilStarted_decorators, { kind: "method", name: "waitUntilStarted", static: false, private: false, access: { has: obj => "waitUntilStarted" in obj, get: obj => obj.waitUntilStarted }, metadata: _metadata }, null, _instanceExtraInitializers);
124
+ tslib_1.__esDecorate(this, null, _waitUntilStopped_decorators, { kind: "method", name: "waitUntilStopped", static: false, private: false, access: { has: obj => "waitUntilStopped" in obj, get: obj => obj.waitUntilStopped }, metadata: _metadata }, null, _instanceExtraInitializers);
125
+ tslib_1.__esDecorate(this, null, _refreshData_decorators, { kind: "method", name: "refreshData", static: false, private: false, access: { has: obj => "refreshData" in obj, get: obj => obj.refreshData }, metadata: _metadata }, null, _instanceExtraInitializers);
126
+ tslib_1.__esDecorate(this, null, _setAutostopInterval_decorators, { kind: "method", name: "setAutostopInterval", static: false, private: false, access: { has: obj => "setAutostopInterval" in obj, get: obj => obj.setAutostopInterval }, metadata: _metadata }, null, _instanceExtraInitializers);
127
+ tslib_1.__esDecorate(this, null, _setAutoArchiveInterval_decorators, { kind: "method", name: "setAutoArchiveInterval", static: false, private: false, access: { has: obj => "setAutoArchiveInterval" in obj, get: obj => obj.setAutoArchiveInterval }, metadata: _metadata }, null, _instanceExtraInitializers);
128
+ tslib_1.__esDecorate(this, null, _setAutoDeleteInterval_decorators, { kind: "method", name: "setAutoDeleteInterval", static: false, private: false, access: { has: obj => "setAutoDeleteInterval" in obj, get: obj => obj.setAutoDeleteInterval }, metadata: _metadata }, null, _instanceExtraInitializers);
129
+ tslib_1.__esDecorate(this, null, _updateNetworkSettings_decorators, { kind: "method", name: "updateNetworkSettings", static: false, private: false, access: { has: obj => "updateNetworkSettings" in obj, get: obj => obj.updateNetworkSettings }, metadata: _metadata }, null, _instanceExtraInitializers);
130
+ tslib_1.__esDecorate(this, null, _getPreviewLink_decorators, { kind: "method", name: "getPreviewLink", static: false, private: false, access: { has: obj => "getPreviewLink" in obj, get: obj => obj.getPreviewLink }, metadata: _metadata }, null, _instanceExtraInitializers);
131
+ tslib_1.__esDecorate(this, null, _archive_decorators, { kind: "method", name: "archive", static: false, private: false, access: { has: obj => "archive" in obj, get: obj => obj.archive }, metadata: _metadata }, null, _instanceExtraInitializers);
132
+ tslib_1.__esDecorate(this, null, _resize_decorators, { kind: "method", name: "resize", static: false, private: false, access: { has: obj => "resize" in obj, get: obj => obj.resize }, metadata: _metadata }, null, _instanceExtraInitializers);
133
+ tslib_1.__esDecorate(this, null, _waitForResizeComplete_decorators, { kind: "method", name: "waitForResizeComplete", static: false, private: false, access: { has: obj => "waitForResizeComplete" in obj, get: obj => obj.waitForResizeComplete }, metadata: _metadata }, null, _instanceExtraInitializers);
134
+ tslib_1.__esDecorate(this, null, _createSshAccess_decorators, { kind: "method", name: "createSshAccess", static: false, private: false, access: { has: obj => "createSshAccess" in obj, get: obj => obj.createSshAccess }, metadata: _metadata }, null, _instanceExtraInitializers);
135
+ tslib_1.__esDecorate(this, null, _revokeSshAccess_decorators, { kind: "method", name: "revokeSshAccess", static: false, private: false, access: { has: obj => "revokeSshAccess" in obj, get: obj => obj.revokeSshAccess }, metadata: _metadata }, null, _instanceExtraInitializers);
136
+ tslib_1.__esDecorate(this, null, _validateSshAccess_decorators, { kind: "method", name: "validateSshAccess", static: false, private: false, access: { has: obj => "validateSshAccess" in obj, get: obj => obj.validateSshAccess }, metadata: _metadata }, null, _instanceExtraInitializers);
137
+ if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
138
+ }
139
+ clientConfig = tslib_1.__runInitializers(this, _instanceExtraInitializers);
140
+ axiosInstance;
141
+ sandboxApi;
142
+ fs;
143
+ git;
144
+ process;
145
+ computerUse;
146
+ codeInterpreter;
147
+ id;
148
+ name;
149
+ organizationId;
150
+ snapshot;
151
+ user;
152
+ env;
153
+ labels;
154
+ public;
155
+ target;
156
+ cpu;
157
+ gpu;
158
+ memory;
159
+ disk;
160
+ state;
161
+ errorReason;
162
+ recoverable;
163
+ backupState;
164
+ backupCreatedAt;
165
+ autoStopInterval;
166
+ autoArchiveInterval;
167
+ autoDeleteInterval;
168
+ volumes;
169
+ buildInfo;
170
+ createdAt;
171
+ updatedAt;
172
+ lastActivityAt;
173
+ networkBlockAll;
174
+ networkAllowList;
175
+ toolboxProxyUrl;
176
+ infoApi;
177
+ /**
178
+ * Creates a new Sandbox instance
179
+ *
180
+ * @param {SandboxDto} sandboxDto - The API Sandbox instance
181
+ */
182
+ constructor(sandboxDto, clientConfig, axiosInstance, sandboxApi) {
183
+ this.clientConfig = clientConfig;
184
+ this.axiosInstance = axiosInstance;
185
+ this.sandboxApi = sandboxApi;
186
+ this.processSandboxDto(sandboxDto);
187
+ // Set the toolbox base URL
188
+ let baseUrl = this.toolboxProxyUrl;
189
+ if (!baseUrl.endsWith('/')) {
190
+ baseUrl += '/';
345
191
  }
346
- if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
347
- throw new DaytonaError_1.DaytonaTimeoutError('Sandbox snapshot did not complete within the timeout period');
192
+ this.axiosInstance.defaults.baseURL = baseUrl + this.id;
193
+ this.clientConfig.basePath = this.axiosInstance.defaults.baseURL;
194
+ // Initialize Services
195
+ const getPreviewToken = async () => (await this.getPreviewLink(1)).token;
196
+ this.fs = new FileSystem_1.FileSystem(this.clientConfig, new toolbox_api_client_1.FileSystemApi(this.clientConfig, '', this.axiosInstance));
197
+ this.git = new Git_1.Git(new toolbox_api_client_1.GitApi(this.clientConfig, '', this.axiosInstance));
198
+ const language = sandboxDto.labels?.[Daytona_2.CODE_TOOLBOX_LANGUAGE_LABEL];
199
+ this.process = new Process_1.Process(this.clientConfig, new toolbox_api_client_1.ProcessApi(this.clientConfig, '', this.axiosInstance), getPreviewToken, language);
200
+ this.codeInterpreter = new CodeInterpreter_1.CodeInterpreter(this.clientConfig, new toolbox_api_client_1.InterpreterApi(this.clientConfig, '', this.axiosInstance), getPreviewToken);
201
+ this.computerUse = new ComputerUse_1.ComputerUse(new toolbox_api_client_1.ComputerUseApi(this.clientConfig, '', this.axiosInstance));
202
+ this.infoApi = new toolbox_api_client_1.InfoApi(this.clientConfig, '', this.axiosInstance);
203
+ }
204
+ /**
205
+ * Gets the user's home directory path for the logged in user inside the Sandbox.
206
+ *
207
+ * @returns {Promise<string | undefined>} The absolute path to the Sandbox user's home directory for the logged in user
208
+ *
209
+ * @example
210
+ * const userHomeDir = await sandbox.getUserHomeDir();
211
+ * console.log(`Sandbox user home: ${userHomeDir}`);
212
+ */
213
+ async getUserHomeDir() {
214
+ const response = await this.infoApi.getUserHomeDir();
215
+ return response.data.dir;
216
+ }
217
+ /**
218
+ * @deprecated Use `getUserHomeDir` instead. This method will be removed in a future version.
219
+ */
220
+ async getUserRootDir() {
221
+ return this.getUserHomeDir();
222
+ }
223
+ /**
224
+ * Gets the working directory path inside the Sandbox.
225
+ *
226
+ * @returns {Promise<string | undefined>} The absolute path to the Sandbox working directory. Uses the WORKDIR specified
227
+ * in the Dockerfile if present, or falling back to the user's home directory if not.
228
+ *
229
+ * @example
230
+ * const workDir = await sandbox.getWorkDir();
231
+ * console.log(`Sandbox working directory: ${workDir}`);
232
+ */
233
+ async getWorkDir() {
234
+ const response = await this.infoApi.getWorkDir();
235
+ return response.data.dir;
236
+ }
237
+ /**
238
+ * Creates a new Language Server Protocol (LSP) server instance.
239
+ *
240
+ * The LSP server provides language-specific features like code completion,
241
+ * diagnostics, and more.
242
+ *
243
+ * @param {LspLanguageId} languageId - The language server type (e.g., "typescript")
244
+ * @param {string} pathToProject - Path to the project root directory. Relative paths are resolved based on the sandbox working directory.
245
+ * @returns {LspServer} A new LSP server instance configured for the specified language
246
+ *
247
+ * @example
248
+ * const lsp = await sandbox.createLspServer('typescript', 'workspace/project');
249
+ */
250
+ async createLspServer(languageId, pathToProject) {
251
+ return new LspServer_1.LspServer(languageId, pathToProject, new toolbox_api_client_1.LspApi(this.clientConfig, '', this.axiosInstance));
252
+ }
253
+ /**
254
+ * Sets labels for the Sandbox.
255
+ *
256
+ * Labels are key-value pairs that can be used to organize and identify Sandboxes.
257
+ *
258
+ * @param {Record<string, string>} labels - Dictionary of key-value pairs representing Sandbox labels
259
+ * @returns {Promise<void>}
260
+ *
261
+ * @example
262
+ * // Set sandbox labels
263
+ * await sandbox.setLabels({
264
+ * project: 'my-project',
265
+ * environment: 'development',
266
+ * team: 'backend'
267
+ * });
268
+ */
269
+ async setLabels(labels) {
270
+ this.labels = (await this.sandboxApi.replaceLabels(this.id, { labels })).data.labels;
271
+ return this.labels;
272
+ }
273
+ /**
274
+ * Start the Sandbox.
275
+ *
276
+ * This method starts the Sandbox and waits for it to be ready.
277
+ *
278
+ * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
279
+ * Defaults to 60-second timeout.
280
+ * @returns {Promise<void>}
281
+ * @throws {DaytonaError} - `DaytonaError` - If Sandbox fails to start or times out
282
+ *
283
+ * @example
284
+ * const sandbox = await daytona.getCurrentSandbox('my-sandbox');
285
+ * await sandbox.start(40); // Wait up to 40 seconds
286
+ * console.log('Sandbox started successfully');
287
+ */
288
+ async start(timeout = 60) {
289
+ if (timeout < 0) {
290
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
348
291
  }
349
- await new Promise((resolve) => setTimeout(resolve, checkInterval));
350
- if (Date.now() - startTime > 5000) {
351
- checkInterval = Math.min(checkInterval * 1.1, 1000);
292
+ const startTime = Date.now();
293
+ const response = await this.sandboxApi.startSandbox(this.id, undefined, { timeout: timeout * 1000 });
294
+ this.processSandboxDto(response.data);
295
+ const timeElapsed = Date.now() - startTime;
296
+ await this.waitUntilStarted(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
297
+ }
298
+ /**
299
+ * Recover the Sandbox from a recoverable error and wait for it to be ready.
300
+ *
301
+ * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
302
+ * Defaults to 60-second timeout.
303
+ * @returns {Promise<void>}
304
+ * @throws {DaytonaError} - `DaytonaError` - If Sandbox fails to recover or times out
305
+ *
306
+ * @example
307
+ * const sandbox = await daytona.get('my-sandbox-id');
308
+ * await sandbox.recover();
309
+ * console.log('Sandbox recovered successfully');
310
+ */
311
+ async recover(timeout = 60) {
312
+ if (timeout < 0) {
313
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
352
314
  }
315
+ const startTime = Date.now();
316
+ const response = await this.sandboxApi.recoverSandbox(this.id, undefined, undefined, { timeout: timeout * 1000 });
317
+ this.processSandboxDto(response.data);
318
+ const timeElapsed = Date.now() - startTime;
319
+ await this.waitUntilStarted(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
353
320
  }
354
- }
355
- /**
356
- * Deletes the Sandbox.
357
- * @returns {Promise<void>}
358
- */
359
- async delete(timeout = 60) {
360
- await this.sandboxApi.deleteSandbox(this.id, undefined, { timeout: timeout * 1000 });
361
- this.refreshDataSafe();
362
- }
363
- /**
364
- * Waits for the Sandbox to reach the 'started' state.
365
- *
366
- * This method polls the Sandbox status until it reaches the 'started' state
367
- * or encounters an error.
368
- *
369
- * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
370
- * Defaults to 60 seconds.
371
- * @returns {Promise<void>}
372
- * @throws {DaytonaError} - `DaytonaError` - If the sandbox ends up in an error state or fails to start within the timeout period.
373
- */
374
- async waitUntilStarted(timeout = 60) {
375
- if (timeout < 0) {
376
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
377
- }
378
- let checkInterval = 100;
379
- const startTime = Date.now();
380
- while (this.state !== 'started') {
381
- await this.refreshData();
382
- // @ts-expect-error this.refreshData() can modify this.state so this check is fine
383
- if (this.state === 'started') {
384
- return;
321
+ /**
322
+ * Stops the Sandbox.
323
+ *
324
+ * This method stops the Sandbox and waits for it to be fully stopped.
325
+ *
326
+ * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
327
+ * Defaults to 60-second timeout.
328
+ * @param {boolean} [force] - If true, uses SIGKILL instead of SIGTERM. Defaults to false.
329
+ * @returns {Promise<void>}
330
+ *
331
+ * @example
332
+ * const sandbox = await daytona.get('my-sandbox-id');
333
+ * await sandbox.stop();
334
+ * console.log('Sandbox stopped successfully');
335
+ */
336
+ async stop(timeout = 60, force = false) {
337
+ if (timeout < 0) {
338
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
385
339
  }
386
- if (this.state === 'error') {
387
- const errMsg = `Sandbox ${this.id} failed to start with status: ${this.state}, error reason: ${this.errorReason}`;
388
- throw new DaytonaError_1.DaytonaError(errMsg);
340
+ const startTime = Date.now();
341
+ await this.sandboxApi.stopSandbox(this.id, undefined, force, { timeout: timeout * 1000 });
342
+ await this.refreshDataSafe();
343
+ const timeElapsed = Date.now() - startTime;
344
+ await this.waitUntilStopped(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
345
+ }
346
+ /**
347
+ * Forks the Sandbox, creating a new Sandbox with an identical filesystem.
348
+ *
349
+ * The forked Sandbox is a copy-on-write clone of the original. It starts
350
+ * with the same disk contents but operates independently from that point on.
351
+ *
352
+ * @param {object} [params] - Fork parameters
353
+ * @param {string} [params.name] - Optional name for the forked Sandbox. If not provided, a unique name will be generated.
354
+ * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
355
+ * Defaults to 60-second timeout.
356
+ * @returns {Promise<Sandbox>} The forked Sandbox.
357
+ * @throws {DaytonaValidationError} - If timeout is a negative number
358
+ * @throws {DaytonaError} - If the fork operation fails or times out
359
+ *
360
+ * @example
361
+ * const sandbox = await daytona.get('my-sandbox');
362
+ * const forked = await sandbox._experimental_fork({ name: 'my-fork' });
363
+ * console.log(`Forked sandbox: ${forked.id}`);
364
+ */
365
+ async _experimental_fork(params, timeout = 60) {
366
+ if (timeout < 0) {
367
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
389
368
  }
390
- if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
391
- throw new DaytonaError_1.DaytonaTimeoutError('Sandbox failed to become ready within the timeout period');
369
+ const startTime = Date.now();
370
+ const response = await this.sandboxApi.forkSandbox(this.id, { name: params?.name }, undefined, {
371
+ timeout: timeout * 1000,
372
+ });
373
+ const sandboxDto = response.data;
374
+ const forkedSandbox = new Sandbox(sandboxDto, structuredClone(this.clientConfig), Daytona_1.Daytona.createAxiosInstance(), this.sandboxApi);
375
+ const timeElapsed = Date.now() - startTime;
376
+ await forkedSandbox.waitUntilStarted(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
377
+ return forkedSandbox;
378
+ }
379
+ /**
380
+ * Creates a snapshot from the current state of the Sandbox.
381
+ *
382
+ * This captures the Sandbox's filesystem into a reusable snapshot that can be
383
+ * used to create new Sandboxes. The Sandbox will temporarily enter a
384
+ * 'snapshotting' state and return to its previous state when complete.
385
+ *
386
+ * @param {string} name - Name for the new snapshot
387
+ * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
388
+ * Defaults to 60-second timeout.
389
+ * @returns {Promise<void>}
390
+ * @throws {DaytonaValidationError} - If timeout is a negative number
391
+ * @throws {DaytonaError} - If the snapshot operation fails or times out
392
+ *
393
+ * @example
394
+ * const sandbox = await daytona.get('my-sandbox');
395
+ * await sandbox._experimental_createSnapshot('my-snapshot');
396
+ * console.log('Snapshot created successfully');
397
+ */
398
+ async _experimental_createSnapshot(name, timeout = 60) {
399
+ if (timeout < 0) {
400
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
392
401
  }
393
- await new Promise((resolve) => setTimeout(resolve, checkInterval));
394
- if (Date.now() - startTime > 5000) {
395
- checkInterval = Math.min(checkInterval * 1.1, 1000);
402
+ const startTime = Date.now();
403
+ const req = { name };
404
+ await this.sandboxApi.createSandboxSnapshot(this.id, req, undefined, {
405
+ timeout: timeout * 1000,
406
+ });
407
+ await this.refreshData();
408
+ const timeElapsed = Date.now() - startTime;
409
+ const remainingTimeout = timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout;
410
+ await this.waitForSnapshotComplete(remainingTimeout);
411
+ }
412
+ async waitForSnapshotComplete(timeout) {
413
+ let checkInterval = 100;
414
+ const startTime = Date.now();
415
+ while (this.state === api_client_1.SandboxState.SNAPSHOTTING) {
416
+ await this.refreshData();
417
+ // @ts-expect-error this.refreshData() can modify this.state so this check is fine
418
+ if (this.state === api_client_1.SandboxState.ERROR || this.state === api_client_1.SandboxState.BUILD_FAILED) {
419
+ throw new DaytonaError_1.DaytonaError(`Sandbox ${this.id} snapshot failed with state: ${this.state}, error reason: ${this.errorReason}`);
420
+ }
421
+ if (this.state !== api_client_1.SandboxState.SNAPSHOTTING) {
422
+ return;
423
+ }
424
+ if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
425
+ throw new DaytonaError_1.DaytonaTimeoutError('Sandbox snapshot did not complete within the timeout period');
426
+ }
427
+ await new Promise((resolve) => setTimeout(resolve, checkInterval));
428
+ if (Date.now() - startTime > 5000) {
429
+ checkInterval = Math.min(checkInterval * 1.1, 1000);
430
+ }
396
431
  }
397
432
  }
398
- }
399
- /**
400
- * Wait for Sandbox to reach 'stopped' state.
401
- *
402
- * This method polls the Sandbox status until it reaches the 'stopped' state
403
- * or encounters an error.
404
- *
405
- * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
406
- * Defaults to 60 seconds.
407
- * @returns {Promise<void>}
408
- * @throws {DaytonaError} - `DaytonaError` - If the sandbox fails to stop within the timeout period.
409
- */
410
- async waitUntilStopped(timeout = 60) {
411
- if (timeout < 0) {
412
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
413
- }
414
- let checkInterval = 100;
415
- const startTime = Date.now();
416
- // Treat destroyed as stopped to cover ephemeral sandboxes that are automatically deleted after stopping
417
- while (this.state !== 'stopped' && this.state !== 'destroyed') {
433
+ /**
434
+ * Deletes the Sandbox.
435
+ * @returns {Promise<void>}
436
+ */
437
+ async delete(timeout = 60) {
438
+ await this.sandboxApi.deleteSandbox(this.id, undefined, { timeout: timeout * 1000 });
418
439
  this.refreshDataSafe();
419
- // @ts-expect-error this.refreshData() can modify this.state so this check is fine
420
- if (this.state === 'stopped' || this.state === 'destroyed') {
421
- return;
440
+ }
441
+ /**
442
+ * Waits for the Sandbox to reach the 'started' state.
443
+ *
444
+ * This method polls the Sandbox status until it reaches the 'started' state
445
+ * or encounters an error.
446
+ *
447
+ * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
448
+ * Defaults to 60 seconds.
449
+ * @returns {Promise<void>}
450
+ * @throws {DaytonaError} - `DaytonaError` - If the sandbox ends up in an error state or fails to start within the timeout period.
451
+ */
452
+ async waitUntilStarted(timeout = 60) {
453
+ if (timeout < 0) {
454
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
422
455
  }
423
- if (this.state === 'error') {
424
- const errMsg = `Sandbox failed to stop with status: ${this.state}, error reason: ${this.errorReason}`;
425
- throw new DaytonaError_1.DaytonaError(errMsg);
456
+ let checkInterval = 100;
457
+ const startTime = Date.now();
458
+ while (this.state !== 'started') {
459
+ await this.refreshData();
460
+ // @ts-expect-error this.refreshData() can modify this.state so this check is fine
461
+ if (this.state === 'started') {
462
+ return;
463
+ }
464
+ if (this.state === 'error') {
465
+ const errMsg = `Sandbox ${this.id} failed to start with status: ${this.state}, error reason: ${this.errorReason}`;
466
+ throw new DaytonaError_1.DaytonaError(errMsg);
467
+ }
468
+ if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
469
+ throw new DaytonaError_1.DaytonaTimeoutError('Sandbox failed to become ready within the timeout period');
470
+ }
471
+ await new Promise((resolve) => setTimeout(resolve, checkInterval));
472
+ if (Date.now() - startTime > 5000) {
473
+ checkInterval = Math.min(checkInterval * 1.1, 1000);
474
+ }
426
475
  }
427
- if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
428
- throw new DaytonaError_1.DaytonaTimeoutError('Sandbox failed to become stopped within the timeout period');
476
+ }
477
+ /**
478
+ * Wait for Sandbox to reach 'stopped' state.
479
+ *
480
+ * This method polls the Sandbox status until it reaches the 'stopped' state
481
+ * or encounters an error.
482
+ *
483
+ * @param {number} [timeout] - Maximum time to wait in seconds. 0 means no timeout.
484
+ * Defaults to 60 seconds.
485
+ * @returns {Promise<void>}
486
+ * @throws {DaytonaError} - `DaytonaError` - If the sandbox fails to stop within the timeout period.
487
+ */
488
+ async waitUntilStopped(timeout = 60) {
489
+ if (timeout < 0) {
490
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
429
491
  }
430
- await new Promise((resolve) => setTimeout(resolve, checkInterval));
431
- if (Date.now() - startTime > 5000) {
432
- checkInterval = Math.min(checkInterval * 1.1, 1000);
492
+ let checkInterval = 100;
493
+ const startTime = Date.now();
494
+ // Treat destroyed as stopped to cover ephemeral sandboxes that are automatically deleted after stopping
495
+ while (this.state !== 'stopped' && this.state !== 'destroyed') {
496
+ this.refreshDataSafe();
497
+ // @ts-expect-error this.refreshData() can modify this.state so this check is fine
498
+ if (this.state === 'stopped' || this.state === 'destroyed') {
499
+ return;
500
+ }
501
+ if (this.state === 'error') {
502
+ const errMsg = `Sandbox failed to stop with status: ${this.state}, error reason: ${this.errorReason}`;
503
+ throw new DaytonaError_1.DaytonaError(errMsg);
504
+ }
505
+ if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
506
+ throw new DaytonaError_1.DaytonaTimeoutError('Sandbox failed to become stopped within the timeout period');
507
+ }
508
+ await new Promise((resolve) => setTimeout(resolve, checkInterval));
509
+ if (Date.now() - startTime > 5000) {
510
+ checkInterval = Math.min(checkInterval * 1.1, 1000);
511
+ }
433
512
  }
434
513
  }
435
- }
436
- /**
437
- * Refreshes the Sandbox data from the API.
438
- *
439
- * @returns {Promise<void>}
440
- *
441
- * @example
442
- * await sandbox.refreshData();
443
- * console.log(`Sandbox ${sandbox.id}:`);
444
- * console.log(`State: ${sandbox.state}`);
445
- * console.log(`Resources: ${sandbox.cpu} CPU, ${sandbox.memory} GiB RAM`);
446
- */
447
- async refreshData() {
448
- const response = await this.sandboxApi.getSandbox(this.id);
449
- this.processSandboxDto(response.data);
450
- }
451
- /**
452
- * Refreshes the sandbox activity to reset the timer for automated lifecycle management actions.
453
- *
454
- * This method updates the sandbox's last activity timestamp without changing its state.
455
- * It is useful for keeping long-running sessions alive while there is still user activity.
456
- *
457
- * @returns {Promise<void>}
458
- *
459
- * @example
460
- * // Keep sandbox activity alive
461
- * await sandbox.refreshActivity();
462
- */
463
- async refreshActivity() {
464
- await this.sandboxApi.updateLastActivity(this.id);
465
- }
466
- /**
467
- * Set the auto-stop interval for the Sandbox.
468
- *
469
- * The Sandbox will automatically stop after being idle (no new events) for the specified interval.
470
- * Events include any state changes or interactions with the Sandbox through the sdk.
471
- * Interactions using Sandbox Previews are not included.
472
- *
473
- * @param {number} interval - Number of minutes of inactivity before auto-stopping.
474
- * Set to 0 to disable auto-stop. Default is 15 minutes.
475
- * @returns {Promise<void>}
476
- * @throws {DaytonaError} - `DaytonaError` - If interval is not a non-negative integer
477
- *
478
- * @example
479
- * // Auto-stop after 1 hour
480
- * await sandbox.setAutostopInterval(60);
481
- * // Or disable auto-stop
482
- * await sandbox.setAutostopInterval(0);
483
- */
484
- async setAutostopInterval(interval) {
485
- if (!Number.isInteger(interval) || interval < 0) {
486
- throw new DaytonaError_1.DaytonaValidationError('autoStopInterval must be a non-negative integer');
487
- }
488
- await this.sandboxApi.setAutostopInterval(this.id, interval);
489
- this.autoStopInterval = interval;
490
- }
491
- /**
492
- * Set the auto-archive interval for the Sandbox.
493
- *
494
- * The Sandbox will automatically archive after being continuously stopped for the specified interval.
495
- *
496
- * @param {number} interval - Number of minutes after which a continuously stopped Sandbox will be auto-archived.
497
- * Set to 0 for the maximum interval. Default is 7 days.
498
- * @returns {Promise<void>}
499
- * @throws {DaytonaError} - `DaytonaError` - If interval is not a non-negative integer
500
- *
501
- * @example
502
- * // Auto-archive after 1 hour
503
- * await sandbox.setAutoArchiveInterval(60);
504
- * // Or use the maximum interval
505
- * await sandbox.setAutoArchiveInterval(0);
506
- */
507
- async setAutoArchiveInterval(interval) {
508
- if (!Number.isInteger(interval) || interval < 0) {
509
- throw new DaytonaError_1.DaytonaValidationError('autoArchiveInterval must be a non-negative integer');
510
- }
511
- await this.sandboxApi.setAutoArchiveInterval(this.id, interval);
512
- this.autoArchiveInterval = interval;
513
- }
514
- /**
515
- * Set the auto-delete interval for the Sandbox.
516
- *
517
- * The Sandbox will automatically delete after being continuously stopped for the specified interval.
518
- *
519
- * @param {number} interval - Number of minutes after which a continuously stopped Sandbox will be auto-deleted.
520
- * Set to negative value to disable auto-delete. Set to 0 to delete immediately upon stopping.
521
- * By default, auto-delete is disabled.
522
- * @returns {Promise<void>}
523
- *
524
- * @example
525
- * // Auto-delete after 1 hour
526
- * await sandbox.setAutoDeleteInterval(60);
527
- * // Or delete immediately upon stopping
528
- * await sandbox.setAutoDeleteInterval(0);
529
- * // Or disable auto-delete
530
- * await sandbox.setAutoDeleteInterval(-1);
531
- */
532
- async setAutoDeleteInterval(interval) {
533
- await this.sandboxApi.setAutoDeleteInterval(this.id, interval);
534
- this.autoDeleteInterval = interval;
535
- }
536
- /**
537
- * Updates outbound network policy for this sandbox on the runner (for example block all traffic,
538
- * restore general internet access, or apply a CIDR allow list) without stopping the sandbox.
539
- *
540
- * This maps to the same mechanism as creating a sandbox with `networkBlockAll` / `networkAllowList`:
541
- * the runner applies iptables rules to the sandbox container.
542
- *
543
- * @param {UpdateSandboxNetworkSettings} settings - At least one of `networkBlockAll` or `networkAllowList` must be set.
544
- * Set `networkBlockAll` to `false` to restore outbound access after a block (and clear a stored allow list).
545
- *
546
- * @example
547
- * // Pause internet (outbound blocked)
548
- * await sandbox.updateNetworkSettings({ networkBlockAll: true });
549
- * // Resume internet
550
- * await sandbox.updateNetworkSettings({ networkBlockAll: false });
551
- */
552
- async updateNetworkSettings(settings) {
553
- if (settings.networkBlockAll === undefined && settings.networkAllowList === undefined) {
554
- throw new DaytonaError_1.DaytonaValidationError('At least one of networkBlockAll or networkAllowList must be set');
555
- }
556
- const response = await this.sandboxApi.updateNetworkSettings(this.id, settings);
557
- this.processSandboxDto(response.data);
558
- }
559
- /**
560
- * Retrieves the preview link for the sandbox at the specified port. If the port is closed,
561
- * it will be opened automatically. For private sandboxes, a token is included to grant access
562
- * to the URL.
563
- *
564
- * @param {number} port - The port to open the preview link on.
565
- * @returns {PortPreviewUrl} The response object for the preview link, which includes the `url`
566
- * and the `token` (to access private sandboxes).
567
- *
568
- * @example
569
- * const previewLink = await sandbox.getPreviewLink(3000);
570
- * console.log(`Preview URL: ${previewLink.url}`);
571
- * console.log(`Token: ${previewLink.token}`);
572
- */
573
- async getPreviewLink(port) {
574
- return (await this.sandboxApi.getPortPreviewUrl(this.id, port)).data;
575
- }
576
- /**
577
- * Retrieves a signed preview url for the sandbox at the specified port.
578
- *
579
- * @param {number} port - The port to open the preview link on.
580
- * @param {number} [expiresInSeconds] - The number of seconds the signed preview url will be valid for. Defaults to 60 seconds.
581
- * @returns {Promise<SignedPortPreviewUrl>} The response object for the signed preview url.
582
- */
583
- async getSignedPreviewUrl(port, expiresInSeconds) {
584
- return (await this.sandboxApi.getSignedPortPreviewUrl(this.id, port, undefined, expiresInSeconds)).data;
585
- }
586
- /**
587
- * Expires a signed preview url for the sandbox at the specified port.
588
- *
589
- * @param {number} port - The port to expire the signed preview url on.
590
- * @param {string} token - The token to expire the signed preview url on.
591
- * @returns {Promise<void>}
592
- */
593
- async expireSignedPreviewUrl(port, token) {
594
- await this.sandboxApi.expireSignedPortPreviewUrl(this.id, port, token);
595
- }
596
- /**
597
- * Archives the sandbox, making it inactive and preserving its state. When sandboxes are archived, the entire filesystem
598
- * state is moved to cost-effective object storage, making it possible to keep sandboxes available for an extended period.
599
- * The tradeoff between archived and stopped states is that starting an archived sandbox takes more time, depending on its size.
600
- * Sandbox must be stopped before archiving.
601
- */
602
- async archive() {
603
- await this.sandboxApi.archiveSandbox(this.id);
604
- await this.refreshData();
605
- }
606
- /**
607
- * Resizes the Sandbox resources.
608
- *
609
- * Changes the CPU, memory, or disk allocation for the Sandbox. Hot resize (on running
610
- * sandbox) only allows CPU/memory increases. Disk resize requires a stopped sandbox.
611
- *
612
- * @param {Resources} resources - New resource configuration. Only specified fields will be updated.
613
- * - cpu: Number of CPU cores (minimum: 1). For hot resize, can only be increased.
614
- * - memory: Memory in GiB (minimum: 1). For hot resize, can only be increased.
615
- * - disk: Disk space in GiB (can only be increased, requires stopped sandbox).
616
- * @param {number} [timeout=60] - Timeout in seconds for the resize operation. 0 means no timeout.
617
- * @returns {Promise<void>}
618
- * @throws {DaytonaError} - If hot resize constraints are violated, disk resize attempted on running sandbox,
619
- * disk size decrease is attempted, no resource changes are specified, or resize operation times out.
620
- *
621
- * @example
622
- * // Increase CPU/memory on running sandbox (hot resize)
623
- * await sandbox.resize({ cpu: 4, memory: 8 });
624
- *
625
- * // Change disk (sandbox must be stopped)
626
- * await sandbox.stop();
627
- * await sandbox.resize({ cpu: 2, memory: 4, disk: 30 });
628
- */
629
- async resize(resources, timeout = 60) {
630
- if (timeout < 0) {
631
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
632
- }
633
- const startTime = Date.now();
634
- const resizeRequest = {
635
- cpu: resources.cpu,
636
- memory: resources.memory,
637
- disk: resources.disk,
638
- };
639
- const response = await this.sandboxApi.resizeSandbox(this.id, resizeRequest, this.organizationId, {
640
- timeout: timeout * 1000,
641
- });
642
- this.processSandboxDto(response.data);
643
- const timeElapsed = Date.now() - startTime;
644
- await this.waitForResizeComplete(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
645
- }
646
- /**
647
- * Waits for the Sandbox resize operation to complete.
648
- *
649
- * This method polls the Sandbox status until the state is no longer 'resizing'.
650
- *
651
- * @param {number} [timeout=60] - Maximum time to wait in seconds. 0 means no timeout.
652
- * @returns {Promise<void>}
653
- * @throws {DaytonaError} - If the sandbox ends up in an error state or resize times out.
654
- */
655
- async waitForResizeComplete(timeout = 60) {
656
- if (timeout < 0) {
657
- throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
658
- }
659
- let checkInterval = 100;
660
- const startTime = Date.now();
661
- while (this.state === api_client_1.SandboxState.RESIZING) {
662
- await this.refreshData();
663
- // @ts-expect-error this.refreshData() can modify this.state so this check is fine
664
- if (this.state === api_client_1.SandboxState.ERROR || this.state === api_client_1.SandboxState.BUILD_FAILED) {
665
- throw new DaytonaError_1.DaytonaError(`Sandbox ${this.id} resize failed with state: ${this.state}, error reason: ${this.errorReason}`);
666
- }
667
- if (this.state !== api_client_1.SandboxState.RESIZING) {
668
- return;
514
+ /**
515
+ * Refreshes the Sandbox data from the API.
516
+ *
517
+ * @returns {Promise<void>}
518
+ *
519
+ * @example
520
+ * await sandbox.refreshData();
521
+ * console.log(`Sandbox ${sandbox.id}:`);
522
+ * console.log(`State: ${sandbox.state}`);
523
+ * console.log(`Resources: ${sandbox.cpu} CPU, ${sandbox.memory} GiB RAM`);
524
+ */
525
+ async refreshData() {
526
+ const response = await this.sandboxApi.getSandbox(this.id);
527
+ this.processSandboxDto(response.data);
528
+ }
529
+ /**
530
+ * Refreshes the sandbox activity to reset the timer for automated lifecycle management actions.
531
+ *
532
+ * This method updates the sandbox's last activity timestamp without changing its state.
533
+ * It is useful for keeping long-running sessions alive while there is still user activity.
534
+ *
535
+ * @returns {Promise<void>}
536
+ *
537
+ * @example
538
+ * // Keep sandbox activity alive
539
+ * await sandbox.refreshActivity();
540
+ */
541
+ async refreshActivity() {
542
+ await this.sandboxApi.updateLastActivity(this.id);
543
+ }
544
+ /**
545
+ * Set the auto-stop interval for the Sandbox.
546
+ *
547
+ * The Sandbox will automatically stop after being idle (no new events) for the specified interval.
548
+ * Events include any state changes or interactions with the Sandbox through the sdk.
549
+ * Interactions using Sandbox Previews are not included.
550
+ *
551
+ * @param {number} interval - Number of minutes of inactivity before auto-stopping.
552
+ * Set to 0 to disable auto-stop. Default is 15 minutes.
553
+ * @returns {Promise<void>}
554
+ * @throws {DaytonaError} - `DaytonaError` - If interval is not a non-negative integer
555
+ *
556
+ * @example
557
+ * // Auto-stop after 1 hour
558
+ * await sandbox.setAutostopInterval(60);
559
+ * // Or disable auto-stop
560
+ * await sandbox.setAutostopInterval(0);
561
+ */
562
+ async setAutostopInterval(interval) {
563
+ if (!Number.isInteger(interval) || interval < 0) {
564
+ throw new DaytonaError_1.DaytonaValidationError('autoStopInterval must be a non-negative integer');
669
565
  }
670
- if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
671
- throw new DaytonaError_1.DaytonaTimeoutError('Sandbox resize did not complete within the timeout period');
566
+ await this.sandboxApi.setAutostopInterval(this.id, interval);
567
+ this.autoStopInterval = interval;
568
+ }
569
+ /**
570
+ * Set the auto-archive interval for the Sandbox.
571
+ *
572
+ * The Sandbox will automatically archive after being continuously stopped for the specified interval.
573
+ *
574
+ * @param {number} interval - Number of minutes after which a continuously stopped Sandbox will be auto-archived.
575
+ * Set to 0 for the maximum interval. Default is 7 days.
576
+ * @returns {Promise<void>}
577
+ * @throws {DaytonaError} - `DaytonaError` - If interval is not a non-negative integer
578
+ *
579
+ * @example
580
+ * // Auto-archive after 1 hour
581
+ * await sandbox.setAutoArchiveInterval(60);
582
+ * // Or use the maximum interval
583
+ * await sandbox.setAutoArchiveInterval(0);
584
+ */
585
+ async setAutoArchiveInterval(interval) {
586
+ if (!Number.isInteger(interval) || interval < 0) {
587
+ throw new DaytonaError_1.DaytonaValidationError('autoArchiveInterval must be a non-negative integer');
672
588
  }
673
- await new Promise((resolve) => setTimeout(resolve, checkInterval));
674
- if (Date.now() - startTime > 5000) {
675
- checkInterval = Math.min(checkInterval * 1.1, 1000);
589
+ await this.sandboxApi.setAutoArchiveInterval(this.id, interval);
590
+ this.autoArchiveInterval = interval;
591
+ }
592
+ /**
593
+ * Set the auto-delete interval for the Sandbox.
594
+ *
595
+ * The Sandbox will automatically delete after being continuously stopped for the specified interval.
596
+ *
597
+ * @param {number} interval - Number of minutes after which a continuously stopped Sandbox will be auto-deleted.
598
+ * Set to negative value to disable auto-delete. Set to 0 to delete immediately upon stopping.
599
+ * By default, auto-delete is disabled.
600
+ * @returns {Promise<void>}
601
+ *
602
+ * @example
603
+ * // Auto-delete after 1 hour
604
+ * await sandbox.setAutoDeleteInterval(60);
605
+ * // Or delete immediately upon stopping
606
+ * await sandbox.setAutoDeleteInterval(0);
607
+ * // Or disable auto-delete
608
+ * await sandbox.setAutoDeleteInterval(-1);
609
+ */
610
+ async setAutoDeleteInterval(interval) {
611
+ await this.sandboxApi.setAutoDeleteInterval(this.id, interval);
612
+ this.autoDeleteInterval = interval;
613
+ }
614
+ /**
615
+ * Updates outbound network policy for this sandbox on the runner (for example block all traffic,
616
+ * restore general internet access, or apply a CIDR allow list) without stopping the sandbox.
617
+ *
618
+ * This maps to the same mechanism as creating a sandbox with `networkBlockAll` / `networkAllowList`:
619
+ * the runner applies iptables rules to the sandbox container.
620
+ *
621
+ * @param {UpdateSandboxNetworkSettings} settings - At least one of `networkBlockAll` or `networkAllowList` must be set.
622
+ * Set `networkBlockAll` to `false` to restore outbound access after a block (and clear a stored allow list).
623
+ *
624
+ * @example
625
+ * // Pause internet (outbound blocked)
626
+ * await sandbox.updateNetworkSettings({ networkBlockAll: true });
627
+ * // Resume internet
628
+ * await sandbox.updateNetworkSettings({ networkBlockAll: false });
629
+ */
630
+ async updateNetworkSettings(settings) {
631
+ if (settings.networkBlockAll === undefined && settings.networkAllowList === undefined) {
632
+ throw new DaytonaError_1.DaytonaValidationError('At least one of networkBlockAll or networkAllowList must be set');
676
633
  }
634
+ const response = await this.sandboxApi.updateNetworkSettings(this.id, settings);
635
+ this.processSandboxDto(response.data);
677
636
  }
678
- }
679
- /**
680
- * Creates an SSH access token for the sandbox.
681
- *
682
- * @param {number} expiresInMinutes - The number of minutes the SSH access token will be valid for.
683
- * @returns {Promise<SshAccessDto>} The SSH access token.
684
- */
685
- async createSshAccess(expiresInMinutes) {
686
- return (await this.sandboxApi.createSshAccess(this.id, undefined, expiresInMinutes)).data;
687
- }
688
- /**
689
- * Revokes an SSH access token for the sandbox.
690
- *
691
- * @param {string} token - The token to revoke.
692
- * @returns {Promise<void>}
693
- */
694
- async revokeSshAccess(token) {
695
- await this.sandboxApi.revokeSshAccess(this.id, undefined, token);
696
- }
697
- /**
698
- * Validates an SSH access token for the sandbox.
699
- *
700
- * @param {string} token - The token to validate.
701
- * @returns {Promise<SshAccessValidationDto>} The SSH access validation result.
702
- */
703
- async validateSshAccess(token) {
704
- return (await this.sandboxApi.validateSshAccess(token)).data;
705
- }
706
- /**
707
- * Assigns the API sandbox data to the Sandbox object.
708
- *
709
- * @param {SandboxDto} sandboxDto - The API sandbox instance to assign data from
710
- * @returns {void}
711
- */
712
- processSandboxDto(sandboxDto) {
713
- this.id = sandboxDto.id;
714
- this.name = sandboxDto.name;
715
- this.organizationId = sandboxDto.organizationId;
716
- this.snapshot = sandboxDto.snapshot;
717
- this.user = sandboxDto.user;
718
- this.env = sandboxDto.env;
719
- this.labels = sandboxDto.labels;
720
- this.public = sandboxDto.public;
721
- this.target = sandboxDto.target;
722
- this.cpu = sandboxDto.cpu;
723
- this.gpu = sandboxDto.gpu;
724
- this.memory = sandboxDto.memory;
725
- this.disk = sandboxDto.disk;
726
- this.state = sandboxDto.state;
727
- this.errorReason = sandboxDto.errorReason;
728
- this.recoverable = sandboxDto.recoverable;
729
- this.backupState = sandboxDto.backupState;
730
- this.backupCreatedAt = sandboxDto.backupCreatedAt;
731
- this.autoStopInterval = sandboxDto.autoStopInterval;
732
- this.autoArchiveInterval = sandboxDto.autoArchiveInterval;
733
- this.autoDeleteInterval = sandboxDto.autoDeleteInterval;
734
- this.volumes = sandboxDto.volumes;
735
- this.buildInfo = sandboxDto.buildInfo;
736
- this.createdAt = sandboxDto.createdAt;
737
- this.updatedAt = sandboxDto.updatedAt;
738
- this.lastActivityAt = sandboxDto.lastActivityAt;
739
- this.networkBlockAll = sandboxDto.networkBlockAll;
740
- this.networkAllowList = sandboxDto.networkAllowList;
741
- this.toolboxProxyUrl = sandboxDto.toolboxProxyUrl;
742
- }
743
- /**
744
- * Refreshes the Sandbox data from the API, but does not throw an error if the sandbox has been deleted.
745
- * Instead, it sets the state to destroyed.
746
- *
747
- * @returns {Promise<void>}
748
- */
749
- async refreshDataSafe() {
750
- try {
637
+ /**
638
+ * Retrieves the preview link for the sandbox at the specified port. If the port is closed,
639
+ * it will be opened automatically. For private sandboxes, a token is included to grant access
640
+ * to the URL.
641
+ *
642
+ * @param {number} port - The port to open the preview link on.
643
+ * @returns {PortPreviewUrl} The response object for the preview link, which includes the `url`
644
+ * and the `token` (to access private sandboxes).
645
+ *
646
+ * @example
647
+ * const previewLink = await sandbox.getPreviewLink(3000);
648
+ * console.log(`Preview URL: ${previewLink.url}`);
649
+ * console.log(`Token: ${previewLink.token}`);
650
+ */
651
+ async getPreviewLink(port) {
652
+ return (await this.sandboxApi.getPortPreviewUrl(this.id, port)).data;
653
+ }
654
+ /**
655
+ * Retrieves a signed preview url for the sandbox at the specified port.
656
+ *
657
+ * @param {number} port - The port to open the preview link on.
658
+ * @param {number} [expiresInSeconds] - The number of seconds the signed preview url will be valid for. Defaults to 60 seconds.
659
+ * @returns {Promise<SignedPortPreviewUrl>} The response object for the signed preview url.
660
+ */
661
+ async getSignedPreviewUrl(port, expiresInSeconds) {
662
+ return (await this.sandboxApi.getSignedPortPreviewUrl(this.id, port, undefined, expiresInSeconds)).data;
663
+ }
664
+ /**
665
+ * Expires a signed preview url for the sandbox at the specified port.
666
+ *
667
+ * @param {number} port - The port to expire the signed preview url on.
668
+ * @param {string} token - The token to expire the signed preview url on.
669
+ * @returns {Promise<void>}
670
+ */
671
+ async expireSignedPreviewUrl(port, token) {
672
+ await this.sandboxApi.expireSignedPortPreviewUrl(this.id, port, token);
673
+ }
674
+ /**
675
+ * Archives the sandbox, making it inactive and preserving its state. When sandboxes are archived, the entire filesystem
676
+ * state is moved to cost-effective object storage, making it possible to keep sandboxes available for an extended period.
677
+ * The tradeoff between archived and stopped states is that starting an archived sandbox takes more time, depending on its size.
678
+ * Sandbox must be stopped before archiving.
679
+ */
680
+ async archive() {
681
+ await this.sandboxApi.archiveSandbox(this.id);
751
682
  await this.refreshData();
752
683
  }
753
- catch (error) {
754
- if (error instanceof DaytonaError_1.DaytonaNotFoundError) {
755
- this.state = api_client_1.SandboxState.DESTROYED;
684
+ /**
685
+ * Resizes the Sandbox resources.
686
+ *
687
+ * Changes the CPU, memory, or disk allocation for the Sandbox. Hot resize (on running
688
+ * sandbox) only allows CPU/memory increases. Disk resize requires a stopped sandbox.
689
+ *
690
+ * @param {Resources} resources - New resource configuration. Only specified fields will be updated.
691
+ * - cpu: Number of CPU cores (minimum: 1). For hot resize, can only be increased.
692
+ * - memory: Memory in GiB (minimum: 1). For hot resize, can only be increased.
693
+ * - disk: Disk space in GiB (can only be increased, requires stopped sandbox).
694
+ * @param {number} [timeout=60] - Timeout in seconds for the resize operation. 0 means no timeout.
695
+ * @returns {Promise<void>}
696
+ * @throws {DaytonaError} - If hot resize constraints are violated, disk resize attempted on running sandbox,
697
+ * disk size decrease is attempted, no resource changes are specified, or resize operation times out.
698
+ *
699
+ * @example
700
+ * // Increase CPU/memory on running sandbox (hot resize)
701
+ * await sandbox.resize({ cpu: 4, memory: 8 });
702
+ *
703
+ * // Change disk (sandbox must be stopped)
704
+ * await sandbox.stop();
705
+ * await sandbox.resize({ cpu: 2, memory: 4, disk: 30 });
706
+ */
707
+ async resize(resources, timeout = 60) {
708
+ if (timeout < 0) {
709
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
710
+ }
711
+ const startTime = Date.now();
712
+ const resizeRequest = {
713
+ cpu: resources.cpu,
714
+ memory: resources.memory,
715
+ disk: resources.disk,
716
+ };
717
+ const response = await this.sandboxApi.resizeSandbox(this.id, resizeRequest, this.organizationId, {
718
+ timeout: timeout * 1000,
719
+ });
720
+ this.processSandboxDto(response.data);
721
+ const timeElapsed = Date.now() - startTime;
722
+ await this.waitForResizeComplete(timeout ? Math.max(0.001, timeout - timeElapsed / 1000) : timeout);
723
+ }
724
+ /**
725
+ * Waits for the Sandbox resize operation to complete.
726
+ *
727
+ * This method polls the Sandbox status until the state is no longer 'resizing'.
728
+ *
729
+ * @param {number} [timeout=60] - Maximum time to wait in seconds. 0 means no timeout.
730
+ * @returns {Promise<void>}
731
+ * @throws {DaytonaError} - If the sandbox ends up in an error state or resize times out.
732
+ */
733
+ async waitForResizeComplete(timeout = 60) {
734
+ if (timeout < 0) {
735
+ throw new DaytonaError_1.DaytonaValidationError('Timeout must be a non-negative number');
736
+ }
737
+ let checkInterval = 100;
738
+ const startTime = Date.now();
739
+ while (this.state === api_client_1.SandboxState.RESIZING) {
740
+ await this.refreshData();
741
+ // @ts-expect-error this.refreshData() can modify this.state so this check is fine
742
+ if (this.state === api_client_1.SandboxState.ERROR || this.state === api_client_1.SandboxState.BUILD_FAILED) {
743
+ throw new DaytonaError_1.DaytonaError(`Sandbox ${this.id} resize failed with state: ${this.state}, error reason: ${this.errorReason}`);
744
+ }
745
+ if (this.state !== api_client_1.SandboxState.RESIZING) {
746
+ return;
747
+ }
748
+ if (timeout !== 0 && Date.now() - startTime > timeout * 1000) {
749
+ throw new DaytonaError_1.DaytonaTimeoutError('Sandbox resize did not complete within the timeout period');
750
+ }
751
+ await new Promise((resolve) => setTimeout(resolve, checkInterval));
752
+ if (Date.now() - startTime > 5000) {
753
+ checkInterval = Math.min(checkInterval * 1.1, 1000);
754
+ }
755
+ }
756
+ }
757
+ /**
758
+ * Creates an SSH access token for the sandbox.
759
+ *
760
+ * @param {number} expiresInMinutes - The number of minutes the SSH access token will be valid for.
761
+ * @returns {Promise<SshAccessDto>} The SSH access token.
762
+ */
763
+ async createSshAccess(expiresInMinutes) {
764
+ return (await this.sandboxApi.createSshAccess(this.id, undefined, expiresInMinutes)).data;
765
+ }
766
+ /**
767
+ * Revokes an SSH access token for the sandbox.
768
+ *
769
+ * @param {string} token - The token to revoke.
770
+ * @returns {Promise<void>}
771
+ */
772
+ async revokeSshAccess(token) {
773
+ await this.sandboxApi.revokeSshAccess(this.id, undefined, token);
774
+ }
775
+ /**
776
+ * Validates an SSH access token for the sandbox.
777
+ *
778
+ * @param {string} token - The token to validate.
779
+ * @returns {Promise<SshAccessValidationDto>} The SSH access validation result.
780
+ */
781
+ async validateSshAccess(token) {
782
+ return (await this.sandboxApi.validateSshAccess(token)).data;
783
+ }
784
+ /**
785
+ * Assigns the API sandbox data to the Sandbox object.
786
+ *
787
+ * @param {SandboxDto} sandboxDto - The API sandbox instance to assign data from
788
+ * @returns {void}
789
+ */
790
+ processSandboxDto(sandboxDto) {
791
+ this.id = sandboxDto.id;
792
+ this.name = sandboxDto.name;
793
+ this.organizationId = sandboxDto.organizationId;
794
+ this.snapshot = sandboxDto.snapshot;
795
+ this.user = sandboxDto.user;
796
+ this.env = sandboxDto.env;
797
+ this.labels = sandboxDto.labels;
798
+ this.public = sandboxDto.public;
799
+ this.target = sandboxDto.target;
800
+ this.cpu = sandboxDto.cpu;
801
+ this.gpu = sandboxDto.gpu;
802
+ this.memory = sandboxDto.memory;
803
+ this.disk = sandboxDto.disk;
804
+ this.state = sandboxDto.state;
805
+ this.errorReason = sandboxDto.errorReason;
806
+ this.recoverable = sandboxDto.recoverable;
807
+ this.backupState = sandboxDto.backupState;
808
+ this.backupCreatedAt = sandboxDto.backupCreatedAt;
809
+ this.autoStopInterval = sandboxDto.autoStopInterval;
810
+ this.autoArchiveInterval = sandboxDto.autoArchiveInterval;
811
+ this.autoDeleteInterval = sandboxDto.autoDeleteInterval;
812
+ this.volumes = sandboxDto.volumes;
813
+ this.buildInfo = sandboxDto.buildInfo;
814
+ this.createdAt = sandboxDto.createdAt;
815
+ this.updatedAt = sandboxDto.updatedAt;
816
+ this.lastActivityAt = sandboxDto.lastActivityAt;
817
+ this.networkBlockAll = sandboxDto.networkBlockAll;
818
+ this.networkAllowList = sandboxDto.networkAllowList;
819
+ this.toolboxProxyUrl = sandboxDto.toolboxProxyUrl;
820
+ }
821
+ /**
822
+ * Refreshes the Sandbox data from the API, but does not throw an error if the sandbox has been deleted.
823
+ * Instead, it sets the state to destroyed.
824
+ *
825
+ * @returns {Promise<void>}
826
+ */
827
+ async refreshDataSafe() {
828
+ try {
829
+ await this.refreshData();
830
+ }
831
+ catch (error) {
832
+ if (error instanceof DaytonaError_1.DaytonaNotFoundError) {
833
+ this.state = api_client_1.SandboxState.DESTROYED;
834
+ }
756
835
  }
757
836
  }
758
- }
759
- }
837
+ };
838
+ })();
760
839
  exports.Sandbox = Sandbox;
761
- tslib_1.__decorate([
762
- (0, otel_decorator_1.WithInstrumentation)(),
763
- tslib_1.__metadata("design:type", Function),
764
- tslib_1.__metadata("design:paramtypes", []),
765
- tslib_1.__metadata("design:returntype", Promise)
766
- ], Sandbox.prototype, "getUserHomeDir", null);
767
- tslib_1.__decorate([
768
- (0, otel_decorator_1.WithInstrumentation)(),
769
- tslib_1.__metadata("design:type", Function),
770
- tslib_1.__metadata("design:paramtypes", []),
771
- tslib_1.__metadata("design:returntype", Promise)
772
- ], Sandbox.prototype, "getUserRootDir", null);
773
- tslib_1.__decorate([
774
- (0, otel_decorator_1.WithInstrumentation)(),
775
- tslib_1.__metadata("design:type", Function),
776
- tslib_1.__metadata("design:paramtypes", []),
777
- tslib_1.__metadata("design:returntype", Promise)
778
- ], Sandbox.prototype, "getWorkDir", null);
779
- tslib_1.__decorate([
780
- (0, otel_decorator_1.WithInstrumentation)(),
781
- tslib_1.__metadata("design:type", Function),
782
- tslib_1.__metadata("design:paramtypes", [String, String]),
783
- tslib_1.__metadata("design:returntype", Promise)
784
- ], Sandbox.prototype, "createLspServer", null);
785
- tslib_1.__decorate([
786
- (0, otel_decorator_1.WithInstrumentation)(),
787
- tslib_1.__metadata("design:type", Function),
788
- tslib_1.__metadata("design:paramtypes", [Object]),
789
- tslib_1.__metadata("design:returntype", Promise)
790
- ], Sandbox.prototype, "setLabels", null);
791
- tslib_1.__decorate([
792
- (0, otel_decorator_1.WithInstrumentation)(),
793
- tslib_1.__metadata("design:type", Function),
794
- tslib_1.__metadata("design:paramtypes", [Object]),
795
- tslib_1.__metadata("design:returntype", Promise)
796
- ], Sandbox.prototype, "start", null);
797
- tslib_1.__decorate([
798
- (0, otel_decorator_1.WithInstrumentation)(),
799
- tslib_1.__metadata("design:type", Function),
800
- tslib_1.__metadata("design:paramtypes", [Object, Object]),
801
- tslib_1.__metadata("design:returntype", Promise)
802
- ], Sandbox.prototype, "stop", null);
803
- tslib_1.__decorate([
804
- (0, otel_decorator_1.WithInstrumentation)(),
805
- tslib_1.__metadata("design:type", Function),
806
- tslib_1.__metadata("design:paramtypes", [Object, Object]),
807
- tslib_1.__metadata("design:returntype", Promise)
808
- ], Sandbox.prototype, "_experimental_fork", null);
809
- tslib_1.__decorate([
810
- (0, otel_decorator_1.WithInstrumentation)(),
811
- tslib_1.__metadata("design:type", Function),
812
- tslib_1.__metadata("design:paramtypes", [String, Object]),
813
- tslib_1.__metadata("design:returntype", Promise)
814
- ], Sandbox.prototype, "_experimental_createSnapshot", null);
815
- tslib_1.__decorate([
816
- (0, otel_decorator_1.WithInstrumentation)(),
817
- tslib_1.__metadata("design:type", Function),
818
- tslib_1.__metadata("design:paramtypes", [Object]),
819
- tslib_1.__metadata("design:returntype", Promise)
820
- ], Sandbox.prototype, "delete", null);
821
- tslib_1.__decorate([
822
- (0, otel_decorator_1.WithInstrumentation)(),
823
- tslib_1.__metadata("design:type", Function),
824
- tslib_1.__metadata("design:paramtypes", [Object]),
825
- tslib_1.__metadata("design:returntype", Promise)
826
- ], Sandbox.prototype, "waitUntilStarted", null);
827
- tslib_1.__decorate([
828
- (0, otel_decorator_1.WithInstrumentation)(),
829
- tslib_1.__metadata("design:type", Function),
830
- tslib_1.__metadata("design:paramtypes", [Object]),
831
- tslib_1.__metadata("design:returntype", Promise)
832
- ], Sandbox.prototype, "waitUntilStopped", null);
833
- tslib_1.__decorate([
834
- (0, otel_decorator_1.WithInstrumentation)(),
835
- tslib_1.__metadata("design:type", Function),
836
- tslib_1.__metadata("design:paramtypes", []),
837
- tslib_1.__metadata("design:returntype", Promise)
838
- ], Sandbox.prototype, "refreshData", null);
839
- tslib_1.__decorate([
840
- (0, otel_decorator_1.WithInstrumentation)(),
841
- tslib_1.__metadata("design:type", Function),
842
- tslib_1.__metadata("design:paramtypes", [Number]),
843
- tslib_1.__metadata("design:returntype", Promise)
844
- ], Sandbox.prototype, "setAutostopInterval", null);
845
- tslib_1.__decorate([
846
- (0, otel_decorator_1.WithInstrumentation)(),
847
- tslib_1.__metadata("design:type", Function),
848
- tslib_1.__metadata("design:paramtypes", [Number]),
849
- tslib_1.__metadata("design:returntype", Promise)
850
- ], Sandbox.prototype, "setAutoArchiveInterval", null);
851
- tslib_1.__decorate([
852
- (0, otel_decorator_1.WithInstrumentation)(),
853
- tslib_1.__metadata("design:type", Function),
854
- tslib_1.__metadata("design:paramtypes", [Number]),
855
- tslib_1.__metadata("design:returntype", Promise)
856
- ], Sandbox.prototype, "setAutoDeleteInterval", null);
857
- tslib_1.__decorate([
858
- (0, otel_decorator_1.WithInstrumentation)(),
859
- tslib_1.__metadata("design:type", Function),
860
- tslib_1.__metadata("design:paramtypes", [Object]),
861
- tslib_1.__metadata("design:returntype", Promise)
862
- ], Sandbox.prototype, "updateNetworkSettings", null);
863
- tslib_1.__decorate([
864
- (0, otel_decorator_1.WithInstrumentation)(),
865
- tslib_1.__metadata("design:type", Function),
866
- tslib_1.__metadata("design:paramtypes", [Number]),
867
- tslib_1.__metadata("design:returntype", Promise)
868
- ], Sandbox.prototype, "getPreviewLink", null);
869
- tslib_1.__decorate([
870
- (0, otel_decorator_1.WithInstrumentation)(),
871
- tslib_1.__metadata("design:type", Function),
872
- tslib_1.__metadata("design:paramtypes", []),
873
- tslib_1.__metadata("design:returntype", Promise)
874
- ], Sandbox.prototype, "archive", null);
875
- tslib_1.__decorate([
876
- (0, otel_decorator_1.WithInstrumentation)(),
877
- tslib_1.__metadata("design:type", Function),
878
- tslib_1.__metadata("design:paramtypes", [Object, Object]),
879
- tslib_1.__metadata("design:returntype", Promise)
880
- ], Sandbox.prototype, "resize", null);
881
- tslib_1.__decorate([
882
- (0, otel_decorator_1.WithInstrumentation)(),
883
- tslib_1.__metadata("design:type", Function),
884
- tslib_1.__metadata("design:paramtypes", [Object]),
885
- tslib_1.__metadata("design:returntype", Promise)
886
- ], Sandbox.prototype, "waitForResizeComplete", null);
887
- tslib_1.__decorate([
888
- (0, otel_decorator_1.WithInstrumentation)(),
889
- tslib_1.__metadata("design:type", Function),
890
- tslib_1.__metadata("design:paramtypes", [Number]),
891
- tslib_1.__metadata("design:returntype", Promise)
892
- ], Sandbox.prototype, "createSshAccess", null);
893
- tslib_1.__decorate([
894
- (0, otel_decorator_1.WithInstrumentation)(),
895
- tslib_1.__metadata("design:type", Function),
896
- tslib_1.__metadata("design:paramtypes", [String]),
897
- tslib_1.__metadata("design:returntype", Promise)
898
- ], Sandbox.prototype, "revokeSshAccess", null);
899
- tslib_1.__decorate([
900
- (0, otel_decorator_1.WithInstrumentation)(),
901
- tslib_1.__metadata("design:type", Function),
902
- tslib_1.__metadata("design:paramtypes", [String]),
903
- tslib_1.__metadata("design:returntype", Promise)
904
- ], Sandbox.prototype, "validateSshAccess", null);
905
840
  //# sourceMappingURL=Sandbox.js.map