Skip to content

Commit

Permalink
stream: combine properties using defineProperties
Browse files Browse the repository at this point in the history
PR-URL: #31187
Refs: #31144
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
antsmartian authored and addaleax committed Feb 10, 2020
1 parent 2b1c18f commit 0ac04ec
Showing 1 changed file with 61 additions and 94 deletions.
155 changes: 61 additions & 94 deletions lib/_stream_writable.js
Expand Up @@ -30,6 +30,7 @@ const {
Boolean,
FunctionPrototype,
ObjectDefineProperty,
ObjectDefineProperties,
ObjectSetPrototypeOf,
Symbol,
SymbolHasInstance,
Expand Down Expand Up @@ -336,46 +337,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.
Expand Down Expand Up @@ -652,16 +613,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 &&
Expand Down Expand Up @@ -774,59 +725,75 @@ function onFinished(stream, state, cb) {
stream.prependListener('error', onerror);
}

ObjectDefineProperty(Writable.prototype, 'writable', {
get() {
const w = this._writableState;
if (!w) return false;
if (w.writable !== undefined) return w.writable;
return Boolean(!w.destroyed && !w.errored && !w.ending);
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;
}
}
},

writable: {
get() {
const w = this._writableState;
if (!w) return false;
if (w.writable !== undefined) return w.writable;
return Boolean(!w.destroyed && !w.errored && !w.ending);
},
set(val) {
// Backwards compatible.
if (this._writableState) {
this._writableState.writable = !!val;
}
}
},
set(val) {
// Backwards compat.
if (this._writableState) {
this._writableState.writable = !!val;

writableFinished: {
get() {
return this._writableState ? this._writableState.finished : false;
}
}
});
},

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;
writableObjectMode: {
get() {
return this._writableState ? this._writableState.objectMode : false;
}
return this._writableState.destroyed;
},
set(value) {
// We ignore the value if the stream
// has not been initialized yet
if (!this._writableState) {
return;

writableBuffer: {
get() {
return this._writableState && this._writableState.getBuffer();
}
},

// Backward compatibility, the user is explicitly
// managing destroyed
this._writableState.destroyed = value;
}
});
writableEnded: {
get() {
return this._writableState ? this._writableState.ending : false;
}
},

ObjectDefineProperty(Writable.prototype, 'writableObjectMode', {
enumerable: false,
get() {
return this._writableState ? this._writableState.objectMode : 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;
}
}
});

Expand Down

0 comments on commit 0ac04ec

Please sign in to comment.