Blog

  • Home /
  • Blog /
  • Detecting the End of a Rainbow Inside a Writable Stream

Detecting the End of a Rainbow Inside a Writable Stream

October 14, 2013

I was implementing a custom writable stream in Node.js the other day when I ran into this issue where I wanted to know whether more data was coming or that we were actually done writing stuff. When looking at the recently revised API for stream implementers, the only thing that gets mentioned in the docs is the _write method.

After some intensive DuckDuckGoing (yes, this might become an actual word in the future), I ran into this thread on the Node.js user group and this issue on GitHub. In short, it is possible and even advisable to listen to the ‘finish’ event inside a Writable stream. Apparently, this happens all the time in the Node.js library itself. This means that by catching the ‘finish’ event, I was able to implement the flush functionality that I was looking for.

So without further ado, here’s a simple example of a custom Readable and Writable stream that are piped together. The Writable stream listen to the ‘finish’ event in order to flush stuff down the drain.

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

//
// Reading stuff
//

var ReadingStuff = function() {
    this._data = [1, 2, 3, 4, 5];

    stream.Readable.call(this);
};

util.inherits(ReadingStuff, stream.Readable);

ReadingStuff.prototype._read = function() {
    if(0 === this._data.length) {
        this.push(null);
        return;
    }

    this.push(this._data[0].toString());
    this._data.shift();
};


//
// Writing stuff
//

var WritingStuff = function() {
    stream.Writable.call(this);

    this.on('finish', function() {
        console.log('Finished writing stuff!!');
    });
};

util.inherits(WritingStuff, stream.Writable);

WritingStuff.prototype._write = function(chunk, encoding, next) {
    console.log(chunk.toString(encoding));
    next();
};

//
// Application
//

var readingStuff = new ReadingStuff();
var writingStuff = new WritingStuff();

readingStuff.pipe(writingStuff);

Notice that inside the _read method of our custom Readable stream, we call this.push(null) in order signal the end of the data.

Hope this helps. 

If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at infonull@nullprincipal-itnull.be.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be