Skip to content

Commit ca48949

Browse files
authored
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 cc8931a commit ca48949

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

doc/api/stream.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1900,12 +1900,10 @@ added: REPLACEME
19001900
* `options` {Object}
19011901
* `signal` {AbortSignal} allows cancelling the toArray operation if the
19021902
signal is aborted.
1903-
* Returns: {Promise} a promise containing an array (if the stream is in
1904-
object mode) or Buffer with the contents of the stream.
1903+
* Returns: {Promise} a promise containing an array with the contents of the
1904+
stream.
19051905

1906-
This method allows easily obtaining the contents of a stream. If the
1907-
stream is in [object mode][object-mode] an array of its contents is returned.
1908-
If the stream is not in object mode a Buffer containing its data is returned.
1906+
This method allows easily obtaining the contents of a stream.
19091907

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

lib/internal/streams/operators.js

-4
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: {
@@ -224,9 +223,6 @@ async function toArray(options) {
224223
}
225224
ArrayPrototypePush(result, val);
226225
}
227-
if (!this.readableObjectMode) {
228-
return Buffer.concat(result);
229-
}
230226
return result;
231227
}
232228

test/parallel/test-stream-toArray.js

+7-5
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)