Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloning a response twice locks the ReadableStream #1294

Closed
cprecioso opened this issue Mar 11, 2024 · 2 comments · Fixed by #1296
Closed

Cloning a response twice locks the ReadableStream #1294

cprecioso opened this issue Mar 11, 2024 · 2 comments · Fixed by #1296
Assignees
Labels
bug Something isn't working

Comments

@cprecioso
Copy link
Contributor

cprecioso commented Mar 11, 2024

Describe the bug

A continuation of #1216

In Response, when cloning a clone, none of them can then be consumed, they throw an "Invalid state: ReadableStream is locked" error.

To Reproduce

// test.mjs
import { Window } from "happy-dom";

const window = new Window();

const originalResponse = window.Response.json({ foo: 'bar' });

const clone1 = originalResponse.clone();
const clone2 = clone1.clone();

// Any of the following statements, in any order
// will throw an Invalid State Error
console.log(await clone1.text());
console.log(await clone2.text());

Expected behavior
Both response objects return the correct response text even if the other one has been consumed.
You can copy the code (minus the new Window()) to your browser console and see it working correctly.

Device:
node v20.11.0 on macOS Sonoma 14.3.1

Additional context
I'm trying to run ky on happy-dom, which uses the following pattern:

// (Pseudo code)

const response = await fetch(...)
const cloneA = response.clone()

const cloneB = cloneA.clone()
if ((await cloneB.arrayBuffer().byteLength === 0) return ""

return cloneA.json()

https://github.com/sindresorhus/ky/blob/051d7ab4fd39743758b49012b62aaf420f7ca53d/source/core/Ky.ts#L82-L106

@cprecioso cprecioso added the bug Something isn't working label Mar 11, 2024
@capricorn86 capricorn86 self-assigned this Mar 11, 2024
capricorn86 added a commit that referenced this issue Mar 11, 2024
…ce-locks-the-readablestream

fix: [#1294] Fixes problem related to cloning a cloned Response in Re…
@capricorn86
Copy link
Owner

Thank you @cprecioso for reporting! 🙂

There is a fix in now.

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v13.7.6

@cprecioso
Copy link
Contributor Author

Works on my project, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants