Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Use undici8.8 when on node >= 22.19 (#1575)

Matt Brockmane5a4bd655d29
diff --git a/.changeset/calm-goaways-retry.md b/.changeset/calm-goaways-retry.md
new file mode 100644
index 000000000..938baae5b
--- /dev/null
+++ b/.changeset/calm-goaways-retry.md
@@ -0,0 +1,5 @@
+---
+'e2b': patch
+---
+
+Prefer Undici 8 on Node 22.19 and newer so graceful HTTP/2 `GOAWAY` frames drain accepted requests instead of failing them, while retaining Undici 7 as the Node 20 fallback.
diff --git a/packages/js-sdk/package.json b/packages/js-sdk/package.json
index d9b9bf5ae..214c1d18c 100644
--- a/packages/js-sdk/package.json
+++ b/packages/js-sdk/package.json
@@ -100,6 +100,9 @@
"tar": "^7.5.16",
"undici": "^7.28.0"
},
+ "optionalDependencies": {
+ "undici8": "npm:[email protected]"
+ },
"engines": {
"node": ">=20.18.1 <21 || >=22"
},
diff --git a/packages/js-sdk/src/api/http2.ts b/packages/js-sdk/src/api/http2.ts
index 49bf1f6be..ad81531bf 100644
--- a/packages/js-sdk/src/api/http2.ts
+++ b/packages/js-sdk/src/api/http2.ts
@@ -74,6 +74,7 @@ async function buildApiFetcher(options: {
uri: options.proxy,
allowH2: true,
connections,
+ proxyTunnel: true,
})
: new Agent({
allowH2: true,
diff --git a/packages/js-sdk/src/envd/http2.ts b/packages/js-sdk/src/envd/http2.ts
index 1de93db57..a0427b3ea 100644
--- a/packages/js-sdk/src/envd/http2.ts
+++ b/packages/js-sdk/src/envd/http2.ts
@@ -60,6 +60,7 @@ async function buildEnvdFetcher(
uri: options.proxy,
allowH2: true,
connections,
+ proxyTunnel: true,
})
: new Agent({
allowH2: true,
diff --git a/packages/js-sdk/src/undici.ts b/packages/js-sdk/src/undici.ts
index e3566c0b4..3314fbf14 100644
--- a/packages/js-sdk/src/undici.ts
+++ b/packages/js-sdk/src/undici.ts
@@ -1,3 +1,5 @@
+import { compareVersions } from 'compare-versions'
+
export type UndiciRequestInit = RequestInit & {
dispatcher?: unknown
duplex?: 'half'
@@ -9,24 +11,42 @@ export type UndiciModule = {
uri: string
allowH2: true
connections?: number
+ proxyTunnel: true
}) => unknown
fetch: unknown
}
+const UNDICI_8_MIN_NODE = '22.19.0'
+
+export function getUndiciPackageCandidates(nodeVersion: string): string[] {
+ if (compareVersions(nodeVersion, UNDICI_8_MIN_NODE) >= 0) {
+ return ['undici8', 'undici']
+ }
+
+ return ['undici']
+}
+
export async function loadUndici(): Promise<UndiciModule | undefined> {
+ let importModule: (moduleName: string) => Promise<UndiciModule>
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.
+ // Keep package imports opaque so downstream bundlers resolve them at runtime.
// eslint-disable-next-line no-new-func
- const importModule = new Function(
- 'moduleName',
- 'return import(moduleName)'
- ) as (moduleName: string) => Promise<UndiciModule>
-
- return await importModule('undici')
+ 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)
+ } catch {
+ // Try the next package supported by this Node version.
+ }
+ }
+
+ return undefined
}
export function toUndiciRequestInput(
diff --git a/packages/js-sdk/tests/api/http2.test.ts b/packages/js-sdk/tests/api/http2.test.ts
index 83ef5e421..dcfced0e0 100644
--- a/packages/js-sdk/tests/api/http2.test.ts
+++ b/packages/js-sdk/tests/api/http2.test.ts
@@ -58,6 +58,7 @@ test('uses a ProxyAgent dispatcher when a proxy is configured', async () => {
uri?: string
allowH2?: boolean
connections?: number
+ proxyTunnel?: boolean
}) {
proxyAgents.push(options)
}
@@ -86,6 +87,7 @@ test('uses a ProxyAgent dispatcher when a proxy is configured', async () => {
uri: 'http://user:[email protected]:8080',
allowH2: true,
connections: 100,
+ proxyTunnel: true,
},
])
expect(requests[0].init?.dispatcher).toBeInstanceOf(ProxyAgent)
diff --git a/packages/js-sdk/tests/envd/http2.test.ts b/packages/js-sdk/tests/envd/http2.test.ts
index 01ca90355..de6118ce7 100644
--- a/packages/js-sdk/tests/envd/http2.test.ts
+++ b/packages/js-sdk/tests/envd/http2.test.ts
@@ -58,6 +58,7 @@ test('uses a ProxyAgent dispatcher when a proxy is configured', async () => {
uri?: string
allowH2?: boolean
connections?: number
+ proxyTunnel?: boolean
}) {
proxyAgents.push(options)
}
@@ -80,7 +81,12 @@ test('uses a ProxyAgent dispatcher when a proxy is configured', async () => {
expect(agents).toHaveLength(0)
expect(proxyAgents).toEqual([
- { uri: 'http://127.0.0.1:8080', allowH2: true, connections: 1 },
+ {
+ uri: 'http://127.0.0.1:8080',
+ allowH2: true,
+ connections: 1,
+ proxyTunnel: true,
+ },
])
expect(requests[0].init?.dispatcher).toBeInstanceOf(ProxyAgent)
})
diff --git a/packages/js-sdk/tests/undici.test.ts b/packages/js-sdk/tests/undici.test.ts
new file mode 100644
index 000000000..c5bae4773
--- /dev/null
+++ b/packages/js-sdk/tests/undici.test.ts
@@ -0,0 +1,12 @@
+import { expect, test } from 'vitest'
+
+import { getUndiciPackageCandidates } from '../src/undici'
+
+test.each([
+ ['20.20.2', ['undici']],
+ ['22.18.0', ['undici']],
+ ['22.19.0', ['undici8', 'undici']],
+ ['24.0.0', ['undici8', 'undici']],
+])('selects the packages supported by Node %s', (version, expected) => {
+ expect(getUndiciPackageCandidates(version as string)).toEqual(expected)
+})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4690a9066..6500fad47 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -164,6 +164,10 @@ importers:
undici:
specifier: ^7.28.0
version: 7.28.0
+ optionalDependencies:
+ undici8:
+ specifier: npm:[email protected]
devDependencies:
'@testing-library/react':
specifier: ^16.2.0
@@ -3331,6 +3335,10 @@ packages:
resolution: {integrity: sha512-cRZYrTDwWznlnRiPjggAGxZXanty6M8RV1ff8Wm4LWXBp7/IG8v5DnOm74DtUBp9OONpK75YlPnIjQqX0dBDtA==}
engines: {node: '>=20.18.1'}
+ resolution: {integrity: sha512-ubshXMXwF3MQIMF1y/WxZdNBnjEKeSg2wF5mcGUtU55YTw34tnVVpKRlLf7ruDXZ5344KokPVX4RBx1wJm64Bw==}
+ engines: {node: '>=22.19.0'}
+
resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==}
engines: {node: '>= 4.0.0'}
@@ -6607,6 +6615,9 @@ snapshots:
+ optional: true
+