@eight/promise-streams
v1.1.0
Published
Promise-based wrappers around Node.js streams
Downloads
856
Maintainers
Keywords
Readme
@eight/promise-streams
Promise-based wrappers around Node.js streams, so stream consumers and producers can be written as plain async code.
Install
npm install @eight/promise-streamsRequires Node.js 24 or newer. The package is private to the @eight scope, so npm needs a read token for it.
Usage
Reading a stream with await
import { PromiseStreams } from "@eight/promise-streams";
const reader = PromiseStreams.toPromise(process.stdin);
let chunk;
while ((chunk = await reader.read()) !== undefined) {
// chunk is whatever the stream emits; undefined means end of stream
}toPromise returns a PromiseReadStream, a PromiseWriteStream or both (a PromiseStream) depending on whether the
Node stream is readable, writable or duplex. Stream errors surface as rejections of the pending read() / write(); pass
an ErrorTransform as the second argument to wrap them.
Writing with backpressure
const writer = PromiseStreams.toPromise(fs.createWriteStream("out.bin"));
await writer.write(buffer); // resolves once the chunk has been accepted by the streamGoing back to a Node stream
const readable = PromiseStreams.toStream(promiseReadStream); // Readable in object modeBinary helpers
ExactReaderreads exactlynbytes at a time from aPromiseReadStream<Buffer>, buffering across chunk boundaries and throwing on an unexpected end of stream.BufferedReaderwraps aReadableand resolves eachread()once at leastminBufferedbytes are available ortimeoutMillishas elapsed, coalescing small chunks.
Development
npm ci
npm run lint # eslint + prettier --check
npm run build # lint + tsc to build/
npm test # compiles the tests and runs ava under nyc
npm run format # prettier --writeReleasing
Bump version in package.json and add a CHANGELOG entry. On merge to master, Codefresh runs the tests, tags the
commit with the version, and the tag-triggered GitHub Actions workflow publishes to npm via OIDC Trusted Publishing.
Codefresh's verify_publish step confirms the version appeared on the registry.
