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 disturbed ReadableStream error #65

Closed
wants to merge 2 commits into from
Closed

fix disturbed ReadableStream error #65

wants to merge 2 commits into from

Conversation

jamesthomp
Copy link

Fixes #52

When using this in the Cloudflare Workers runtime, I noticed an error:
Screenshot 2024-03-24 at 15 09 08

The issue doesn't reliably reproduce, but given the error message I suspect that the first Request on line 55 in the screenshot is consuming some of the body ReadableStream, which causes it to then throw on line 58.

The fetch spec states that the duplex property must be set if the body is a ReadableStream:
Screenshot 2024-03-24 at 15 18 05

So I've adjusted the code to do this

@@ -91,17 +91,9 @@ export class AwsClient {
input = url
}
const signer = new AwsV4Signer(Object.assign({ url: input }, init, this, init && init.aws))
const signed = Object.assign({}, init, await signer.sign())
const signed = Object.assign(signer.body instanceof ReadableStream ? { duplex: 'half' } : {}, init, await signer.sign())
Copy link
Author

Choose a reason for hiding this comment

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

potentially we could always include { duplex: 'half' }, I'm not sure how necessary it is to actually run the signer.body instanceof ReadableStream check.

return new Request(signed.url.toString(), signed)
} catch (e) {
if (e instanceof TypeError) {
// https://bugs.chromium.org/p/chromium/issues/detail?id=1360943
Copy link
Owner

Choose a reason for hiding this comment

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

As you can see from here – it's a Chrome workaround specifically.

You'd need to check that { duplex: 'half' } works on all platforms (and that it's appropriate)

@mhart
Copy link
Owner

mhart commented Mar 24, 2024

@jamesthomp there might be something else going on here – namely that there is a TypeError being thrown for a different reason during the construction of the Request (or that it's being thrown because the stream has already been read from, and specifying { duplex: 'half' } isn't going to make any difference)

@jamesthomp
Copy link
Author

Thanks for taking a look - I'll close this out if you don't believe its the right fix. Hopefully I'll catch the original TypeError the next time this happens to understand this problem better

@jamesthomp jamesthomp closed this Mar 24, 2024
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.

Race condition with ReadableStream
2 participants