next-chunk

Asynchronously returns the next chunk in a Node.js readable stream

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
next-chunk
302.0.22 years ago6 years agoMinified + gzip package size for next-chunk in KB

Readme

!npm versionnpm-imagenpm-url !downloadsdownloads-imagenpm-url !build statusbuild-imagebuild-url !coverage statuscoverage-imagecoverage-url !Language grade: JavaScriptlgtm-imagelgtm-url !Node.JS versionnode-versionnode-url
next-chunk
This package provides an easy mechanism to read the first chunk in a readable stream. It returns a promise which will be resolved with the chunk (either a Buffer or a string), or null if the stream was ended. It rejects the promise if an error occured on the stream.

Versions

Since v2 this is a pure ESMpure-esm package, and requires Node.js >=12.20. It cannot be used from CommonJS.
API

Importing

If importing using TypeScript or ES6 modules:
import nextChunk from 'next-chunk'

Usage

const readable = getSomeReadableStream( );

const firstChunk = await nextChunk( readable );
const secondChunk = await nextChunk( readable );
// ... eventually the stream may end, we get
const endedChunk = await nextChunk( readable );
const againChunk = await nextChunk( readable );

// These chunks contain buffers or strings
expect( firstChunk ).to.not.be.null;
expect( secondChunk ).to.not.be.null;
// These are all null
expect( endedChunk ).to.be.null;
expect( againChunk ).to.be.null;