-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathconnectionConfig.ts
More file actions
302 lines (261 loc) · 8.53 KB
/
Copy pathconnectionConfig.ts
File metadata and controls
302 lines (261 loc) · 8.53 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
import { Logger } from './logs'
import { getEnvVar, version } from './api/metadata'
import { runtime } from './utils'
import { resolveMaxRetries } from './retry'
// 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
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
/**
* E2B access token to use for authentication.
*
* @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
/**
* Number of times to retry a request after a transient failure (e.g. a
* network error, a `429` rate-limit, or a `502`/`503`/`504`). Retries use
* exponential backoff with jitter and honor a server-provided `Retry-After`
* header. Non-idempotent requests (e.g. creating a sandbox) are only retried
* when the server provably did not process the request (e.g. throttling, a
* refused connection, or a DNS failure), avoiding duplicate side effects.
*
* Set to `0` to disable retries.
*
* @default E2B_MAX_RETRIES // environment variable or `3`
*/
retries?: 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.
*/
headers?: Record<string, 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 {
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 }
}
/**
* 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 retries: number
readonly apiKey?: string
readonly accessToken?: string
readonly headers?: Record<string, string>
constructor(opts?: ConnectionOpts) {
this.apiKey = opts?.apiKey || ConnectionConfig.apiKey
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.retries = resolveMaxRetries(opts?.retries)
this.logger = opts?.logger
this.headers = opts?.headers || {}
this.headers['User-Agent'] = `e2b-js-sdk/${version}`
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 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