Skip to content

Commit 8d1eeb1

Browse files
antsmartiantargos
authored andcommittedApr 20, 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 fa7e975 commit 8d1eeb1

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,
@@ -348,46 +349,6 @@ Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
348349
return this;
349350
};
350351

351-
ObjectDefineProperty(Writable.prototype, 'writableBuffer', {
352-
// Making it explicit this property is not enumerable
353-
// because otherwise some prototype manipulation in
354-
// userland will fail
355-
enumerable: false,
356-
get: function() {
357-
return this._writableState && this._writableState.getBuffer();
358-
}
359-
});
360-
361-
ObjectDefineProperty(Writable.prototype, 'writableEnded', {
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.ending : false;
368-
}
369-
});
370-
371-
ObjectDefineProperty(Writable.prototype, 'writableHighWaterMark', {
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.highWaterMark;
378-
}
379-
});
380-
381-
ObjectDefineProperty(Writable.prototype, 'writableCorked', {
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.corked : 0;
388-
}
389-
});
390-
391352
// If we're already writing something, then just put this
392353
// in the queue, and wait our turn. Otherwise, call _write
393354
// If we return false, then we need a drain event, so set that flag.
@@ -636,16 +597,6 @@ Writable.prototype.end = function(chunk, encoding, cb) {
636597
return this;
637598
};
638599

639-
ObjectDefineProperty(Writable.prototype, 'writableLength', {
640-
// Making it explicit this property is not enumerable
641-
// because otherwise some prototype manipulation in
642-
// userland will fail
643-
enumerable: false,
644-
get() {
645-
return this._writableState.length;
646-
}
647-
});
648-
649600
function needFinish(state) {
650601
return (state.ending &&
651602
state.length === 0 &&
@@ -727,44 +678,60 @@ function onCorkedFinish(corkReq, state, err) {
727678
state.corkedRequestsFree.next = corkReq;
728679
}
729680

730-
ObjectDefineProperty(Writable.prototype, 'destroyed', {
731-
// Making it explicit this property is not enumerable
732-
// because otherwise some prototype manipulation in
733-
// userland will fail
734-
enumerable: false,
735-
get() {
736-
if (this._writableState === undefined) {
737-
return false;
681+
ObjectDefineProperties(Writable.prototype, {
682+
683+
destroyed: {
684+
get() {
685+
return this._writableState ? this._writableState.destroyed : false;
686+
},
687+
set(value) {
688+
// Backward compatibility, the user is explicitly managing destroyed
689+
if (this._writableState) {
690+
this._writableState.destroyed = value;
691+
}
738692
}
739-
return this._writableState.destroyed;
740693
},
741-
set(value) {
742-
// We ignore the value if the stream
743-
// has not been initialized yet
744-
if (!this._writableState) {
745-
return;
694+
695+
writableFinished: {
696+
get() {
697+
return this._writableState ? this._writableState.finished : false;
746698
}
699+
},
747700

748-
// Backward compatibility, the user is explicitly
749-
// managing destroyed
750-
this._writableState.destroyed = value;
751-
}
752-
});
701+
writableObjectMode: {
702+
get() {
703+
return this._writableState ? this._writableState.objectMode : false;
704+
}
705+
},
753706

754-
ObjectDefineProperty(Writable.prototype, 'writableObjectMode', {
755-
enumerable: false,
756-
get() {
757-
return this._writableState ? this._writableState.objectMode : false;
758-
}
759-
});
707+
writableBuffer: {
708+
get() {
709+
return this._writableState && this._writableState.getBuffer();
710+
}
711+
},
712+
713+
writableEnded: {
714+
get() {
715+
return this._writableState ? this._writableState.ending : false;
716+
}
717+
},
718+
719+
writableHighWaterMark: {
720+
get() {
721+
return this._writableState && this._writableState.highWaterMark;
722+
}
723+
},
760724

761-
ObjectDefineProperty(Writable.prototype, 'writableFinished', {
762-
// Making it explicit this property is not enumerable
763-
// because otherwise some prototype manipulation in
764-
// userland will fail
765-
enumerable: false,
766-
get() {
767-
return this._writableState ? this._writableState.finished : false;
725+
writableCorked: {
726+
get() {
727+
return this._writableState ? this._writableState.corked : 0;
728+
}
729+
},
730+
731+
writableLength: {
732+
get() {
733+
return this._writableState && this._writableState.length;
734+
}
768735
}
769736
});
770737

0 commit comments

Comments
 (0)
Please sign in to comment.