Conversation
HttpClientResponse now exposes the platform response it was built from and adds toWeb, which returns the original Web Response when the client provides one. Runtimes can attach capabilities to native response bodies that a stream rebuilt in JavaScript cannot carry. On workerd, R2Bucket.put accepts toWeb(response).body but rejects Stream.toReadableStreamEffect(response.stream) with "Provided readable stream must have a known length". Refs Effect-TS#8205
🦋 Changeset detectedLatest commit: b2595ea The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
Contributor
Author
|
@tim-smart |
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 was wrong
HttpClientResponsealready holds the original WebResponse, but the field wasprivate, so nobody could get to it. The only public body API was the EffectStream.That bites on workerd. R2 refuses a stream unless it knows the length up front:
The original response body knows its length.
Stream.toReadableStreamEffect(response.stream)builds a brand new stream and throws that knowledge away. So this fails:What this PR does
Adds
HttpClientResponse.toWeband exposessource, so you can hand R2 the original body:HttpClientRequest,HttpServerRequestandHttpServerResponsealready hadtoWeb. This just fills the gap.Why not fix the stream conversion instead
Because it can't be fixed. The length is a marker on native workerd streams, and any
new ReadableStream({...})drops it. AContent-Lengthheader does not bring it back. On top of that,HttpClient.executewraps the body inStream.ensuring(..., controller.abort())to abort the request on interruption, and that finalizer has to keep running — we can't just pass the original stream through.Same bytes, put into R2 in workerd:
new Response(bytes).bodybody.tee()[0]new ReadableStream({ start })new Response(stream, { "content-length": "3" }).bodytoReadableStreamEffect(response.stream)Tests
I ran the whole repo suite from the repo root, the same way CI does (
pnpm test --run --max-concurrency=10): 11848 passed.The only failures are environment problems that already fail on
main:platform-node-shared > NodeChildProcessSpawner: the process-group kill tests also fail onmainon this machine (started from a clean checkout ofmain)effect > cluster/ClusterWorkflowEngine: one flaky timeout under full-suite parallelism — it passes when run on its own, and 3 out of 3 on repeatPlus:
HttpClientResponse.test.tscheckstoWebreturns the very sameResponse, also throughHttpClient.executepnpm check,test-types,oxlint,dprint check→ passtoWeb(response).bodyaccepted by R2,toReadableStreamEffect(response.stream)rejectedpnpm jsdocs --checkfails onunstable/ai/McpSchema.tsboth before and after this change