Repository navigation
Commit 62c65db
authored
fix(teeny-request): set Content-Length for buffered request bodies in Bun (#9748)
## Description
Sets `Content-Length` for `string`, `Buffer`, and `Uint8Array` bodies in
`bunFetch`.
## Impact
Prevents unexpected chunked transfer encoding on buffered requests under
Bun.
## Changes
- **Stack Overview** (for `b/578555176`, replacing #9741):
1. **#9748 (this PR — kept as small as possible)**:
`fix/teeny-request-bun-part-1` $\to$ `main` — Set `Content-Length` for
buffered request bodies (`string`, `Buffer`, `Uint8Array`) in
`bunFetch`.
2. #9749: `fix/teeny-request-bun-part-2` $\to$
`fix/teeny-request-bun-part-1` — Default `Content-Length: '0'` for
bodyless `POST` and `PUT` requests in `bunFetch`.
3. #9750: `fix/teeny-request-bun-part-3` $\to$
`fix/teeny-request-bun-part-2` — Lazily initialize `zlib` decompressor
in `bunFetch` so `0`-byte compressed responses do not throw
`Z_BUF_ERROR`.
4. #9751: `fix/teeny-request-bun-part-4` $\to$
`fix/teeny-request-bun-part-3` — Auto-detect RFC 1950 vs. raw RFC 1951
`deflate` streams in `bunFetch`.
5. #9752: `fix/teeny-request-bun-style-maintenance` $\to$
`fix/teeny-request-bun-part-4` — `[Style Maintenance]` JSDoc
documentation and `CONTRIBUTING.md` / `gts` alignment.
- Compute `headers['Content-Length'] =
String(Buffer.byteLength(init.body))` in `bunFetch`
(`core/packages/teeny-request/src/index.ts`) for `string`, `Buffer`, and
`Uint8Array` request bodies when neither `Content-Length` nor
`Transfer-Encoding` is already set.
## Testing
- Added unit test in `core/packages/teeny-request/test/index.ts`
verifying `Content-Length: 5` on `POST` requests with a string body
under `bunFetch`.
- **Steps to Reproduce (in terminal)**:
- Direct terminal command: Run `pnpm --dir core/packages/teeny-request
run compile && node bin/run-test.cjs --no-c8
core/packages/teeny-request/build/test/index.js` against `main` (fails
`matchHeader('content-length', '5')`), then re-run on
`fix/teeny-request-bun-part-1` to confirm it passes.
- Or via shortened reproduction Gist
([repro.mjs](https://gist.github.com/danieljbruce/990f5fdb7806eb3454cccea3c1ceb3bd)):
`gh gist view
https://gist.github.com/danieljbruce/990f5fdb7806eb3454cccea3c1ceb3bd
--raw | node --input-type=module -`
## Alternatives
- Relying on Node's `http.request` default chunked encoding for buffered
bodies was considered and rejected because `node-fetch` sets
`Content-Length` on in-memory request payloads and downstream Google
Cloud endpoints expect `Content-Length`.
- Not merging this PR was considered, but doing so leaves `bunFetch`
omitting `Content-Length` on buffered request bodies under Bun.1 parent 71972b8 commit 62c65db
2 files changed
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
86 | 101 | | |
87 | 102 | | |
88 | 103 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
706 | 706 | | |
707 | 707 | | |
708 | 708 | | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
709 | 730 | | |
710 | 731 | | |
711 | 732 | | |
| |||
0 commit comments