diff --git a/lib/api/api-request.js b/lib/api/api-request.js index de6a7bc774d..42e6662a47b 100644 --- a/lib/api/api-request.js +++ b/lib/api/api-request.js @@ -43,30 +43,21 @@ class RequestHandler { this.abort = null this.body = body this.highWaterMark = highWaterMark - this.signal = signal this.reason = null this.removeAbortListener = null - if (util.isStream(body)) { - body.on('error', (err) => { - this.onError(err) + if (signal?.aborted) { + this.reason = signal.reason ?? new RequestAbortedError() + } else if (signal) { + this.removeAbortListener = util.addAbortListener(signal, () => { + this.reason = signal.reason ?? new RequestAbortedError() + if (this.res) { + util.destroy(this.res, this.reason) + } else if (this.abort) { + this.abort(this.reason) + } }) } - - if (this.signal) { - if (this.signal.aborted) { - this.reason = this.signal.reason ?? new RequestAbortedError() - } else { - this.removeAbortListener = util.addAbortListener(this.signal, () => { - this.reason = this.signal.reason ?? new RequestAbortedError() - if (this.res) { - util.destroy(this.res, this.reason) - } else if (this.abort) { - this.abort(this.reason) - } - }) - } - } } onConnect (abort) { @@ -143,7 +134,11 @@ class RequestHandler { if (body) { this.body = null - util.destroy(body, err) + + if (util.isStream(body)) { + body.on('error', util.nop) + util.destroy(body, err) + } } if (this.removeAbortListener) {