Skip to content

Commit 31cee4f

Browse files
benjamingrtargos
authored andcommitted
stream: never flatten on toArray
PR-URL: #41615 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 2922fbb commit 31cee4f

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

doc/api/stream.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,12 +1851,10 @@ added: v16.15.0
18511851
* `options` {Object}
18521852
* `signal` {AbortSignal} allows cancelling the toArray operation if the
18531853
signal is aborted.
1854-
* Returns: {Promise} a promise containing an array (if the stream is in
1855-
object mode) or Buffer with the contents of the stream.
1854+
* Returns: {Promise} a promise containing an array with the contents of the
1855+
stream.
18561856

1857-
This method allows easily obtaining the contents of a stream. If the
1858-
stream is in [object mode][object-mode] an array of its contents is returned.
1859-
If the stream is not in object mode a Buffer containing its data is returned.
1857+
This method allows easily obtaining the contents of a stream.
18601858

18611859
As this method reads the entire stream into memory, it negates the benefits of
18621860
streams. It's intended for interoperability and convenience, not as the primary

lib/internal/streams/operators.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const { AbortController } = require('internal/abort_controller');
4-
const { Buffer } = require('buffer');
54

65
const {
76
codes: {
@@ -291,9 +290,6 @@ async function toArray(options) {
291290
}
292291
ArrayPrototypePush(result, val);
293292
}
294-
if (!this.readableObjectMode) {
295-
return Buffer.concat(result);
296-
}
297293
return result;
298294
}
299295

test/parallel/test-stream-toArray.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ const assert = require('assert');
2424
}
2525

2626
{
27-
// Works on a non-object-mode stream and flattens it
27+
// Works on a non-object-mode stream
2828
(async () => {
29+
const firstBuffer = Buffer.from([1, 2, 3]);
30+
const secondBuffer = Buffer.from([4, 5, 6]);
2931
const stream = Readable.from(
30-
[Buffer.from([1, 2, 3]), Buffer.from([4, 5, 6])]
31-
, { objectMode: false });
32+
[firstBuffer, secondBuffer],
33+
{ objectMode: false });
3234
const result = await stream.toArray();
33-
assert.strictEqual(Buffer.isBuffer(result), true);
34-
assert.deepStrictEqual(Array.from(result), [1, 2, 3, 4, 5, 6]);
35+
assert.strictEqual(Array.isArray(result), true);
36+
assert.deepStrictEqual(result, [firstBuffer, secondBuffer]);
3537
})().then(common.mustCall());
3638
}
3739

0 commit comments

Comments
 (0)