get-stream

Consume a Node.js or web stream into a string, Buffer, ArrayBuffer, or array

Library
npm
v9.0.1
358stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
41/100Fair
Development Activity0
Maintenance20
Community64
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
72/100Good
Architecture70
Code Quality82
Innovation55
Learning Curve80

get-stream is a small utility by Sindre Sorhus that reads an entire Node.js stream, web ReadableStream, or async iterable into memory and resolves a Promise with the result — as a string, Buffer, ArrayBuffer, or array, depending on which export you use. It works identically in Node.js and browsers, supports a maxBuffer option to cap memory use, and returns partially-read data alongside the error if the stream fails midway, making it a common building block anywhere code needs to buffer a whole stream instead of processing it chunk by chunk.

What You Get

  • A default export that reads a stream to a UTF-8 string, plus getStreamAsBuffer, getStreamAsArrayBuffer, and getStreamAsArray variants for binary and object streams
  • Consistent behavior across Node.js streams, web ReadableStream, and async iterables
  • A maxBuffer option to cap how much data is buffered before rejecting
  • Partially-read data attached to the error object when a stream errors mid-read, so callers don’t lose already-consumed data

Common Use Cases

  • Reading an entire child process’s stdout/stderr into a string for logging or assertions
  • Buffering an HTTP response body into a Buffer before parsing or hashing it
  • Collecting an object-mode stream’s emitted items into a plain array for further processing
  • Reading file or network streams in test fixtures where full-buffer comparison is simpler than streaming assertions

Under The Hood

Architecture - The shared engine lives in source/contents.js’s getStreamContents(), which normalizes any input (Node.js stream, web ReadableStream, or async iterable) into an async iterable via stream.js, then iterates chunks applying pluggable convertChunk/addChunk/getSize/truncateChunk strategies passed in by each format-specific entry point (string.js, buffer.js, array-buffer.js, array.js); a maxBuffer check in appendChunk truncates and throws MaxBufferError once the running size exceeds the limit, and on any error the partially-accumulated state is attached to the thrown error via normalizedError.bufferedData before rethrowing. Tech Stack - Pure ESM JavaScript ("type": "module") with two small runtime dependencies, @sec-ant/readable-stream (a cross-environment ReadableStream helper) and is-stream; linted with xo, tested with ava, and type-checked against source/index.d.ts via tsd. Code Quality - Format-specific test files (test/string.js, test/buffer.js, test/array.js, test/array-buffer.js) plus test/web-stream.js, test/browser.js, and test/integration.js give each output path and each stream source its own coverage, and the xo && ava && tsd test script combines linting, runtime tests, and type-definition tests in one gate. API Design - A single shared chunk-processing engine parameterized per output type keeps string.js/buffer.js/array.js each under ~40 lines, giving a small, consistent surface (getStream, getStreamAsBuffer, getStreamAsArrayBuffer, getStreamAsArray) that behaves the same whether the source is a Node stream, a web stream, or a plain async iterable.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search