fix(ext/node): track bytesRead on HTTP server request sockets#33228
Open
jeanibarz wants to merge 1 commit intodenoland:mainfrom
Open
fix(ext/node): track bytesRead on HTTP server request sockets#33228jeanibarz wants to merge 1 commit intodenoland:mainfrom
jeanibarz wants to merge 1 commit intodenoland:mainfrom
Conversation
Add bytesRead and bytesWritten properties to FakeSocket used by the node:http server. Previously req.socket.bytesRead returned undefined because FakeSocket (an EventEmitter, not a real Socket) had no such property. The fix estimates request head bytes from the method, URL, and headers, then tracks body bytes as they are consumed from the ReadableStream reader. This ensures libraries that check socket.bytesRead for bandwidth tracking or logging get a meaningful number instead of undefined. Closes denoland#33090 This PR was authored with assistance from Claude Code (AI tool).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #33090
Summary
req.socket.bytesReadreturnsundefinedin Deno'snode:httpserver because the HTTP server uses aFakeSocket(anEventEmittersubclass) instead of a realSocketwith a TCP handle. Node.js returns the total bytes received on the underlying TCP connection.This PR:
bytesReadandbytesWrittenproperties toFakeSocket, initialized to0bytesReadaccordingly when a new request arrivesReadableStreamreader inIncomingMessageForServerThe estimate may not be byte-exact compared to Node.js (Deno's
Deno.serve()handles HTTP parsing internally, so the raw TCP byte count is unavailable), but it provides a meaningful value instead ofundefined, which is what libraries relying on this property need.Verification
Built with
cargo build --features hmrand ran the exact reproducer from the issue:undefined74Node.js outputs
107for the same request (includes raw TCP framing for chunked transfer encoding). The difference is expected becauseDeno.serve()handles HTTP parsing internally and only exposes decoded body bytes — the exact raw TCP byte count is unavailable. The important fix is thatbytesReadis now anumber > 0instead ofundefined.Test plan
tests/specs/node/http_server_socket_bytes_read/that verifiesreq.socket.bytesReadis anumber> 0 after the request body is consumedcargo build --features hmrdeno fmt --checkpasses on changed filesdeno lintpasses on changed filesAI disclosure
This PR was authored with assistance from Claude Code.