Skip to content

Instantly share code, notes, and snippets.

@SimonJang
Forked from nicolashery/example.js
Created July 29, 2020 18:32
Show Gist options
  • Save SimonJang/e5b9f89d7ca40ecc4a99cfd322e1d5b2 to your computer and use it in GitHub Desktop.
Save SimonJang/e5b9f89d7ca40ecc4a99cfd322e1d5b2 to your computer and use it in GitHub Desktop.

Revisions

  1. @nicolashery nicolashery revised this gist Jul 5, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion streamcombiner.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ var StreamCombiner = function() {
    // off the source stream piped into our internally managed streams.
    this.on('pipe', function(source) {
    source.unpipe(this);
    for(i in this.streams) {
    for(var i in this.streams) {
    source = source.pipe(this.streams[i]);
    }
    this.transformStream = source;
  2. @nicolashery nicolashery revised this gist Jul 5, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    // Combine a pipe of two streams into one stream

    var util = require('util')
    , Transform = require('stream').Transform
    , StreamCombiner = require('./streamcombiner');
  3. @nicolashery nicolashery revised this gist Jul 5, 2013. 2 changed files with 49 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions streams-combine.js → example.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    // Combine a pipe of two streams into one stream

    var util = require('util')
    , Transform = require('stream').Transform;
    , Transform = require('stream').Transform
    , StreamCombiner = require('./streamcombiner');

    var chunks1 = [];
    var stream1 = new Transform();
    @@ -36,27 +37,25 @@ process.on('exit', function () {
    });
    process.stdout.on('error', process.exit);


    // Test with `stream1` and `stream2`
    // stdin.pipe(stream1).pipe(stream2).pipe(stdout);

    // $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node streams-combine.js
    // $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node example.js
    // Outputs:
    // 1 abc
    // 2 def
    // 3 ghi
    // chunks1: ["abc\nd","ef\nghi\n"]
    // chunks2: ["abc","def","ghi"]

    // Best working solution I could find
    var stream3 = function(src) {
    return src.pipe(stream1).pipe(stream2);
    };
    stream3(stdin).pipe(stdout);
    // Now combine into `stream3` to "hide" `stream1` and `stream2` from user
    var stream3 = new StreamCombiner(stream1, stream2);
    stdin.pipe(stream3).pipe(stdout);

    // $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node streams-combine.js
    // $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node example.js
    // Outputs:
    // 1 abc
    // 2 def
    // 3 ghi
    // chunks1: ["abc\nd","ef\nghi\n"]
    // chunks2: ["abc","def","ghi"]
    // chunks2: ["abc","def","ghi"]
    40 changes: 40 additions & 0 deletions streamcombiner.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    /* StreamCombiner
    Combine a pipe of multiple streams into one stream.
    Example:
    var stream3 = new StreamCombiner(stream1, stream2);
    process.stdin.pipe(stream3).pipe(process.stdout);
    // The line above will do this:
    // process.stdin.pipe(stream1).pipe(stream2).pipe(process.stdout);
    Thanks to Brandon Tilley (https://github.com/BinaryMuse)
    for this code snippet.
    */

    var util = require('util')
    , PassThrough = require('stream').PassThrough;

    var StreamCombiner = function() {
    this.streams = Array.prototype.slice.apply(arguments);

    // When a source stream is piped to us, undo that pipe, and save
    // off the source stream piped into our internally managed streams.
    this.on('pipe', function(source) {
    source.unpipe(this);
    for(i in this.streams) {
    source = source.pipe(this.streams[i]);
    }
    this.transformStream = source;
    });
    };

    util.inherits(StreamCombiner, PassThrough);

    // When we're piped to another stream, instead pipe our internal
    // transform stream to that destination.
    StreamCombiner.prototype.pipe = function(dest, options) {
    return this.transformStream.pipe(dest, options);
    };

    module.exports = StreamCombiner;
  4. @nicolashery nicolashery revised this gist Jul 4, 2013. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions streams-combine.js
    Original file line number Diff line number Diff line change
    @@ -47,15 +47,16 @@ process.stdout.on('error', process.exit);
    // chunks1: ["abc\nd","ef\nghi\n"]
    // chunks2: ["abc","def","ghi"]

    var stream3 = stream1.pipe(stream2);
    stdin.pipe(stream3).pipe(stdout);
    // Best working solution I could find
    var stream3 = function(src) {
    return src.pipe(stream1).pipe(stream2);
    };
    stream3(stdin).pipe(stdout);

    // $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node streams-combine.js
    // Outputs:
    // 1 abc
    // d
    // 2 ef
    // ghi

    // chunks1: []
    // chunks2: ["abc\nd","ef\nghi\n"]
    // 2 def
    // 3 ghi
    // chunks1: ["abc\nd","ef\nghi\n"]
    // chunks2: ["abc","def","ghi"]
  5. @nicolashery nicolashery revised this gist Jul 2, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions streams-combine.js
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,7 @@ process.stdout.on('error', process.exit);

    // stdin.pipe(stream1).pipe(stream2).pipe(stdout);

    // $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node streams-combine.js
    // Outputs:
    // 1 abc
    // 2 def
    @@ -49,6 +50,7 @@ process.stdout.on('error', process.exit);
    var stream3 = stream1.pipe(stream2);
    stdin.pipe(stream3).pipe(stdout);

    // $ (printf "abc\nd"; sleep 1; printf "ef\nghi\n") | node streams-combine.js
    // Outputs:
    // 1 abc
    // d
  6. @nicolashery nicolashery created this gist Jul 2, 2013.
    59 changes: 59 additions & 0 deletions streams-combine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // Combine a pipe of two streams into one stream

    var util = require('util')
    , Transform = require('stream').Transform;

    var chunks1 = [];
    var stream1 = new Transform();
    var soFar = '';
    stream1._transform = function(chunk, encoding, done) {
    chunks1.push(chunk.toString());
    var pieces = (soFar + chunk).split('\n');
    soFar = pieces.pop();
    for (var i = 0; i < pieces.length; i++) {
    var piece = pieces[i];
    this.push(piece);
    }
    return done();
    };

    var chunks2 = [];
    var count = 0;
    var stream2 = new Transform();
    stream2._transform = function(chunk, encoding, done) {
    chunks2.push(chunk.toString());
    count = count + 1;
    this.push(count + ' ' + chunk.toString() + '\n');
    done();
    };

    var stdin = process.stdin;
    var stdout = process.stdout;

    process.on('exit', function () {
    console.error('chunks1: ' + JSON.stringify(chunks1));
    console.error('chunks2: ' + JSON.stringify(chunks2));
    });
    process.stdout.on('error', process.exit);


    // stdin.pipe(stream1).pipe(stream2).pipe(stdout);

    // Outputs:
    // 1 abc
    // 2 def
    // 3 ghi
    // chunks1: ["abc\nd","ef\nghi\n"]
    // chunks2: ["abc","def","ghi"]

    var stream3 = stream1.pipe(stream2);
    stdin.pipe(stream3).pipe(stdout);

    // Outputs:
    // 1 abc
    // d
    // 2 ef
    // ghi

    // chunks1: []
    // chunks2: ["abc\nd","ef\nghi\n"]