From b6a5a1888726180eb188be5e35b3f85bdba89146 Mon Sep 17 00:00:00 2001 From: wantaek Date: Mon, 27 Oct 2025 15:35:23 +0900 Subject: [PATCH 1/5] 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: https://github.com/nodejs/node/issues/59542 --- doc/api/webstreams.md | 35 ++++++++++++++++++++++++++ lib/stream/consumers.js | 10 ++++++++ test/parallel/test-stream-consumers.js | 31 +++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/doc/api/webstreams.md b/doc/api/webstreams.md index 5bc8734971aac1..f3e79a103f6e18 100644 --- a/doc/api/webstreams.md +++ b/doc/api/webstreams.md @@ -1694,6 +1694,41 @@ buffer(readable).then((data) => { }); ``` +#### `streamConsumers.bytes(stream)` + + + +* `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!'); + +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)` * `stream` {ReadableStream|stream.Readable|AsyncIterator} From 2cad01035dbd868067affbe615221df5783ef5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Wed, 28 Jan 2026 16:47:22 +0000 Subject: [PATCH 5/5] test: satisfy eslint --- test/parallel/test-stream-consumers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-stream-consumers.js b/test/parallel/test-stream-consumers.js index 118efce831bc0b..150808037252fa 100644 --- a/test/parallel/test-stream-consumers.js +++ b/test/parallel/test-stream-consumers.js @@ -65,7 +65,7 @@ const kArrayBuffer = { const passthrough = new PassThrough(); - bytes(passthrough).then(common.mustCall(async (uint8arr) => { + bytes(passthrough).then(common.mustCall((uint8arr) => { assert(uint8arr instanceof Uint8Array); assert.strictEqual(uint8arr.byteLength, 10); assert.deepStrictEqual(Buffer.from(uint8arr), buf);