Files
core/test/read-body.test.ts
T
chrnx 8b67b1a46c
CI / dco (pull_request) Successful in 6s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 28s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 32s
perf(http): trim per-request work on the Node path
Profiling the validate (POST) bench route against 4d1639f showed the
loss since then spread over several small costs on the Node request
path rather than one regression. This removes the ones that do not
change behaviour:

- readBody reads with data/end/error/close listeners instead of
  `for await`, whose async iterator costs a promise and a microtask per
  chunk. It keeps the iterator's semantics: the running total enforces
  the limit, an over-limit body destroys the request as leaving the loop
  did, an error or a close before `end` rejects (the latter with
  ERR_STREAM_PREMATURE_CLOSE), and the declared-length 413 is still a
  rejection rather than a synchronous throw. A single chunk is returned
  without Buffer.concat.
- writeResult is no longer async. A buffered response without
  compression is written synchronously and returns nothing to await;
  only a stream or a compressed body returns a promise, which serve
  awaits, so a failure still reaches failSafe. The compression request
  object is not built when compression is off.
- declaresOverLimit compares the number first and runs its /^\d+$/
  shape test only when the length is over the limit.

The `serve().catch(failSafe)` wrapper stays: it is the #108 guarantee,
and handle() is asynchronous, so there is no synchronous path to skip.

Local A/B against 74faa55 (5 s x 4 rounds, macOS, indicative only):
validate (POST) +4.6%. test/read-body.test.ts pins readBody's
semantics: joining, empty body, 413 with destroy, declared-length 413,
stream error and premature close.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-26 08:02:03 -05:00

49 lines
1.7 KiB
TypeScript

// readBody reads with listeners instead of `for await`; these pin the iterator's semantics it keeps.
import { describe, expect, it } from 'vitest';
import { PassThrough } from 'node:stream';
import type http from 'node:http';
import { readBody } from '../src/http/request';
const request = (headers: Record<string, string> = {}) =>
Object.assign(new PassThrough(), { headers }) as unknown as http.IncomingMessage & PassThrough;
describe('readBody', () => {
it('joins every chunk, and answers undefined for an empty body', async () => {
const req = request();
const read = readBody(req, 100);
req.write('ab');
req.end('cd');
expect((await read)?.toString()).toBe('abcd');
const empty = request();
const none = readBody(empty, 100);
empty.end();
expect(await none).toBeUndefined();
});
it('a body over the limit rejects 413 and destroys the request', async () => {
const req = request();
const read = readBody(req, 3);
req.write('abcd');
await expect(read).rejects.toMatchObject({ status: 413 });
expect(req.destroyed).toBe(true);
});
it('a declared content-length over the limit rejects without reading', async () => {
await expect(readBody(request({ 'content-length': '10' }), 3)).rejects.toMatchObject({ status: 413 });
});
it('a stream error or a close before end rejects', async () => {
const failing = request();
const read = readBody(failing, 100);
failing.destroy(new Error('boom'));
await expect(read).rejects.toThrow('boom');
const closed = request();
const cut = readBody(closed, 100);
closed.write('ab');
closed.destroy();
await expect(cut).rejects.toMatchObject({ code: 'ERR_STREAM_PREMATURE_CLOSE' });
});
});