Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Bug e2b 2195 throws dynamic usage of require is not eng 4003 (#1323)
Matt Brockmanb2a27867073b
diff --git a/.changeset/wild-lions-call.md b/.changeset/wild-lions-call.mdnew file mode 100644index 000000000..7ffdf5e89--- /dev/null+++ b/.changeset/wild-lions-call.md@@ -0,0 +1,5 @@+---+'e2b': minor+---++Compatibility for turbopackdiff --git a/packages/js-sdk/src/envd/http2.ts b/packages/js-sdk/src/envd/http2.tsindex ff5816e2c..a8092ba93 100644--- a/packages/js-sdk/src/envd/http2.ts+++ b/packages/js-sdk/src/envd/http2.ts@@ -1,17 +1,21 @@-import { dynamicRequire, runtime } from '../utils'+import { runtime } from '../utils'-type Undici = typeof import('undici')-type UndiciDispatcher = InstanceType<Undici['Agent']>type UndiciRequestInit = RequestInit & {- dispatcher: UndiciDispatcher+ dispatcher?: unknownduplex?: 'half'}+type UndiciModule = {+ Agent: new (options: { allowH2: true; connections?: number }) => unknown+ fetch: unknown+}type EnvdFetchOptions = {connectionLimit?: number+ loadUndici?: () => Promise<UndiciModule | undefined>}let envdFetch: typeof fetch | undefinedlet envdRpcFetch: typeof fetch | undefined+let hasWarnedUndiciFallback = falseexport function createEnvdFetchForRuntime(currentRuntime = runtime,@@ -21,13 +25,35 @@ export function createEnvdFetchForRuntime(return fetch}- const { Agent, fetch: undiciFetch } = dynamicRequire<Undici>('undici')+ 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)()++ if (!undici) {+ warnUndiciFallback()++ return fetch+ }++ const { Agent, fetch: undiciFetch } = undiciconst dispatcherOptions: { allowH2: true; connections?: number } = {allowH2: true,}if (options.connectionLimit !== undefined) {dispatcherOptions.connections = options.connectionLimit}+const dispatcher = new Agent(dispatcherOptions)const fetchWithDispatcher = undiciFetch as unknown as (input: RequestInfo | URL,@@ -44,6 +70,33 @@ export function createEnvdFetchForRuntime(}) as typeof fetch}+async function loadUndici(): Promise<UndiciModule | undefined> {+ try {+ // Keep this import opaque to bundlers. It must resolve as a package name+ // from the runtime environment, not as a path relative to this file.+ // eslint-disable-next-line no-new-func+ const importModule = new Function(+ 'moduleName',+ 'return import(moduleName)'+ ) as (moduleName: string) => Promise<UndiciModule>++ return await importModule('undici')+ } catch {+ return undefined+ }+}++function warnUndiciFallback() {+ if (hasWarnedUndiciFallback) {+ return+ }++ hasWarnedUndiciFallback = true+ console.warn(+ 'Failed to load undici for envd HTTP/2 transport; falling back to global fetch.'+ )+}+export function createEnvdFetch(): typeof fetch {if (envdFetch) {return envdFetchdiff --git a/packages/js-sdk/tests/envd/http2.test.ts b/packages/js-sdk/tests/envd/http2.test.tsindex b550f9e18..e673a0008 100644--- a/packages/js-sdk/tests/envd/http2.test.ts+++ b/packages/js-sdk/tests/envd/http2.test.ts@@ -3,6 +3,7 @@ import { afterEach, expect, test, vi } from 'vitest'afterEach(() => {vi.restoreAllMocks()vi.resetModules()+ vi.doUnmock('undici')vi.doUnmock('../../src/utils')})@@ -21,13 +22,12 @@ test('uses undici with HTTP/2 enabled in Node', async () => {return Promise.resolve(new Response('ok'))})- vi.doMock('../../src/utils', () => ({- dynamicRequire: () => ({ Agent, fetch: undiciFetch }),- runtime: 'node',- }))const { createEnvdFetchForRuntime } = await import('../../src/envd/http2')- const fetcher = createEnvdFetchForRuntime('node')+ const fetcher = createEnvdFetchForRuntime('node', {+ connectionLimit: 1,+ loadUndici: () => Promise.resolve({ Agent, fetch: undiciFetch }),+ })const res = await fetcher('https://example.com/status')expect(await res.text()).toBe('ok')@@ -45,13 +45,12 @@ test('passes Request objects to undici as URL plus init', async () => {return Promise.resolve(new Response('ok'))})- vi.doMock('../../src/utils', () => ({- dynamicRequire: () => ({ Agent, fetch: undiciFetch }),- runtime: 'node',- }))const { createEnvdFetchForRuntime } = await import('../../src/envd/http2')- const fetcher = createEnvdFetchForRuntime('node')+ const fetcher = createEnvdFetchForRuntime('node', {+ connectionLimit: 1,+ loadUndici: () => Promise.resolve({ Agent, fetch: undiciFetch }),+ })const body = JSON.stringify({ ok: true })await fetcher(new Request('https://example.com/rpc', {@@ -78,18 +77,61 @@ test('can create an uncapped dispatcher for RPC streams', async () => {const undiciFetch = vi.fn(() => Promise.resolve(new Response('ok')))- vi.doMock('../../src/utils', () => ({- dynamicRequire: () => ({ Agent, fetch: undiciFetch }),- runtime: 'node',- }))const { createEnvdFetchForRuntime } = await import('../../src/envd/http2')- const fetcher = createEnvdFetchForRuntime('node', {})+ const fetcher = createEnvdFetchForRuntime('node', {+ loadUndici: () => Promise.resolve({ Agent, fetch: undiciFetch }),+ })await fetcher('https://example.com/rpc')expect(agents).toEqual([{ allowH2: true }])})+test('defers loading undici until the first Node request', async () => {+ const Agent = vi.fn()+ const undiciFetch = vi.fn(() => Promise.resolve(new Response('ok')))+ const loadUndici = vi.fn(() => Promise.resolve({ Agent, fetch: undiciFetch }))++ const { createEnvdFetchForRuntime } = await import('../../src/envd/http2')++ const fetcher = createEnvdFetchForRuntime('node', {+ connectionLimit: 1,+ loadUndici,+ })++ expect(loadUndici).not.toHaveBeenCalled()+ expect(Agent).not.toHaveBeenCalled()+ expect(undiciFetch).not.toHaveBeenCalled()++ await fetcher('https://example.com/status')++ expect(loadUndici).toHaveBeenCalledOnce()+ expect(Agent).toHaveBeenCalledOnce()+ expect(undiciFetch).toHaveBeenCalledOnce()+})++test('falls back to global fetch when undici cannot be loaded', async () => {+ const fallbackFetch = vi.fn(() =>+ Promise.resolve(new Response('fallback ok'))+ ) as unknown as typeof fetch+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})+ vi.stubGlobal('fetch', fallbackFetch)++ const { createEnvdFetchForRuntime } = await import('../../src/envd/http2')++ const fetcher = createEnvdFetchForRuntime('node', {+ loadUndici: () => Promise.resolve(undefined),+ })+ const res = await fetcher('https://example.com/status')++ expect(await res.text()).toBe('fallback ok')+ expect(fallbackFetch).toHaveBeenCalledWith(+ 'https://example.com/status',+ undefined+ )+ expect(warn).toHaveBeenCalledOnce()+})+test('uses global fetch outside Node', async () => {const fallbackFetch = vi.fn() as unknown as typeof fetchvi.stubGlobal('fetch', fallbackFetch)