Skip to content

fetch: make readAllBytes sync #4349

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

fetch: make readAllBytes sync #4349

wants to merge 3 commits into from

Conversation

Uzlopak
Copy link
Contributor

@Uzlopak Uzlopak commented Jul 18, 2025

Because we cant access the ReadableStreamDefaultReader logic inside node core to provide our own reader with the specified logic as mentioned in the spec. Before readAllBytes is async, which is confusing because we provide two callbacks to it. Now it reads more natural for me.

Status

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

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

There is no reason to change it

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

This allocates way less promises and I expect to improve performance.

I don't really understand how this work tho. What if part of the body is not available?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jul 19, 2025

If body is null or invalid then it would break already in an earlier stage in the function which calls readAllBytes, because we call getReader() on the body which can throw an error. We expect in readAllFiles the generated Reader.

The only place we can error is when the reader is locked, so that read will throw. Other than that i dont see any code path possible to result in an error.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jul 19, 2025

LOL, I actually found a bug in the sri code. I was wondering why setting the rejection handler in readLoop is not working.

@mcollina
Copy link
Member

What about if a large file is transmitted? Is fetch already buffering all of that in memory?

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Jul 19, 2025

Only if you use one of those methods from the bodyMixing, like .text()``,.json() and so on. If you know that you expect a huge file you should stream it anyway.

For streams you have that logic directly in mainFetch called incrementalReadBody or so. There we use alot of magic like async generators.

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.

3 participants