计算变换流中的行数。节点js

时间:2016-08-15 06:56:09

标签: javascript node.js stream transform

我有一个test.log文件,在我的IDE中说它有2842000行文本。 我有一个转换流,我想根据有关行数和其他信息的信息吐出一个对象。当我在console.log中时,我的总行数总和高于2846596行。我从哪里获得额外的数据?

var fs = require('fs');
var util = require('util');
const Transform = require('stream').Transform;


//init timer and transform stream
var TransformStream = function(){
  Transform.call(this, {objectMode: true});

  //adding timer to TransformStream prototype;
  this.timer = process.hrtime();

  // adding a buffer to the transform stream
  this.buffer = new Buffer('');
}
util.inherits(TransformStream, Transform);//inheriting Transform into TransformStream

//total lines and bytes init
var sumBytes = 0;
var totalLines = 0;

//_transform function that needs to be defined in a transform stream.
TransformStream.prototype._transform = function(chunk, encoding, callback){
  // transform before
  // console.log("Transform:" + chunk);
  this.buffer = new Buffer(chunk);

  // transforming here
  // getting total number of lines
  var lines = (this.buffer.toString().split('\n').length);
  console.log(typeof(lines));
  console.log(totalLines + " plus " + lines);
  totalLines += lines;

  console.log(totalLines);
  // summing total length
  sumBytes += (this.buffer.length);
  //transform after 
  // console.log(this.buffer);
  var time = process.hrtime(this.timer);
  // pushing chunk out of transform
  var summaryObj = {elapsed_time: time, total_length_in_bytes: totalLines, total_lines: sumBytes}
  // console.log(summaryObj);
  // this.push(chunk);
  this.push(summaryObj);
  callback();
}


ts = new TransformStream;

ts.on('data', function(data){
  // sumBytes += data.length;
  console.log("This is the total amount of bytes in test.log: " + sumBytes);


  // console.log("This are the number lines in the chunk: " + readableData.length)


  // console.log(string);
})

ts.on('end', function(chunk){
  // WHY THE F IS CHUNK UNDEFINED HERE ON THE END EVENT!!!!!!!!!
  // clearing start
  // ending timer on end emitter for transformation
  // totalLines += string.split("\n").length;
  // console.log(totalLines);

  // console.log(time);
  // console.log(totalLines);
  console.log('we are in the end event for the transform stream ' + chunk);
})


rs = fs.createReadStream('test.log');
ws = fs.createWriteStream('transform.log');
rs.pipe(ts);

这是我运行时在终端输出的输出结束:

2840564
This is the total amount of bytes in test.log: NaN
number
2840564 plus 619
2841183
This is the total amount of bytes in test.log: NaN
number
2841183 plus 620
2841803
This is the total amount of bytes in test.log: NaN
number
2841803 plus 619
2842422
This is the total amount of bytes in test.log: NaN
number
2842422 plus 619
2843041
This is the total amount of bytes in test.log: NaN
number
2843041 plus 619
2843660
This is the total amount of bytes in test.log: NaN
number
2843660 plus 620
2844280
This is the total amount of bytes in test.log: NaN
number
2844280 plus 619
2844899
This is the total amount of bytes in test.log: NaN
number
2844899 plus 619
2845518
This is the total amount of bytes in test.log: NaN
number
2845518 plus 620
2846138
This is the total amount of bytes in test.log: NaN
number
2846138 plus 458
2846596
This is the total amount of bytes in test.log: NaN
we are in the end event for the transform stream undefined

1 个答案:

答案 0 :(得分:0)

部分问题是this.buffer.toStrin‌​g().split('\n').lengt‌​h,如果bufferUint8Array,则不会返回this.buffer的字节数,而是.length.split("\n")返回的结果数组。

var buffer = new Uint8Array(100);
var lines = buffer.toString().split("\n");
console.log(`lines:${lines}\nlines.length:${lines.length}\nbuffer.byteLength:${buffer.byteLength}`);

您可以使用buffer.byteLength返回ArrayBuffer中设置的字节数。