Skip to content

Commit dcb3d2c

Browse files
committed
Refactor method to use beta prefix
1 parent 9bed9e8 commit dcb3d2c

16 files changed

Lines changed: 716 additions & 932 deletions

File tree

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

Lines changed: 47 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { createRpcLogger } from '../logs'
1212
import { Commands, Pty } from './commands'
1313
import { Filesystem } from './filesystem'
1414
import {
15-
SandboxApi,
1615
SandboxOpts,
1716
SandboxConnectOpts,
18-
SandboxResumeOpts,
1917
SandboxMetricsOpts,
20-
SandboxApiBeta,
18+
SandboxApi,
19+
SandboxListOpts,
20+
SandboxPaginator,
2121
} from './sandboxApi'
2222
import { getSignature } from './signature'
2323
import { compareVersions } from 'compare-versions'
@@ -63,7 +63,6 @@ export interface SandboxUrlOpts {
6363
export class Sandbox extends SandboxApi {
6464
protected static readonly defaultTemplate: string = 'base'
6565
protected static readonly defaultSandboxTimeoutMs = DEFAULT_SANDBOX_TIMEOUT_MS
66-
private static _beta?: StaticBeta<typeof this>
6766

6867
/**
6968
* Module for interacting with the sandbox filesystem
@@ -94,7 +93,6 @@ export class Sandbox extends SandboxApi {
9493
private readonly envdApiUrl: string
9594
private readonly envdAccessToken?: string
9695
private readonly envdApi: EnvdApiClient
97-
private _beta?: InstanceBeta<this>
9896

9997
/**
10098
* Use {@link Sandbox.create} to create a new Sandbox instead.
@@ -176,39 +174,14 @@ export class Sandbox extends SandboxApi {
176174
}
177175

178176
/**
179-
* Module for beta features.
180-
*/
181-
static get beta() {
182-
const Ctor = this.constructor as typeof Sandbox
183-
if (!Ctor._beta) {
184-
// create a new one
185-
const betaClass = makeStaticBeta(this)
186-
return new betaClass() as unknown as StaticBeta<typeof this>
187-
}
188-
189-
// return the existing one
190-
return Ctor._beta
191-
}
192-
193-
/**
194-
* Module for beta features.
177+
* List all sandboxes.
178+
*
179+
* @param opts connection options.
180+
*
181+
* @returns paginator for listing sandboxes.
195182
*/
196-
get beta(): InstanceBeta<this> {
197-
if (!this._beta) {
198-
// Bound the resume and pause methods to the instance
199-
const resumeBound = this.resume.bind(this) as (
200-
opts?: SandboxResumeOpts
201-
) => Promise<this>
202-
203-
const pauseBound = this.pause.bind(this) as (
204-
opts?: ConnectionOpts
205-
) => Promise<boolean>
206-
207-
this._beta = new InstanceBeta<this>(resumeBound, pauseBound)
208-
}
209-
210-
// return the existing one
211-
return this._beta
183+
static list(opts?: SandboxListOpts): SandboxPaginator {
184+
return new SandboxPaginator(opts)
212185
}
213186

214187
/**
@@ -266,7 +239,7 @@ export class Sandbox extends SandboxApi {
266239
}) as InstanceType<S>
267240
}
268241

269-
const sandbox = await this.createSandbox(
242+
const sandbox = await SandboxApi.createSandbox(
270243
template,
271244
sandboxOpts?.timeoutMs ?? this.defaultSandboxTimeoutMs,
272245
sandboxOpts
@@ -298,7 +271,7 @@ export class Sandbox extends SandboxApi {
298271
sandboxId: string,
299272
opts?: SandboxConnectOpts
300273
): Promise<InstanceType<S>> {
301-
const info = await this.getFullInfo(sandboxId, opts)
274+
const info = await SandboxApi.getFullInfo(sandboxId, opts)
302275

303276
const config = new ConnectionConfig(opts)
304277

@@ -311,15 +284,32 @@ export class Sandbox extends SandboxApi {
311284
}) as InstanceType<S>
312285
}
313286

314-
static async betaCreate<S extends typeof Sandbox>(
287+
static async betaConnect<S extends typeof Sandbox>(
315288
this: S,
316-
templateOrOpts?: SandboxOpts | string,
317-
opts?: SandboxOpts
289+
sandboxId: string,
290+
timeoutMs: number,
291+
opts?: SandboxConnectOpts
318292
): Promise<InstanceType<S>> {
319-
if (typeof templateOrOpts === 'string') {
320-
return await this.create(templateOrOpts, opts)
293+
try {
294+
await SandboxApi.setTimeout(sandboxId, timeoutMs, opts)
295+
} catch (e) {
296+
await SandboxApi.resumeSandbox(sandboxId, opts)
297+
}
298+
299+
return await this.connect(sandboxId, opts)
300+
}
301+
302+
async betaConnect(
303+
timeoutMs: number,
304+
opts?: SandboxConnectOpts
305+
): Promise<this> {
306+
try {
307+
await SandboxApi.setTimeout(this.sandboxId, timeoutMs, opts)
308+
} catch (e) {
309+
await SandboxApi.resumeSandbox(this.sandboxId, opts)
321310
}
322-
return await this.create(templateOrOpts)
311+
312+
return this
323313
}
324314

325315
/**
@@ -401,7 +391,7 @@ export class Sandbox extends SandboxApi {
401391
return
402392
}
403393

404-
await Sandbox.setTimeout(this.sandboxId, timeoutMs, {
394+
await SandboxApi.setTimeout(this.sandboxId, timeoutMs, {
405395
...this.connectionConfig,
406396
...opts,
407397
})
@@ -418,7 +408,15 @@ export class Sandbox extends SandboxApi {
418408
return
419409
}
420410

421-
await Sandbox.kill(this.sandboxId, { ...this.connectionConfig, ...opts })
411+
await SandboxApi.kill(this.sandboxId, { ...this.connectionConfig, ...opts })
412+
}
413+
414+
/**
415+
* Pause the sandbox.
416+
*
417+
*/
418+
async betaPause(opts?: ConnectionOpts): Promise<boolean> {
419+
return await SandboxApi.betaPause(this.sandboxId, opts)
422420
}
423421

424422
/**
@@ -520,7 +518,7 @@ export class Sandbox extends SandboxApi {
520518
* @returns information about the sandbox
521519
*/
522520
async getInfo(opts?: Pick<SandboxOpts, 'requestTimeoutMs'>) {
523-
return await Sandbox.getInfo(this.sandboxId, {
521+
return await SandboxApi.getInfo(this.sandboxId, {
524522
...this.connectionConfig,
525523
...opts,
526524
})
@@ -549,25 +547,12 @@ export class Sandbox extends SandboxApi {
549547
}
550548
}
551549

552-
return await Sandbox.getMetrics(this.sandboxId, {
550+
return await SandboxApi.getMetrics(this.sandboxId, {
553551
...this.connectionConfig,
554552
...opts,
555553
})
556554
}
557555

558-
protected async resume<T extends typeof Sandbox>(
559-
this: InstanceType<T>,
560-
opts?: SandboxResumeOpts
561-
): Promise<InstanceType<T>> {
562-
const Ctor = this.constructor as T
563-
await SandboxApiBeta.resumeSandbox(this.sandboxId, opts)
564-
return await Ctor.connect(this.sandboxId, opts)
565-
}
566-
567-
private async pause(opts?: ConnectionOpts): Promise<boolean> {
568-
return await SandboxApiBeta.pauseSandbox(this.sandboxId, opts)
569-
}
570-
571556
private fileUrl(path?: string, username?: string) {
572557
const url = new URL('/files', this.envdApiUrl)
573558

@@ -579,143 +564,3 @@ export class Sandbox extends SandboxApi {
579564
return url.toString()
580565
}
581566
}
582-
583-
/**
584-
* Beta features for Sandbox instances
585-
*/
586-
class InstanceBeta<I extends Sandbox> {
587-
constructor(
588-
private resumeSandbox: (opts?: SandboxResumeOpts) => Promise<I>,
589-
private pauseSandbox: (opts?: ConnectionOpts) => Promise<boolean>
590-
) {}
591-
592-
/**
593-
* Resume the sandbox.
594-
*
595-
* The **default sandbox timeout of 300 seconds** ({@link Sandbox.defaultSandboxTimeoutMs}) will be used for the resumed sandbox.
596-
* If you pass a custom timeout in the `opts` parameter via {@link SandboxOpts.timeoutMs} property, it will be used instead.
597-
*
598-
* @param opts connection options.
599-
*
600-
* @returns a running sandbox instance.
601-
*/
602-
async resume(opts?: SandboxResumeOpts) {
603-
return await this.resumeSandbox(opts)
604-
}
605-
606-
/**
607-
* Pause a sandbox by its ID.
608-
*
609-
* @param opts connection options.
610-
*
611-
* @returns sandbox ID that can be used to resume the sandbox.
612-
*/
613-
async pause(opts?: ConnectionOpts) {
614-
return await this.pauseSandbox(opts)
615-
}
616-
}
617-
618-
/**
619-
* Beta features for static Sandbox methods
620-
*/
621-
interface StaticBeta<I extends typeof Sandbox> {
622-
/**
623-
**
624-
* Create a new sandbox from the specified sandbox template.
625-
*
626-
* @param opts connection options.
627-
*
628-
* @returns sandbox instance for the new sandbox.
629-
*
630-
* @example
631-
* ```ts
632-
* const sandbox = await Sandbox.create('<template-name-or-id>')
633-
* ```
634-
* @constructs Sandbox
635-
*/
636-
create(opts?: SandboxOpts): Promise<InstanceType<I>>
637-
/**
638-
* Create a new sandbox from the specified sandbox template.
639-
*
640-
* @param template sandbox template name or ID.
641-
* @param opts connection options.
642-
*
643-
* @returns sandbox instance for the new sandbox.
644-
*
645-
* @example
646-
* ```ts
647-
* const sandbox = await Sandbox.create('<template-name-or-id>')
648-
* ```
649-
* @constructs Sandbox
650-
*/
651-
create(template: string, opts?: SandboxOpts): Promise<InstanceType<I>>
652-
653-
/**
654-
* Resume the sandbox.
655-
*
656-
* The **default sandbox timeout of 300 seconds** ({@link Sandbox.defaultSandboxTimeoutMs}) will be used for the resumed sandbox.
657-
* If you pass a custom timeout in the `opts` parameter via {@link SandboxOpts.timeoutMs} property, it will be used instead.
658-
*
659-
* @param sandboxId sandbox ID.
660-
* @param opts connection options.
661-
*
662-
* @returns a running sandbox instance.
663-
*/
664-
resume(sandboxId: string, opts?: SandboxResumeOpts): Promise<InstanceType<I>>
665-
666-
/**
667-
* Pause a sandbox by its ID.
668-
*
669-
* @param sandboxId sandbox ID.
670-
* @param opts connection options.
671-
*
672-
* @returns sandbox ID that can be used to resume the sandbox.
673-
*/
674-
pause(sandboxId: string, opts?: ConnectionOpts): Promise<boolean>
675-
}
676-
677-
function makeStaticBeta<S extends typeof Sandbox>(Ctor: S) {
678-
return class {
679-
static async create(
680-
templateOrOpts?: SandboxOpts | string,
681-
opts?: SandboxOpts
682-
): Promise<InstanceType<S>> {
683-
if (typeof templateOrOpts === 'string') {
684-
return await Ctor.betaCreate(templateOrOpts, opts)
685-
}
686-
return await Ctor.betaCreate(templateOrOpts)
687-
}
688-
689-
/**
690-
* Resume the sandbox.
691-
*
692-
* The **default sandbox timeout of 300 seconds** ({@link Sandbox.defaultSandboxTimeoutMs}) will be used for the resumed sandbox.
693-
* If you pass a custom timeout in the `opts` parameter via {@link SandboxOpts.timeoutMs} property, it will be used instead.
694-
*
695-
* @param sandboxId sandbox ID.
696-
* @param opts connection options.
697-
*
698-
* @returns a running sandbox instance.
699-
*/
700-
async resume(
701-
sandboxId: string,
702-
opts?: SandboxResumeOpts
703-
): Promise<InstanceType<S>> {
704-
await SandboxApiBeta.resumeSandbox(sandboxId, opts)
705-
706-
return await Ctor.connect(sandboxId, opts)
707-
}
708-
709-
/**
710-
* Pause a sandbox by its ID.
711-
*
712-
* @param sandboxId sandbox ID.
713-
* @param opts connection options.
714-
*
715-
* @returns sandbox ID that can be used to resume the sandbox.
716-
*/
717-
async pause(sandboxId: string, opts?: ConnectionOpts): Promise<boolean> {
718-
return SandboxApiBeta.pauseSandbox(sandboxId, opts)
719-
}
720-
}
721-
}

0 commit comments

Comments
 (0)