http: add req.signal to IncomingMessage · nodejs/node@aa1d8a9 · GitHub
Skip to content

Commit aa1d8a9

Browse files
akshatsrivastava11aduh95
authored andcommitted
http: add req.signal to IncomingMessage
PR-URL: #62541 Fixes: #62481 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent 81bac1e commit aa1d8a9

3 files changed

Lines changed: 153 additions & 0 deletions

File tree

doc/api/http.md

Lines changed: 45 additions & 0 deletions

lib/_http_incoming.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ const {
2929

3030
const { Readable, finished } = require('stream');
3131

32+
const { AbortController } = require('internal/abort_controller');
33+
3234
const kHeaders = Symbol('kHeaders');
3335
const kHeadersDistinct = Symbol('kHeadersDistinct');
3436
const kHeadersCount = Symbol('kHeadersCount');
3537
const kTrailers = Symbol('kTrailers');
3638
const kTrailersDistinct = Symbol('kTrailersDistinct');
3739
const kTrailersCount = Symbol('kTrailersCount');
40+
const kAbortController = Symbol('kAbortController');
3841

3942
function readStart(socket) {
4043
if (socket && !socket._paused && socket.readable)
@@ -90,6 +93,7 @@ function IncomingMessage(socket) {
9093
// Flag for when we decide that this message cannot possibly be
9194
// read by the user, so there's no point continuing to handle it.
9295
this._dumped = false;
96+
this[kAbortController] = null;
9397
}
9498
ObjectSetPrototypeOf(IncomingMessage.prototype, Readable.prototype);
9599
ObjectSetPrototypeOf(IncomingMessage, Readable);
@@ -184,6 +188,25 @@ ObjectDefineProperty(IncomingMessage.prototype, 'trailersDistinct', {
184188
},
185189
});
186190

191+
ObjectDefineProperty(IncomingMessage.prototype, 'signal', {
192+
__proto__: null,
193+
configurable: true,
194+
get: function() {
195+
if (this[kAbortController] === null) {
196+
const ac = new AbortController();
197+
this[kAbortController] = ac;
198+
if (this.destroyed) {
199+
ac.abort();
200+
} else {
201+
this.once('close', function() {
202+
ac.abort();
203+
});
204+
}
205+
}
206+
return this[kAbortController].signal;
207+
},
208+
});
209+
187210
IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
188211
if (callback)
189212
this.on('timeout', callback);
Lines changed: 85 additions & 0 deletions

0 commit comments

Comments
 (0)