github.com/cloudcredo/cloudrocker@v0.0.0-20160108110610-1320f8cc2dfd/sample-apps/node/node_modules/logfmt/lib/streaming.js (about)

     1  var split       = require('split')
     2  var through     = require('through');
     3  var PassThrough = require('stream').PassThrough;
     4  
     5  //returns a stream that splits and parses logfmt into objects
     6  exports.streamParser = function(options){
     7    var options = options || {};
     8  
     9    var streamParser = new PassThrough();
    10    var self = this;
    11  
    12    var logfmtStream = through(function(line){
    13      if(line !== '') this.queue(self.parse(line))
    14    })
    15  
    16    // When a source stream is piped to us, undo that pipe, and save
    17    // off the source stream piped into our internally managed streams.
    18    streamParser.on('pipe', function(source) {
    19      if(source.unpipe) source.unpipe(this);
    20      this.transformStream = source.pipe(split()).pipe(logfmtStream);
    21    });
    22  
    23    // When we're piped to another stream, instead pipe our internal
    24    // transform stream to that destination.
    25    streamParser.pipe = function(destination, options) {
    26      return this.transformStream.pipe(destination, options);
    27    };
    28  
    29    return streamParser;
    30  }
    31  
    32  // returns a stream that stringifies objects
    33  exports.streamStringify = function(options){
    34    var self = this;
    35    var options = options || {};
    36    if(options.hasOwnProperty('delimiter')){
    37      var delim = options.delimiter;
    38    }else{
    39      var delim = "\n";
    40    }
    41  
    42    return through(function(data){
    43      this.queue(self.stringify(data) + delim)
    44    }, function(){
    45      this.queue(null)
    46    })
    47  }