feat: implement Response.clone()#312
Open
godronus wants to merge 1 commit into
Open
Conversation
f11e19c to
1c3a6cf
Compare
This was referenced Jun 8, 2026
90647e3 to
c28ec93
Compare
… tee Fix Headers cloning bug -- re: Immutable.
c28ec93 to
43436f3
Compare
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.
Implement Response.clone() (WHATWG Fetch)
Implements
Response.clone()per the WHATWG Fetch spec. All 21 WPT tests infetch/api/response/response-clone.any.jspass.Approach
Response bodies are modelled as default (non-byte)
ReadableStreams, whose tee shares a singlechunk object between both branches. Fetch's "clone a body" requires the clone to receive independent
copies, so
Response::cloneimplements the WHATWGReadableStreamDefaultTeealgorithm withcloneForBranch2 = true:object identity, and a
JSAutoStructuredCloneBuffercopy is enqueued into branch2 (the clone'sbody).
lazy copy would be read-order dependent and would duplicate already-mutated bytes if the original
branch is read and mutated in place first.
Cancellation is tracked per branch (
canceled1/canceled2): a cancelled branch is dropped from theread loop while the other continues.
sc_callbacksis not needed — body chunks areTypedArray/ArrayBuffer/DataView, handled by thebuilt-in structured clone.
Design notes
count as the spec byte-stream tee.
highWaterMarkis 0 and chunks are pulled on demand, so thebody is never fully buffered.
this directly (
CloneAsUint8Array, no hand-rolled tee), but that is a large change across allbody I/O and is out of scope here.
Request::clonecurrently tees without copying branch2; aligning it is a reasonablefollow-up.
Changes
builtins/web/fetch/request-response.hResponse::clonedeclaration.builtins/web/fetch/request-response.cpp#include "js/StructuredClone.h".Request::bodyUsed_get/Response::bodyUsed_get: fall back toJS::ReadableStreamIsDisturbedwhen the
BodyUsedslot is false, sobodyUsedreportstrueafterbody.cancel()and otherexternal stream disturbances.
CloneTeeSlot(shared-state layout),clone_tee_pump,clone_tee_read_then_handler,clone_tee_read_catch_handler,clone_tee_cancel_algorithm,clone_tee_pull_algorithm.Response::clone: acquires a default reader on the body, creates twoNativeStreamSourcebranches over shared tee state, and assigns branch1 to the original responseand branch2 to the clone.
JS_FN("clone", Response::clone, 0, JSPROP_ENUMERATE).tests/wpt-harness/expectations/fetch/api/response/response-clone.any.js.jsonWPT results
fetch/api/response/response-clone.any.js— 21/21 pass.Side-effect fixes from the
ReadableStreamIsDisturbedfallback inbodyUsed_get:fetch/api/response/response-stream-disturbed-6.any.js— 5 FAIL → PASS (partially resolves Stream disturbed test failures #184)fetch/api/response/response-stream-disturbed-by-pipe.any.js— 2 FAIL → PASSfetch/api/request/request-disturbed.any.js— 1 FAIL → PASSfetch/api/abort/general.any.js— 2 FAIL → PASSfetch/api/body/mime-type.any.js— new expectations file (6 PASS, 14 FAIL). The file previouslyaborted during initialisation on a top-level
clone()that threwTypeError; it now loads, and the14 failures expose a pre-existing gap where
parse_body<Blob>does not populateblob.typefromthe
Content-Typeheader. Tracked in #311.Downstream testing: PASSING
https://github.com/G-Core/FastEdge-sdk-js/blob/main/integration-tests/test-application/handlers/response-clone.ts