0

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?

Erik
  • 14,060
  • 49
  • 132
  • 218

1 Answers1

0

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.

markuz-gj
  • 219
  • 1
  • 8