Skip to content

Commit 75b134e

Browse files
committed
support local dev for envd in typescript
1 parent 6e6791b commit 75b134e

File tree

6 files changed

+55
-51
lines changed

6 files changed

+55
-51
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module.exports = {
2222
quotes: ['error', 'single', { avoidEscape: true }],
2323
// No extra semicolon
2424
'@stylistic/ts/semi': ['error', 'never'],
25+
'object-curly-spacing': ['error', 'always'],
2526
},
2627
}

packages/cli/.envrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source_up
2+
3+
dotenv ../../../.env.local

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"commander": "^11.1.0",
8787
"console-table-printer": "^2.11.2",
8888
"dockerfile-ast": "^0.6.1",
89-
"e2b": "^2.3.4",
89+
"e2b": "^2.6.4",
9090
"handlebars": "^4.7.8",
9191
"inquirer": "^9.2.12",
9292
"open": "^9.1.0",

packages/js-sdk/src/connectionConfig.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Logger } from './logs'
2-
import { getEnvVar, version } from './api/metadata'
1+
import {Logger} from './logs'
2+
import {getEnvVar, version} from './api/metadata'
33
import {sandboxTest} from "../tests/setup";
44

55
export const REQUEST_TIMEOUT_MS = 60_000 // 60 seconds
@@ -69,6 +69,8 @@ export interface ConnectionOpts {
6969
* Configuration for connecting to the API.
7070
*/
7171
export class ConnectionConfig {
72+
public static envdPort = 49983
73+
7274
readonly debug: boolean
7375
readonly domain: string
7476
readonly apiUrl: string
@@ -127,12 +129,21 @@ export class ConnectionConfig {
127129
return timeout ? AbortSignal.timeout(timeout) : undefined
128130
}
129131

130-
getSandboxUrl(sandboxId: string, sandboxDomain: string): string {
131-
if (this.sandboxUrl) {
132-
return this.sandboxUrl
132+
getSandboxUrl(sandboxId: string, opts: { sandboxDomain: string, envdPort: number }) {
133+
const sandboxUrl = ConnectionConfig.sandboxUrl
134+
if (sandboxUrl) {
135+
return sandboxUrl
136+
}
137+
138+
return `${this.debug ? 'http' : 'https'}://${this.getHost(sandboxId, opts.envdPort, opts.sandboxDomain)}`
139+
}
140+
141+
getHost(sandboxId: string, port: number, sandboxDomain: string) {
142+
if (this.debug) {
143+
return `localhost:${port}`
133144
}
134145

135-
return `${this.debug ? "http" : "https"}://${this.getHost(sandboxId, sandboxDomain)}`
146+
return `${port}-${sandboxId}.${sandboxDomain ?? this.domain}`
136147
}
137148
}
138149

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,15 @@ export class Sandbox extends SandboxApi {
123123
this.sandboxDomain = opts.sandboxDomain ?? this.connectionConfig.domain
124124

125125
this.envdAccessToken = opts.envdAccessToken
126-
this.envdApiUrl = `${
127-
this.connectionConfig.debug ? 'http' : 'https'
128-
}://${this.getHost(this.envdPort)}`
126+
this.envdApiUrl = this.connectionConfig.getSandboxUrl(this.sandboxId, {
127+
sandboxDomain: this.sandboxDomain,
128+
envdPort: this.envdPort
129+
})
130+
131+
const sandboxHeaders = {
132+
'E2b-Sandbox-Id': this.sandboxId,
133+
'E2b-Sandbox-Port': this.envdPort.toString(),
134+
}
129135

130136
const rpcTransport = createConnectTransport({
131137
baseUrl: this.envdApiUrl,
@@ -141,6 +147,9 @@ export class Sandbox extends SandboxApi {
141147
new Headers(options?.headers).forEach((value, key) =>
142148
headers.append(key, value)
143149
)
150+
new Headers(sandboxHeaders).forEach((value, key) =>
151+
headers.append(key, value)
152+
)
144153

145154
if (this.envdAccessToken) {
146155
headers.append('X-Access-Token', this.envdAccessToken)
@@ -238,15 +247,15 @@ export class Sandbox extends SandboxApi {
238247
const { template, sandboxOpts } =
239248
typeof templateOrOpts === 'string'
240249
? {
241-
template: templateOrOpts,
242-
sandboxOpts: opts,
243-
}
250+
template: templateOrOpts,
251+
sandboxOpts: opts,
252+
}
244253
: {
245-
template: templateOrOpts?.mcp
246-
? this.defaultMcpTemplate
247-
: this.defaultTemplate,
248-
sandboxOpts: templateOrOpts,
249-
}
254+
template: templateOrOpts?.mcp
255+
? this.defaultMcpTemplate
256+
: this.defaultTemplate,
257+
sandboxOpts: templateOrOpts,
258+
}
250259

251260
const config = new ConnectionConfig(sandboxOpts)
252261
if (config.debug) {
@@ -333,15 +342,15 @@ export class Sandbox extends SandboxApi {
333342
const { template, sandboxOpts } =
334343
typeof templateOrOpts === 'string'
335344
? {
336-
template: templateOrOpts,
337-
sandboxOpts: opts,
338-
}
345+
template: templateOrOpts,
346+
sandboxOpts: opts,
347+
}
339348
: {
340-
template: templateOrOpts?.mcp
341-
? this.defaultMcpTemplate
342-
: this.defaultTemplate,
343-
sandboxOpts: templateOrOpts,
344-
}
349+
template: templateOrOpts?.mcp
350+
? this.defaultMcpTemplate
351+
: this.defaultTemplate,
352+
sandboxOpts: templateOrOpts,
353+
}
345354

346355
const config = new ConnectionConfig(sandboxOpts)
347356
if (config.debug) {
@@ -482,11 +491,7 @@ export class Sandbox extends SandboxApi {
482491
* ```
483492
*/
484493
getHost(port: number) {
485-
if (this.connectionConfig.debug) {
486-
return `localhost:${port}`
487-
}
488-
489-
return `${port}-${this.sandboxId}.${this.sandboxDomain}`
494+
return this.connectionConfig.getHost(this.sandboxId, port, this.sandboxDomain)
490495
}
491496

492497
/**
@@ -732,7 +737,7 @@ export class Sandbox extends SandboxApi {
732737
if (compareVersions(this.envdApi.version, '0.1.5') < 0) {
733738
throw new SandboxError(
734739
'You need to update the template to use the new SDK. ' +
735-
'You can do this by running `e2b template build` in the directory with the template.'
740+
'You can do this by running `e2b template build` in the directory with the template.'
736741
)
737742
}
738743

pnpm-lock.yaml

Lines changed: 3 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)