Easy way to read stream data with co.
$ npm i co-readable
Read a stream data buffer piece by piece.
const fs = require('fs');
const readable = require('co-readable');
const co = require('co');
const read = readable(fs.createReadStream('bigfile'));
co(function* () {
let buf;
let size = 0;
while (buf = yield read(1024)) {
console.log('read %d bytes', buf.length);
size += buf.length;
}
console.log('total read %d bytes', size);
}).catch(function (err) {
console.error(err.stack);
});
Read all data into one buffer.
const fs = require('fs');
const readable = require('co-readable');
const co = require('co');
co(function* () {
const buf = yield readable.readAll(fs.createReadStream('bigfile'));
console.log('total read %d bytes', buf.length);
}).catch(function (err) {
console.error(err.stack);
});