Skip to content

Conversation

@wantaekchoi
Copy link

stream: add bytes() method to stream/consumers

Add a bytes() method to the stream/consumers module that returns a Uint8Array, providing a convenient way to consume streams as typed arrays.

Fixes: #59542

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. labels Oct 27, 2025
@H4ad H4ad added the semver-minor PRs that contain new features and should be released in the next minor version. label Oct 27, 2025
@codecov
Copy link

codecov bot commented Oct 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.56%. Comparing base (4fe325d) to head (79491e0).
⚠️ Report is 62 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #60426      +/-   ##
==========================================
- Coverage   88.58%   88.56%   -0.02%     
==========================================
  Files         704      704              
  Lines      207823   207867      +44     
  Branches    40049    40052       +3     
==========================================
  Hits       184101   184101              
- Misses      15772    15816      +44     
  Partials     7950     7950              
Files with missing lines Coverage Δ
lib/stream/consumers.js 100.00% <100.00%> (ø)

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wantaekchoi wantaekchoi force-pushed the add-stream-consumers-bytes branch 2 times, most recently from 3887f53 to 4117fc0 Compare October 27, 2025 21:41
- Add bytes() method to get Uint8Array from streams
- Add tests for bytes() method in PassThrough and ObjectMode scenarios
- Update documentation

Fixes: nodejs#59542
@wantaekchoi wantaekchoi force-pushed the add-stream-consumers-bytes branch from 4117fc0 to b6a5a18 Compare October 27, 2025 21:53
Copy link
Contributor

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

This LGTM but I'm not explicitly approving it. I don't know why we really need this when user's could do new Uint8Array(await arrayBuffer(stream)); themselves (or rather any conversion from the raw ArrayBuffer).

@Ethan-Arrowood
Copy link
Contributor

Looking at the issue, I see this is meant to align us with the new bytes() method from fetch spec: https://developer.mozilla.org/en-US/docs/Web/API/Response/bytes

I'm okay with that then :) nice work!

import { bytes } from 'node:stream/consumers';
import { Readable } from 'node:stream';

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version. stream Issues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node:stream/consumers should have a bytes method for getting a Uint8Array

5 participants