Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
fix(js-sdk): share lazy fetcher loading and drop the new Function import trick (#1607)
Mish Ushakovb5119539ca7e
diff --git a/.changeset/olive-pugs-refuse.md b/.changeset/olive-pugs-refuse.mdnew file mode 100644index 000000000..2f63509d9--- /dev/null+++ b/.changeset/olive-pugs-refuse.md@@ -0,0 +1,5 @@+---+'e2b': patch+---++Remove the `new Function('return import(...)')` trick from undici loading. `loadUndici` now uses the shared `dynamicImport` helper, whose dynamic import is kept opaque to downstream bundlers with `webpackIgnore`/`@vite-ignore` annotations instead of runtime code generation. Environments that disallow code generation from strings (CSP, `--disallow-code-generation-from-strings`) now load undici normally instead of silently falling back to the global fetch.diff --git a/.changeset/tidy-moons-shout.md b/.changeset/tidy-moons-shout.mdnew file mode 100644index 000000000..75f4699b3--- /dev/null+++ b/.changeset/tidy-moons-shout.md@@ -0,0 +1,5 @@+---+'e2b': patch+---++Fix the lazy fetcher loading in `api/http2.ts` and `envd/http2.ts`: a failed fetcher build is no longer cached forever (the next request retries instead of replaying the stale rejection), and the no-undici fallback now late-binds `globalThis.fetch` so fetch replacements installed after the first request (msw, instrumentation) are picked up. The previously duplicated loading logic is shared in `undici.ts`.diff --git a/packages/js-sdk/src/api/http2.ts b/packages/js-sdk/src/api/http2.tsindex f33241b84..2a46c3fcb 100644--- a/packages/js-sdk/src/api/http2.ts+++ b/packages/js-sdk/src/api/http2.ts@@ -1,11 +1,9 @@import { runtime } from '../utils'import { parseInflightLimitEnv, parsePositiveIntEnv } from './metadata'-import { limitConcurrency } from './inflight'import {- loadUndici,- toUndiciRequestInput,+ buildDispatchedFetch,+ createRuntimeFetch,type UndiciModule,- type UndiciRequestInit,} from '../undici'const DEFAULT_API_CONNECTION_LIMIT = 100@@ -40,64 +38,16 @@ export function createApiFetchForRuntime(loadUndici?: () => Promise<UndiciModule | undefined>} = {}): typeof fetch {- if (currentRuntime !== 'node') {- // Late-bind the global fetch: runtimes and tools (msw, instrumentation)- // may replace `globalThis.fetch` after this factory runs, and a fresh- // closure keeps the per-proxy cache entries distinct.- return ((input, init) => globalThis.fetch(input, init)) as typeof fetch- }-- let fetcherPromise: Promise<typeof fetch> | undefined-- return (async (input, init) => {- fetcherPromise ??= buildApiFetcher(options)- const fetcher = await fetcherPromise-- return fetcher(input, init)- }) as typeof fetch-}--async function buildApiFetcher(options: {- connectionLimit?: number- inflightLimit?: number- proxy?: string- loadUndici?: () => Promise<UndiciModule | undefined>-}): Promise<typeof fetch> {- const undici = await (options.loadUndici ?? loadUndici)()- const inflightLimit = options.inflightLimit ?? getApiInflightLimit()-- if (!undici) {- return limitConcurrency(fetch, inflightLimit)- }-- const { Agent, ProxyAgent, fetch: undiciFetch } = undici- const connections = options.connectionLimit ?? getApiConnectionLimit()- const dispatcher = options.proxy- ? new ProxyAgent({- uri: options.proxy,- allowH2: true,- connections,- proxyTunnel: true,- })- : new Agent({- allowH2: true,- connections,- })- const fetchWithDispatcher = undiciFetch as unknown as (- input: RequestInfo | URL,- init?: UndiciRequestInit- ) => Promise<Response>-- const wrapped: typeof fetch = ((input, init) => {- const request = toUndiciRequestInput(input, init)-- return fetchWithDispatcher(request.input, {- ...request.init,- dispatcher,+ // Defaults resolve inside the thunk so env vars are read lazily, at the+ // first request rather than at factory creation.+ return createRuntimeFetch(currentRuntime, () =>+ buildDispatchedFetch({+ connections: options.connectionLimit ?? getApiConnectionLimit(),+ inflightLimit: options.inflightLimit ?? getApiInflightLimit(),+ proxy: options.proxy,+ loadUndici: options.loadUndici,})- }) as typeof fetch-- return limitConcurrency(wrapped, inflightLimit)+ )}export function getApiConnectionLimit(): number {@@ -111,7 +61,7 @@ export function getApiConnectionLimit(): number {* Returns the configured max number of API requests that can be in flight at* once, or `0` to disable the cap.*- * Defaults to {@link DEFAULT_API_INFLIGHT_LIMIT} ({@link 1000}). Override via+ * Defaults to `1000` ({@link DEFAULT_API_INFLIGHT_LIMIT}). Override via* `E2B_API_INFLIGHT_REQUESTS` env var; set to `0` to disable the cap entirely.*/export function getApiInflightLimit(): number {diff --git a/packages/js-sdk/src/envd/http2.ts b/packages/js-sdk/src/envd/http2.tsindex edaa3a65a..db674df3c 100644--- a/packages/js-sdk/src/envd/http2.ts+++ b/packages/js-sdk/src/envd/http2.ts@@ -1,11 +1,9 @@import { runtime } from '../utils'import { parseInflightLimitEnv, parsePositiveIntEnv } from '../api/metadata'-import { limitConcurrency } from '../api/inflight'import {- loadUndici,- toUndiciRequestInput,+ buildDispatchedFetch,+ createRuntimeFetch,type UndiciModule,- type UndiciRequestInit,} from '../undici'type EnvdFetchOptions = {@@ -28,62 +26,14 @@ export function createEnvdFetchForRuntime(currentRuntime = runtime,options: EnvdFetchOptions = {}): typeof fetch {- if (currentRuntime !== 'node') {- // Late-bind the global fetch: runtimes and tools (msw, instrumentation)- // may replace `globalThis.fetch` after this factory runs, and a fresh- // closure keeps the per-proxy cache entries distinct.- return ((input, init) => globalThis.fetch(input, init)) as typeof fetch- }-- let fetcherPromise: Promise<typeof fetch> | undefined-- return (async (input, init) => {- fetcherPromise ??= buildEnvdFetcher(options)- const fetcher = await fetcherPromise-- return fetcher(input, init)- }) as typeof fetch-}--async function buildEnvdFetcher(- options: EnvdFetchOptions-): Promise<typeof fetch> {- const undici = await (options.loadUndici ?? loadUndici)()- const inflightLimit = options.inflightLimit ?? 0-- if (!undici) {- return limitConcurrency(fetch, inflightLimit)- }-- const { Agent, ProxyAgent, fetch: undiciFetch } = undici- const connections = options.connectionLimit ?? DEFAULT_ENVD_CONNECTION_LIMIT-- const dispatcher = options.proxy- ? new ProxyAgent({- uri: options.proxy,- allowH2: true,- connections,- proxyTunnel: true,- })- : new Agent({- allowH2: true,- connections,- })- const fetchWithDispatcher = undiciFetch as unknown as (- input: RequestInfo | URL,- init?: UndiciRequestInit- ) => Promise<Response>-- const wrapped: typeof fetch = ((input, init) => {- const request = toUndiciRequestInput(input, init)-- return fetchWithDispatcher(request.input, {- ...request.init,- dispatcher,+ return createRuntimeFetch(currentRuntime, () =>+ buildDispatchedFetch({+ connections: options.connectionLimit ?? DEFAULT_ENVD_CONNECTION_LIMIT,+ inflightLimit: options.inflightLimit ?? 0,+ proxy: options.proxy,+ loadUndici: options.loadUndici,})- }) as typeof fetch-- return limitConcurrency(wrapped, inflightLimit)+ )}export function createEnvdFetch(proxy?: string): typeof fetch {@@ -135,7 +85,7 @@ export function getEnvdRpcConnectionLimit(): number {* `files.read`/`files.write`) that can be in flight at once across all* sandboxes in this SDK process, or `0` to disable the cap.*- * Defaults to {@link DEFAULT_ENVD_INFLIGHT_LIMIT} ({@link 2000}). Override+ * Defaults to `2000` ({@link DEFAULT_ENVD_INFLIGHT_LIMIT}). Override* via `E2B_ENVD_INFLIGHT_REQUESTS` env var; set to `0` to disable the cap* entirely.*/@@ -151,7 +101,7 @@ export function getEnvdInflightLimit(): number {* can be in flight at once across all sandboxes in this SDK process,* or `0` to disable the cap.*- * Defaults to {@link DEFAULT_ENVD_RPC_INFLIGHT_LIMIT} ({@link 2000}). Override+ * Defaults to `2000` ({@link DEFAULT_ENVD_RPC_INFLIGHT_LIMIT}). Override* via `E2B_ENVD_RPC_INFLIGHT_REQUESTS` env var; set to `0` to disable the cap* entirely.*/diff --git a/packages/js-sdk/src/undici.ts b/packages/js-sdk/src/undici.tsindex 3314fbf14..01728470f 100644--- a/packages/js-sdk/src/undici.ts+++ b/packages/js-sdk/src/undici.ts@@ -1,6 +1,9 @@import { compareVersions } from 'compare-versions'-export type UndiciRequestInit = RequestInit & {+import { limitConcurrency } from './api/inflight'+import { dynamicImport } from './utils'++type UndiciRequestInit = RequestInit & {dispatcher?: unknownduplex?: 'half'}@@ -27,20 +30,9 @@ export function getUndiciPackageCandidates(nodeVersion: string): string[] {}export async function loadUndici(): Promise<UndiciModule | undefined> {- let importModule: (moduleName: string) => Promise<UndiciModule>- try {- // Keep package imports opaque so downstream bundlers resolve them at runtime.- // eslint-disable-next-line no-new-func- importModule = new Function('moduleName', 'return import(moduleName)') as (- moduleName: string- ) => Promise<UndiciModule>- } catch {- return undefined- }-for (const packageName of getUndiciPackageCandidates(process.versions.node)) {try {- return await importModule(packageName)+ return await dynamicImport<UndiciModule>(packageName)} catch {// Try the next package supported by this Node version.}@@ -49,7 +41,98 @@ export async function loadUndici(): Promise<UndiciModule | undefined> {return undefined}-export function toUndiciRequestInput(+/**+ * Late-bind the global fetch: runtimes and tools (msw, instrumentation) may+ * replace `globalThis.fetch` after the SDK builds a fetcher. A factory rather+ * than a shared const so per-proxy cache entries stay distinct closures.+ */+function lateBoundGlobalFetch(): typeof fetch {+ return ((input, init) => globalThis.fetch(input, init)) as typeof fetch+}++/**+ * Create a fetch for the given runtime. Outside Node it late-binds the global+ * fetch. On Node it lazily runs `build` on the first request and caches the+ * built fetcher; a failed build is not cached, so the next request retries+ * instead of replaying the same stale rejection forever.+ */+export function createRuntimeFetch(+ currentRuntime: string,+ build: () => Promise<typeof fetch>+): typeof fetch {+ if (currentRuntime !== 'node') {+ return lateBoundGlobalFetch()+ }++ let fetcherPromise: Promise<typeof fetch> | undefined++ return (async (input, init) => {+ const promise = (fetcherPromise ??= build())++ let fetcher: typeof fetch+ try {+ fetcher = await promise+ } catch (err) {+ // Clear only our own failed build: a stale awaiter of an already-+ // rejected promise must not clobber a newer in-flight build.+ if (fetcherPromise === promise) {+ fetcherPromise = undefined+ }+ throw err+ }++ return fetcher(input, init)+ }) as typeof fetch+}++/**+ * Build a fetch bound to a bounded undici dispatcher (HTTP/2 enabled,+ * `connections` origin connections, optional proxy tunnel), capped at+ * `inflightLimit` in-flight requests (`0` disables the cap). Falls back to+ * the global fetch — still capped — when undici cannot be loaded.+ */+export async function buildDispatchedFetch(options: {+ connections: number+ inflightLimit: number+ proxy?: string+ loadUndici?: () => Promise<UndiciModule | undefined>+}): Promise<typeof fetch> {+ const undici = await (options.loadUndici ?? loadUndici)()++ if (!undici) {+ return limitConcurrency(lateBoundGlobalFetch(), options.inflightLimit)+ }++ const { Agent, ProxyAgent, fetch: undiciFetch } = undici+ const dispatcher = options.proxy+ ? new ProxyAgent({+ uri: options.proxy,+ allowH2: true,+ connections: options.connections,+ proxyTunnel: true,+ })+ : new Agent({+ allowH2: true,+ connections: options.connections,+ })+ const fetchWithDispatcher = undiciFetch as unknown as (+ input: RequestInfo | URL,+ init?: UndiciRequestInit+ ) => Promise<Response>++ const wrapped: typeof fetch = ((input, init) => {+ const request = toUndiciRequestInput(input, init)++ return fetchWithDispatcher(request.input, {+ ...request.init,+ dispatcher,+ })+ }) as typeof fetch++ return limitConcurrency(wrapped, options.inflightLimit)+}++function toUndiciRequestInput(input: RequestInfo | URL,init?: RequestInit): { input: RequestInfo | URL; init?: RequestInit & { duplex?: 'half' } } {diff --git a/packages/js-sdk/src/utils.ts b/packages/js-sdk/src/utils.tsindex 9a2cf63c0..ce4f61f3e 100644--- a/packages/js-sdk/src/utils.ts+++ b/packages/js-sdk/src/utils.ts@@ -62,13 +62,22 @@ export function timeoutToSeconds(timeout: number): number {return Math.ceil(timeout / 1000)}+/**+ * Import an optional, runtime-resolved package (e.g. `undici`, `glob`, `tar`)+ * without letting downstream bundlers resolve it at build time.+ *+ * The variable specifier plus the `webpackIgnore`/`@vite-ignore` annotations+ * keep the import opaque to bundlers, so browser/edge builds don't try to+ * pull node-only packages into the bundle, while plain Node resolves it+ * natively at runtime.+ */export async function dynamicImport<T>(module: string): Promise<T> {if (runtime === 'browser') {throw new Error('Browser runtime is not supported for dynamic import')}// @ts-ignore- return await import(module)+ return await import(/* webpackIgnore: true */ /* @vite-ignore */ module)}// Source: https://github.com/chalk/ansi-regex/blob/main/index.jsdiff --git a/packages/js-sdk/tests/api/http2.test.ts b/packages/js-sdk/tests/api/http2.test.tsindex dcfced0e0..262bf96bf 100644--- a/packages/js-sdk/tests/api/http2.test.ts+++ b/packages/js-sdk/tests/api/http2.test.ts@@ -93,6 +93,59 @@ test('uses a ProxyAgent dispatcher when a proxy is configured', async () => {expect(requests[0].init?.dispatcher).toBeInstanceOf(ProxyAgent)})+test('retries the fetcher build after a failed load instead of caching the rejection', async () => {+ const Agent = vi.fn()+ const undiciFetch = vi.fn(() => Promise.resolve(new Response('ok')))+ const loadUndici = vi+ .fn<() => Promise<unknown>>()+ .mockRejectedValueOnce(new Error('transient import failure'))+ .mockResolvedValue({ Agent, fetch: undiciFetch })++ const { createApiFetchForRuntime } = await import('../../src/api/http2')++ const fetcher = createApiFetchForRuntime('node', {+ connectionLimit: 1,+ loadUndici: loadUndici as never,+ })++ await expect(fetcher('https://example.com/sandboxes')).rejects.toThrow(+ 'transient import failure'+ )++ const res = await fetcher('https://example.com/sandboxes')+ expect(await res.text()).toBe('ok')+ expect(loadUndici).toHaveBeenCalledTimes(2)+})++test('late-binds the global fetch fallback when undici cannot be loaded', async () => {+ const { createApiFetchForRuntime } = await import('../../src/api/http2')++ const fetcher = createApiFetchForRuntime('node', {+ loadUndici: () => Promise.resolve(undefined),+ })++ const firstFetch = vi.fn(() => Promise.resolve(new Response('first')))+ vi.stubGlobal('fetch', firstFetch)+ try {+ expect(await (await fetcher('https://example.com/sandboxes')).text()).toBe(+ 'first'+ )++ // A fetch swapped in after the fallback was built (msw, instrumentation)+ // must still be picked up.+ const secondFetch = vi.fn(() => Promise.resolve(new Response('second')))+ vi.stubGlobal('fetch', secondFetch)++ expect(await (await fetcher('https://example.com/sandboxes')).text()).toBe(+ 'second'+ )+ expect(firstFetch).toHaveBeenCalledOnce()+ expect(secondFetch).toHaveBeenCalledOnce()+ } finally {+ vi.unstubAllGlobals()+ }+})+test('caches API fetchers per proxy', async () => {const { createApiFetch } = await import('../../src/api/http2')diff --git a/packages/js-sdk/tests/globalFetchFallback.setup.ts b/packages/js-sdk/tests/globalFetchFallback.setup.tsnew file mode 100644index 000000000..36d5acef3--- /dev/null+++ b/packages/js-sdk/tests/globalFetchFallback.setup.ts@@ -0,0 +1,22 @@+import { vi } from 'vitest'++// msw mocks the network by patching `globalThis.fetch`, which the undici+// dispatcher the SDK uses on Node bypasses entirely — mocked requests would+// escape to the real API. Registered via `setupFiles` for every Node project,+// this reroutes the SDK through its real undici-unavailable fallback, which+// late-binds `globalThis.fetch`, so msw suites need no per-file mock. Tests+// that inject their own `loadUndici` (the api/envd transport suites) keep it,+// so the dispatcher wiring itself stays covered.+vi.mock('../src/undici', async (importOriginal) => {+ const original = await importOriginal<typeof import('../src/undici')>()+ return {+ ...original,+ buildDispatchedFetch: (+ options: Parameters<typeof original.buildDispatchedFetch>[0]+ ) =>+ original.buildDispatchedFetch({+ ...options,+ loadUndici: options.loadUndici ?? (async () => undefined),+ }),+ }+})diff --git a/packages/js-sdk/tests/undici.test.ts b/packages/js-sdk/tests/undici.test.tsindex c5bae4773..bbf44966e 100644--- a/packages/js-sdk/tests/undici.test.ts+++ b/packages/js-sdk/tests/undici.test.ts@@ -1,6 +1,11 @@-import { expect, test } from 'vitest'+import { expect, test, vi } from 'vitest'-import { getUndiciPackageCandidates } from '../src/undici'+import {+ createRuntimeFetch,+ getUndiciPackageCandidates,+ loadUndici,+} from '../src/undici'+import { runtime } from '../src/utils'test.each([['20.20.2', ['undici']],@@ -10,3 +15,53 @@ test.each([])('selects the packages supported by Node %s', (version, expected) => {expect(getUndiciPackageCandidates(version as string)).toEqual(expected)})++test('a stale awaiter of a failed build does not clobber a newer successful build', async () => {+ let rejectFirstBuild: ((err: Error) => void) | undefined+ const builtFetch = vi.fn(() => Promise.resolve(new Response('ok')))+ const build = vi.fn<() => Promise<typeof fetch>>().mockImplementation(() =>+ build.mock.calls.length === 1+ ? new Promise((_, reject) => {+ rejectFirstBuild = reject+ })+ : Promise.resolve(builtFetch as unknown as typeof fetch)+ )++ const fetcher = createRuntimeFetch('node', build)++ // Interleaving: A awaits pending build P1; L (queued before P1 rejects)+ // attaches to the already-rejected P1, so its catch runs one microtask+ // after A's; C runs in between, sees the cleared cache, and installs a+ // fresh successful build that L's stale catch must not discard.+ const requestA = fetcher('https://example.com/').catch(() => 'a-failed')+ let requestL: Promise<unknown> = Promise.resolve()+ queueMicrotask(() => {+ requestL = fetcher('https://example.com/').catch(() => 'l-failed')+ })+ rejectFirstBuild?.(new Error('transient build failure'))+ let requestC: Promise<unknown> = Promise.resolve()+ queueMicrotask(() => {+ requestC = fetcher('https://example.com/')+ })++ expect(await requestA).toBe('a-failed')+ expect(await requestL).toBe('l-failed')+ await requestC++ // A later request must reuse C's cached build instead of building a third.+ await fetcher('https://example.com/')+ expect(build).toHaveBeenCalledTimes(2)+})++// loadUndici is only reached in production when the runtime is Node; other+// runtimes (Bun, Deno) use their global fetch instead.+test.skipIf(runtime !== 'node')(+ 'loads a real undici module at runtime on Node',+ async () => {+ const undici = await loadUndici()++ expect(undici).toBeDefined()+ expect(typeof undici?.fetch).toBe('function')+ expect(typeof undici?.Agent).toBe('function')+ }+)diff --git a/packages/js-sdk/vitest.config.mts b/packages/js-sdk/vitest.config.mtsindex d770c91b0..63439b011 100644--- a/packages/js-sdk/vitest.config.mts+++ b/packages/js-sdk/vitest.config.mts@@ -25,6 +25,7 @@ export default defineConfig({testTimeout: 30_000,environment: 'node',bail: 0,+ setupFiles: ['tests/globalFetchFallback.setup.ts'],server: {},deps: {interopDefault: true,@@ -59,6 +60,7 @@ export default defineConfig({globals: false,testTimeout: 180_000,environment: 'node',+ setupFiles: ['tests/globalFetchFallback.setup.ts'],},},{