Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Js api http2 dispatcher (#1340)
Matt Brockmane10958d87ecf
diff --git a/.changeset/tasty-dogs-refuse.md b/.changeset/tasty-dogs-refuse.mdnew file mode 100644index 000000000..1dcfd7b36--- /dev/null+++ b/.changeset/tasty-dogs-refuse.md@@ -0,0 +1,5 @@+---+'e2b': minor+---++support for api h2diff --git a/packages/js-sdk/src/api/http2.ts b/packages/js-sdk/src/api/http2.tsnew file mode 100644index 000000000..06b321070--- /dev/null+++ b/packages/js-sdk/src/api/http2.ts@@ -0,0 +1,86 @@+import { runtime } from '../utils'+import {+ loadUndici,+ toUndiciRequestInput,+ type UndiciModule,+ type UndiciRequestInit,+} from '../undici'++const API_CONNECTION_LIMIT = 100+const API_UNDICI_FALLBACK_WARNING =+ 'Failed to load undici for API HTTP/2 transport; falling back to global fetch.'++let apiFetch: typeof fetch | undefined+let hasWarnedUndiciFallback = false++export function createApiFetch(): typeof fetch {+ if (apiFetch) {+ return apiFetch+ }++ apiFetch = createApiFetchForRuntime(runtime)++ return apiFetch+}++export function createApiFetchForRuntime(+ currentRuntime = runtime,+ options: {+ connectionLimit?: number+ loadUndici?: () => Promise<UndiciModule | undefined>+ } = { connectionLimit: API_CONNECTION_LIMIT }+): typeof fetch {+ if (currentRuntime !== 'node') {+ return 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+ loadUndici?: () => Promise<UndiciModule | undefined>+}): Promise<typeof fetch> {+ const undici = await (options.loadUndici ?? loadUndici)()++ if (!undici) {+ warnUndiciFallback()++ return fetch+ }++ const { Agent, fetch: undiciFetch } = undici+ const dispatcher = new Agent({+ allowH2: true,+ connections: options.connectionLimit,+ })+ const fetchWithDispatcher = undiciFetch as unknown as (+ input: RequestInfo | URL,+ init?: UndiciRequestInit+ ) => Promise<Response>++ return ((input, init) => {+ const request = toUndiciRequestInput(input, init)++ return fetchWithDispatcher(request.input, {+ ...request.init,+ dispatcher,+ })+ }) as typeof fetch+}++function warnUndiciFallback() {+ if (hasWarnedUndiciFallback) {+ return+ }++ hasWarnedUndiciFallback = true+ console.warn(API_UNDICI_FALLBACK_WARNING)+}diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.tsindex 109d69bb3..7b6db519e 100644--- a/packages/js-sdk/src/api/index.ts+++ b/packages/js-sdk/src/api/index.ts@@ -2,6 +2,7 @@ import createClient, { FetchResponse } from 'openapi-fetch'import type { components, paths } from './schema.gen'import { defaultHeaders } from './metadata'+import { createApiFetch } from './http2'import { ConnectionConfig } from '../connectionConfig'import { AuthenticationError, RateLimitError, SandboxError } from '../errors'import { createApiLogger } from '../logs'@@ -74,6 +75,7 @@ class ApiClient {this.api = createClient<paths>({baseUrl: config.apiUrl,+ fetch: createApiFetch(),// In HTTP 1.1, all connections are considered persistent unless declared otherwise// keepalive: true,headers: {diff --git a/packages/js-sdk/src/envd/http2.ts b/packages/js-sdk/src/envd/http2.tsindex a8092ba93..b4a276bbe 100644--- a/packages/js-sdk/src/envd/http2.ts+++ b/packages/js-sdk/src/envd/http2.ts@@ -1,13 +1,11 @@import { runtime } from '../utils'+import {+ loadUndici,+ toUndiciRequestInput,+ type UndiciModule,+ type UndiciRequestInit,+} from '../undici'-type UndiciRequestInit = RequestInit & {- dispatcher?: unknown- duplex?: 'half'-}-type UndiciModule = {- Agent: new (options: { allowH2: true; connections?: number }) => unknown- fetch: unknown-}type EnvdFetchOptions = {connectionLimit?: numberloadUndici?: () => Promise<UndiciModule | undefined>@@ -61,7 +59,7 @@ async function buildEnvdFetcher() => Promise<Response>return ((input, init) => {- const request = toRequestInput(input, init)+ const request = toUndiciRequestInput(input, init)return fetchWithDispatcher(request.input, {...request.init,@@ -70,22 +68,6 @@ async function buildEnvdFetcher(}) 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@@ -120,37 +102,3 @@ export function createEnvdRpcFetch(): typeof fetch {return envdRpcFetch}--function toRequestInput(- input: RequestInfo | URL,- init?: RequestInit-): { input: RequestInfo | URL; init?: RequestInit & { duplex?: 'half' } } {- if (!(input instanceof Request)) {- return { input, init }- }-- const requestInit: RequestInit & { duplex?: 'half' } = {- body: input.body,- cache: input.cache,- credentials: input.credentials,- headers: input.headers,- integrity: input.integrity,- keepalive: input.keepalive,- method: input.method,- mode: input.mode,- redirect: input.redirect,- referrer: input.referrer,- referrerPolicy: input.referrerPolicy,- signal: input.signal,- ...init,- }-- if (requestInit.body) {- requestInit.duplex = 'half'- }-- return {- input: input.url,- init: requestInit,- }-}diff --git a/packages/js-sdk/src/undici.ts b/packages/js-sdk/src/undici.tsnew file mode 100644index 000000000..993d69f6f--- /dev/null+++ b/packages/js-sdk/src/undici.ts@@ -0,0 +1,59 @@+export type UndiciRequestInit = RequestInit & {+ dispatcher?: unknown+ duplex?: 'half'+}++export type UndiciModule = {+ Agent: new (options: { allowH2: true; connections?: number }) => unknown+ fetch: unknown+}++export 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+ }+}++export function toUndiciRequestInput(+ input: RequestInfo | URL,+ init?: RequestInit+): { input: RequestInfo | URL; init?: RequestInit & { duplex?: 'half' } } {+ if (!(input instanceof Request)) {+ return { input, init }+ }++ const requestInit: RequestInit & { duplex?: 'half' } = {+ body: input.body,+ cache: input.cache,+ credentials: input.credentials,+ headers: input.headers,+ integrity: input.integrity,+ keepalive: input.keepalive,+ method: input.method,+ mode: input.mode,+ redirect: input.redirect,+ referrer: input.referrer,+ referrerPolicy: input.referrerPolicy,+ signal: input.signal,+ ...init,+ }++ if (requestInit.body) {+ requestInit.duplex = 'half'+ }++ return {+ input: input.url,+ init: requestInit,+ }+}diff --git a/packages/js-sdk/tests/api/http2.test.ts b/packages/js-sdk/tests/api/http2.test.tsnew file mode 100644index 000000000..d19498ed4--- /dev/null+++ b/packages/js-sdk/tests/api/http2.test.ts@@ -0,0 +1,37 @@+import { afterEach, expect, test, vi } from 'vitest'++afterEach(() => {+ vi.restoreAllMocks()+ vi.resetModules()+ vi.doUnmock('undici')+ vi.doUnmock('../../src/utils')+})++test('uses undici with a bounded HTTP/2 dispatcher for API requests', async () => {+ const agents: Array<{ allowH2?: boolean; connections?: number }> = []+ const requests: Array<{ init?: RequestInit & { dispatcher?: unknown } }> = []++ class Agent {+ constructor(options: { allowH2?: boolean; connections?: number }) {+ agents.push(options)+ }+ }++ const undiciFetch = vi.fn((input, init) => {+ requests.push({ init })+ return Promise.resolve(new Response('ok'))+ })+ const loadUndici = vi.fn(() => Promise.resolve({ Agent, fetch: undiciFetch }))++ const { createApiFetchForRuntime } = await import('../../src/api/http2')++ const fetcher = createApiFetchForRuntime('node', {+ connectionLimit: 100,+ loadUndici,+ })+ await fetcher('https://example.com/sandboxes')++ expect(loadUndici).toHaveBeenCalledOnce()+ expect(agents).toEqual([{ allowH2: true, connections: 100 }])+ expect(requests[0].init?.dispatcher).toBeInstanceOf(Agent)+})