Skip to content

Commit 3887f53

Browse files
committed
stream: add bytes() method to stream/consumers
- Add bytes() method to get Uint8Array from streams - Add tests for bytes() method in PassThrough and ObjectMode scenarios - Update documentation Fixes: #59542
1 parent 4fe325d commit 3887f53

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

doc/api/webstreams.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,41 @@ buffer(readable).then((data) => {
16941694
});
16951695
```
16961696
1697+
#### `streamConsumers.bytes(stream)`
1698+
1699+
<!-- YAML
1700+
added: v25.0.0
1701+
-->
1702+
1703+
* `stream` {ReadableStream|stream.Readable|AsyncIterator}
1704+
* Returns: {Promise} Fulfills with a {Uint8Array} containing the full
1705+
contents of the stream.
1706+
1707+
```mjs
1708+
import { bytes } from 'node:stream/consumers';
1709+
import { Readable } from 'node:stream';
1710+
1711+
const dataBuffer = Buffer.from('hello world from consumers!');
1712+
1713+
const readable = Readable.from(dataBuffer);
1714+
const data = await bytes(readable);
1715+
console.log(`from readable: ${data.length}`);
1716+
// Prints: from readable: 27
1717+
```
1718+
1719+
```cjs
1720+
const { bytes } = require('node:stream/consumers');
1721+
const { Readable } = require('node:stream');
1722+
1723+
const dataBuffer = Buffer.from('hello world from consumers!');
1724+
1725+
const readable = Readable.from(dataBuffer);
1726+
bytes(readable).then((data) => {
1727+
console.log(`from readable: ${data.length}`);
1728+
// Prints: from readable: 27
1729+
});
1730+
```
1731+
16971732
#### `streamConsumers.json(stream)`
16981733
16991734
<!-- YAML

lib/stream/consumers.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ async function buffer(stream) {
5050
return Buffer.from(await arrayBuffer(stream));
5151
}
5252

53+
/**
54+
* @param {AsyncIterable|ReadableStream|Readable} stream
55+
* @returns {Promise<Uint8Array>}
56+
*/
57+
async function bytes(stream) {
58+
return new Uint8Array(await buffer(stream));
59+
}
60+
5361
/**
5462
* @param {AsyncIterable|ReadableStream|Readable} stream
5563
* @returns {Promise<string>}
@@ -82,6 +90,7 @@ module.exports = {
8290
arrayBuffer,
8391
blob,
8492
buffer,
93+
bytes,
8594
text,
8695
json,
8796
};

test/parallel/test-stream-consumers.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
arrayBuffer,
99
blob,
1010
buffer,
11+
bytes,
1112
text,
1213
json,
1314
} = require('stream/consumers');
@@ -61,6 +62,18 @@ const kArrayBuffer =
6162
setTimeout(() => passthrough.end('there'), 10);
6263
}
6364

65+
{
66+
const passthrough = new PassThrough();
67+
68+
bytes(passthrough).then(common.mustCall(async (uint8arr) => {
69+
assert(uint8arr instanceof Uint8Array);
70+
assert.strictEqual(uint8arr.byteLength, 10);
71+
assert.deepStrictEqual(Buffer.from(uint8arr), buf);
72+
}));
73+
74+
passthrough.write('hello');
75+
setTimeout(() => passthrough.end('there'), 10);
76+
}
6477

6578
{
6679
const passthrough = new PassThrough();
@@ -219,6 +232,24 @@ const kArrayBuffer =
219232
stream.end({});
220233
}
221234

235+
{
236+
const stream = new PassThrough({
237+
readableObjectMode: true,
238+
writableObjectMode: true,
239+
});
240+
241+
bytes(stream).then(common.mustCall((uint8arr) => {
242+
assert(uint8arr instanceof Uint8Array);
243+
assert.strictEqual(uint8arr.byteLength, 30);
244+
assert.strictEqual(
245+
Buffer.from(uint8arr).toString(),
246+
'[object Object][object Object]');
247+
}));
248+
249+
stream.write({});
250+
stream.end({});
251+
}
252+
222253
{
223254
const stream = new PassThrough({
224255
readableObjectMode: true,

0 commit comments

Comments
 (0)