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

fix: clone methods correctly handle teeing body stream #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

slightlytyler
Copy link

@slightlytyler slightlytyler commented Jun 5, 2023

Encountered error This stream has already been locked for exclusive reading by another reader when cloning a response that gets read as a stream. Problem was we did not handle teeing the body stream when cloning happens. The fix is:

  1. Response/Request clone method creates a new instance with a null body
  2. Source body is cloned
  3. Body clone method tees stream, sets its stream to one output, and returns a body with second output
  4. Response/Request clone sets body on cloned instance
  5. Response/Request clone returns new instance

Also added a few updates to correct the behavior of this library wrt the spec. Namely body class handles explicitly null bodyInit and request/response clone methods check if the body has been used before executing.

Test cases added / modified to verify the new cloning behavior

@slightlytyler
Copy link
Author

Was hoping the tests would work in CI since I can't get them to run locally but looks like they're running into similar environment problems

@@ -5,7 +5,8 @@
"requires": true,
"packages": {
"": {
"version": "1.0.2",
"name": "react-native-fetch-api",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk why this is changing when i npm install

@@ -228,6 +233,16 @@ class Body {
},
});
}

clone() {
Copy link
Author

@slightlytyler slightlytyler Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -85,7 +85,14 @@ class Request {
}

clone() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -21,12 +21,19 @@ class Response {
}

clone() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -85,7 +85,14 @@ class Request {
}

clone() {
return new Request(this, { body: this._body._bodyInit });
if (this.bodyUsed) {
throw new TypeError("Already read");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -21,12 +21,19 @@ class Response {
}

clone() {
return new Response(this._body._bodyInit, {
if (this.bodyUsed) {
throw new TypeError("Already read");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -46,7 +46,7 @@ class Request {
this.signal = request.signal;
this.headers = new Headers(options.headers ?? request.headers);

if (!options.body && request._body._bodyInit) {
if (options.body === undefined && request._body._bodyInit) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to avoid explicit null body triggering this case I think

@slightlytyler slightlytyler changed the title Fix text streaming clone bug fix: clone methods correctly handle teeing body stream Jun 7, 2023
@slightlytyler slightlytyler marked this pull request as ready for review June 7, 2023 21:19
@slightlytyler
Copy link
Author

@acostalima are you still maintaining? Code here is good to go but the test infra is broken locally and in CI. I got it working locally by editing the generated rn test app but unsure of what the real fix is. seems like a problem with react-native-test-runner rather than config in this project though? happy to do whatever needed to get this merged in 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants