@@ -70,15 +70,18 @@ class RequestHandler extends AsyncResource {
70
70
this . reason = this . signal . reason ?? new RequestAbortedError ( )
71
71
} else {
72
72
this . removeAbortListener = util . addAbortListener ( this . signal , ( ) => {
73
- this . removeAbortListener ?. ( )
74
- this . removeAbortListener = null
75
-
76
73
this . reason = this . signal . reason ?? new RequestAbortedError ( )
77
74
if ( this . res ) {
78
75
util . destroy ( this . res , this . reason )
79
76
} else if ( this . abort ) {
80
77
this . abort ( this . reason )
81
78
}
79
+
80
+ if ( this . removeAbortListener ) {
81
+ this . res ?. off ( 'close' , this . removeAbortListener )
82
+ this . removeAbortListener ( )
83
+ this . removeAbortListener = null
84
+ }
82
85
} )
83
86
}
84
87
}
@@ -111,54 +114,44 @@ class RequestHandler extends AsyncResource {
111
114
const parsedHeaders = responseHeaders === 'raw' ? util . parseHeaders ( rawHeaders ) : headers
112
115
const contentType = parsedHeaders [ 'content-type' ]
113
116
const contentLength = parsedHeaders [ 'content-length' ]
114
- const body = new Readable ( { resume, abort, contentType, contentLength, highWaterMark } )
117
+ const res = new Readable ( { resume, abort, contentType, contentLength, highWaterMark } )
115
118
116
119
if ( this . removeAbortListener ) {
117
- // TODO (fix): 'close' is sufficient but breaks tests.
118
- body
119
- . on ( 'end' , this . removeAbortListener )
120
- . on ( 'error' , this . removeAbortListener )
120
+ res . on ( 'close' , this . removeAbortListener )
121
121
}
122
122
123
123
this . callback = null
124
- this . res = body
124
+ this . res = res
125
125
if ( callback !== null ) {
126
126
if ( this . throwOnError && statusCode >= 400 ) {
127
127
this . runInAsyncScope ( getResolveErrorBodyCallback , null ,
128
- { callback, body, contentType, statusCode, statusMessage, headers }
128
+ { callback, body : res , contentType, statusCode, statusMessage, headers }
129
129
)
130
130
} else {
131
131
this . runInAsyncScope ( callback , null , null , {
132
132
statusCode,
133
133
headers,
134
134
trailers : this . trailers ,
135
135
opaque,
136
- body,
136
+ body : res ,
137
137
context
138
138
} )
139
139
}
140
140
}
141
141
}
142
142
143
143
onData ( chunk ) {
144
- const { res } = this
145
- return res . push ( chunk )
144
+ return this . res . push ( chunk )
146
145
}
147
146
148
147
onComplete ( trailers ) {
149
- const { res } = this
150
-
151
148
util . parseHeaders ( trailers , this . trailers )
152
-
153
- res . push ( null )
149
+ this . res . push ( null )
154
150
}
155
151
156
152
onError ( err ) {
157
153
const { res, callback, body, opaque } = this
158
154
159
- this . removeAbortListener ?. ( )
160
- this . removeAbortListener = null
161
-
162
155
if ( callback ) {
163
156
// TODO: Does this need queueMicrotask?
164
157
this . callback = null
@@ -179,6 +172,12 @@ class RequestHandler extends AsyncResource {
179
172
this . body = null
180
173
util . destroy ( body , err )
181
174
}
175
+
176
+ if ( this . removeAbortListener ) {
177
+ res ?. off ( 'close' , this . removeAbortListener )
178
+ this . removeAbortListener ( )
179
+ this . removeAbortListener = null
180
+ }
182
181
}
183
182
}
184
183
0 commit comments