Skip to content
Discussion options

You must be logged in to vote

Readable.from() when passed a plain string treats the whole thing as a single chunk, so by the time you call .read(16) the internal buffer already has one 1,000,000-byte chunk in it and Node returns that whole chunk rather than slicing it.

The simplest fix is to slice the result after reading:

const partialData = (readable.read(16) ?? '').slice(0, 16);
console.log(partialData.length); // 16

Or if you want a stream that genuinely yields data in controlled chunk sizes from the start, use a custom Readable that pushes in pieces:

import { Readable } from "node:stream";

function chunkedReadable(str, chunkSize) {
  let offset = 0;
  return new Readable({
    read() {
      if (offset >= str.le…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ericmorand
Comment options

Answer selected by ericmorand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants