perf(http): skip the body stream for a request without a body
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 21s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 24s
CI / runtimes (deno) (pull_request) Successful in 32s

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 <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-26 22:48:15 -05:00
1 parent 704622cf65
commit 88be9b1cb2
2 files changed
+19 -3

No files matched your search

+10 -1
View File
@@ -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<string, string> = {}) =>
// Chunked by default: a request with neither header has no body and never touches the stream.
const request = (headers: Record<string, string> = { '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<string, string>[]) {
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);