I'm trying to upload and parse file line by line by like the following:
var fs = require('fs'),
es = require('event-stream'),
filePath = './file.txt';
fs.createReadStream(filePath)
.pipe(new Iconv('cp866', 'windows-1251'))
.pipe(es.split("\n"))
.pipe(es.map(function (line, cb) {
//do something with the line
cb(null, line)
}))
.pipe(res);
But unfortunately I get 'line' string in utf-8 encoding. Is it possible to prevent evented-stream change encoding?
event-stream is based on a older version of streams. And IMO, it tries to do too much.
I would use through2
instead. But that would require a little extra work.
var fs = require('fs'),
thr = require('through2'),
filePath = './file.txt';
fs.createReadStream(filePath)
.pipe(new Iconv('cp866', 'windows-1251'))
.pipe(thr(function(chunk, enc, next){
var push = this.push
chunk.toString().split('\n').map(function(line){
// do something with line
push(line)
})
next()
}))
.pipe(res);
Obviously, you could avoid calling toString()
and manipulate the buffer yourself.
through2
is a thin wrapper around a stream.Transform
and gives you more control.