Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions doc/api/webstreams.md
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,41 @@
});
```
#### `streamConsumers.bytes(stream)`
<!-- YAML
added: v25.0.0
-->
* `stream` {ReadableStream|stream.Readable|AsyncIterator}
* Returns: {Promise} Fulfills with a {Uint8Array} containing the full
contents of the stream.
```mjs
import { bytes } from 'node:stream/consumers';
import { Readable } from 'node:stream';

const dataBuffer = Buffer.from('hello world from consumers!');

Check failure on line 1711 in doc/api/webstreams.md

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'Buffer'. Import 'Buffer' instead of using the global
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like lint-md is complaining about this line – you'll need to add an explicit Buffer import from node:buffer to your examples to make it happy!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks for catching that! I've added explicit imports
for Buffer from 'node:buffer' in both the ESM and CommonJS examples.
Local lint verification confirms it passes now.


const readable = Readable.from(dataBuffer);
const data = await bytes(readable);
console.log(`from readable: ${data.length}`);
// Prints: from readable: 27
```
```cjs
const { bytes } = require('node:stream/consumers');
const { Readable } = require('node:stream');

const dataBuffer = Buffer.from('hello world from consumers!');

const readable = Readable.from(dataBuffer);
bytes(readable).then((data) => {
console.log(`from readable: ${data.length}`);
// Prints: from readable: 27
});
```
#### `streamConsumers.json(stream)`
<!-- YAML
Expand Down
10 changes: 10 additions & 0 deletions lib/stream/consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
JSONParse,
Uint8Array,
} = primordials;

const {
Expand Down Expand Up @@ -50,6 +51,14 @@ async function buffer(stream) {
return Buffer.from(await arrayBuffer(stream));
}

/**
* @param {AsyncIterable|ReadableStream|Readable} stream
* @returns {Promise<Uint8Array>}
*/
async function bytes(stream) {
return new Uint8Array(await arrayBuffer(stream));
}

/**
* @param {AsyncIterable|ReadableStream|Readable} stream
* @returns {Promise<string>}
Expand Down Expand Up @@ -82,6 +91,7 @@ module.exports = {
arrayBuffer,
blob,
buffer,
bytes,
text,
json,
};
31 changes: 31 additions & 0 deletions test/parallel/test-stream-consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
arrayBuffer,
blob,
buffer,
bytes,
text,
json,
} = require('stream/consumers');
Expand Down Expand Up @@ -61,6 +62,18 @@ const kArrayBuffer =
setTimeout(() => passthrough.end('there'), 10);
}

{
const passthrough = new PassThrough();

bytes(passthrough).then(common.mustCall(async (uint8arr) => {
assert(uint8arr instanceof Uint8Array);
assert.strictEqual(uint8arr.byteLength, 10);
assert.deepStrictEqual(Buffer.from(uint8arr), buf);
}));

passthrough.write('hello');
setTimeout(() => passthrough.end('there'), 10);
}

{
const passthrough = new PassThrough();
Expand Down Expand Up @@ -219,6 +232,24 @@ const kArrayBuffer =
stream.end({});
}

{
const stream = new PassThrough({
readableObjectMode: true,
writableObjectMode: true,
});

bytes(stream).then(common.mustCall((uint8arr) => {
assert(uint8arr instanceof Uint8Array);
assert.strictEqual(uint8arr.byteLength, 30);
assert.strictEqual(
Buffer.from(uint8arr).toString(),
'[object Object][object Object]');
}));

stream.write({});
stream.end({});
}

{
const stream = new PassThrough({
readableObjectMode: true,
Expand Down
Loading