Skip to content

Commit

Permalink
stream: dont read more than can handle in webstream
Browse files Browse the repository at this point in the history
  • Loading branch information
rluvaton committed Feb 26, 2023
1 parent d953049 commit e0a2204
Showing 1 changed file with 19 additions and 9 deletions.
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 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

0 comments on commit e0a2204

Please sign in to comment.