Skip to content

Commit a565291

Browse files
authored
use the native node:http2 when available (#10536)
1 parent c71d59e commit a565291

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

.changeset/tiny-hotels-win.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/unenv-preset": patch
3+
---
4+
5+
Use the native `node:http2` when available.
6+
7+
It is enabled starting on 2025-09-01 or when the `enable_nodejs_http2_module` compatibility flag is set.

packages/unenv-preset/src/preset.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ export function getCloudflarePreset({
6969
};
7070

7171
const httpOverrides = getHttpOverrides(compat);
72+
const http2Overrides = getHttp2Overrides(compat);
7273
const osOverrides = getOsOverrides(compat);
7374
const fsOverrides = getFsOverrides(compat);
7475

7576
// "dynamic" as they depend on the compatibility date and flags
7677
const dynamicNativeModules = [
7778
...nativeModules,
7879
...httpOverrides.nativeModules,
80+
...http2Overrides.nativeModules,
7981
...osOverrides.nativeModules,
8082
...fsOverrides.nativeModules,
8183
];
@@ -84,6 +86,7 @@ export function getCloudflarePreset({
8486
const dynamicHybridModules = [
8587
...hybridModules,
8688
...httpOverrides.hybridModules,
89+
...http2Overrides.hybridModules,
8790
...osOverrides.hybridModules,
8891
...fsOverrides.hybridModules,
8992
];
@@ -192,6 +195,42 @@ function getHttpOverrides({
192195
};
193196
}
194197

198+
/**
199+
* Returns the overrides for the `node:http2` module (unenv or workerd)
200+
*
201+
* The native http2 implementation:
202+
* - is enabled starting from 2025-09-01
203+
* - can be enabled with the "enable_nodejs_http2_module" flag
204+
* - can be disabled with the "disable_nodejs_http2_module" flag
205+
*/
206+
function getHttp2Overrides({
207+
compatibilityDate,
208+
compatibilityFlags,
209+
}: {
210+
compatibilityDate: string;
211+
compatibilityFlags: string[];
212+
}): { nativeModules: string[]; hybridModules: string[] } {
213+
const disabledByFlag = compatibilityFlags.includes(
214+
"disable_nodejs_http2_module"
215+
);
216+
const enabledByFlag = compatibilityFlags.includes(
217+
"enable_nodejs_http2_module"
218+
);
219+
const enabledByDate = compatibilityDate >= "2025-09-01";
220+
221+
const enabled = (enabledByFlag || enabledByDate) && !disabledByFlag;
222+
223+
return enabled
224+
? {
225+
nativeModules: ["http2"],
226+
hybridModules: [],
227+
}
228+
: {
229+
nativeModules: [],
230+
hybridModules: [],
231+
};
232+
}
233+
195234
/**
196235
* Returns the overrides for `node:os` (unenv or workerd)
197236
*

packages/wrangler/e2e/unenv-preset/preset.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,39 @@ const testConfigs: TestConfig[] = [
9999
},
100100
},
101101
],
102+
// node:http2
103+
[
104+
{
105+
name: "http2 disabled by date",
106+
compatibilityDate: "2024-09-23",
107+
expectRuntimeFlags: {
108+
enable_nodejs_http2_module: false,
109+
},
110+
},
111+
{
112+
name: "http2 disabled by flag",
113+
compatibilityDate: "2025-09-01",
114+
compatibilityFlags: ["disable_nodejs_http2_module"],
115+
expectRuntimeFlags: {
116+
enable_nodejs_http2_module: false,
117+
},
118+
},
119+
{
120+
name: "http2 enabled by flag",
121+
compatibilityDate: "2024-09-23",
122+
compatibilityFlags: ["enable_nodejs_http2_module"],
123+
expectRuntimeFlags: {
124+
enable_nodejs_http2_module: true,
125+
},
126+
},
127+
{
128+
name: "http2 enabled by date",
129+
compatibilityDate: "2025-09-01",
130+
expectRuntimeFlags: {
131+
enable_nodejs_http2_module: true,
132+
},
133+
},
134+
],
102135
// node:os
103136
[
104137
{

packages/wrangler/e2e/unenv-preset/worker/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,12 @@ export const WorkerdTests: Record<string, () => void> = {
497497
assert.deepStrictEqual(constants.O_WRONLY, 1);
498498
assert.deepStrictEqual(constants.O_RDWR, 2);
499499
},
500+
501+
async testHttp2() {
502+
const http2 = await import("node:http2");
503+
504+
assert.strictEqual(typeof http2.createSecureServer, "function");
505+
assert.strictEqual(typeof http2.connect, "function");
506+
assert.strictEqual(http2.constants.HTTP2_HEADER_STATUS, ":status");
507+
},
500508
};

0 commit comments

Comments
 (0)