Skip to content

fix: resolve writeEarlyHints hang with empty hints on Node.js#1400

Open
leno23 wants to merge 1 commit into
h3js:mainfrom
leno23:fix/write-early-hints-empty-hints-1383
Open

fix: resolve writeEarlyHints hang with empty hints on Node.js#1400
leno23 wants to merge 1 commit into
h3js:mainfrom
leno23:fix/write-early-hints-empty-hints-1383

Conversation

@leno23

@leno23 leno23 commented May 26, 2026

Copy link
Copy Markdown

Summary

  • Fix writeEarlyHints() hanging indefinitely on Node.js when called with empty hints ({})
  • Normalize Link headers to Node's expected lowercase link key before calling native writeEarlyHints
  • Skip native early hints call when there are no link hints to send

Fixes #1383

Root cause

Node.js ServerResponse.writeEarlyHints() only invokes its callback when valid link hints are provided. Passing {} or { Link: ... } (capital L) never triggers the callback, causing await writeEarlyHints(event, {}) to hang forever.

Test plan

  • Added Node test: empty hints resolve immediately and handler completes
  • Manual repro confirmed fixed (await writeEarlyHints(event, {}) no longer hangs)
  • pnpm vitest run test/utils.test.ts -t writeEarlyHints passes

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced early HTTP link hint handling for improved compatibility and reliability across different runtime environments.
  • Tests

    • Added test coverage for edge cases in early hint processing.

Review Change Stack

Closes h3js#1383

Co-authored-by: Cursor <cursoragent@cursor.com>
@leno23 leno23 requested a review from pi0 as a code owner May 26, 2026 05:33
@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR fixes a bug where writeEarlyHints never resolves on Node runtime. The function now extracts link hints into a normalized array via a new helper, returns early if empty, and uses the normalized links for both Node's res.writeEarlyHints and the non-Node fallback path. A new test validates the fix resolves successfully with empty hints.

Changes

Early Hints Processing

Layer / File(s) Summary
Link hints extraction and normalization
src/utils/response.ts
writeEarlyHints refactored to normalize link hints via new getEarlyHintLinks helper that extracts and flattens both string and array forms, early-returns when empty, and uses normalized links for Node and fallback paths.
Node runtime test coverage
test/utils.test.ts
New Node-only test verifies writeEarlyHints(event, {}) resolves immediately and returns "ok" without hanging.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • h3js/h3#1288: Both PRs modify writeEarlyHints to normalize link-based fallback behavior for CDN/headers emission.

Suggested reviewers

  • pi0

Poem

🐰 The hints now flow without delay,
Links extracted in a tidy way,
No more hanging, promises resolve fast,
Early delivery at long last!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fix: resolving a hang issue with writeEarlyHints when empty hints are passed on Node.js, which matches the primary change in the PR.
Linked Issues check ✅ Passed The PR successfully addresses all objectives from issue #1383: it reproduces and confirms the hang, identifies Node's requirement for lowercase 'link' key, and implements a fix that normalizes headers and skips native API calls when no link hints exist.
Out of Scope Changes check ✅ Passed All changes are in-scope: modifications to writeEarlyHints logic and an added test case directly address the hang issue described in issue #1383.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
test/utils.test.ts (1)

473-484: ⚡ Quick win

Add a Node case for capitalized Link hints.

This only locks in the {} hang fix. The other half of the regression was { Link: ... } on the native Node path, and the web-only fallback test below does not exercise that normalization. Please add a Node-targeted case that awaits writeEarlyHints(event, { Link: ... }) and asserts the handler still completes.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/utils.test.ts` around lines 473 - 484, Add a Node-targeted test that
covers the capitalized header shape by creating a test (using the same pattern
as the existing "resolves immediately when hints are empty on Node.js" block)
which runs only when t.target === "node", registers a handler that awaits
writeEarlyHints(event, { Link: '<...>' }) (use a valid Link value like
"</style.css>; rel=preload; as=style"), then returns "ok", and finally fetches
the route and asserts the response body is "ok"; reference writeEarlyHints and
the existing test name to locate where to add this case so the native Node
normalization of { Link: ... } is exercised.
src/utils/response.ts (1)

136-149: ⚡ Quick win

Move getEarlyHintLinks to an internal location.

This helper is internal-only, but it's now sitting in the middle of src/utils/response.ts. Please move it to the end of the file or into src/utils/internal/ so the file keeps the expected layout.

As per coding guidelines, src/**/*.ts: Place internal helpers at the end of files or in utils/internal/ subdirectory.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utils/response.ts` around lines 136 - 149, The getEarlyHintLinks helper
is internal-only and should be relocated out of the main body: move the
getEarlyHintLinks function to the end of this file (or into the utils/internal/
module) so internal helpers stay separate; after moving, update any local
references or imports to point to the new location (or keep it file-local and
unexported) and run type-checks to ensure no exported API changed; keep the
function name getEarlyHintLinks unchanged and ensure its behavior and signatures
remain identical.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/utils/response.ts`:
- Around line 136-149: The getEarlyHintLinks helper is internal-only and should
be relocated out of the main body: move the getEarlyHintLinks function to the
end of this file (or into the utils/internal/ module) so internal helpers stay
separate; after moving, update any local references or imports to point to the
new location (or keep it file-local and unexported) and run type-checks to
ensure no exported API changed; keep the function name getEarlyHintLinks
unchanged and ensure its behavior and signatures remain identical.

In `@test/utils.test.ts`:
- Around line 473-484: Add a Node-targeted test that covers the capitalized
header shape by creating a test (using the same pattern as the existing
"resolves immediately when hints are empty on Node.js" block) which runs only
when t.target === "node", registers a handler that awaits writeEarlyHints(event,
{ Link: '<...>' }) (use a valid Link value like "</style.css>; rel=preload;
as=style"), then returns "ok", and finally fetches the route and asserts the
response body is "ok"; reference writeEarlyHints and the existing test name to
locate where to add this case so the native Node normalization of { Link: ... }
is exercised.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9f09122b-0266-41a3-97ed-5ecf51a152ee

📥 Commits

Reviewing files that changed from the base of the PR and between 84244b4 and a03c6c0.

📒 Files selected for processing (2)
  • src/utils/response.ts
  • test/utils.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

writeEarlyHints never resolves with node runtime

1 participant