stream: add bytes() method to stream/consumers · nodejs/node@4c80031 · GitHub
Skip to content

Commit 4c80031

Browse files
wantaekchoiaduh95
authored andcommitted
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 PR-URL: #60426 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 972f824 commit 4c80031

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

doc/api/webstreams.md

Lines changed: 37 additions & 0 deletions

lib/stream/consumers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
JSONParse,
5+
Uint8Array,
56
} = primordials;
67

78
const {
@@ -50,6 +51,14 @@ async function buffer(stream) {
5051
return Buffer.from(await arrayBuffer(stream));
5152
}
5253

54+
/**
55+
* @param {AsyncIterable|ReadableStream|Readable} stream
56+
* @returns {Promise<Uint8Array>}
57+
*/
58+
async function bytes(stream) {
59+
return new Uint8Array(await arrayBuffer(stream));
60+
}
61+
5362
/**
5463
* @param {AsyncIterable|ReadableStream|Readable} stream
5564
* @returns {Promise<string>}
@@ -82,6 +91,7 @@ module.exports = {
8291
arrayBuffer,
8392
blob,
8493
buffer,
94+
bytes,
8595
text,
8696
json,
8797
};

test/parallel/test-stream-consumers.js

Lines changed: 31 additions & 0 deletions

0 commit comments

Comments
 (0)