2
2
3
3
// Undocumented cb() API, needed for core, not for public API
4
4
function destroy ( err , cb ) {
5
- const readableDestroyed = this . _readableState &&
6
- this . _readableState . destroyed ;
7
- const writableDestroyed = this . _writableState &&
8
- this . _writableState . destroyed ;
5
+ const rState = this . _readableState ;
6
+ const wState = this . _writableState ;
7
+
8
+ const readableDestroyed = rState && rState . destroyed ;
9
+ const writableDestroyed = wState && wState . destroyed ;
9
10
10
11
if ( readableDestroyed || writableDestroyed ) {
11
12
if ( cb ) {
12
13
cb ( err ) ;
13
14
} else if ( err ) {
14
- if ( ! this . _writableState ) {
15
- process . nextTick ( emitErrorNT , this , err ) ;
16
- } else if ( ! this . _writableState . errorEmitted ) {
17
- this . _writableState . errorEmitted = true ;
18
- process . nextTick ( emitErrorNT , this , err ) ;
15
+ const errorEmitted = ( rState && rState . errorEmitted ) ||
16
+ ( wState && wState . errorEmitted ) ;
17
+
18
+ if ( errorEmitted ) {
19
+ return this ;
20
+ }
21
+
22
+ if ( rState ) {
23
+ rState . errorEmitted = true ;
19
24
}
25
+
26
+ if ( wState ) {
27
+ wState . errorEmitted = true ;
28
+ }
29
+
30
+ process . nextTick ( emitErrorNT , this , err ) ;
20
31
}
21
32
22
33
return this ;
@@ -25,13 +36,13 @@ function destroy(err, cb) {
25
36
// We set destroyed to true before firing error callbacks in order
26
37
// to make it re-entrance safe in case destroy() is called within callbacks
27
38
28
- if ( this . _readableState ) {
29
- this . _readableState . destroyed = true ;
39
+ if ( rState ) {
40
+ rState . destroyed = true ;
30
41
}
31
42
32
43
// If this is a duplex stream mark the writable part as destroyed as well
33
- if ( this . _writableState ) {
34
- this . _writableState . destroyed = true ;
44
+ if ( wState ) {
45
+ wState . destroyed = true ;
35
46
}
36
47
37
48
this . _destroy ( err || null , ( err ) => {
@@ -74,6 +85,7 @@ function undestroy() {
74
85
this . _readableState . reading = false ;
75
86
this . _readableState . ended = false ;
76
87
this . _readableState . endEmitted = false ;
88
+ this . _readableState . errorEmitted = false ;
77
89
}
78
90
79
91
if ( this . _writableState ) {
@@ -107,6 +119,9 @@ function errorOrDestroy(stream, err) {
107
119
if ( wState ) {
108
120
wState . errorEmitted = true ;
109
121
}
122
+ if ( rState ) {
123
+ rState . errorEmitted = true ;
124
+ }
110
125
stream . emit ( 'error' , err ) ;
111
126
}
112
127
}
0 commit comments