Skip to content

inspector,http: support builtin http request bodies - #64369

Open
GrinZero wants to merge 2 commits into
nodejs:mainfrom
GrinZero:feat/inspector-http-body-tracking
Open

GrinZero wants to merge 2 commits into
nodejs:mainfrom
GrinZero:feat/inspector-http-body-tracking

Conversation

@GrinZero

@GrinZero GrinZero commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR supersedes #62915 with the same feature work rebased onto current main.

It adds builtin http/https request-body support to network inspection, so Network.getRequestPostData can return text request bodies while preserving the existing rejection behavior for binary request bodies.

It also moves builtin http response-body tracking to a raw-byte hook before IncomingMessage decoding, so response inspection remains correct even when user code calls response.setEncoding(...).

In addition, this PR lets Network.requestWillBeSent carry JS-provided initiator data captured at request construction time. The C++ inspector side validates structured initiator.stack objects against the inspector protocol schema before forwarding them to DevTools.

Problem

Builtin http/https network inspection currently emits request and response lifecycle events, but the builtin client path does not expose request-body bytes to the inspector. As a result, Network.getRequestPostData cannot return POST data for builtin http/https requests.

There are two related gaps:

  • IncomingMessage 'data' events are not a stable raw-byte source. If user code calls response.setEncoding('utf8'), chunks observed through userland become strings, while the inspector protocol expects byte-oriented payloads.
  • Delaying requestWillBeSent until request-body state is known means a C++ stack captured at emission time would point into Node.js internals rather than the user call site. The initiator needs to be captured when the request is created and validated before being emitted.

Approach

  1. Reuse the existing Network.dataSent(...) -> NetworkAgent::getRequestPostData(...) buffering pipeline instead of changing the CDP schema.
  2. Add builtin HTTP diagnostics events for request body chunks and request body completion:
    • http.client.request.bodyChunkSent
    • http.client.request.bodySent
  3. Capture builtin HTTP response body bytes through a parser-side raw-byte diagnostics event before stream decoding:
    • http.client.response.bodyChunkReceived
  4. Defer requestWillBeSent until body state is known, so hasPostData is accurate for both body and body-less requests.
  5. Capture the initiator stack in JS at request creation time, pass it through the inspector binding, and validate initiator.stack with generated inspector protocol schema support in C++.
  6. Document the new diagnostics channels and message shapes.

Behavior

After this change:

  • builtin http and https POST requests with UTF-8 text bodies are available through Network.getRequestPostData
  • binary request bodies still reject with the existing inspector error behavior
  • builtin http response inspection continues to work even if user code calls response.setEncoding('utf8')
  • Network.requestWillBeSent reports accurate hasPostData
  • Network.requestWillBeSent can carry JS-captured initiator stack data from the request construction site
  • malformed initiator stack input is rejected by C++ schema validation

Tests

This PR adds and extends coverage in:

  • test/parallel/test-diagnostics-channel-http.js
  • test/parallel/test-inspector-network-http.js
  • test/parallel/test-inspector-emit-protocol-event.js
  • test/parallel/test-inspector-emit-protocol-event-errors.js

The updated tests cover:

  • request body chunk and request body finished diagnostics events
  • response body raw-byte diagnostics events
  • text request bodies split across write() and end()
  • binary request bodies
  • http and https Network.getRequestPostData
  • binary request-body rejection semantics
  • response inspection when the client calls response.setEncoding('utf8')
  • accurate hasPostData for body-less requests
  • JS-provided initiator stack parsing and forwarding
  • malformed initiator stack error handling

Verification

Validated with the targeted test suite:

python3 tools/test.py \
  parallel/test-diagnostics-channel-http \
  parallel/test-inspector-network-http \
  parallel/test-inspector-emit-protocol-event \
  parallel/test-inspector-emit-protocol-event-errors

Also validated manually with an end-to-end inspector run:

./out/Release/node --inspect --experimental-network-inspection ./test.js

Refs

@GrinZero

GrinZero commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

e2e same like: #62915 (comment)
image

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/inspector
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Jul 8, 2026
@GrinZero
GrinZero force-pushed the feat/inspector-http-body-tracking branch 2 times, most recently from 7865a8d to 4bd2033 Compare July 8, 2026 17:41
@codecov

codecov Bot commented Jul 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.81250% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.37%. Comparing base (cff7b12) to head (71b18a4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/inspector/network_agent.cc 94.06% 4 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64369      +/-   ##
==========================================
- Coverage   90.37%   90.37%   -0.01%     
==========================================
  Files         792      792              
  Lines      275426   275724     +298     
  Branches    52800    52855      +55     
==========================================
+ Hits       248926   249176     +250     
- Misses      16931    16975      +44     
- Partials     9569     9573       +4     
Files with missing lines Coverage Δ
lib/_http_client.js 97.43% <100.00%> (+0.04%) ⬆️
lib/_http_common.js 98.61% <100.00%> (+0.06%) ⬆️
lib/_http_outgoing.js 97.92% <100.00%> (+0.02%) ⬆️
lib/internal/http.js 92.54% <100.00%> (+0.05%) ⬆️
lib/internal/inspector/network_http.js 97.57% <100.00%> (+1.87%) ⬆️
src/inspector/network_agent.cc 89.56% <94.06%> (+1.53%) ⬆️

... and 33 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.

@GrinZero
GrinZero force-pushed the feat/inspector-http-body-tracking branch from 929ddfe to eaa29a9 Compare July 10, 2026 05:21
@GrinZero
GrinZero force-pushed the feat/inspector-http-body-tracking branch 3 times, most recently from d499741 to 8fa14bc Compare July 28, 2026 03:44
@GrinZero

Copy link
Copy Markdown
Contributor Author

I think it's time to conduct a re-review. Thank you. @metcoder95 @ShogunPanda @mcollina

@GrinZero

Copy link
Copy Markdown
Contributor Author

push

Comment thread src/inspector/network_agent.cc Outdated
@GrinZero

Copy link
Copy Markdown
Contributor Author

push

@GrinZero

Copy link
Copy Markdown
Contributor Author

Friendly ping on the next step.

The existing review feedback has been addressed, @metcoder95 has approved, and the PR is currently mergeable. It is still labeled "needs-ci" — could a collaborator please trigger the full CI?

If CI is green, could @nodejs/http and @nodejs/inspector give it a final look or let me know if anything else is needed before landing?

Happy to address any follow-up feedback promptly. Thanks!

@GrinZero

GrinZero commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

push

@GrinZero

Copy link
Copy Markdown
Contributor Author

push... @jasnell Can you help me add the label to run the CI

Signed-off-by: GrinZero <774933704@qq.com>
Signed-off-by: GrinZero <774933704@qq.com>
@GrinZero
GrinZero force-pushed the feat/inspector-http-body-tracking branch from 0cb2dec to 71b18a4 Compare September 28, 2026 08:48
@GrinZero

Copy link
Copy Markdown
Contributor Author

Hi @mcollina , I’ve rebased #64369 onto the latest main and resolved the HTTP outgoing conflict. The PR is mergeable again, and CI is running. The targeted HTTP/Inspector tests pass locally. When you have a chance, could you review the HTTP integration, particularly the request-body tracking and raw response-body hook? I’d appreciate your feedback and will address any comments promptly. Thanks!
And, can you help me add the label to run the CI...

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 28, 2026
@mcollina

Copy link
Copy Markdown
Member

Can you do a run of the benchmarks?

@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 28, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@panva panva added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Sep 28, 2026
@GrinZero

Copy link
Copy Markdown
Contributor Author

Can you do a run of the benchmarks?

I tried comparing the main branch and found that the newly added features resulted in a performance drop of around 18%. Due to the collection of body data, this decrease is actually within expectations. But did CI just fail? I think while investigating, I can also see if there are any ways to optimize.

GrinZero is missing the Overall/Read permission

I can’t access the Jenkins results due to the CI read permissions. Both node-test-commit/92841 and node-test-pull-request/78032 failed, although the reported platform checks appear green. Could someone with CI access share the failing job/platform, test name, and relevant console output? If the failure is unrelated to this change, could you please rerun the affected job? Thanks! @mcollina

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

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants