Skip to content

Commit ff60a0e

Browse files
antsmartianMylesBorins
authored andcommitted
stream: clean up definition using defineProperties
PR-URL: #31236 Refs: #31187 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent d76deca commit ff60a0e

File tree

1 file changed

+50
-80
lines changed

1 file changed

+50
-80
lines changed

lib/_stream_duplex.js

+50-80
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
'use strict';
2828

2929
const {
30-
ObjectDefineProperty,
30+
ObjectDefineProperties,
3131
ObjectKeys,
3232
ObjectSetPrototypeOf,
3333
} = primordials;
@@ -70,63 +70,60 @@ function Duplex(options) {
7070
}
7171
}
7272

73-
ObjectDefineProperty(Duplex.prototype, 'writableHighWaterMark', {
74-
// Making it explicit this property is not enumerable
75-
// because otherwise some prototype manipulation in
76-
// userland will fail
77-
enumerable: false,
78-
get() {
79-
return this._writableState && this._writableState.highWaterMark;
80-
}
81-
});
73+
ObjectDefineProperties(Duplex.prototype, {
8274

83-
ObjectDefineProperty(Duplex.prototype, 'writableBuffer', {
84-
// Making it explicit this property is not enumerable
85-
// because otherwise some prototype manipulation in
86-
// userland will fail
87-
enumerable: false,
88-
get: function() {
89-
return this._writableState && this._writableState.getBuffer();
90-
}
91-
});
75+
destroyed: {
76+
get() {
77+
if (this._readableState === undefined ||
78+
this._writableState === undefined) {
79+
return false;
80+
}
81+
return this._readableState.destroyed && this._writableState.destroyed;
82+
},
83+
set(value) {
84+
// Backward compatibility, the user is explicitly
85+
// managing destroyed
86+
if (this._readableState && this._writableState) {
87+
this._readableState.destroyed = value;
88+
this._writableState.destroyed = value;
89+
}
90+
}
91+
},
9292

93-
ObjectDefineProperty(Duplex.prototype, 'writableLength', {
94-
// Making it explicit this property is not enumerable
95-
// because otherwise some prototype manipulation in
96-
// userland will fail
97-
enumerable: false,
98-
get() {
99-
return this._writableState && this._writableState.length;
100-
}
101-
});
93+
writableHighWaterMark: {
94+
get() {
95+
return this._writableState && this._writableState.highWaterMark;
96+
}
97+
},
10298

103-
ObjectDefineProperty(Duplex.prototype, 'writableFinished', {
104-
// Making it explicit this property is not enumerable
105-
// because otherwise some prototype manipulation in
106-
// userland will fail
107-
enumerable: false,
108-
get() {
109-
return this._writableState ? this._writableState.finished : false;
110-
}
111-
});
99+
writableBuffer: {
100+
get() {
101+
return this._writableState && this._writableState.getBuffer();
102+
}
103+
},
112104

113-
ObjectDefineProperty(Duplex.prototype, 'writableCorked', {
114-
// Making it explicit this property is not enumerable
115-
// because otherwise some prototype manipulation in
116-
// userland will fail
117-
enumerable: false,
118-
get() {
119-
return this._writableState ? this._writableState.corked : 0;
120-
}
121-
});
105+
writableLength: {
106+
get() {
107+
return this._writableState && this._writableState.length;
108+
}
109+
},
122110

123-
ObjectDefineProperty(Duplex.prototype, 'writableEnded', {
124-
// Making it explicit this property is not enumerable
125-
// because otherwise some prototype manipulation in
126-
// userland will fail
127-
enumerable: false,
128-
get() {
129-
return this._writableState ? this._writableState.ending : false;
111+
writableFinished: {
112+
get() {
113+
return this._writableState ? this._writableState.finished : false;
114+
}
115+
},
116+
117+
writableCorked: {
118+
get() {
119+
return this._writableState ? this._writableState.corked : 0;
120+
}
121+
},
122+
123+
writableEnded: {
124+
get() {
125+
return this._writableState ? this._writableState.ending : false;
126+
}
130127
}
131128
});
132129

@@ -144,30 +141,3 @@ function onend() {
144141
function onEndNT(self) {
145142
self.end();
146143
}
147-
148-
ObjectDefineProperty(Duplex.prototype, 'destroyed', {
149-
// Making it explicit this property is not enumerable
150-
// because otherwise some prototype manipulation in
151-
// userland will fail
152-
enumerable: false,
153-
get() {
154-
if (this._readableState === undefined ||
155-
this._writableState === undefined) {
156-
return false;
157-
}
158-
return this._readableState.destroyed && this._writableState.destroyed;
159-
},
160-
set(value) {
161-
// We ignore the value if the stream
162-
// has not been initialized yet
163-
if (this._readableState === undefined ||
164-
this._writableState === undefined) {
165-
return;
166-
}
167-
168-
// Backward compatibility, the user is explicitly
169-
// managing destroyed
170-
this._readableState.destroyed = value;
171-
this._writableState.destroyed = value;
172-
}
173-
});

0 commit comments

Comments
 (0)