Repository navigation
fix(combo): normalize upstream Headers for Node 24 undici interop (#2751) #2823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 3 commits into
release/v3.8.6
from
fix/2751-node24-undici-headers-normalize
May 29, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * normalizeHeaders — cross-undici-instance Headers normalizer. | ||
| * | ||
| * On Node 24, accessing an undici-backed `Headers` object that was constructed | ||
| * by a *different* undici copy (e.g. a mock global `Response` in tests, or a | ||
| * version mismatch between undici instances bundled into different workspaces) | ||
| * throws `Cannot read private member #headers` because undici guards its internal | ||
| * state with private class fields. | ||
| * | ||
| * This helper avoids that crash by iterating through the public iterable API | ||
| * (`forEach`, then `entries`) first, falling back to plain-object enumeration | ||
| * for any non-standard map-like value. | ||
| * | ||
| * Always returns a plain `Record<string, string>` with lower-cased keys so | ||
| * callers never need to worry about header case sensitivity. | ||
| */ | ||
| export function normalizeHeaders(h: unknown): Record<string, string> { | ||
| if (!h) return {}; | ||
| const out: Record<string, string> = {}; | ||
|
|
||
| // Preferred path: forEach avoids creating an iterator object | ||
| try { | ||
| if (typeof (h as Headers).forEach === "function") { | ||
| (h as Headers).forEach((value: string, key: string) => { | ||
| out[key.toLowerCase()] = value; | ||
| }); | ||
| return out; | ||
| } | ||
| } catch { | ||
| // fall through — different undici instance, try next approach | ||
| } | ||
|
|
||
| // Second path: entries() iterator | ||
| try { | ||
| if (typeof (h as Headers).entries === "function") { | ||
| for (const [k, v] of (h as Headers).entries()) { | ||
| out[k.toLowerCase()] = String(v); | ||
| } | ||
| return out; | ||
| } | ||
| } catch { | ||
| // fall through — try plain object enumeration | ||
| } | ||
|
|
||
| // Final fallback: treat as plain object | ||
| if (typeof h === "object") { | ||
| for (const [k, v] of Object.entries(h as Record<string, unknown>)) { | ||
| out[k.toLowerCase()] = String(v ?? ""); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Convenience wrapper: get a header value from any headers-like object. | ||
| * Returns null (same contract as `Headers.prototype.get`) when absent. | ||
| */ | ||
| export function getHeader(h: unknown, name: string): string | null { | ||
| const plain = normalizeHeaders(h); | ||
| return plain[name.toLowerCase()] ?? null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| const { normalizeHeaders, getHeader } = await import("../../open-sse/utils/headers.ts"); | ||
|
|
||
| test("normalizeHeaders flattens a global Headers instance to plain object with lower-cased keys", () => { | ||
| const h = new Headers({ | ||
| "Content-Type": "application/json", | ||
| "Retry-After": "10", | ||
| "X-Custom": "value", | ||
| }); | ||
|
|
||
| const plain = normalizeHeaders(h); | ||
|
|
||
| assert.deepEqual(plain, { | ||
| "content-type": "application/json", | ||
| "retry-after": "10", | ||
| "x-custom": "value", | ||
| }); | ||
| }); | ||
|
|
||
| test("normalizeHeaders accepts a plain object and lower-cases its keys", () => { | ||
| const plain = normalizeHeaders({ | ||
| "X-Codex-5h-Usage": "42", | ||
| "Retry-After": "5", | ||
| }); | ||
|
|
||
| assert.equal(plain["x-codex-5h-usage"], "42"); | ||
| assert.equal(plain["retry-after"], "5"); | ||
| }); | ||
|
|
||
| test("normalizeHeaders returns {} for null / undefined / empty inputs", () => { | ||
| assert.deepEqual(normalizeHeaders(null), {}); | ||
| assert.deepEqual(normalizeHeaders(undefined), {}); | ||
| assert.deepEqual(normalizeHeaders({}), {}); | ||
| }); | ||
|
|
||
| test("normalizeHeaders survives an object that throws on forEach (cross-undici-instance simulation)", () => { | ||
| // Simulate the failure mode described in #2751: an object that *looks* like Headers | ||
| // but throws when .forEach is called (because the private #headers slot belongs to a | ||
| // different undici copy). The helper must fall back to entries(), then plain enum. | ||
| const throwingForEach = { | ||
| forEach: () => { | ||
| throw new TypeError("Cannot read private member #headers"); | ||
| }, | ||
| entries: () => [ | ||
| ["X-Survived", "yes"], | ||
| ["Retry-After", "1"], | ||
| ][Symbol.iterator](), | ||
| }; | ||
|
|
||
| const plain = normalizeHeaders(throwingForEach as unknown as Headers); | ||
| assert.equal(plain["x-survived"], "yes"); | ||
| assert.equal(plain["retry-after"], "1"); | ||
| }); | ||
|
|
||
| test("normalizeHeaders falls back to Object.entries when neither forEach nor entries works", () => { | ||
| const noIterators = { "X-Plain": "ok", "Other": "value" }; | ||
| const plain = normalizeHeaders(noIterators); | ||
| assert.equal(plain["x-plain"], "ok"); | ||
| assert.equal(plain["other"], "value"); | ||
| }); | ||
|
|
||
| test("getHeader returns the value (case-insensitive) or null", () => { | ||
| const h = new Headers({ "Retry-After": "30" }); | ||
| assert.equal(getHeader(h, "retry-after"), "30"); | ||
| assert.equal(getHeader(h, "Retry-After"), "30"); | ||
| assert.equal(getHeader(h, "missing"), null); | ||
| assert.equal(getHeader(null, "any"), null); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isolating the
outobject creation inside each try/catch block ensures that if any of the iteration methods (likeforEachorentries()) throw an error halfway through execution, we do not return a partially populated or corruptedoutobject. Additionally, converting thevalueto a string usingString(value)in theforEachpath ensures strict type safety and consistency with the other paths, preventing non-string values from leaking into the returnedRecord<string, string>.