Created
September 1, 2014 21:20
-
-
Save bennadel/3fa23e610d6e7d747b85 to your computer and use it in GitHub Desktop.
Error Events Don't Inherently Stop Streams In Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include module references. | |
var events = require( "events" ); | |
var stream = require( "stream" ); | |
var util = require( "util" ); | |
var chalk = require( "chalk" ); | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// I am a reabable stream in object-mode. | |
function Outie() { | |
stream.Readable.call( | |
this, | |
{ | |
objectMode: true | |
} | |
); | |
this._source = [ "What", "it", "be", "like?" ]; | |
} | |
util.inherits( Outie, stream.Readable ); | |
Outie.prototype._read = function( sizeIsIgnored ) { | |
while ( this._source.length ) { | |
// Emit an error every time we go to push data into the internal buffer. | |
// -- | |
// NOTE: You would never want to do this - I am only doing this to | |
// demonstrate the interplay between Readable streams and error events. | |
this.emit( "error", new Error( "StreamError" ) ); | |
if ( ! this.push( this._source.shift() ) ) { | |
break; | |
} | |
} | |
if ( ! this._source.length ) { | |
this.push( null ); | |
} | |
}; | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// Create an instance of our readable stream. | |
var readable = new Outie(); | |
// Make sure that we bind an error-event handler. If we don't, then the EventEmitter | |
// will raise an exception (and crash the process) when our stream emits an error event. | |
readable.on( | |
"error", | |
function handleError( error ) { | |
console.log( chalk.red( "Readable error:", error.message ) ); | |
} | |
); | |
readable.on( | |
"readable", | |
function handleReadable() { | |
var data = null; | |
while ( null !== ( data = this.read() ) ) { | |
console.log( chalk.cyan( "Data:", data ) ); | |
} | |
} | |
); | |
readable.on( | |
"end", | |
function handleEnd() { | |
console.log( chalk.yellow( "End" ) ); | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include module references. | |
var events = require( "events" ); | |
var stream = require( "stream" ); | |
var util = require( "util" ); | |
var chalk = require( "chalk" ); | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// I am a writable stream in object-mode. | |
function Innie() { | |
stream.Writable.call( | |
this, | |
{ | |
objectMode: true | |
} | |
); | |
this._buffer = ""; | |
this.on( | |
"finish", | |
function handleFinish() { | |
this.emit( "debug", this._buffer ); | |
} | |
); | |
} | |
util.inherits( Innie, stream.Writable ); | |
Innie.prototype._write = function( chunk, encoding, writeDone ) { | |
this._buffer += ( chunk + " " ); | |
// Emit an error every time we go to write data into the running buffer. | |
// -- | |
// NOTE: You would never want to do this - I am only doing this to | |
// demonstrate the interplay between Readable streams and error events. | |
this.emit( "error", new Error( "StreamError" ) ); | |
// Alternatively, we can also pass an error as the first argument to our | |
// callback to signify that the chunk was not consumed properly (this will | |
// cause another error event to be emitted). | |
writeDone( new Error( "CallbackError" ) ); | |
}; | |
// ---------------------------------------------------------- // | |
// ---------------------------------------------------------- // | |
// Create an instance of our writable stream. | |
var writable = new Innie(); | |
// Make sure that we bind an error-event handler. If we don't, then the EventEmitter | |
// will raise an exception (and crash the process) when our stream emits an error event. | |
writable.on( | |
"error", | |
function handleError( error ) { | |
console.log( chalk.red( "Writable error:", error.message ) ); | |
} | |
); | |
writable.on( | |
"finish", | |
function handleFinish() { | |
console.log( chalk.yellow( "Finish" ) ); | |
} | |
); | |
writable.on( | |
"debug", | |
function handleDebug( buffer ) { | |
console.log( chalk.cyan( "Debug:", buffer ) ); | |
} | |
); | |
writable.write( "What" ); | |
writable.write( "it" ); | |
writable.write( "be" ); | |
writable.end( "like?" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment