Skip to content

http2: add callback to informational header methods - #67470

Open
inoway46 wants to merge 1 commit into
nodejs:mainfrom
inoway46:http2-write-continue-callback
Open

inoway46 wants to merge 1 commit into
nodejs:mainfrom
inoway46:http2-write-continue-callback

Conversation

@inoway46

@inoway46 inoway46 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

This adds an optional callback to http2stream.additionalHeaders() and to the compat writeContinue(), writeEarlyHints() and writeInformation(). As in HTTP/1, it is called once the informational headers have been written to the socket, or with an error if they could not be written. Until now the compat methods ignored the callback.

Without the callback, there is no reliable way to send Early Hints before starting blocking work. With a handler that sends hints and then renders for 500 ms, the client receives:

Server code 103 200
res.writeEarlyHints(hints); res.end(renderSync()) 504 ms 505 ms
res.writeEarlyHints(hints, () => res.end(renderSync())) 4 ms 505 ms

It also fixes code that relies on the callback and currently never responds over HTTP/2. For example, Fastify's documented reply.writeEarlyHints(hints, callback) passes the callback straight to res.writeEarlyHints() (https://github.com/fastify/fastify/blob/main/lib/reply.js), so with http2: true a handler that responds from the callback never responds (reproduced with Fastify 5.12.8 on Node.js 26.13.1).

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/http2
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. http2 Issues and PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. labels Nov 14, 2026
@inoway46
inoway46 force-pushed the http2-write-continue-callback branch 4 times, most recently from be273bf to cfa6762 Compare November 14, 2026 18:22
@inoway46 inoway46 added the semver-minor PRs that contain new features and should be released in the next minor version. label Nov 14, 2026
@inoway46
inoway46 marked this pull request as ready for review November 14, 2026 18:25
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.12346% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.67%. Comparing base (2e59a6d) to head (2af555e).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/node_http2.cc 81.39% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67470      +/-   ##
==========================================
- Coverage   90.67%   90.67%   -0.01%     
==========================================
  Files         799      799              
  Lines      280239   280313      +74     
  Branches    54126    54148      +22     
==========================================
+ Hits       254116   254167      +51     
- Misses      16240    16273      +33     
+ Partials     9883     9873      -10     
Files with missing lines Coverage Δ
lib/internal/http2/compat.js 97.26% <100.00%> (+<0.01%) ⬆️
lib/internal/http2/core.js 94.97% <100.00%> (+0.03%) ⬆️
src/node_http2.h 93.30% <ø> (ø)
src/node_http2.cc 82.04% <81.39%> (-0.03%) ⬇️

... and 39 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@inoway46
inoway46 force-pushed the http2-write-continue-callback branch 3 times, most recently from 2f29fbb to a24bd18 Compare November 15, 2026 16:35
In the HTTP/1 server, writeContinue(), writeEarlyHints() and
writeInformation() accept a callback that is called once the response
has been written to the socket. The HTTP/2 compat versions did not, so
code moving to the compat API had its callback ignored.

Accept an optional callback in http2stream.additionalHeaders() and in
the three compat methods. The request for the HEADERS frame is tracked
until nghttp2 serializes the frame, and completed together with the
write to the underlying stream that carries it.

Signed-off-by: inoway46 <inoueyuya416@gmail.com>
Assisted-by: claude:opus-5.5
@inoway46
inoway46 force-pushed the http2-write-continue-callback branch from a24bd18 to 2af555e Compare November 16, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. http2 Issues and PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants