Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Improve reconnect type when inheriting from sandbox; Cleanup

Tomas Valenta30887f6f7860
diff --git a/.changeset/small-melons-raise.md b/.changeset/small-melons-raise.md
new file mode 100644
index 000000000..ebe0c9cdf
--- /dev/null
+++ b/.changeset/small-melons-raise.md
@@ -0,0 +1,5 @@
+---
+"@e2b/sdk": patch
+---
+
+Improve sandbox reconnect inheriting
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 6ebc1645d..fecf0b8a8 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -79,4 +79,4 @@
// "python.analysis.inlayHints.variableTypes": true,
// "python.analysis.autoImportCompletions": true,
// "python.analysis.completeFunctionParens": true
-}
+}
\ No newline at end of file
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 8bbe0fa62..4438e3b3a 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -20,6 +20,9 @@ export type DownloadFileFormat =
| 'arraybuffer'
| 'text';
+
+type ConstructorType = new (...args: any[]) => any;
+
export interface SandboxOpts extends SandboxConnectionOpts {
onScanPorts?: ScanOpenPortsHandler;
/** Timeout for sandbox to start */
@@ -453,7 +456,7 @@ export class Sandbox extends SandboxConnection {
* const sandbox = await Sandbox.create()
* ```
*/
- static async create<S extends Sandbox>(): Promise<S>;
+ static async create<S extends ConstructorType>(this: S): Promise<InstanceType<S>>;
/**
* Creates a new Sandbox from the template with the specified ID.
* @param template Sandbox template ID or name
@@ -464,7 +467,7 @@ export class Sandbox extends SandboxConnection {
* const sandbox = await Sandbox.create("sandboxTemplateID")
* ```
*/
- static async create<S extends Sandbox>(template: string): Promise<S>;
+ static async create<S extends ConstructorType>(this: S, template: string): Promise<InstanceType<S>>;
/**
* Creates a new Sandbox from the specified options.
* @param opts Sandbox options
@@ -478,7 +481,7 @@ export class Sandbox extends SandboxConnection {
* })
* ```
*/
- static async create<S extends Sandbox>(opts: SandboxOpts): Promise<S>;
+ static async create<S extends ConstructorType>(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 Sandbox(opts)
@@ -503,7 +506,7 @@ export class Sandbox extends SandboxConnection {
* const reconnectedSandbox = await Sandbox.reconnect(sandboxID)
* ```
*/
- static async reconnect<S extends Sandbox>(sandboxID: string): Promise<S>;
+ static async reconnect<S extends ConstructorType>(this: S, sandboxID: string): Promise<InstanceType<S>>;
/**
* Reconnects to an existing Sandbox.
* @param opts Sandbox options
@@ -522,8 +525,8 @@ export class Sandbox extends SandboxConnection {
* })
* ```
*/
- static async reconnect<S extends Sandbox>(opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<S>;
- static async reconnect<S extends Sandbox>(sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<S> {
+ static async reconnect<S extends ConstructorType>(this: S, opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<InstanceType<S>>;
+ static async reconnect<S extends ConstructorType>(this: S, sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<InstanceType<S>> {
let sandboxID: string
let opts: SandboxOpts
if (typeof sandboxIDorOpts === 'string') {
@@ -539,7 +542,7 @@ export class Sandbox extends SandboxConnection {
const clientID = instanceIDAndClientID[1]
opts.__sandbox = { instanceID, clientID, envID: 'unknown' }
- const sandbox = new this(opts) as S
+ const sandbox = new this(opts)
await sandbox._open({ timeout: opts?.timeout })
return sandbox
diff --git a/packages/js-sdk/src/templates/dataAnalysis.ts b/packages/js-sdk/src/templates/dataAnalysis.ts
index 0365bb1da..f2764f077 100644
--- a/packages/js-sdk/src/templates/dataAnalysis.ts
+++ b/packages/js-sdk/src/templates/dataAnalysis.ts
@@ -23,26 +23,6 @@ export class DataAnalysis extends Sandbox {
super({ template: DataAnalysis.template, ...opts })
}
- /**
- * Reconnects to an existing Sandbox.
- * @param sandboxID Sandbox ID
- * @returns Sandbox
- */
- static async reconnect<S extends DataAnalysis>(sandboxID: string): Promise<S>;
- /**
- * Reconnects to an existing Sandbox with the specified options.
- * @param opts Sandbox options
- * @returns Sandbox
- */
- static async reconnect<S extends DataAnalysis>(opts: Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }): Promise<S>;
- static async reconnect(sandboxIDorOpts: string | Omit<SandboxOpts, 'id' | 'template'> & { sandboxID: string }) {
- if (typeof sandboxIDorOpts === 'string') {
- sandboxIDorOpts = { sandboxID: sandboxIDorOpts }
- }
-
- return super.reconnect(sandboxIDorOpts)
- }
-
/**
* Creates a new Sandbox from the template.
* @returns New Sandbox