Skip to content

Commit 5edfd50

Browse files
antsmartiantargos
authored andcommitted
stream: group all properties using defineProperties
PR-URL: #31144 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 61da657 commit 5edfd50

File tree

1 file changed

+83
-95
lines changed

1 file changed

+83
-95
lines changed

lib/_stream_readable.js

+83-95
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const {
2525
ArrayIsArray,
2626
NumberIsInteger,
2727
NumberIsNaN,
28-
ObjectDefineProperty,
28+
ObjectDefineProperties,
2929
ObjectSetPrototypeOf,
3030
SymbolAsyncIterator,
3131
Symbol
@@ -162,15 +162,6 @@ function ReadableState(options, stream, isDuplex) {
162162
}
163163
}
164164

165-
// Legacy property for `paused`
166-
ObjectDefineProperty(ReadableState.prototype, 'paused', {
167-
get() {
168-
return this[kPaused] !== false;
169-
},
170-
set(value) {
171-
this[kPaused] = !!value;
172-
}
173-
});
174165

175166
function Readable(options) {
176167
if (!(this instanceof Readable))
@@ -196,40 +187,6 @@ function Readable(options) {
196187
Stream.call(this, options);
197188
}
198189

199-
ObjectDefineProperty(Readable.prototype, 'destroyed', {
200-
// Making it explicit this property is not enumerable
201-
// because otherwise some prototype manipulation in
202-
// userland will fail
203-
enumerable: false,
204-
get() {
205-
if (this._readableState === undefined) {
206-
return false;
207-
}
208-
return this._readableState.destroyed;
209-
},
210-
set(value) {
211-
// We ignore the value if the stream
212-
// has not been initialized yet
213-
if (!this._readableState) {
214-
return;
215-
}
216-
217-
// Backward compatibility, the user is explicitly
218-
// managing destroyed
219-
this._readableState.destroyed = value;
220-
}
221-
});
222-
223-
ObjectDefineProperty(Readable.prototype, 'readableEnded', {
224-
// Making it explicit this property is not enumerable
225-
// because otherwise some prototype manipulation in
226-
// userland will fail
227-
enumerable: false,
228-
get() {
229-
return this._readableState ? this._readableState.endEmitted : false;
230-
}
231-
});
232-
233190
Readable.prototype.destroy = destroyImpl.destroy;
234191
Readable.prototype._undestroy = destroyImpl.undestroy;
235192
Readable.prototype._destroy = function(err, cb) {
@@ -1105,68 +1062,99 @@ Readable.prototype[SymbolAsyncIterator] = function() {
11051062
return createReadableStreamAsyncIterator(this);
11061063
};
11071064

1108-
ObjectDefineProperty(Readable.prototype, 'readableHighWaterMark', {
1109-
// Making it explicit this property is not enumerable
1110-
// because otherwise some prototype manipulation in
1111-
// userland will fail
1112-
enumerable: false,
1113-
get: function() {
1114-
return this._readableState.highWaterMark;
1115-
}
1116-
});
1065+
// Making it explicit these properties are not enumerable
1066+
// because otherwise some prototype manipulation in
1067+
// userland will fail
1068+
ObjectDefineProperties(Readable.prototype, {
11171069

1118-
ObjectDefineProperty(Readable.prototype, 'readableBuffer', {
1119-
// Making it explicit this property is not enumerable
1120-
// because otherwise some prototype manipulation in
1121-
// userland will fail
1122-
enumerable: false,
1123-
get: function() {
1124-
return this._readableState && this._readableState.buffer;
1125-
}
1126-
});
1070+
readableHighWaterMark: {
1071+
enumerable: false,
1072+
get: function() {
1073+
return this._readableState.highWaterMark;
1074+
}
1075+
},
11271076

1128-
ObjectDefineProperty(Readable.prototype, 'readableFlowing', {
1129-
// Making it explicit this property is not enumerable
1130-
// because otherwise some prototype manipulation in
1131-
// userland will fail
1132-
enumerable: false,
1133-
get: function() {
1134-
return this._readableState.flowing;
1077+
readableBuffer: {
1078+
enumerable: false,
1079+
get: function() {
1080+
return this._readableState && this._readableState.buffer;
1081+
}
11351082
},
1136-
set: function(state) {
1137-
if (this._readableState) {
1138-
this._readableState.flowing = state;
1083+
1084+
readableFlowing: {
1085+
enumerable: false,
1086+
get: function() {
1087+
return this._readableState.flowing;
1088+
},
1089+
set: function(state) {
1090+
if (this._readableState) {
1091+
this._readableState.flowing = state;
1092+
}
11391093
}
1140-
}
1141-
});
1094+
},
11421095

1143-
// Exposed for testing purposes only.
1144-
Readable._fromList = fromList;
1096+
readableLength: {
1097+
enumerable: false,
1098+
get() {
1099+
return this._readableState.length;
1100+
}
1101+
},
11451102

1146-
ObjectDefineProperty(Readable.prototype, 'readableLength', {
1147-
// Making it explicit this property is not enumerable
1148-
// because otherwise some prototype manipulation in
1149-
// userland will fail
1150-
enumerable: false,
1151-
get() {
1152-
return this._readableState.length;
1153-
}
1154-
});
1103+
readableObjectMode: {
1104+
enumerable: false,
1105+
get() {
1106+
return this._readableState ? this._readableState.objectMode : false;
1107+
}
1108+
},
11551109

1156-
ObjectDefineProperty(Readable.prototype, 'readableObjectMode', {
1157-
enumerable: false,
1158-
get() {
1159-
return this._readableState ? this._readableState.objectMode : false;
1160-
}
1161-
});
1110+
readableEncoding: {
1111+
enumerable: false,
1112+
get() {
1113+
return this._readableState ? this._readableState.encoding : null;
1114+
}
1115+
},
1116+
1117+
destroyed: {
1118+
enumerable: false,
1119+
get() {
1120+
if (this._readableState === undefined) {
1121+
return false;
1122+
}
1123+
return this._readableState.destroyed;
1124+
},
1125+
set(value) {
1126+
// We ignore the value if the stream
1127+
// has not been initialized yet
1128+
if (!this._readableState) {
1129+
return;
1130+
}
11621131

1163-
ObjectDefineProperty(Readable.prototype, 'readableEncoding', {
1164-
enumerable: false,
1165-
get() {
1166-
return this._readableState ? this._readableState.encoding : null;
1132+
// Backward compatibility, the user is explicitly
1133+
// managing destroyed
1134+
this._readableState.destroyed = value;
1135+
}
1136+
},
1137+
1138+
readableEnded: {
1139+
enumerable: false,
1140+
get() {
1141+
return this._readableState ? this._readableState.endEmitted : false;
1142+
}
1143+
},
1144+
1145+
paused: {
1146+
get() {
1147+
return this[kPaused] !== false;
1148+
},
1149+
set(value) {
1150+
this[kPaused] = !!value;
1151+
}
11671152
}
11681153
});
11691154

1155+
// Exposed for testing purposes only.
1156+
Readable._fromList = fromList;
1157+
11701158
// Pluck off n bytes from an array of buffers.
11711159
// Length is the combined lengths of all the buffers in the list.
11721160
// This function is designed to be inlinable, so please take care when making

0 commit comments

Comments
 (0)