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

stream: don't read more than can handle in webstream #46846

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,21 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj

let controller;

function onData(chunk) {
// Copy the Buffer to detach it from the pool.
if (Buffer.isBuffer(chunk) && !objectMode)
chunk = new Uint8Array(chunk);
controller.enqueue(chunk);
if (controller.desiredSize <= 0)
streamReadable.pause();
function fillData() {
if (controller.desiredSize <= 0) {
return;
}

let chunk;
while ((chunk = streamReadable.read(controller.desiredSize)) !== null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to make sure controller.desiredSize must be less than 1GB as the docs specify

[...] The size argument must be less than or equal to 1 GiB. [...]

from readable.read([size]) - Node Docs

Copy link
Member Author

Choose a reason for hiding this comment

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

Should I don't specify size argument here, in case the desiredSize is really big?

// Copy the Buffer to detach it from the pool.
if (Buffer.isBuffer(chunk) && !objectMode)
chunk = new Uint8Array(chunk);
controller.enqueue(chunk);

if (controller.desiredSize <= 0)
break;
}
}

streamReadable.pause();
Expand All @@ -454,12 +462,14 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
controller.close();
});

streamReadable.on('data', onData);
// Using 'readable' and not 'data' event as we want to know when the data
// is available but not consume it as maybe the ReadableStream internal queue is full
streamReadable.on('readable', fillData);

return new ReadableStream({
start(c) { controller = c; },

pull() { streamReadable.resume(); },
pull() { fillData(); },

cancel(reason) {
destroy(streamReadable, reason);
Expand Down