deno
5208330c - fix(ext/fetch): preserve static request body length (#34546)

Commit
58 days ago
fix(ext/fetch): preserve static request body length (#34546) Preserve the known byte length when an already-buffered request body is exposed as `req.body` and then forwarded via `fetch()`. The buffered request-body fast path from #33844 can produce a static `InnerBody` from bytes that Deno already has in memory. However, when that body is materialized as a public `ReadableStream`, the known length was lost. As a result, forwarding the body with `fetch(url, { method: "POST", body: req.body })` sent an unknown-length stream and omitted `Content-Length`. This stores the length in an internal `WeakMap` only for `ReadableStream`s materialized from Deno's static `InnerBody`, then restores that length in `extractBody()`. User-created `ReadableStream` bodies remain unknown-length. ## Test plan - [x] Added a regression spec for forwarding `req.body` and asserting that the upstream request receives the original `Content-Length`. - [x] Confirmed arbitrary user-created `ReadableStream` bodies remain unknown-length. - [x] Benchmarked twice on Ryzen 9 7940HS Linux with `oha`. Benchmark result: - `req.text()` / `req.json()` / `req.arrayBuffer()` consume throughput remains unchanged within noise. - Forwarding throughput improves by `+14–25%` in run 1 and `+11–25%` in run 2, because the known length avoids chunked framing.
Author
Parents
Loading