Skip to content

fix(ext/node): track bytesRead on HTTP server request sockets#33228

Open
jeanibarz wants to merge 1 commit intodenoland:mainfrom
jeanibarz:fix/33090-socket-bytes-read
Open

fix(ext/node): track bytesRead on HTTP server request sockets#33228
jeanibarz wants to merge 1 commit intodenoland:mainfrom
jeanibarz:fix/33090-socket-bytes-read

Conversation

@jeanibarz
Copy link
Copy Markdown

@jeanibarz jeanibarz commented Apr 9, 2026

Closes #33090

Summary

req.socket.bytesRead returns undefined in Deno's node:http server because the HTTP server uses a FakeSocket (an EventEmitter subclass) instead of a real Socket with a TCP handle. Node.js returns the total bytes received on the underlying TCP connection.

This PR:

  • Adds bytesRead and bytesWritten properties to FakeSocket, initialized to 0
  • Estimates the HTTP request head size (request line + headers) and sets bytesRead accordingly when a new request arrives
  • Tracks body bytes as they are consumed from the ReadableStream reader in IncomingMessageForServer

The 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 of undefined, which is what libraries relying on this property need.

Verification

Built with cargo build --features hmr and ran the exact reproducer from the issue:

import http from "node:http";
const server = http.createServer((req, res) => {
  req.on("end", () => {
    console.log(req.socket.bytesRead);
    res.end("ok");
    server.close();
  });
  req.resume();
});
server.listen(0, () => {
  const port = server.address().port;
  const req = http.request({ method: "PUT", port, hostname: "127.0.0.1" });
  req.write("hello");
  req.end();
});
Output
Stock Deno 2.7.12 (before) undefined
HMR build with fix (after) 74

Node.js outputs 107 for the same request (includes raw TCP framing for chunked transfer encoding). The difference is expected because Deno.serve() handles HTTP parsing internally and only exposes decoded body bytes — the exact raw TCP byte count is unavailable. The important fix is that bytesRead is now a number > 0 instead of undefined.

Test plan

  • Added spec test tests/specs/node/http_server_socket_bytes_read/ that verifies req.socket.bytesRead is a number > 0 after the request body is consumed
  • Verified fix locally using cargo build --features hmr
  • deno fmt --check passes on changed files
  • deno lint passes on changed files

AI disclosure

This PR was authored with assistance from Claude Code.

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).
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 9, 2026

CLA assistant check
All committers have signed the CLA.

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.

req.socket.bytesRead is undefined in node:http server

2 participants