Repository navigation
[CB-0] [sdk-superfan] Let fetch set content-length - #241
Draft
spicysouvlaki wants to merge 1 commit into
Draft
spicysouvlaki wants to merge 1 commit into
spicysouvlaki wants to merge 1 commit into
Conversation
buildHeaders() set content-length from calculateContentLength() on every request with a body. Since #191 (v2.14.1) the auto Node runtime routes requests through native fetch, so that header now meets undici's fetch. undici fetch < 7.26 / < 8.3 (Node 18/20/22 bundled builds, e.g. Node 22.14 bundles 6.21.1) appends its own computed Content-Length to the caller's in httpNetworkOrCacheFetch, giving "52, 52". The bundled dispatcher parses that leniently with parseInt, so nobody noticed. A userland undici >= 7.26 / 8.3 dispatcher installed with setGlobalDispatcher() validates the value with isValidContentLengthHeaderValue() in core/request.js and throws InvalidArgumentError: invalid content-length header. undici 7.26 / 8.3 also stopped appending when the header already exists, so Node 24.19 (bundled undici 7.29) does not reproduce. Measured on Node 24.19 against api.dev.browserbase.com, sessions.create: - main, native fetch + undici 8.8.0 dispatcher: request accepted (429 burst limit, headers fine) - main, undici 6.21.1 fetch (Node 22.14 stand-in) + undici 8.8.0 dispatcher: APIConnectionError, cause InvalidArgumentError invalid content-length header UND_ERR_INVALID_ARG - this commit, same two combos: OK, session released Local server sweep: fetch 6.21.1 or 7.24.0 with dispatcher 7.26.0 / 8.8.0 / 8.12.4 all throw on main. Both runtimes compute the header themselves: native fetch from the body, node-fetch v2 via getTotalBytes() for strings and ArrayBufferViews. Multipart streams keep their explicit Content-Length from getMultipartRequestOptions(). src/core.ts is generated; this edit is sealed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What happened
#197: with a userland undici dispatcher installed via
setGlobalDispatcher(), everysessions.createfails withInvalidArgumentError: invalid content-length header(UND_ERR_INVALID_ARG).Why
buildHeaders()always setscontent-length. Since v2.14.1 the Node runtime sends requests through native fetch (runtime-node.ts:51). undici fetch before 7.26 / 8.3 (bundled in Node 18, 20 and 22) appends its own value, so the header becomes"52, 52". A dispatcher from undici 7.26+ or 8.3+ rejects that value. Node 24.19 bundles undici 7.29, which no longer appends, so it does not reproduce there.Fix
Stop setting
content-length. Native fetch and node-fetch both compute it from the body. Multipart uploads keep their explicit length.Tests
buildRequestcontent-length assertions with one that checks the header is unset (fails on main:Received: "20"). Added a local-server test that checks exactly one correctcontent-lengtharrives, under native fetch and under node-fetch../scripts/lint,./scripts/test(203 passed),./scripts/buildall pass.Risk and rollout
src/core.tsis generated, so this is a sealed edit that may conflict on the next Stainless regen. It rolls out with the next SDK release. It removes a header that both supported fetch implementations already set.Fixes #197
🤖 Generated with Claude Code