Skip to content

Read all data from a Readable stream with Promise

License

Notifications You must be signed in to change notification settings

rousan/read-all

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status codecov NPM version NPM total downloads Contributors License

read-all

Read all data from a Readable stream and get notified when Promise is resolved.

Installation

Using npm:

$ npm install node-read-all

Using yarn:

$ yarn add node-read-all

Usage

const fs = require('fs');
const readAll = require('node-read-all');

const rStream = fs.createReadStream('file.txt');
rStream.setEncoding('utf8');
readAll(rStream)
  .then(data => console.log(data))
  .catch(console.error.bind(console));

When stream is in object mode:

const { Transform } = require('stream');
const readAll = require('node-read-all');

const transformStream = new Transform({
  readableObjectMode: true,
  transform(chunk, encoding, callback) {
    this.push({ value: chunk.toString() });
    callback();
  },
});

readAll(transformStream)
  .then(data => console.log(data))
  .catch(console.error.bind(console));

setTimeout(() => {
  transformStream.write('a');
  transformStream.write('b');
  transformStream.write('c');
  transformStream.end();
}, 1000);

Contributing

Your PRs and stars are always welcome.