diff options
author | Matteo Collina <hello@matteocollina.com> | 2017-05-06 14:20:52 +0200 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2017-05-22 08:34:14 +0200 |
commit | 330c8d743e33a83f85389ea8a64e3d3854ea0048 (patch) | |
tree | e3f72136564bc2637e1043133b9f99713d4753b2 /lib/_stream_duplex.js | |
parent | d54ec726cc9d0d293e61dc0dba61a09429e4cbaf (diff) | |
download | node-new-330c8d743e33a83f85389ea8a64e3d3854ea0048.tar.gz |
stream: add destroy and _destroy methods.
Adds destroy() and _destroy() methods to Readable, Writable, Duplex
and Transform. It also standardizes the behavior and the implementation
of destroy(), which has been inconsistent in userland and core.
This PR also updates all the subsystems of core to use the new
destroy().
PR-URL: https://github.com/nodejs/node/pull/12925
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Calvin Metcalf <calvin.metcalf@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib/_stream_duplex.js')
-rw-r--r-- | lib/_stream_duplex.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 4422b62aac..7440cd0872 100644 --- a/lib/_stream_duplex.js +++ b/lib/_stream_duplex.js @@ -76,3 +76,33 @@ function onend() { function onEndNT(self) { self.end(); } + +Object.defineProperty(Duplex.prototype, 'destroyed', { + get() { + if (this._readableState === undefined || + this._writableState === undefined) { + return false; + } + return this._readableState.destroyed && this._writableState.destroyed; + }, + set(value) { + // we ignore the value if the stream + // has not been initialized yet + if (this._readableState === undefined || + this._writableState === undefined) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._readableState.destroyed = value; + this._writableState.destroyed = value; + } +}); + +Duplex.prototype._destroy = function(err, cb) { + this.push(null); + this.end(); + + process.nextTick(cb, err); +}; |