Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b3390b9

Browse filesBrowse files
benjamingrBethGriggs
authored andcommitted
stream: never flatten on toArray
PR-URL: #41615 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 46ec74d commit b3390b9
Copy full SHA for b3390b9

File tree

Expand file treeCollapse file tree

3 files changed

+10
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+10
-14
lines changed
Open diff view settings
Collapse file

‎doc/api/stream.md‎

Copy file name to clipboardExpand all lines: doc/api/stream.md
+3-5Lines changed: 3 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1846,12 +1846,10 @@ added: REPLACEME
18461846
* `options` {Object}
18471847
* `signal` {AbortSignal} allows cancelling the toArray operation if the
18481848
signal is aborted.
1849-
* Returns: {Promise} a promise containing an array (if the stream is in
1850-
object mode) or Buffer with the contents of the stream.
1849+
* Returns: {Promise} a promise containing an array with the contents of the
1850+
stream.
18511851

1852-
This method allows easily obtaining the contents of a stream. If the
1853-
stream is in [object mode][object-mode] an array of its contents is returned.
1854-
If the stream is not in object mode a Buffer containing its data is returned.
1852+
This method allows easily obtaining the contents of a stream.
18551853

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

‎lib/internal/streams/operators.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/operators.js
-4Lines 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: {
@@ -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

Collapse file

‎test/parallel/test-stream-toArray.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-stream-toArray.js
+7-5Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.