From ff60a0e2b15428ab427e15afb9eeb573039c216d Mon Sep 17 00:00:00 2001 From: antsmartian Date: Tue, 7 Jan 2020 10:43:44 +0530 Subject: [PATCH] stream: clean up definition using defineProperties PR-URL: https://github.com/nodejs/node/pull/31236 Refs: https://github.com/nodejs/node/pull/31187 Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott --- lib/_stream_duplex.js | 130 ++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 80 deletions(-) diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index af2522313f80a2..3f56b95b702e16 100644 --- a/lib/_stream_duplex.js +++ b/lib/_stream_duplex.js @@ -27,7 +27,7 @@ 'use strict'; const { - ObjectDefineProperty, + ObjectDefineProperties, ObjectKeys, ObjectSetPrototypeOf, } = primordials; @@ -70,63 +70,60 @@ function Duplex(options) { } } -ObjectDefineProperty(Duplex.prototype, 'writableHighWaterMark', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - get() { - return this._writableState && this._writableState.highWaterMark; - } -}); +ObjectDefineProperties(Duplex.prototype, { -ObjectDefineProperty(Duplex.prototype, 'writableBuffer', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - get: function() { - return this._writableState && this._writableState.getBuffer(); - } -}); + destroyed: { + get() { + if (this._readableState === undefined || + this._writableState === undefined) { + return false; + } + return this._readableState.destroyed && this._writableState.destroyed; + }, + set(value) { + // Backward compatibility, the user is explicitly + // managing destroyed + if (this._readableState && this._writableState) { + this._readableState.destroyed = value; + this._writableState.destroyed = value; + } + } + }, -ObjectDefineProperty(Duplex.prototype, 'writableLength', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - get() { - return this._writableState && this._writableState.length; - } -}); + writableHighWaterMark: { + get() { + return this._writableState && this._writableState.highWaterMark; + } + }, -ObjectDefineProperty(Duplex.prototype, 'writableFinished', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - get() { - return this._writableState ? this._writableState.finished : false; - } -}); + writableBuffer: { + get() { + return this._writableState && this._writableState.getBuffer(); + } + }, -ObjectDefineProperty(Duplex.prototype, 'writableCorked', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - get() { - return this._writableState ? this._writableState.corked : 0; - } -}); + writableLength: { + get() { + return this._writableState && this._writableState.length; + } + }, -ObjectDefineProperty(Duplex.prototype, 'writableEnded', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - get() { - return this._writableState ? this._writableState.ending : false; + writableFinished: { + get() { + return this._writableState ? this._writableState.finished : false; + } + }, + + writableCorked: { + get() { + return this._writableState ? this._writableState.corked : 0; + } + }, + + writableEnded: { + get() { + return this._writableState ? this._writableState.ending : false; + } } }); @@ -144,30 +141,3 @@ function onend() { function onEndNT(self) { self.end(); } - -ObjectDefineProperty(Duplex.prototype, 'destroyed', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - 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; - } -});