Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Enforce calling create method in JS SDK

Jakub Novakd036a37c6f2b
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 87f8af81e..f12c6fe76 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -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
@@ -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
@@ -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
diff --git a/packages/js-sdk/src/sandbox/sandboxConnection.ts b/packages/js-sdk/src/sandbox/sandboxConnection.ts
index 86fcf0e9b..ab8b221da 100644
--- a/packages/js-sdk/src/sandbox/sandboxConnection.ts
+++ b/packages/js-sdk/src/sandbox/sandboxConnection.ts
@@ -111,7 +111,11 @@ export class SandboxConnection {
private readonly client: APIClient
// let's keep opts readonly, but public - for convenience, mainly when debugging
- protected constructor(readonly opts: SandboxConnectionOpts) {
+ protected constructor(readonly opts: SandboxConnectionOpts, protected createCalled: boolean) {
+ if (!createCalled) {
+ throw new Error('Cannot instantiate Sandbox directly, use a `create()` method instead.')
+ }
+
this.sandbox = opts.__sandbox
this.apiKey = getApiKey(opts.apiKey)