From 545f5126190acacd14ce54799d1e3c5d6dcd9edb Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 8 Oct 2012 14:43:17 -0700 Subject: [PATCH] streams2: ctor guards on Stream classes --- lib/_stream_duplex.js | 3 +++ lib/_stream_passthrough.js | 3 +++ lib/_stream_readable.js | 3 +++ lib/_stream_transform.js | 3 +++ lib/_stream_writable.js | 6 ++++++ 5 files changed, 18 insertions(+) diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 0256b0f2f2..30b4692233 100644 --- a/lib/_stream_duplex.js +++ b/lib/_stream_duplex.js @@ -37,6 +37,9 @@ Object.keys(Writable.prototype).forEach(function(method) { }); function Duplex(options) { + if (!(this instanceof Duplex)) + return new Duplex(options); + Readable.call(this, options); Writable.call(this, options); diff --git a/lib/_stream_passthrough.js b/lib/_stream_passthrough.js index 5acd27b260..0f2fe14c78 100644 --- a/lib/_stream_passthrough.js +++ b/lib/_stream_passthrough.js @@ -30,6 +30,9 @@ var util = require('util'); util.inherits(PassThrough, Transform); function PassThrough(options) { + if (!(this instanceof PassThrough)) + return new PassThrough(options); + Transform.call(this, options); } diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 916ebc2056..180ab1d55b 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -59,6 +59,9 @@ function ReadableState(options) { } function Readable(options) { + if (!(this instanceof Readable)) + return new Readable(options); + this._readableState = new ReadableState(options, this); Stream.apply(this); } diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js index 16f2cacb93..abf7479fdc 100644 --- a/lib/_stream_transform.js +++ b/lib/_stream_transform.js @@ -77,6 +77,9 @@ function TransformState() { } function Transform(options) { + if (!(this instanceof Transform)) + return new Transform(options); + Duplex.call(this, options); // bind output so that it can be passed around as a regular function. diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 90aa285865..ce56afdff9 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -27,6 +27,7 @@ module.exports = Writable var util = require('util'); var Stream = require('stream'); +var Duplex = Stream.Duplex; util.inherits(Writable, Stream); @@ -51,6 +52,11 @@ function WritableState(options) { } function Writable(options) { + // Writable ctor is applied to Duplexes, though they're not + // instanceof Writable, they're instanceof Readable. + if (!(this instanceof Writable) && !(this instanceof Duplex)) + return new Writable(options); + this._writableState = new WritableState(options); // legacy.