-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathconnectionConfig.ts
More file actions
426 lines (369 loc) · 12 KB
/
Copy pathconnectionConfig.ts
File metadata and controls
426 lines (369 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { Logger } from './logs'
import { getEnvVar, version } from './api/metadata'
import { runtime } from './utils'
// Remove once all deployments support sandbox subdomains
const supportedDomains = ['e2b.app', 'e2b.dev', 'e2b.pro', 'e2b-staging.dev']
export const REQUEST_TIMEOUT_MS = 60_000 // 60 seconds
export const DEFAULT_SANDBOX_TIMEOUT_MS = 300_000 // 300 seconds
// Default timeout for streaming file transfers (uploads/downloads). A streamed
// body can take far longer than a regular request, so it must not inherit the
// short `REQUEST_TIMEOUT_MS`.
export const FILE_TIMEOUT_MS = 3_600_000 // 1 hour
export const KEEPALIVE_PING_INTERVAL_SEC = 50 // 50 seconds
export const KEEPALIVE_PING_HEADER = 'Keepalive-Ping-Interval'
/**
* Connection options for requests to the API.
*/
export interface ConnectionOpts {
/**
* E2B API key to use for authentication.
*
* @default E2B_API_KEY // environment variable
*/
apiKey?: string
/**
* Whether to validate the format of the E2B API key on the client side.
* Disable this when your deployment issues API keys that don't match the
* default `e2b_` format.
*
* @default E2B_VALIDATE_API_KEY // environment variable or `true`
*/
validateApiKey?: boolean
/**
* E2B access token to use for authentication.
*
* @deprecated Pass the token through `apiHeaders` instead, e.g.
* `apiHeaders: { Authorization: \`Bearer ${token}\` }`.
*
* @default E2B_ACCESS_TOKEN // environment variable
*/
accessToken?: string
/**
* Domain to use for the API.
*
* @default E2B_DOMAIN // environment variable or `e2b.app`
*/
domain?: string
/**
* API Url to use for the API.
* @internal
* @default E2B_API_URL // environment variable or `https://api.${domain}`
*/
apiUrl?: string
/**
* Sandbox Url to use for the API.
* @internal
* @default E2B_SANDBOX_URL // environment variable, `https://sandbox.${domain}`
*/
sandboxUrl?: string
/**
* If true the SDK starts in the debug mode and connects to the local envd API server.
* @internal
* @default E2B_DEBUG // environment variable or `false`
*/
debug?: boolean
/**
* Timeout for requests to the API in **milliseconds**.
*
* @default 60_000 // 60 seconds
*/
requestTimeoutMs?: number
/**
* Logger to use for logging messages. It can accept any object that implements `Logger` interface—for example, {@link console}.
*/
logger?: Logger
/**
* Additional headers to send with the request.
*
* @deprecated Use `apiHeaders` instead.
*/
headers?: Record<string, string>
/**
* Proxy URL to use for requests. In case of a sandbox it applies to all
* requests made to the returned sandbox.
*
* @example 'http://user:pass@127.0.0.1:8080'
*/
proxy?: string
/**
* Additional headers to send with E2B API requests.
*/
apiHeaders?: Record<string, string>
/**
* Integration wrapping the E2B SDK, appended to the `User-Agent`.
*
* @example 'e2b-code-interpreter/0.1.0'
*/
integration?: string
/**
* An optional `AbortSignal` that can be used to cancel the in-flight request.
* When the signal is aborted, the underlying `fetch` is aborted and the
* returned promise rejects with an `AbortError`.
*/
signal?: AbortSignal
}
/**
* Build an `AbortSignal` that combines an optional request-timeout signal
* (via `AbortSignal.timeout`) with an optional user-provided signal.
*
* Returns `undefined` when neither input would produce a signal.
*
* @internal
*/
export function buildRequestSignal(
requestTimeoutMs: number | undefined,
userSignal: AbortSignal | undefined
): AbortSignal | undefined {
// `0` (and `undefined`) disable the request timeout.
const timeoutSignal = requestTimeoutMs
? AbortSignal.timeout(requestTimeoutMs)
: undefined
if (timeoutSignal && userSignal) {
return AbortSignal.any([timeoutSignal, userSignal])
}
return timeoutSignal ?? userSignal
}
/**
* Set up an internal `AbortController` for a streaming request.
*
* Until `clearStartTimeout` is called, the controller aborts when either
* - the optional user signal aborts, or
* - the optional request timeout elapses (used to bound the initial
* handshake; long-lived streams should call `clearStartTimeout` once
* the handshake succeeds).
*
* The user-signal listener stays attached for the full stream lifetime
* so the caller can cancel a long-running stream by aborting the signal.
*
* `cleanup` is idempotent and detaches the listener, clears the handshake
* timer (if still pending), and aborts the controller. Call it when the
* stream finishes or when startup fails.
*
* @internal
*/
export function setupRequestController(
requestTimeoutMs: number | undefined,
userSignal: AbortSignal | undefined
): {
controller: AbortController
clearStartTimeout: () => void
cleanup: () => void
} {
const controller = new AbortController()
const onUserAbort = () => controller.abort(userSignal?.reason)
if (userSignal) {
if (userSignal.aborted) {
controller.abort(userSignal.reason)
} else {
userSignal.addEventListener('abort', onUserAbort, { once: true })
}
}
let reqTimeout: ReturnType<typeof setTimeout> | undefined = requestTimeoutMs
? setTimeout(
() =>
controller.abort(
new DOMException(
`Request handshake timed out after ${requestTimeoutMs}ms`,
'TimeoutError'
)
),
requestTimeoutMs
)
: undefined
const clearStartTimeout = () => {
if (reqTimeout) {
clearTimeout(reqTimeout)
reqTimeout = undefined
}
}
let cleaned = false
const cleanup = () => {
if (cleaned) return
cleaned = true
userSignal?.removeEventListener('abort', onUserAbort)
clearStartTimeout()
controller.abort()
}
return { controller, clearStartTimeout, cleanup }
}
// GC safety net for streamed reads: if the consumer drops a streamed response
// body without reading it to completion or cancelling it, the registered
// cleanup releases the underlying connection when the stream is garbage
// collected. This mirrors the Python SDK's `weakref.finalize` on
// `FileStreamReader`. The held value is the cleanup function, which must not
// reference the stream itself or it would never be collected.
const streamReadFinalizers = new FinalizationRegistry<() => void>((cleanup) =>
cleanup()
)
/**
* Wrap a streaming response body so its pooled connection is released when the
* stream is fully read, cancelled, errors, or (as a GC safety net) abandoned.
*
* The request timeout configured via {@link setupRequestController} bounds only
* the initial handshake; this clears that timeout so consuming the body is not
* killed by it. Call once the handshake has succeeded (after error handling).
*
* @internal
*/
export function wrapStreamWithConnectionCleanup(
body: ReadableStream<Uint8Array> | null,
{
clearStartTimeout,
cleanup,
}: { clearStartTimeout: () => void; cleanup: () => void }
): ReadableStream<Uint8Array> {
clearStartTimeout()
if (!body) {
cleanup()
return new Blob([]).stream()
}
const reader = body.getReader()
const unregisterToken = {}
// Detach the GC finalizer and release the connection. Idempotent via
// `cleanup`, so it's safe to call from multiple stream callbacks.
const release = () => {
streamReadFinalizers.unregister(unregisterToken)
cleanup()
}
const stream = new ReadableStream<Uint8Array>({
async pull(streamController) {
try {
const { done, value } = await reader.read()
if (done) {
streamController.close()
release()
} else {
streamController.enqueue(value)
}
} catch (err) {
release()
streamController.error(err)
}
},
async cancel(reason) {
try {
await reader.cancel(reason)
} finally {
release()
}
},
})
// Release the connection if the consumer abandons the stream without
// reading it to completion or cancelling it.
streamReadFinalizers.register(stream, cleanup, unregisterToken)
return stream
}
function buildUserAgent(integration?: string) {
const userAgentParts = [`e2b-js-sdk/${version}`]
if (integration) {
userAgentParts.push(integration)
}
return userAgentParts.join(' ')
}
/**
* Configuration for connecting to the API.
*/
export class ConnectionConfig {
public static envdPort = 49983
readonly debug: boolean
readonly domain: string
readonly apiUrl: string
readonly sandboxUrl?: string
readonly logger?: Logger
readonly requestTimeoutMs: number
readonly apiKey?: string
readonly validateApiKey: boolean
/**
* @deprecated Pass the token through `apiHeaders` instead.
*/
readonly accessToken?: string
readonly integration?: string
readonly headers?: Record<string, string>
readonly proxy?: string
constructor(opts?: ConnectionOpts) {
this.apiKey = opts?.apiKey || ConnectionConfig.apiKey
this.validateApiKey =
opts?.validateApiKey ?? ConnectionConfig.validateApiKey
this.debug = opts?.debug ?? ConnectionConfig.debug
this.domain = opts?.domain || ConnectionConfig.domain
this.accessToken = opts?.accessToken || ConnectionConfig.accessToken
this.requestTimeoutMs = opts?.requestTimeoutMs ?? REQUEST_TIMEOUT_MS
this.logger = opts?.logger
this.integration = opts?.integration
this.headers = { ...(opts?.headers ?? {}), ...(opts?.apiHeaders ?? {}) }
this.headers['User-Agent'] = buildUserAgent(this.integration)
this.proxy = opts?.proxy
this.apiUrl =
opts?.apiUrl ||
ConnectionConfig.apiUrl ||
(this.debug ? 'http://localhost:3000' : `https://api.${this.domain}`)
this.sandboxUrl = opts?.sandboxUrl || ConnectionConfig.sandboxUrl
}
private static get domain() {
return getEnvVar('E2B_DOMAIN') || 'e2b.app'
}
private static get apiUrl() {
return getEnvVar('E2B_API_URL')
}
private static get sandboxUrl() {
return getEnvVar('E2B_SANDBOX_URL')
}
private static get debug() {
return (getEnvVar('E2B_DEBUG') || 'false').toLowerCase() === 'true'
}
private static get apiKey() {
return getEnvVar('E2B_API_KEY')
}
private static get validateApiKey() {
return (
(getEnvVar('E2B_VALIDATE_API_KEY') || 'true').toLowerCase() !== 'false'
)
}
private static get accessToken() {
return getEnvVar('E2B_ACCESS_TOKEN')
}
getSignal(requestTimeoutMs?: number, signal?: AbortSignal) {
return buildRequestSignal(requestTimeoutMs ?? this.requestTimeoutMs, signal)
}
getSandboxUrl(
sandboxId: string,
opts: { sandboxDomain: string; envdPort: number }
) {
if (this.sandboxUrl) {
return this.sandboxUrl
}
if (this.debug) {
return `http://${this.getHost(sandboxId, opts.envdPort, opts.sandboxDomain)}`
}
const sandboxDomain = opts.sandboxDomain ?? this.domain
// The stable sandbox host is only guaranteed for E2B prod; the various other hosted domains may not serve sandbox.<domain> yet and will follow up once those are updated.
// Issue with cors from browser so holding off on using in browser as well.
if (runtime !== 'browser' && supportedDomains.includes(sandboxDomain)) {
return `https://sandbox.${sandboxDomain}`
}
return `https://${this.getHost(sandboxId, opts.envdPort, sandboxDomain)}`
}
getSandboxDirectUrl(
sandboxId: string,
opts: { sandboxDomain: string; envdPort: number }
) {
if (this.sandboxUrl) {
return this.sandboxUrl
}
if (this.debug) {
return `http://${this.getHost(sandboxId, opts.envdPort, opts.sandboxDomain)}`
}
return `https://${this.getHost(sandboxId, opts.envdPort, opts.sandboxDomain)}`
}
getHost(sandboxId: string, port: number, sandboxDomain: string) {
if (this.debug) {
return `localhost:${port}`
}
return `${port}-${sandboxId}.${sandboxDomain ?? this.domain}`
}
}
/**
* User used for the operation in the sandbox.
*/
export const defaultUsername: Username = 'user'
export type Username = string