From e5971f4728e4fbf9bdfbed23963de0ef05a246d0 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 23 Jan 2022 10:19:53 +0200 Subject: [PATCH] stream: never flatten on toArray PR-URL: https://github.com/nodejs/node/pull/41615 Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy Reviewed-By: Antoine du Hamel --- doc/api/stream.md | 8 +++----- lib/internal/streams/operators.js | 4 ---- test/parallel/test-stream-toArray.js | 12 +++++++----- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index dab170820d5b46..372995262df69e 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1851,12 +1851,10 @@ added: v16.15.0 * `options` {Object} * `signal` {AbortSignal} allows cancelling the toArray operation if the signal is aborted. -* Returns: {Promise} a promise containing an array (if the stream is in - object mode) or Buffer with the contents of the stream. +* Returns: {Promise} a promise containing an array with the contents of the + stream. -This method allows easily obtaining the contents of a stream. If the -stream is in [object mode][object-mode] an array of its contents is returned. -If the stream is not in object mode a Buffer containing its data is returned. +This method allows easily obtaining the contents of a stream. As this method reads the entire stream into memory, it negates the benefits of streams. It's intended for interoperability and convenience, not as the primary diff --git a/lib/internal/streams/operators.js b/lib/internal/streams/operators.js index 42807edf1c7eca..707112f75e6c84 100644 --- a/lib/internal/streams/operators.js +++ b/lib/internal/streams/operators.js @@ -1,7 +1,6 @@ 'use strict'; const { AbortController } = require('internal/abort_controller'); -const { Buffer } = require('buffer'); const { codes: { @@ -291,9 +290,6 @@ async function toArray(options) { } ArrayPrototypePush(result, val); } - if (!this.readableObjectMode) { - return Buffer.concat(result); - } return result; } diff --git a/test/parallel/test-stream-toArray.js b/test/parallel/test-stream-toArray.js index 3bd15e7c0fbf34..8cc2350edc11f8 100644 --- a/test/parallel/test-stream-toArray.js +++ b/test/parallel/test-stream-toArray.js @@ -24,14 +24,16 @@ const assert = require('assert'); } { - // Works on a non-object-mode stream and flattens it + // Works on a non-object-mode stream (async () => { + const firstBuffer = Buffer.from([1, 2, 3]); + const secondBuffer = Buffer.from([4, 5, 6]); const stream = Readable.from( - [Buffer.from([1, 2, 3]), Buffer.from([4, 5, 6])] - , { objectMode: false }); + [firstBuffer, secondBuffer], + { objectMode: false }); const result = await stream.toArray(); - assert.strictEqual(Buffer.isBuffer(result), true); - assert.deepStrictEqual(Array.from(result), [1, 2, 3, 4, 5, 6]); + assert.strictEqual(Array.isArray(result), true); + assert.deepStrictEqual(result, [firstBuffer, secondBuffer]); })().then(common.mustCall()); }