Skip to content

Commit 5bf2772

Browse files
antsmartianBridgeAR
authored andcommitted
stream: group all properties using defineProperties
PR-URL: #31144 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 33352c2 commit 5bf2772

File tree

1 file changed

+90
-102
lines changed

1 file changed

+90
-102
lines changed

lib/_stream_readable.js

+90-102
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
@@ -166,22 +166,6 @@ function ReadableState(options, stream, isDuplex) {
166166
}
167167
}
168168

169-
// Legacy getter for `pipesCount`
170-
ObjectDefineProperty(ReadableState.prototype, 'pipesCount', {
171-
get() {
172-
return this.pipes.length;
173-
}
174-
});
175-
176-
// Legacy property for `paused`
177-
ObjectDefineProperty(ReadableState.prototype, 'paused', {
178-
get() {
179-
return this[kPaused] !== false;
180-
},
181-
set(value) {
182-
this[kPaused] = !!value;
183-
}
184-
});
185169

186170
function Readable(options) {
187171
if (!(this instanceof Readable))
@@ -207,40 +191,6 @@ function Readable(options) {
207191
Stream.call(this, options);
208192
}
209193

210-
ObjectDefineProperty(Readable.prototype, 'destroyed', {
211-
// Making it explicit this property is not enumerable
212-
// because otherwise some prototype manipulation in
213-
// userland will fail
214-
enumerable: false,
215-
get() {
216-
if (this._readableState === undefined) {
217-
return false;
218-
}
219-
return this._readableState.destroyed;
220-
},
221-
set(value) {
222-
// We ignore the value if the stream
223-
// has not been initialized yet
224-
if (!this._readableState) {
225-
return;
226-
}
227-
228-
// Backward compatibility, the user is explicitly
229-
// managing destroyed
230-
this._readableState.destroyed = value;
231-
}
232-
});
233-
234-
ObjectDefineProperty(Readable.prototype, 'readableEnded', {
235-
// Making it explicit this property is not enumerable
236-
// because otherwise some prototype manipulation in
237-
// userland will fail
238-
enumerable: false,
239-
get() {
240-
return this._readableState ? this._readableState.endEmitted : false;
241-
}
242-
});
243-
244194
Readable.prototype.destroy = destroyImpl.destroy;
245195
Readable.prototype._undestroy = destroyImpl.undestroy;
246196
Readable.prototype._destroy = function(err, cb) {
@@ -1120,68 +1070,106 @@ Readable.prototype[SymbolAsyncIterator] = function() {
11201070
return createReadableStreamAsyncIterator(this);
11211071
};
11221072

1123-
ObjectDefineProperty(Readable.prototype, 'readableHighWaterMark', {
1124-
// Making it explicit this property is not enumerable
1125-
// because otherwise some prototype manipulation in
1126-
// userland will fail
1127-
enumerable: false,
1128-
get: function() {
1129-
return this._readableState.highWaterMark;
1130-
}
1131-
});
1073+
// Making it explicit these properties are not enumerable
1074+
// because otherwise some prototype manipulation in
1075+
// userland will fail
1076+
ObjectDefineProperties(Readable.prototype, {
11321077

1133-
ObjectDefineProperty(Readable.prototype, 'readableBuffer', {
1134-
// Making it explicit this property is not enumerable
1135-
// because otherwise some prototype manipulation in
1136-
// userland will fail
1137-
enumerable: false,
1138-
get: function() {
1139-
return this._readableState && this._readableState.buffer;
1140-
}
1141-
});
1078+
readableHighWaterMark: {
1079+
enumerable: false,
1080+
get: function() {
1081+
return this._readableState.highWaterMark;
1082+
}
1083+
},
11421084

1143-
ObjectDefineProperty(Readable.prototype, 'readableFlowing', {
1144-
// Making it explicit this property is not enumerable
1145-
// because otherwise some prototype manipulation in
1146-
// userland will fail
1147-
enumerable: false,
1148-
get: function() {
1149-
return this._readableState.flowing;
1085+
readableBuffer: {
1086+
enumerable: false,
1087+
get: function() {
1088+
return this._readableState && this._readableState.buffer;
1089+
}
11501090
},
1151-
set: function(state) {
1152-
if (this._readableState) {
1153-
this._readableState.flowing = state;
1091+
1092+
readableFlowing: {
1093+
enumerable: false,
1094+
get: function() {
1095+
return this._readableState.flowing;
1096+
},
1097+
set: function(state) {
1098+
if (this._readableState) {
1099+
this._readableState.flowing = state;
1100+
}
11541101
}
1155-
}
1156-
});
1102+
},
11571103

1158-
// Exposed for testing purposes only.
1159-
Readable._fromList = fromList;
1104+
readableLength: {
1105+
enumerable: false,
1106+
get() {
1107+
return this._readableState.length;
1108+
}
1109+
},
11601110

1161-
ObjectDefineProperty(Readable.prototype, 'readableLength', {
1162-
// Making it explicit this property is not enumerable
1163-
// because otherwise some prototype manipulation in
1164-
// userland will fail
1165-
enumerable: false,
1166-
get() {
1167-
return this._readableState.length;
1168-
}
1169-
});
1111+
readableObjectMode: {
1112+
enumerable: false,
1113+
get() {
1114+
return this._readableState ? this._readableState.objectMode : false;
1115+
}
1116+
},
11701117

1171-
ObjectDefineProperty(Readable.prototype, 'readableObjectMode', {
1172-
enumerable: false,
1173-
get() {
1174-
return this._readableState ? this._readableState.objectMode : false;
1175-
}
1176-
});
1118+
readableEncoding: {
1119+
enumerable: false,
1120+
get() {
1121+
return this._readableState ? this._readableState.encoding : null;
1122+
}
1123+
},
11771124

1178-
ObjectDefineProperty(Readable.prototype, 'readableEncoding', {
1179-
enumerable: false,
1180-
get() {
1181-
return this._readableState ? this._readableState.encoding : null;
1125+
destroyed: {
1126+
enumerable: false,
1127+
get() {
1128+
if (this._readableState === undefined) {
1129+
return false;
1130+
}
1131+
return this._readableState.destroyed;
1132+
},
1133+
set(value) {
1134+
// We ignore the value if the stream
1135+
// has not been initialized yet
1136+
if (!this._readableState) {
1137+
return;
1138+
}
1139+
1140+
// Backward compatibility, the user is explicitly
1141+
// managing destroyed
1142+
this._readableState.destroyed = value;
1143+
}
1144+
},
1145+
1146+
readableEnded: {
1147+
enumerable: false,
1148+
get() {
1149+
return this._readableState ? this._readableState.endEmitted : false;
1150+
}
1151+
},
1152+
1153+
// Legacy getter for `pipesCount`
1154+
pipesCount: {
1155+
get() {
1156+
return this.pipes.length;
1157+
}
1158+
},
1159+
1160+
paused: {
1161+
get() {
1162+
return this[kPaused] !== false;
1163+
},
1164+
set(value) {
1165+
this[kPaused] = !!value;
1166+
}
11821167
}
11831168
});
11841169

1170+
// Exposed for testing purposes only.
1171+
Readable._fromList = fromList;
1172+
11851173
// Pluck off n bytes from an array of buffers.
11861174
// Length is the combined lengths of all the buffers in the list.
11871175
// This function is designed to be inlinable, so please take care when making

0 commit comments

Comments
 (0)