Skip to content

Commit

Permalink
streams: make streams lazy by default
Browse files Browse the repository at this point in the history
Makes streams lazy by default so crypto does not need to monky patch them in
order to get good performance. This helps the state objects become part of the
private api in nodejs#445.
  • Loading branch information
calvinmetcalf committed Jan 30, 2015
1 parent cbc1262 commit 784bce3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 28 deletions.
19 changes: 19 additions & 0 deletions lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function Duplex(options) {
Readable.call(this, options);
Writable.call(this, options);

this._options = this._options;

if (options && options.readable === false)
this.readable = false;

Expand All @@ -40,6 +42,23 @@ function Duplex(options) {
this.once('end', onend);
}

Object.defineProperty(Duplex.prototype, '_writableState', {
get: function() {
this._writableState = new Writable.WritableState(this._options, this);
return this._writableState;
},
set: function(val) {
Object.defineProperty(this, '_writableState', {
value: val,
enumerable: true,
configurable: true,
writable: true
});
},
configurable: true,
enumerable: false
});

// the no-half-open enforcer
function onend() {
// if we allow half-open state, or if the writable side ended,
Expand Down
19 changes: 18 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,31 @@ function Readable(options) {
if (!(this instanceof Readable))
return new Readable(options);

this._readableState = new ReadableState(options, this);
this._options = options;

// legacy
this.readable = true;

Stream.call(this);
}

Object.defineProperty(Readable.prototype, '_readableState', {
get: function() {
this._readableState = new ReadableState(this._options, this);
return this._readableState;
},
set: function(val) {
Object.defineProperty(this, '_readableState', {
value: val,
enumerable: true,
configurable: true,
writable: true
});
},
configurable: true,
enumerable: true
});

// Manually shove something into the read() buffer.
// This returns true if the highWaterMark has not been hit yet,
// similar to how Writable.write() returns true if you should
Expand Down
16 changes: 16 additions & 0 deletions lib/_stream_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ function Transform(options) {
});
}

Object.defineProperty(Transform.prototype, '_transformState', {
get: function() {
this._transformState = new TransformState(this);
return this._transformState;
},
set: function(val) {
Object.defineProperty(this, '_transformState', {
value: val,
enumerable: true,
configurable: true,
writable: true
});
},
configurable: true,
enumerable: true
});
Transform.prototype.push = function(chunk, encoding) {
this._transformState.needTransform = false;
return Duplex.prototype.push.call(this, chunk, encoding);
Expand Down
20 changes: 19 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,32 @@ function Writable(options) {
if (!(this instanceof Writable) && !(this instanceof Stream.Duplex))
return new Writable(options);

this._writableState = new WritableState(options, this);
this._options = options;

// legacy.
this.writable = true;

Stream.call(this);
}


Object.defineProperty(Writable.prototype, '_writableState', {
get: function() {
this._writableState = new WritableState(this._options, this);
return this._writableState;
},
set: function(val) {
Object.defineProperty(this, '_writableState', {
value: val,
enumerable: true,
configurable: true,
writable: true
});
},
configurable: true,
enumerable: false
});

// Otherwise people can pipe Writable streams, which is just wrong.
Writable.prototype.pipe = function() {
this.emit('error', new Error('Cannot pipe. Not readable.'));
Expand Down
30 changes: 4 additions & 26 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,13 @@ const StringDecoder = require('string_decoder').StringDecoder;


function LazyTransform(options) {
this._options = options;
options = options || {};
options.defaultEncoding = 'binary';
options.decodeStrings = false;
stream.Transform.call(this, options);
}
util.inherits(LazyTransform, stream.Transform);

[
'_readableState',
'_writableState',
'_transformState'
].forEach(function(prop, i, props) {
Object.defineProperty(LazyTransform.prototype, prop, {
get: function() {
stream.Transform.call(this, this._options);
this._writableState.decodeStrings = false;
this._writableState.defaultEncoding = 'binary';
return this[prop];
},
set: function(val) {
Object.defineProperty(this, prop, {
value: val,
enumerable: true,
configurable: true,
writable: true
});
},
configurable: true,
enumerable: true
});
});


exports.createHash = exports.Hash = Hash;
function Hash(algorithm, options) {
Expand Down

0 comments on commit 784bce3

Please sign in to comment.