@@ -68,34 +68,39 @@ function destroy(err, cb) {
6868 return this ;
6969}
7070
71- function _destroy ( self , err , cb ) {
72- let called = false ;
73- const result = self . _destroy ( err || null , ( err ) => {
74- const r = self . _readableState ;
75- const w = self . _writableState ;
71+ function onDestroy ( self , err , cb ) {
72+ const r = self . _readableState ;
73+ const w = self . _writableState ;
7674
77- called = true ;
75+ checkError ( err , w , r ) ;
7876
79- checkError ( err , w , r ) ;
77+ if ( w ) {
78+ w . closed = true ;
79+ }
80+ if ( r ) {
81+ r . closed = true ;
82+ }
8083
81- if ( w ) {
82- w . closed = true ;
83- }
84- if ( r ) {
85- r . closed = true ;
86- }
84+ if ( typeof cb === 'function' ) {
85+ cb ( err ) ;
86+ }
8787
88- if ( typeof cb === 'function' ) {
89- cb ( err ) ;
90- }
88+ if ( err ) {
89+ process . nextTick ( emitErrorCloseNT , self , err ) ;
90+ } else {
91+ process . nextTick ( emitCloseNT , self ) ;
92+ }
93+ }
9194
92- if ( err ) {
93- process . nextTick ( emitErrorCloseNT , self , err ) ;
94- } else {
95- process . nextTick ( emitCloseNT , self ) ;
96- }
95+ function _destroy ( self , err , cb ) {
96+ let called = false ;
97+ const result = self . _destroy ( err || null , ( err ) => {
98+ if ( called )
99+ return ;
100+ called = true ;
101+ onDestroy ( self , err , cb ) ;
97102 } ) ;
98- if ( result !== undefined && result !== null ) {
103+ if ( result != null ) {
99104 try {
100105 const then = result . then ;
101106 if ( typeof then === 'function' ) {
@@ -104,49 +109,14 @@ function _destroy(self, err, cb) {
104109 function ( ) {
105110 if ( called )
106111 return ;
107-
108- const r = self . _readableState ;
109- const w = self . _writableState ;
110-
111- if ( w ) {
112- w . closed = true ;
113- }
114- if ( r ) {
115- r . closed = true ;
116- }
117-
118- if ( typeof cb === 'function' ) {
119- process . nextTick ( cb ) ;
120- }
121-
122- process . nextTick ( emitCloseNT , self ) ;
112+ called = true ;
113+ onDestroy ( self , null , cb ) ;
123114 } ,
124115 function ( err ) {
125- const r = self . _readableState ;
126- const w = self . _writableState ;
127- err . stack ; // eslint-disable-line no-unused-expressions
128-
116+ if ( called )
117+ return ;
129118 called = true ;
130-
131- if ( w && ! w . errored ) {
132- w . errored = err ;
133- }
134- if ( r && ! r . errored ) {
135- r . errored = err ;
136- }
137-
138- if ( w ) {
139- w . closed = true ;
140- }
141- if ( r ) {
142- r . closed = true ;
143- }
144-
145- if ( typeof cb === 'function' ) {
146- process . nextTick ( cb , err ) ;
147- }
148-
149- process . nextTick ( emitErrorCloseNT , self , err ) ;
119+ onDestroy ( self , err , cb ) ;
150120 } ) ;
151121 }
152122 } catch ( err ) {
@@ -284,69 +254,57 @@ function construct(stream, cb) {
284254 process . nextTick ( constructNT , stream ) ;
285255}
286256
287- function constructNT ( stream ) {
257+ function onConstruct ( stream , err ) {
288258 const r = stream . _readableState ;
289259 const w = stream . _writableState ;
290- // With duplex streams we use the writable side for state.
291260 const s = w || r ;
292261
262+ if ( r ) {
263+ r . constructed = true ;
264+ }
265+ if ( w ) {
266+ w . constructed = true ;
267+ }
268+
269+ if ( s . destroyed ) {
270+ stream . emit ( kDestroy , err ) ;
271+ } else if ( err ) {
272+ errorOrDestroy ( stream , err , true ) ;
273+ } else {
274+ process . nextTick ( emitConstructNT , stream ) ;
275+ }
276+ }
277+
278+ function constructNT ( stream ) {
293279 let called = false ;
294280 const result = stream . _construct ( ( err ) => {
295- if ( r ) {
296- r . constructed = true ;
297- }
298- if ( w ) {
299- w . constructed = true ;
300- }
301-
302281 if ( called ) {
303- err = new ERR_MULTIPLE_CALLBACK ( ) ;
282+ errorOrDestroy ( stream , err ?? new ERR_MULTIPLE_CALLBACK ( ) ) ;
304283 } else {
305284 called = true ;
306- }
307-
308- if ( s . destroyed ) {
309- stream . emit ( kDestroy , err ) ;
310- } else if ( err ) {
311- errorOrDestroy ( stream , err , true ) ;
312- } else {
313- process . nextTick ( emitConstructNT , stream ) ;
285+ onConstruct ( stream , err ) ;
314286 }
315287 } ) ;
316- if ( result !== undefined && result !== null ) {
288+ if ( result != null ) {
317289 try {
318290 const then = result . then ;
319291 if ( typeof then === 'function' ) {
320292 then . call (
321293 result ,
322294 function ( ) {
323- // If the callback was invoked, do nothing further.
324- if ( called )
325- return ;
326- if ( r ) {
327- r . constructed = true ;
328- }
329- if ( w ) {
330- w . constructed = true ;
331- }
332- if ( s . destroyed ) {
333- process . nextTick ( ( ) => stream . emit ( kDestroy ) ) ;
295+ if ( called ) {
296+ errorOrDestroy ( stream , new ERR_MULTIPLE_CALLBACK ( ) ) ;
334297 } else {
335- process . nextTick ( emitConstructNT , stream ) ;
298+ called = true ;
299+ onConstruct ( stream , null ) ;
336300 }
337301 } ,
338302 function ( err ) {
339- if ( r ) {
340- r . constructed = true ;
341- }
342- if ( w ) {
343- w . constructed = true ;
344- }
345- called = true ;
346- if ( s . destroyed ) {
347- process . nextTick ( ( ) => stream . emit ( kDestroy , err ) ) ;
303+ if ( called ) {
304+ errorOrDestroy ( stream , err ) ;
348305 } else {
349- process . nextTick ( errorOrDestroy , stream , err ) ;
306+ called = true ;
307+ onConstruct ( stream , err ) ;
350308 }
351309 } ) ;
352310 }
0 commit comments