Skip to content

Commit 1b20fee

Browse files
committed
Beta submodule in JS SDK
1 parent ed08d1b commit 1b20fee

13 files changed

Lines changed: 1005 additions & 171 deletions

File tree

packages/js-sdk/src/api/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class ApiClient {
6161
}),
6262
...config.headers,
6363
},
64+
querySerializer: {
65+
array: {
66+
style: 'form',
67+
explode: false,
68+
},
69+
},
6470
})
6571

6672
if (config.logger) {

packages/js-sdk/src/api/schema.gen.ts

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/js-sdk/src/connectionConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Logger } from './logs'
22
import { getEnvVar, version } from './api/metadata'
33

4-
const REQUEST_TIMEOUT_MS = 60_000 // 60 seconds
4+
export const REQUEST_TIMEOUT_MS = 60_000 // 60 seconds
55
export const KEEPALIVE_PING_INTERVAL_SEC = 50 // 50 seconds
66

77
export const KEEPALIVE_PING_HEADER = 'Keepalive-Ping-Interval'

packages/js-sdk/src/sandbox/index.ts

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,69 @@ export class Sandbox extends SandboxApi {
209209
this.pty = new Pty(rpcTransport, this.connectionConfig)
210210
}
211211

212+
static get beta() {
213+
return {
214+
/**
215+
* Resume the sandbox.
216+
*
217+
* The **default sandbox timeout of 300 seconds** ({@link Sandbox.defaultSandboxTimeoutMs}) will be used for the resumed sandbox.
218+
* If you pass a custom timeout in the `opts` parameter via {@link SandboxOpts.timeoutMs} property, it will be used instead.
219+
*
220+
* @param sandboxId sandbox ID.
221+
* @param opts connection options.
222+
*
223+
* @returns a running sandbox instance.
224+
*/
225+
resume: Sandbox.resumeSandbox.bind(Sandbox),
226+
/**
227+
* Pause a sandbox by its ID.
228+
*
229+
* @param sandboxId sandbox ID.
230+
* @param opts connection options.
231+
*
232+
* @returns sandbox ID that can be used to resume the sandbox.
233+
*/
234+
pause: Sandbox.pauseSandbox.bind(Sandbox),
235+
}
236+
}
237+
238+
get beta() {
239+
return {
240+
/**
241+
* Resume the sandbox.
242+
*
243+
* The **default sandbox timeout of 300 seconds** ({@link Sandbox.defaultSandboxTimeoutMs}) will be used for the resumed sandbox.
244+
* If you pass a custom timeout in the `opts` parameter via {@link SandboxOpts.timeoutMs} property, it will be used instead.
245+
*
246+
* @param opts connection options.
247+
*
248+
* @returns a running sandbox instance.
249+
*/
250+
resume: async (opts?: Omit<SandboxOpts, 'metadata' | 'envs'>) => {
251+
const timeoutMs = opts?.timeoutMs ?? Sandbox.defaultSandboxTimeoutMs
252+
return await Sandbox.resumeSandbox(this.sandboxId, timeoutMs, {
253+
...this.connectionConfig,
254+
...opts,
255+
})
256+
},
257+
/**
258+
* Pause a sandbox by its ID.
259+
*
260+
* @param opts connection options.
261+
*
262+
* @returns sandbox ID that can be used to resume the sandbox.
263+
*/
264+
pause: async (
265+
opts?: Omit<SandboxOpts, 'metadata' | 'envs' | 'timeoutMs'>
266+
) => {
267+
return await Sandbox.pauseSandbox(this.sandboxId, {
268+
...this.connectionConfig,
269+
...opts,
270+
})
271+
},
272+
}
273+
}
274+
212275
/**
213276
* Create a new sandbox from the default `base` sandbox template.
214277
*
@@ -262,14 +325,15 @@ export class Sandbox extends SandboxApi {
262325
sandboxId: 'debug_sandbox_id',
263326
...config,
264327
}) as InstanceType<S>
265-
} else {
266-
const sandbox = await this.createSandbox(
267-
template,
268-
sandboxOpts?.timeoutMs ?? this.defaultSandboxTimeoutMs,
269-
sandboxOpts
270-
)
271-
return new this({ ...sandbox, ...config }) as InstanceType<S>
272328
}
329+
330+
const sandbox = await this.createSandbox(
331+
template,
332+
sandboxOpts?.timeoutMs ?? this.defaultSandboxTimeoutMs,
333+
sandboxOpts
334+
)
335+
336+
return new this({ ...sandbox, ...config }) as InstanceType<S>
273337
}
274338

275339
/**
@@ -295,8 +359,14 @@ export class Sandbox extends SandboxApi {
295359
sandboxId: string,
296360
opts?: Omit<SandboxOpts, 'metadata' | 'envs' | 'timeoutMs'>
297361
): Promise<InstanceType<S>> {
298-
const config = new ConnectionConfig(opts)
299-
const info = await this.getInfo(sandboxId, opts)
362+
const info = await this.getFullInfo(sandboxId, opts)
363+
364+
const config = new ConnectionConfig({
365+
...opts,
366+
// We don't want to pass headers to the connection config as they would then be inherited by all requests,
367+
// which can be confusing.
368+
headers: undefined,
369+
})
300370

301371
return new this({
302372
sandboxId,
@@ -424,7 +494,7 @@ export class Sandbox extends SandboxApi {
424494

425495
if (!useSignature && opts.useSignatureExpiration != undefined) {
426496
throw new Error(
427-
'Signature expiration can be used only when sandbox is created as secured.'
497+
'Signature expiration can be used only when sandbox is created as secured.'
428498
)
429499
}
430500

@@ -518,7 +588,9 @@ export class Sandbox extends SandboxApi {
518588
*
519589
* @returns List of sandbox metrics containing CPU, memory and disk usage information.
520590
*/
521-
async getMetrics(opts?: Pick<SandboxMetricsOpts, 'start' | 'end' | 'requestTimeoutMs'>) {
591+
async getMetrics(
592+
opts?: Pick<SandboxMetricsOpts, 'start' | 'end' | 'requestTimeoutMs'>
593+
) {
522594
if (this.envdApi.version) {
523595
if (compareVersions(this.envdApi.version, '0.1.5') < 0) {
524596
throw new SandboxError(

0 commit comments

Comments
 (0)