From 90a4d438cb154cf471ad59ae82d75e433f4a4649 Mon Sep 17 00:00:00 2001 From: antsmartian Date: Mon, 10 Feb 2020 11:44:06 +0530 Subject: [PATCH] stream: combine properties using defineProperties Backport-PR-URL: https://github.com/nodejs/node/pull/32164 PR-URL: https://github.com/nodejs/node/pull/31187 Refs: https://github.com/nodejs/node/pull/31144 Reviewed-By: Luigi Pinca Reviewed-By: Denys Otrishko Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott Reviewed-By: Robert Nagy Reviewed-By: Anna Henningsen --- lib/_stream_writable.js | 131 +++++++++++++++------------------------- 1 file changed, 49 insertions(+), 82 deletions(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index d2c109c302b40c..2e27ae1feae491 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -29,6 +29,7 @@ const { Array, FunctionPrototype, ObjectDefineProperty, + ObjectDefineProperties, ObjectSetPrototypeOf, Symbol, SymbolHasInstance, @@ -358,46 +359,6 @@ Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { return this; }; -ObjectDefineProperty(Writable.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(); - } -}); - -ObjectDefineProperty(Writable.prototype, 'writableEnded', { - // 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.ending : false; - } -}); - -ObjectDefineProperty(Writable.prototype, 'writableHighWaterMark', { - // 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.highWaterMark; - } -}); - -ObjectDefineProperty(Writable.prototype, 'writableCorked', { - // 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.corked : 0; - } -}); - // If we're already writing something, then just put this // in the queue, and wait our turn. Otherwise, call _write // If we return false, then we need a drain event, so set that flag. @@ -669,16 +630,6 @@ Writable.prototype.end = function(chunk, encoding, cb) { return this; }; -ObjectDefineProperty(Writable.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.length; - } -}); - function needFinish(state) { return (state.ending && state.length === 0 && @@ -762,44 +713,60 @@ function onCorkedFinish(corkReq, state, err) { state.corkedRequestsFree.next = corkReq; } -ObjectDefineProperty(Writable.prototype, 'destroyed', { - // Making it explicit this property is not enumerable - // because otherwise some prototype manipulation in - // userland will fail - enumerable: false, - get() { - if (this._writableState === undefined) { - return false; +ObjectDefineProperties(Writable.prototype, { + + destroyed: { + get() { + return this._writableState ? this._writableState.destroyed : false; + }, + set(value) { + // Backward compatibility, the user is explicitly managing destroyed + if (this._writableState) { + this._writableState.destroyed = value; + } } - return this._writableState.destroyed; }, - set(value) { - // We ignore the value if the stream - // has not been initialized yet - if (!this._writableState) { - return; + + writableFinished: { + get() { + return this._writableState ? this._writableState.finished : false; } + }, - // Backward compatibility, the user is explicitly - // managing destroyed - this._writableState.destroyed = value; - } -}); + writableObjectMode: { + get() { + return this._writableState ? this._writableState.objectMode : false; + } + }, -ObjectDefineProperty(Writable.prototype, 'writableObjectMode', { - enumerable: false, - get() { - return this._writableState ? this._writableState.objectMode : false; - } -}); + writableBuffer: { + get() { + return this._writableState && this._writableState.getBuffer(); + } + }, + + writableEnded: { + get() { + return this._writableState ? this._writableState.ending : false; + } + }, + + writableHighWaterMark: { + get() { + return this._writableState && this._writableState.highWaterMark; + } + }, -ObjectDefineProperty(Writable.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; + writableCorked: { + get() { + return this._writableState ? this._writableState.corked : 0; + } + }, + + writableLength: { + get() { + return this._writableState && this._writableState.length; + } } });