From 1c22337b6eef8eb4b0de72d1c919393a0e170fc9 Mon Sep 17 00:00:00 2001 From: lilsweetcaligula <15699226+lilsweetcaligula@users.noreply.github.com> Date: Fri, 17 Mar 2023 01:38:11 +0200 Subject: [PATCH 1/2] stream: prevent memory overflow due to the default strategy --- lib/internal/webstreams/adapters.js | 3 ++- .../test-stream-readable-strategy-option.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 2ebc18d7645cc9..589be2d7ff81de 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -25,6 +25,7 @@ const { const { CountQueuingStrategy, + ByteLengthQueuingStrategy, } = require('internal/webstreams/queuingstrategies'); const { @@ -421,7 +422,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj // back to a minimal strategy that just specifies the highWaterMark // and no size algorithm. Using a ByteLengthQueuingStrategy here // is unnecessary. - return { highWaterMark }; + return new ByteLengthQueuingStrategy({ highWaterMark }); }; const strategy = evaluateStrategyOrFallback(options?.strategy); diff --git a/test/parallel/test-stream-readable-strategy-option.js b/test/parallel/test-stream-readable-strategy-option.js index a32e70ef2155ea..c4b9417d199e24 100644 --- a/test/parallel/test-stream-readable-strategy-option.js +++ b/test/parallel/test-stream-readable-strategy-option.js @@ -1,8 +1,10 @@ +// Flags: --expose-internals 'use strict'; const common = require('../common'); const { Readable } = require('stream'); const assert = require('assert'); const { strictEqual } = require('assert'); +const { kState } = require('internal/webstreams/util'); { // Strategy 2 @@ -73,3 +75,16 @@ const { strictEqual } = require('assert'); strictEqual(done, true); }); } + +{ + const readable = new Readable({ + read: common.mustCall(() => { + readable.push(null); + }), + }); + + const readableStream = Readable.toWeb(readable); + const { sizeAlgorithm: size } = readableStream[kState].controller[kState]; + + assert.strictEqual(size(new ArrayBuffer(19)), 19); +} From 3f1d68039c247d9a28ab468ebf90f115a939aa02 Mon Sep 17 00:00:00 2001 From: lilsweetcaligula <15699226+lilsweetcaligula@users.noreply.github.com> Date: Fri, 17 Mar 2023 10:32:11 +0200 Subject: [PATCH 2/2] Update the comment --- lib/internal/webstreams/adapters.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 589be2d7ff81de..1facc01f22e1e6 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -419,9 +419,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj } // When not running in objectMode explicitly, we just fall - // back to a minimal strategy that just specifies the highWaterMark - // and no size algorithm. Using a ByteLengthQueuingStrategy here - // is unnecessary. + // back to ByteLengthQueuingStrategy. return new ByteLengthQueuingStrategy({ highWaterMark }); };