From 88be9b1cb27f3462e7f8208c8cca68f80812af4b Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Mon, 26 Oct 2026 22:48:15 -0500 Subject: [PATCH] perf(http): skip the body stream for a request without a body On Node every request goes through readBody, GET included. Subscribing listeners to an empty request stream measured ~4% slower than the old async iterator on an 8 KB GET response, in both orders on jarvis. A request with no Transfer-Encoding and no or a zero Content-Length has no body (RFC 9112 6.3), so resolve undefined without touching the stream. Node drains the unread request when the response ends. Signed-off-by: Diego Resendez --- src/http/request.ts | 11 +++++++++-- test/read-body.test.ts | 11 ++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/http/request.ts b/src/http/request.ts index 67a0b21..a3c0333 100644 --- a/src/http/request.ts +++ b/src/http/request.ts @@ -6,8 +6,15 @@ import { declaresOverLimit } from './body'; export function readBody(req: http.IncomingMessage, maxBodyBytes: number): Promise { // A declared length over the limit is refused without waiting for the first chunk, which could // otherwise sit until the request timeout. The fetch adapter makes the same check. - if (declaresOverLimit(req.headers['content-length'], maxBodyBytes)) - return Promise.reject(new HttpError(413, 'Payload Too Large')); + const contentLength = req.headers['content-length']; + if (declaresOverLimit(contentLength, maxBodyBytes)) return Promise.reject(new HttpError(413, 'Payload Too Large')); + + // No `Transfer-Encoding` and no (or a zero) `Content-Length` is a request without a body (RFC 9112 + // §6.3) — every GET. Node's parser has already finished it, so there is nothing to wait for, and + // subscribing to the stream anyway measured ~4% on an 8 KB GET response. Node drains the unread + // request itself when the response ends. + if (req.headers['transfer-encoding'] === undefined && (contentLength === undefined || contentLength === '0')) + return Promise.resolve(undefined); // Listeners rather than `for await`: the async iterator costs a promise and a microtask per chunk, // which a JSON POST pays on every request. The semantics are the iterator's: the running total diff --git a/test/read-body.test.ts b/test/read-body.test.ts index 927762d..411ee8c 100644 --- a/test/read-body.test.ts +++ b/test/read-body.test.ts @@ -4,7 +4,8 @@ import { PassThrough } from 'node:stream'; import type http from 'node:http'; import { readBody } from '../src/http/request'; -const request = (headers: Record = {}) => +// Chunked by default: a request with neither header has no body and never touches the stream. +const request = (headers: Record = { 'transfer-encoding': 'chunked' }) => Object.assign(new PassThrough(), { headers }) as unknown as http.IncomingMessage & PassThrough; describe('readBody', () => { @@ -21,6 +22,14 @@ describe('readBody', () => { expect(await none).toBeUndefined(); }); + it('a request without a body resolves undefined without subscribing to the stream', async () => { + for (const headers of [{}, { 'content-length': '0' }] as Record[]) { + const req = request(headers); + expect(await readBody(req, 100)).toBeUndefined(); + expect(req.listenerCount('data')).toBe(0); + } + }); + it('a body over the limit rejects 413 and destroys the request', async () => { const req = request(); const read = readBody(req, 3);