diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 9a5e7f02f8..b8f88be270 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -111,6 +111,11 @@ function Writable(options) { Stream.call(this); } +// Otherwise people can pipe Writable streams, which is just wrong. +Writable.prototype.pipe = function() { + this.emit('error', new Error('Cannot pipe. Not readable.')); +}; + // Override this method or _write(chunk, cb) Writable.prototype.write = function(chunk, encoding, cb) { var state = this._writableState; diff --git a/test/simple/test-stream2-writable.js b/test/simple/test-stream2-writable.js index 4cd37fd822..efd49021bb 100644 --- a/test/simple/test-stream2-writable.js +++ b/test/simple/test-stream2-writable.js @@ -21,6 +21,7 @@ var common = require('../common.js'); var W = require('_stream_writable'); +var D = require('_stream_duplex'); var assert = require('assert'); var util = require('util'); @@ -285,3 +286,28 @@ test('encoding should be ignored for buffers', function(t) { var buf = new Buffer(hex, 'hex'); tw.write(buf, 'binary'); }); + +test('writables are not pipable', function(t) { + var w = new W(); + w._write = function() {}; + var gotError = false; + w.on('error', function(er) { + gotError = true; + }); + w.pipe(process.stdout); + assert(gotError); + t.end(); +}); + +test('duplexes are pipable', function(t) { + var d = new D(); + d._read = function() {}; + d._write = function() {}; + var gotError = false; + d.on('error', function(er) { + gotError = true; + }); + d.pipe(process.stdout); + assert(!gotError); + t.end(); +});