summaryrefslogtreecommitdiff
path: root/lib/_stream_duplex.js
diff options
context:
space:
mode:
authorMatteo Collina <hello@matteocollina.com>2017-05-06 14:20:52 +0200
committerMatteo Collina <hello@matteocollina.com>2017-05-22 08:34:14 +0200
commit330c8d743e33a83f85389ea8a64e3d3854ea0048 (patch)
treee3f72136564bc2637e1043133b9f99713d4753b2 /lib/_stream_duplex.js
parentd54ec726cc9d0d293e61dc0dba61a09429e4cbaf (diff)
downloadnode-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.js30
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);
+};