From 19d7bfd7adcd099681576612b851a24aba840d97 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 10 Jan 2021 23:43:08 -0500 Subject: [PATCH] emit res close event This commit updates the response logic to emit the 'close' event if the request is closed. Refs: https://github.com/hapijs/hapi/issues/4208 --- lib/response.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index 5c97956..012d66d 100755 --- a/lib/response.js +++ b/lib/response.js @@ -14,9 +14,26 @@ exports = module.exports = internals.Response = class extends Http.ServerRespons constructor(req, onEnd) { super({ method: req.method, httpVersionMajor: 1, httpVersionMinor: 1 }); - this._shot = { headers: null, trailers: {}, payloadChunks: [] }; + this._shot = { headers: null, trailers: {}, payloadChunks: [], closed: false }; this.assignSocket(internals.nullSocket()); + this.once('close', () => { + + this._shot.closed = true; + }); + + req.once('close', () => { + + process.nextTick(() => { + + /* $lab:coverage:off$ */ + if (!this._shot.closed) { + /* $lab:coverage:on$ */ + this.emit('close'); + } + }); + }); + this.once('finish', () => { const res = internals.payload(this);