@@ -15,6 +15,15 @@ const {
1515 isDestroyed,
1616 isFinished,
1717 isServerRequest,
18+ kState,
19+ kErrorEmitted,
20+ kEmitClose,
21+ kClosed,
22+ kCloseEmitted,
23+ kConstructed,
24+ kDestroyed,
25+ kAutoDestroy,
26+ kErrored,
1827} = require ( 'internal/streams/utils' ) ;
1928
2029const kDestroy = Symbol ( 'kDestroy' ) ;
@@ -42,7 +51,10 @@ function destroy(err, cb) {
4251 // With duplex streams we use the writable side for state.
4352 const s = w || r ;
4453
45- if ( w ?. destroyed || r ?. destroyed ) {
54+ if (
55+ ( w && ( w [ kState ] ? ( w [ kState ] & kDestroyed ) !== 0 : w . destroyed ) ) ||
56+ ( r && ( r [ kState ] ? ( r [ kState ] & kDestroyed ) !== 0 : r . destroyed ) )
57+ ) {
4658 if ( typeof cb === 'function' ) {
4759 cb ( ) ;
4860 }
@@ -56,14 +68,14 @@ function destroy(err, cb) {
5668 checkError ( err , w , r ) ;
5769
5870 if ( w ) {
59- w . destroyed = true ;
71+ w [ kState ] |= kDestroyed ;
6072 }
6173 if ( r ) {
62- r . destroyed = true ;
74+ r [ kState ] |= kDestroyed ;
6375 }
6476
6577 // If still constructing then defer calling _destroy.
66- if ( ! s . constructed ) {
78+ if ( ( s [ kState ] & kConstructed ) === 0 ) {
6779 this . once ( kDestroy , function ( er ) {
6880 _destroy ( this , aggregateTwoErrors ( er , err ) , cb ) ;
6981 } ) ;
@@ -89,10 +101,10 @@ function _destroy(self, err, cb) {
89101 checkError ( err , w , r ) ;
90102
91103 if ( w ) {
92- w . closed = true ;
104+ w [ kState ] |= kClosed ;
93105 }
94106 if ( r ) {
95- r . closed = true ;
107+ r [ kState ] |= kClosed ;
96108 }
97109
98110 if ( typeof cb === 'function' ) {
@@ -122,13 +134,16 @@ function emitCloseNT(self) {
122134 const w = self . _writableState ;
123135
124136 if ( w ) {
125- w . closeEmitted = true ;
137+ w [ kState ] |= kCloseEmitted ;
126138 }
127139 if ( r ) {
128- r . closeEmitted = true ;
140+ r [ kState ] |= kCloseEmitted ;
129141 }
130142
131- if ( w ?. emitClose || r ?. emitClose ) {
143+ if (
144+ ( w && ( w [ kState ] & kEmitClose ) !== 0 ) ||
145+ ( r && ( r [ kState ] & kEmitClose ) !== 0 )
146+ ) {
132147 self . emit ( 'close' ) ;
133148 }
134149}
@@ -137,15 +152,18 @@ function emitErrorNT(self, err) {
137152 const r = self . _readableState ;
138153 const w = self . _writableState ;
139154
140- if ( w ?. errorEmitted || r ?. errorEmitted ) {
155+ if (
156+ ( w && ( w [ kState ] & kErrorEmitted ) !== 0 ) ||
157+ ( r && ( r [ kState ] & kErrorEmitted ) !== 0 )
158+ ) {
141159 return ;
142160 }
143161
144162 if ( w ) {
145- w . errorEmitted = true ;
163+ w [ kState ] |= kErrorEmitted ;
146164 }
147165 if ( r ) {
148- r . errorEmitted = true ;
166+ r [ kState ] |= kErrorEmitted ;
149167 }
150168
151169 self . emit ( 'error' , err ) ;
@@ -192,20 +210,26 @@ function errorOrDestroy(stream, err, sync) {
192210 const r = stream . _readableState ;
193211 const w = stream . _writableState ;
194212
195- if ( w ?. destroyed || r ?. destroyed ) {
213+ if (
214+ ( w && ( w [ kState ] ? ( w [ kState ] & kDestroyed ) !== 0 : w . destroyed ) ) ||
215+ ( r && ( r [ kState ] ? ( r [ kState ] & kDestroyed ) !== 0 : r . destroyed ) )
216+ ) {
196217 return this ;
197218 }
198219
199- if ( r ?. autoDestroy || w ?. autoDestroy )
220+ if (
221+ ( r && ( r [ kState ] & kAutoDestroy ) !== 0 ) ||
222+ ( w && ( w [ kState ] & kAutoDestroy ) !== 0 )
223+ ) {
200224 stream . destroy ( err ) ;
201- else if ( err ) {
225+ } else if ( err ) {
202226 // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
203227 err . stack ; // eslint-disable-line no-unused-expressions
204228
205- if ( w && ! w . errored ) {
229+ if ( w && ( w [ kState ] & kErrored ) === 0 ) {
206230 w . errored = err ;
207231 }
208- if ( r && ! r . errored ) {
232+ if ( r && ( r [ kState ] & kErrored ) === 0 ) {
209233 r . errored = err ;
210234 }
211235 if ( sync ) {
@@ -225,10 +249,10 @@ function construct(stream, cb) {
225249 const w = stream . _writableState ;
226250
227251 if ( r ) {
228- r . constructed = false ;
252+ r [ kState ] &= ~ kConstructed ;
229253 }
230254 if ( w ) {
231- w . constructed = false ;
255+ w [ kState ] &= ~ kConstructed ;
232256 }
233257
234258 stream . once ( kConstruct , cb ) ;
@@ -256,10 +280,10 @@ function constructNT(stream) {
256280 const s = w || r ;
257281
258282 if ( r ) {
259- r . constructed = true ;
283+ r [ kState ] |= kConstructed ;
260284 }
261285 if ( w ) {
262- w . constructed = true ;
286+ w [ kState ] |= kConstructed ;
263287 }
264288
265289 if ( s . destroyed ) {
0 commit comments