streams2: ctor guards on Stream classes

This commit is contained in:
isaacs 2012-10-08 14:43:17 -07:00
Родитель 9b1b85490b
Коммит 545f512619
5 изменённых файлов: 18 добавлений и 0 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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.

Просмотреть файл

@ -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.