Merge pull request 'perf(http): recover the fetch adapter's per-request cost on Deno and Bun' (#175) from perf/web-headers-fast-path into develop
CI / dco (push) Has been skipped
CI / runtimes (bun) (push) Successful in 22s
CI / build (push) Successful in 29s
CI / runtimes (edge) (push) Successful in 23s
CI / runtimes (deno) (push) Successful in 33s
Promote → GitHub / push-github (push) Successful in 7s
CI / dco (push) Has been skipped
CI / runtimes (bun) (push) Successful in 22s
CI / build (push) Successful in 29s
CI / runtimes (edge) (push) Successful in 23s
CI / runtimes (deno) (push) Successful in 33s
Promote → GitHub / push-github (push) Successful in 7s
This commit was merged in pull request #175.
This commit is contained in:
commit
109297ca4c
5 files changed
+53
-2
No files matched your search
@@ -281,6 +281,15 @@ npm treats versions as semver and semver forbids leading zeros.
|
||||
only for a body that would be refused. Behaviour is unchanged: the same `413`s, the same
|
||||
rejection when a client goes away mid-body.
|
||||
|
||||
- **The fetch adapter (Deno, Bun, workerd) regains what two earlier changes in this release cost.**
|
||||
Response headers that are all plain strings, which is nearly every response, now go to
|
||||
`new Response` as the record they already are, not copied into a `Headers` one by one. That copy,
|
||||
added with `set-cookie` arrays, cost 5–8% on every Deno request. A body with a well-formed
|
||||
`Content-Length` inside the limit is read with `arrayBuffer()`, not chunk by chunk. The runtime
|
||||
frames the body by that header, so the bounded read is kept only for chunked uploads. The chunked
|
||||
read, added for the incremental `413`, had cut a Bun JSON `POST` by more than half. Behaviour is
|
||||
unchanged: the same `413`s, and `set-cookie` arrays still reach the client one line per cookie.
|
||||
|
||||
### Deprecated
|
||||
|
||||
- **The `Headers` type** (D19). Import `RequestHeaders` instead: same type, a name that does not
|
||||
|
||||
+7
-1
@@ -70,8 +70,14 @@ export function joinListHeaders<T extends Record<string, unknown>>(headers: T):
|
||||
* the Fetch spec stringifies an array value to `a,b`, which for `set-cookie` is one broken cookie.
|
||||
* `set-cookie` elements are appended one by one, so each reaches the client as its own line;
|
||||
* every other array is joined with `, `, exactly as {@link joinListHeaders} does for Node.
|
||||
*
|
||||
* A record of plain strings, which is nearly every response, is returned untouched: Deno and Bun
|
||||
* build their native headers from a record directly, and filling a `Headers` from JS instead measured
|
||||
* 5–8% on every Deno request.
|
||||
*/
|
||||
export function toWebHeaders(headers: Record<string, unknown>): Headers {
|
||||
export function toWebHeaders(headers: Record<string, unknown>): Headers | Record<string, string> {
|
||||
if (Object.values(headers).every((value) => typeof value === 'string')) return headers as Record<string, string>;
|
||||
|
||||
const out = new Headers();
|
||||
|
||||
for (const key of Object.keys(headers)) {
|
||||
|
||||
@@ -130,6 +130,20 @@ function readFetchBytes(
|
||||
return tooLarge(request, opts);
|
||||
}
|
||||
|
||||
// A well-formed declared length within the limit bounds the body already: the runtime's HTTP parser
|
||||
// frames it by that header, so `arrayBuffer()`, native in Bun, Deno and workerd, cannot overrun.
|
||||
// The reader loop below measured 4x slower on a Bun JSON POST. `byteLength` is still checked, for
|
||||
// a `Request` built in user code whose header and body disagree.
|
||||
if (/^\d+$/.test(request.headers.get('content-length') ?? '')) {
|
||||
return request
|
||||
.arrayBuffer()
|
||||
.then((buffer) =>
|
||||
buffer.byteLength > limit
|
||||
? tooLarge(request, opts)
|
||||
: { bytes: buffer.byteLength === 0 ? undefined : Buffer.from(buffer) },
|
||||
);
|
||||
}
|
||||
|
||||
// ponytail: one reader loop; a TransformStream byte-counter only if another consumer needs it
|
||||
return readBounded(request.body.getReader(), limit).then((bytes) =>
|
||||
bytes === null ? tooLarge(request, opts) : { bytes },
|
||||
|
||||
@@ -284,6 +284,14 @@ describe('app.fetch bounded body reads (#105)', () => {
|
||||
expect(await res.json()).toEqual({ length: 1_500_000 });
|
||||
});
|
||||
|
||||
it('a declared content-length under the limit does not let a longer body through', async () => {
|
||||
const body = countingStream(1, 20);
|
||||
|
||||
const res = await limitsApp.fetch(post('/limits/small', body.stream, { 'content-length': '5' }));
|
||||
|
||||
expect(res.status).toBe(413);
|
||||
});
|
||||
|
||||
it('a malformed content-length is ignored, and the stream count enforces the limit', async () => {
|
||||
const body = countingStream(1, 20);
|
||||
|
||||
|
||||
@@ -41,8 +41,22 @@ describe('array header values (D11)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('toWebHeaders returns a record of plain strings untouched', () => {
|
||||
const record = { 'content-type': 'application/json', vary: 'origin' };
|
||||
expect(toWebHeaders(record)).toBe(record);
|
||||
});
|
||||
|
||||
it('toWebHeaders drops undefined values', () => {
|
||||
const h = toWebHeaders({ 'content-type': 'text/plain', 'x-gone': undefined }) as Headers;
|
||||
expect(h.has('x-gone')).toBe(false);
|
||||
expect(h.get('content-type')).toBe('text/plain');
|
||||
});
|
||||
|
||||
it('toWebHeaders appends one set-cookie per element', () => {
|
||||
const h = toWebHeaders({ 'set-cookie': ['a=1; Expires=Tue, 01 Jan 2030 00:00:00 GMT', 'b=2'], 'x-list': ['a', 'b'] });
|
||||
const h = toWebHeaders({
|
||||
'set-cookie': ['a=1; Expires=Tue, 01 Jan 2030 00:00:00 GMT', 'b=2'],
|
||||
'x-list': ['a', 'b'],
|
||||
}) as Headers;
|
||||
expect(h.getSetCookie()).toEqual(['a=1; Expires=Tue, 01 Jan 2030 00:00:00 GMT', 'b=2']);
|
||||
expect(h.get('x-list')).toBe('a, b');
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user