Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

streams: make streams lazy by default #673

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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