Skip to content

Commit

Permalink
Enforce calling create method in JS SDK (#374)
Browse files Browse the repository at this point in the history
This prevents to accidentally use `new Sandbox()`, which doesn't
initialize `Sandbox` class correctly
  • Loading branch information
jakubno committed May 9, 2024
2 parents e801c3b + ca466f8 commit 24d9bcc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 77 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-seals-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"e2b": patch
---

Enforce calling create() method in JS SDK
8 changes: 4 additions & 4 deletions packages/js-sdk/src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export class Sandbox extends SandboxConnection {
* @internal
* @access protected
*/
constructor(opts?: SandboxOpts) {
constructor(opts?: SandboxOpts, protected createCalled: boolean = false) {
opts = opts || {}
super(opts)
super(opts, createCalled)
this.onScanPorts = opts.onScanPorts

// Init Filesystem handler
Expand Down Expand Up @@ -462,7 +462,7 @@ export class Sandbox extends SandboxConnection {
static async create<S extends typeof Sandbox>(this: S, opts: SandboxOpts): Promise<InstanceType<S>>
static async create(optsOrTemplate?: string | SandboxOpts) {
const opts: SandboxOpts | undefined = typeof optsOrTemplate === 'string' ? { template: optsOrTemplate } : optsOrTemplate
const sandbox = new this(opts)
const sandbox = new this(opts, true)
await sandbox._open({ timeout: opts?.timeout })

return sandbox
Expand Down Expand Up @@ -520,7 +520,7 @@ export class Sandbox extends SandboxConnection {
const clientID = sandboxIDAndClientID[1]
opts.__sandbox = { sandboxID, clientID, templateID: 'unknown' }

const sandbox = new this(opts) as InstanceType<S>
const sandbox = new this(opts, true) as InstanceType<S>
await sandbox._open({ timeout: opts?.timeout })

return sandbox
Expand Down
Loading

0 comments on commit 24d9bcc

Please sign in to comment.