I/O APIs are very important. Node.js’ file stream API is very rough for reading file stream.

View project in Github

readable stream in node.js

The fact is that, the API is hard to use, for example:

1
2
3
var fs = require('fs');
var readStream = fs.createReadStream('myfile.txt');
readStream.on('data', chunk => console.log(chunk));

The pain point is that, the data is read in an closure, you cannot read a chunk buffer with an expected length (even though you can use readable), so it makes it difficult to code. I don’t like to have too many callbacks neither. So I created a stream reader to get rid of the callback hell.

Install

1
npm i --save ginkgoch-stream-reader

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
const fs = require('fs');
const stream = fs.createReadStream(filename, { encoding: 'utf-8' });
async function load(stream) {
let sr = new StreamReader(stream);
await sr.open();

let r = undefined;
while(r = await sr.read(16)) {
console.log(r);
}
}

load(stream);

See, we use async function, no callbacks and we can read any length of buffer as we need to.

Reference