mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
stream: never flatten on toArray
PR-URL: https://github.com/nodejs/node/pull/41615 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
46ec74dcd1
commit
b3390b9ddb
3 files changed
+10
-14
No files matched your search
+3
-5
@@ -1846,12 +1846,10 @@ added: REPLACEME
|
||||
* `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
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const { AbortController } = require('internal/abort_controller');
|
||||
const { Buffer } = require('buffer');
|
||||
|
||||
const {
|
||||
codes: {
|
||||
@@ -224,9 +223,6 @@ async function toArray(options) {
|
||||
}
|
||||
ArrayPrototypePush(result, val);
|
||||
}
|
||||
if (!this.readableObjectMode) {
|
||||
return Buffer.concat(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user