11import {
22 Sandbox as SandboxBase ,
33 SandboxOpts as SandboxOptsBase ,
4+ SandboxBetaCreateOpts as SandboxBetaCreateOptsBase ,
45 CommandHandle ,
56 CommandResult ,
67 CommandExitError ,
7- ConnectionConfig ,
88 TimeoutError ,
99} from 'e2b'
1010
@@ -116,6 +116,31 @@ export interface SandboxOpts extends SandboxOptsBase {
116116 display ?: string
117117}
118118
119+ /**
120+ * Configuration options for the Sandbox environment.
121+ * @interface SandboxOpts
122+ * @extends {SandboxOptsBase }
123+ */
124+ export interface SandboxBetaCreateOpts extends SandboxBetaCreateOptsBase {
125+ /**
126+ * The screen resolution in pixels, specified as [width, height].
127+ * @type {[number, number] }
128+ */
129+ resolution ?: [ number , number ]
130+
131+ /**
132+ * Dots per inch (DPI) setting for the display.
133+ * @type {number }
134+ */
135+ dpi ?: number
136+
137+ /**
138+ * Display identifier.
139+ * @type {string }
140+ */
141+ display ?: string
142+ }
143+
119144export class Sandbox extends SandboxBase {
120145 protected static override readonly defaultTemplate : string = 'desktop'
121146 private lastXfce4Pid : number | null = null
@@ -138,6 +163,7 @@ export class Sandbox extends SandboxBase {
138163 ) {
139164 super ( opts )
140165 }
166+
141167 /**
142168 * Create a new sandbox from the default `desktop` sandbox template.
143169 *
@@ -184,8 +210,6 @@ export class Sandbox extends SandboxBase {
184210 ? { template : templateOrOpts , sandboxOpts : opts }
185211 : { template : this . defaultTemplate , sandboxOpts : templateOrOpts }
186212
187- const config = new ConnectionConfig ( sandboxOpts )
188-
189213 // Add DISPLAY environment variable if not already set
190214 const display = opts ?. display || ':0'
191215 const sandboxOptsWithDisplay = {
@@ -196,46 +220,70 @@ export class Sandbox extends SandboxBase {
196220 } ,
197221 }
198222
199- let sbx
200- if ( config . debug ) {
201- sbx = new this ( {
202- sandboxId : 'desktop' ,
203- ...sandboxOptsWithDisplay ,
204- ...config ,
205- } ) as InstanceType < S >
206- } else {
207- const sandbox = await this . createSandbox (
208- template ,
209- sandboxOptsWithDisplay ?. timeoutMs ?? this . defaultSandboxTimeoutMs ,
210- sandboxOptsWithDisplay
211- )
212- sbx = new this ( {
213- ...sandbox ,
214- ...sandboxOptsWithDisplay ,
215- ...config ,
216- } ) as InstanceType < S >
217- }
223+ const sbx = await super . create ( template , sandboxOptsWithDisplay ) as InstanceType < S >
224+ await sbx . _start ( display , sandboxOptsWithDisplay )
218225
219- sbx . display = display
220- sbx . lastXfce4Pid = null
221- sbx . stream = new VNCServer ( sbx )
226+ return sbx
227+ }
222228
223- const [ width , height ] = sandboxOpts ?. resolution ?? [ 1024 , 768 ]
224- await sbx . commands . run (
225- `Xvfb ${ sbx . display } -ac -screen 0 ${ width } x${ height } x24 ` +
226- `-retro -dpi ${ sandboxOpts ?. dpi ?? 96 } -nolisten tcp -nolisten unix` ,
227- { background : true , timeoutMs : 0 }
228- )
229+ /**
230+ * Create a new sandbox from the default `desktop` sandbox template.
231+ *
232+ * @param opts connection options.
233+ *
234+ * @returns sandbox instance for the new sandbox.
235+ *
236+ * @example
237+ * ```ts
238+ * const sandbox = await Sandbox.create()
239+ * ```
240+ * @constructs Sandbox
241+ */
242+ static async betaCreate < S extends typeof Sandbox > (
243+ this : S ,
244+ opts ?: SandboxBetaCreateOpts
245+ ) : Promise < InstanceType < S > >
246+ /**
247+ * Create a new sandbox from the specified sandbox template.
248+ *
249+ * @param template sandbox template name or ID.
250+ * @param opts connection options.
251+ *
252+ * @returns sandbox instance for the new sandbox.
253+ *
254+ * @example
255+ * ```ts
256+ * const sandbox = await Sandbox.create('<template-name-or-id>')
257+ * ```
258+ * @constructs Sandbox
259+ */
260+ static async betaCreate < S extends typeof Sandbox > (
261+ this : S ,
262+ template : string ,
263+ opts ?: SandboxBetaCreateOpts
264+ ) : Promise < InstanceType < S > >
265+ static async betaCreate < S extends typeof Sandbox > (
266+ this : S ,
267+ templateOrOpts ?: SandboxBetaCreateOpts | string ,
268+ opts ?: SandboxOpts
269+ ) : Promise < InstanceType < S > > {
270+ const { template, sandboxOpts } =
271+ typeof templateOrOpts === 'string'
272+ ? { template : templateOrOpts , sandboxOpts : opts }
273+ : { template : this . defaultTemplate , sandboxOpts : templateOrOpts }
229274
230- let hasStarted = await sbx . waitAndVerify (
231- `xdpyinfo -display ${ sbx . display } ` ,
232- ( r : CommandResult ) => r . exitCode === 0
233- )
234- if ( ! hasStarted ) {
235- throw new TimeoutError ( 'Could not start Xvfb' )
275+ // Add DISPLAY environment variable if not already set
276+ const display = opts ?. display || ':0'
277+ const sandboxOptsWithDisplay = {
278+ ...sandboxOpts ,
279+ envs : {
280+ ...sandboxOpts ?. envs ,
281+ DISPLAY : display ,
282+ } ,
236283 }
237284
238- await sbx . startXfce4 ( )
285+ const sbx = await super . betaCreate ( template , sandboxOptsWithDisplay ) as InstanceType < S >
286+ await sbx . _start ( display , sandboxOptsWithDisplay )
239287
240288 return sbx
241289 }
@@ -593,6 +641,29 @@ export class Sandbox extends SandboxBase {
593641 timeoutMs : 0 ,
594642 } )
595643 }
644+
645+ protected async _start ( display :string , opts ?: SandboxOpts ) : Promise < void > {
646+ this . display = display
647+ this . lastXfce4Pid = null
648+ this . stream = new VNCServer ( this )
649+
650+ const [ width , height ] = opts ?. resolution ?? [ 1024 , 768 ]
651+ await this . commands . run (
652+ `Xvfb ${ display } -ac -screen 0 ${ width } x${ height } x24 ` +
653+ `-retro -dpi ${ opts ?. dpi ?? 96 } -nolisten tcp -nolisten unix` ,
654+ { background : true , timeoutMs : 0 }
655+ )
656+
657+ let hasStarted = await this . waitAndVerify (
658+ `xdpyinfo -display ${ display } ` ,
659+ ( r : CommandResult ) => r . exitCode === 0
660+ )
661+ if ( ! hasStarted ) {
662+ throw new TimeoutError ( 'Could not start Xvfb' )
663+ }
664+
665+ await this . startXfce4 ( )
666+ }
596667}
597668
598669interface VNCServerOptions {
0 commit comments