Nodejs createReadStream True Async

I created a module to split file on '\r\n' and emit each line to app.js listener. Following is snap code of the module.

var fs = require('fs');
var util = require('util');
var EventEmitter = require('events').EventEmitter;

var data_array = [];


function STF(fname){

  if (!(this instanceof STF)) {
    return new STF(fname);
  }
  this.fname = fname;
  var counts=0;
  this.counts = counts;
}

util.inherits(STF, EventEmitter);

STF.prototype.readLines=function readLines(){
  var self = this;
  var time = process.hrtime();
  var input = fs.createReadStream(this.fname);

  var remaining = '';
  input.on('data', function(data) {

    remaining += data;
    var index = remaining.indexOf('\r\n');
    while (index > -1) {
      self.counts++;
      var line = remaining.substring(0, index);
      remaining = remaining.substring(index + 1);
      data_array.push(line);
      index = remaining.indexOf('\r\n');
      //console.log(line);
      self.emit('line',line,process.hrtime(time),self.counts);
    }
  });

  input.on('end', function() {
    if (remaining.length > 0) {
      console.log('Leftover: '+remaining);
      data_array.push(remaining);
    }
    self.emit('array',data_array,process.hrtime(time));


  });

  input.on('error',function(err){
    self.emit('error',err);
  });

}

module.exports = STF;

My App creates 2 new instance of the module loaded with different file and add listeners to each instance.

  var STF = require('./stf.js');
  var test = new STF('A1B.file');
  var test2 = new STF('A1TI.file');

  test.on('array',function(data,time,count){
    //console.log(data);
  });

  test.on('line',function(data,time,count){
    //console.log(data);
    console.log('A1B:::::::::::::::::::::::: '+time,count);
  });

  test2.on('array',function(data,time,count){
    //console.log(data);
  });

  test2.on('line',function(data,time,count){
    //console.log(data);
    console.log('A1TI::  '+time,count);
  });
  test2.readLines();
  test.readLines();

Partial Results of my code looks like the following:

    A1TI::  0,425262376 490
    A1TI::  0,425756742 491
    A1TI::  0,426177125 492
    A1TI::  0,426620173 493
    A1TI::  0,427051246 494
    A1B:::::::::::::::::::::::: 0,426992657 372
    A1B:::::::::::::::::::::::: 0,427497715 373
    A1B:::::::::::::::::::::::: 0,427928361 374
    A1B:::::::::::::::::::::::: 0,428358152 375
    A1B:::::::::::::::::::::::: 0,428786232 376
    A1B:::::::::::::::::::::::: 0,429217306 377
    A1B:::::::::::::::::::::::: 0,429645386 378
    A1B:::::::::::::::::::::::: 0,430100408 379
    A1B:::::::::::::::::::::::: 0,430565267 380
    A1B:::::::::::::::::::::::: 0,431001472 381
    A1B:::::::::::::::::::::::: 0,431477449 382
    A1B:::::::::::::::::::::::: 0,431902964 383
    A1B:::::::::::::::::::::::: 0,432334037 384
    A1B:::::::::::::::::::::::: 0,432809159 385
    A1B:::::::::::::::::::::::: 0,433348429 386
    A1B:::::::::::::::::::::::: 0,433819702 387
    A1B:::::::::::::::::::::::: 0,434271303 388
    A1B:::::::::::::::::::::::: 0,434702805 389
    A1B:::::::::::::::::::::::: 0,435139010 390
    A1B:::::::::::::::::::::::: 0,435578637 391
    A1B:::::::::::::::::::::::: 0,436037508 392
    A1B:::::::::::::::::::::::: 0,436451904 393
    A1B:::::::::::::::::::::::: 0,436868437 394
    A1B:::::::::::::::::::::::: 0,437438070 395
    A1B:::::::::::::::::::::::: 0,437864868 396
    A1B:::::::::::::::::::::::: 0,438327160 397
    A1B:::::::::::::::::::::::: 0,438799716 398
    A1B:::::::::::::::::::::::: 0,439244902 399
    A1B:::::::::::::::::::::::: 0,439669989 400
    A1B:::::::::::::::::::::::: 0,440128005 401
    A1B:::::::::::::::::::::::: 0,440544966 402
    A1B:::::::::::::::::::::::: 0,441036339 403
    A1B:::::::::::::::::::::::: 0,441516165 404
    A1B:::::::::::::::::::::::: 0,441976747 405
    A1B:::::::::::::::::::::::: 0,442480521 406
    A1B:::::::::::::::::::::::: 0,442927418 407
    A1B:::::::::::::::::::::::: 0,443472675 408
    A1B:::::::::::::::::::::::: 0,443924276 409
    A1B:::::::::::::::::::::::: 0,444369035 410
    A1B:::::::::::::::::::::::: 0,444814221 411
    A1B:::::::::::::::::::::::: 0,445266677 412
    A1B:::::::::::::::::::::::: 0,445685349 413
    A1B:::::::::::::::::::::::: 0,446134812 414
    A1B:::::::::::::::::::::::: 0,446554339 415
    A1B:::::::::::::::::::::::: 0,447000381 416
    A1B:::::::::::::::::::::::: 0,447436586 417
    A1B:::::::::::::::::::::::: 0,447907859 418
    A1B:::::::::::::::::::::::: 0,448356039 419
    A1B:::::::::::::::::::::::: 0,448796521 420
    A1B:::::::::::::::::::::::: 0,449245129 421
    A1B:::::::::::::::::::::::: 0,449723244 422
    A1B:::::::::::::::::::::::: 0,450223598 423
    A1B:::::::::::::::::::::::: 0,450791520 424
    A1B:::::::::::::::::::::::: 0,451458231 425
    A1B:::::::::::::::::::::::: 0,451982105 426
    A1B:::::::::::::::::::::::: 0,452548317 427
    A1B:::::::::::::::::::::::: 0,453083310 428
    A1B:::::::::::::::::::::::: 0,453572117 429
    A1B:::::::::::::::::::::::: 0,454091287 430
    A1B:::::::::::::::::::::::: 0,454527065 431
    A1B:::::::::::::::::::::::: 0,455027846 432
    A1B:::::::::::::::::::::::: 0,455476026 433
    A1B:::::::::::::::::::::::: 0,455912232 434
    A1B:::::::::::::::::::::::: 0,456366826 435
    A1B:::::::::::::::::::::::: 0,456863759 436
    A1B:::::::::::::::::::::::: 0,457317070 437
    A1B:::::::::::::::::::::::: 0,457758835 438
    A1B:::::::::::::::::::::::: 0,458191620 439
    A1B:::::::::::::::::::::::: 0,458659899 440
    A1B:::::::::::::::::::::::: 0,459090545 441
    A1B:::::::::::::::::::::::: 0,459516060 442
    A1B:::::::::::::::::::::::: 0,459953548 443
    A1B:::::::::::::::::::::::: 0,460386760 444
    A1B:::::::::::::::::::::::: 0,460824249 445
    A1B:::::::::::::::::::::::: 0,461253184 446
    A1B:::::::::::::::::::::::: 0,461676133 447
    A1B:::::::::::::::::::::::: 0,462105496 448
    A1B:::::::::::::::::::::::: 0,462534003 449
    A1B:::::::::::::::::::::::: 0,462959945 450
    A1B:::::::::::::::::::::::: 0,463426087 451
    A1B:::::::::::::::::::::::: 0,463880681 452
    A1B:::::::::::::::::::::::: 0,464371626 453
    A1B:::::::::::::::::::::::: 0,464866848 454
    A1B:::::::::::::::::::::::: 0,465302198 455
    A1B:::::::::::::::::::::::: 0,465733700 456
    A1B:::::::::::::::::::::::: 0,466184445 457
    A1B:::::::::::::::::::::::: 0,466690786 458
    A1B:::::::::::::::::::::::: 0,467174889 459
    A1B:::::::::::::::::::::::: 0,467606818 460
    A1B:::::::::::::::::::::::: 0,468117007 461
    A1B:::::::::::::::::::::::: 0,468543377 462
    A1B:::::::::::::::::::::::: 0,469048007 463
    A1B:::::::::::::::::::::::: 0,469470955 464
    A1B:::::::::::::::::::::::: 0,469884923 465
    A1B:::::::::::::::::::::::: 0,470324550 466
    A1B:::::::::::::::::::::::: 0,470746643 467
    A1B:::::::::::::::::::::::: 0,471202093 468
    A1B:::::::::::::::::::::::: 0,471628890 469
    A1B:::::::::::::::::::::::: 0,472128816 470
    A1B:::::::::::::::::::::::: 0,472595384 471
    A1B:::::::::::::::::::::::: 0,473091461 472
    A1B:::::::::::::::::::::::: 0,473549477 473
    A1B:::::::::::::::::::::::: 0,473962162 474
    A1B:::::::::::::::::::::::: 0,474409059 475
    A1B:::::::::::::::::::::::: 0,474842271 476
    A1B:::::::::::::::::::::::: 0,475334927 477
    A1B:::::::::::::::::::::::: 0,475833997 478
    A1B:::::::::::::::::::::::: 0,476282605 479
    A1B:::::::::::::::::::::::: 0,476713678 480
    A1B:::::::::::::::::::::::: 0,477156726 481
    A1B:::::::::::::::::::::::: 0,477570266 482
    A1B:::::::::::::::::::::::: 0,478079600 483
    A1B:::::::::::::::::::::::: 0,478530346 484
    A1B:::::::::::::::::::::::: 0,479006751 485
    A1B:::::::::::::::::::::::: 0,479551153 486
    A1B:::::::::::::::::::::::: 0,480061342 487
    A1B:::::::::::::::::::::::: 0,480487284 488
    A1B:::::::::::::::::::::::: 0,480899969 489
    A1B:::::::::::::::::::::::: 0,481521348 490
    A1B:::::::::::::::::::::::: 0,481973804 491
    A1B:::::::::::::::::::::::: 0,482422412 492
    A1B:::::::::::::::::::::::: 0,482835097 493
    A1B:::::::::::::::::::::::: 0,483317489 494
    A1TI::  0,484679135 495
    A1TI::  0,485216266 496
    A1TI::  0,485742279 497
    A1TI::  0,486227237 498
    A1TI::  0,486674134 499
    A1TI::  0,487127445 500
    A1TI::  0,487537992 501
    A1TI::  0,487973770 502
    A1TI::  0,488392869 503
    A1TI::  0,488840194 504
    A1TI::  0,489251596 505
    A1TI::  0,489715599 506
    A1TI::  0,490158219 507
    A1TI::  0,490578174 508
    A1TI::  0,491020794 509
    A1TI::  0,491448019 510
    A1TI::  0,491941102 511
    A1TI::  0,492403822 512
    A1TI::  0,492867825 513
    A1TI::  0,493332684 514
    A1TI::  0,493759908 515
    A1TI::  0,494264538 516
    A1TI::  0,494692191 517
    A1TI::  0,495180570 518
    A1TI::  0,495587695 519
    A1TI::  0,496034165 520
    A1TI::  0,496475074 521
    A1TI::  0,496927103 522
    A1TI::  0,497385974 523
    A1TI::  0,497831588 524
    A1TI::  0,498271643 525
    A1TI::  0,498680906 526
    A1TI::  0,499120533 527
    A1TI::  0,499538349 528
    A1TI::  0,500105417 529
    A1TI::  0,500698998 530
    A1TI::  0,501199352 531
    A1TI::  0,501674474 532
    A1TI::  0,502114101 533
    A1TI::  0,502525075 534
    A1TI::  0,502962563 535
    A1TI::  0,503373537 536
    A1TI::  0,503828560 537
    A1TI::  0,504277167 538
    A1TI::  0,504695411 539
    A1TI::  0,505141025 540
    A1TI::  0,505550289 541
    A1TI::  0,506002745 542
    A1TI::  0,506443227 543
    A1TI::  0,506915355 544
    A1TI::  0,507373371 545
    A1TI::  0,507832670 546
    A1TI::  0,508292397 547
    A1TI::  0,508717483 548
    A1TI::  0,509237936 549
    A1TI::  0,509678846 550
    A1TI::  0,510116762 551
    A1TI::  0,510526453 552
    A1TI::  0,510970357 553
    A1TI::  0,511380903 554
    A1TI::  0,511937279 555
    A1TI::  0,512414967 556
    A1TI::  0,512872555 557
    A1TI::  0,513283957 558
    A1TI::  0,513691082 559
    A1TI::  0,514134130 560
    A1TI::  0,514545104 561
    A1TI::  0,514983876 562
    A1TI::  0,515452155 563
    A1TI::  0,515960634 564
    A1TI::  0,516437039 565
    A1TI::  0,516916865 566
    A1TI::  0,517391987 567
    A1TI::  0,517870103 568
    A1TI::  0,518336244 569
    A1TI::  0,518747646 570
    A1TI::  0,519193687 571
    A1TI::  0,519672658 572
    A1TI::  0,520116989 573

The result is not what I was expecting;

I was hoping to see A1TI & A1B lines to be emitted and printed on each line break in orders like A1TI >> A1B >> A1TI >> A1B etc.

Result of my code seems to print 122~124 of A1B lines >> 122-124 A1T lines >> 122~124 of A1B lines

I can't figure out where this magic number came from. Can someone enlighten me please?

Thanks ahead

You can use the the buffer size to control the volume of data read at one time:

fs.createReadStream(this.fname,{'bufferSize': xxx});

But in your case, if you really need to alternate lines, I think you should rethink your structure, maybe using bi-directional communication (child process) so your app can tell test and test2 when it's ready to receive the next line.