feat(node): add clone() support to ImpitResponse#419
Merged
barjin merged 6 commits intoapify:masterfrom Mar 24, 2026
Merged
Conversation
d4b51c2 to
abb5deb
Compare
Implement Response.clone() on ImpitResponse using ReadableStream.tee() to split the body into two independent streams. The original response's body methods are re-bound to one stream; the clone is a standard Response backed by the other. - Streaming preserved (no eager buffering) - Charset-aware text() preserved on original via decodeBuffer - Throws TypeError on double clone or clone after body consumed - Clone returns standard Response (with url set via defineProperty)
- Add bodyConsumed flag: clone() after body consumption now throws
a clear TypeError ("body has already been consumed") instead of a
cryptic ReadableStream error
- Move cloned=true after successful tee() to avoid side effects on
failure
- Change dts-header from class to interface: TypeScript does not
merge duplicate class declarations, causing TS2300 when prepended
to the auto-generated index.d.ts
- Separate error messages: "body has already been consumed" vs
"Response has already been cloned"
- Add tests: arrayBuffer on both, read-original-first order, non-200
status preservation, strict TypeError + message assertion
abb5deb to
f71719d
Compare
- Override body getter after tee() so response.body returns the delegate's stream instead of the original locked stream - Remove single-clone restriction (cloned flag) to match Fetch spec - Add markConsumed + configurable to re-patched methods so consumption tracking and subsequent clones work correctly - Add tests: multiple clones, body streaming after clone
barjin
approved these changes
Mar 24, 2026
Member
barjin
left a comment
There was a problem hiding this comment.
Thank you @privatenumber !
I just cleaned up some rough edges (e.g., moving the clone() declaration to the Rust code or making the text decoding work on the cloned response as well). Otherwise I am happy with the proposed changes.
Thanks again!
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.
Summary
Implements
Response.clone()onImpitResponseusingReadableStream.tee(), making impit compatible with libraries like ky that callresponse.clone()internally.Approach
When
.clone()is called:this.body.tee()splits the underlyingReadableStreaminto two independent streams (synchronous, no eager buffering).bodygetter and body methods (text,json,arrayBuffer,bytes) are re-patched to read from one streamResponseis returned as the clone, backed by the other streamMultiple clones are supported (matching the Fetch spec) — each call tees the current branch, so the original and all clones can be read independently.
Charset-aware
text()decoding is preserved on the original viadecodeBuffer. The clone uses standardResponse.text()(UTF-8).Throws
TypeErroron clone after body consumption, matching Fetch API semantics.Changes
index.wrapper.js— addclone()in#wrapResponse, re-patch.bodygetter and body methods withconfigurable: trueso subsequent clones workdts-header.d.ts— addclone(): ResponsetoImpitResponsevia declaration mergingtest/basics.test.ts— tests covering: return type, url/header preservation, independent body reads, text() on both, multiple clones, body streaming after clone, clone-after-consume error, arrayBuffer on both, read ordering, non-200 status