Skip to content

Commit 90a4d43

Browse files
antsmartianMylesBorins
authored andcommittedMar 10, 2020
stream: combine properties using defineProperties
Backport-PR-URL: #32164 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>
1 parent 4640ea2 commit 90a4d43

File tree

1 file changed

+49
-82
lines changed

1 file changed

+49
-82
lines changed
 

‎lib/_stream_writable.js

+49-82
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const {
2929
Array,
3030
FunctionPrototype,
3131
ObjectDefineProperty,
32+
ObjectDefineProperties,
3233
ObjectSetPrototypeOf,
3334
Symbol,
3435
SymbolHasInstance,
@@ -358,46 +359,6 @@ Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
358359
return this;
359360
};
360361

361-
ObjectDefineProperty(Writable.prototype, 'writableBuffer', {
362-
// Making it explicit this property is not enumerable
363-
// because otherwise some prototype manipulation in
364-
// userland will fail
365-
enumerable: false,
366-
get: function() {
367-
return this._writableState && this._writableState.getBuffer();
368-
}
369-
});
370-
371-
ObjectDefineProperty(Writable.prototype, 'writableEnded', {
372-
// Making it explicit this property is not enumerable
373-
// because otherwise some prototype manipulation in
374-
// userland will fail
375-
enumerable: false,
376-
get: function() {
377-
return this._writableState ? this._writableState.ending : false;
378-
}
379-
});
380-
381-
ObjectDefineProperty(Writable.prototype, 'writableHighWaterMark', {
382-
// Making it explicit this property is not enumerable
383-
// because otherwise some prototype manipulation in
384-
// userland will fail
385-
enumerable: false,
386-
get: function() {
387-
return this._writableState && this._writableState.highWaterMark;
388-
}
389-
});
390-
391-
ObjectDefineProperty(Writable.prototype, 'writableCorked', {
392-
// Making it explicit this property is not enumerable
393-
// because otherwise some prototype manipulation in
394-
// userland will fail
395-
enumerable: false,
396-
get: function() {
397-
return this._writableState ? this._writableState.corked : 0;
398-
}
399-
});
400-
401362
// If we're already writing something, then just put this
402363
// in the queue, and wait our turn. Otherwise, call _write
403364
// 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) {
669630
return this;
670631
};
671632

672-
ObjectDefineProperty(Writable.prototype, 'writableLength', {
673-
// Making it explicit this property is not enumerable
674-
// because otherwise some prototype manipulation in
675-
// userland will fail
676-
enumerable: false,
677-
get() {
678-
return this._writableState.length;
679-
}
680-
});
681-
682633
function needFinish(state) {
683634
return (state.ending &&
684635
state.length === 0 &&
@@ -762,44 +713,60 @@ function onCorkedFinish(corkReq, state, err) {
762713
state.corkedRequestsFree.next = corkReq;
763714
}
764715

765-
ObjectDefineProperty(Writable.prototype, 'destroyed', {
766-
// Making it explicit this property is not enumerable
767-
// because otherwise some prototype manipulation in
768-
// userland will fail
769-
enumerable: false,
770-
get() {
771-
if (this._writableState === undefined) {
772-
return false;
716+
ObjectDefineProperties(Writable.prototype, {
717+
718+
destroyed: {
719+
get() {
720+
return this._writableState ? this._writableState.destroyed : false;
721+
},
722+
set(value) {
723+
// Backward compatibility, the user is explicitly managing destroyed
724+
if (this._writableState) {
725+
this._writableState.destroyed = value;
726+
}
773727
}
774-
return this._writableState.destroyed;
775728
},
776-
set(value) {
777-
// We ignore the value if the stream
778-
// has not been initialized yet
779-
if (!this._writableState) {
780-
return;
729+
730+
writableFinished: {
731+
get() {
732+
return this._writableState ? this._writableState.finished : false;
781733
}
734+
},
782735

783-
// Backward compatibility, the user is explicitly
784-
// managing destroyed
785-
this._writableState.destroyed = value;
786-
}
787-
});
736+
writableObjectMode: {
737+
get() {
738+
return this._writableState ? this._writableState.objectMode : false;
739+
}
740+
},
788741

789-
ObjectDefineProperty(Writable.prototype, 'writableObjectMode', {
790-
enumerable: false,
791-
get() {
792-
return this._writableState ? this._writableState.objectMode : false;
793-
}
794-
});
742+
writableBuffer: {
743+
get() {
744+
return this._writableState && this._writableState.getBuffer();
745+
}
746+
},
747+
748+
writableEnded: {
749+
get() {
750+
return this._writableState ? this._writableState.ending : false;
751+
}
752+
},
753+
754+
writableHighWaterMark: {
755+
get() {
756+
return this._writableState && this._writableState.highWaterMark;
757+
}
758+
},
795759

796-
ObjectDefineProperty(Writable.prototype, 'writableFinished', {
797-
// Making it explicit this property is not enumerable
798-
// because otherwise some prototype manipulation in
799-
// userland will fail
800-
enumerable: false,
801-
get() {
802-
return this._writableState ? this._writableState.finished : false;
760+
writableCorked: {
761+
get() {
762+
return this._writableState ? this._writableState.corked : 0;
763+
}
764+
},
765+
766+
writableLength: {
767+
get() {
768+
return this._writableState && this._writableState.length;
769+
}
803770
}
804771
});
805772

0 commit comments

Comments
 (0)
Please sign in to comment.