From eea97abf42809e98832038ac5e405d9ebf6da03f Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Tue, 16 Jun 2020 20:33:05 +0530 Subject: [PATCH] http: allow passing callbacks for [Incoming,Outgoing]Message#destroy Fixes: https://github.com/nodejs/node/issues/28236 --- lib/_http_incoming.js | 4 +- lib/_http_outgoing.js | 6 +- ...http-incoming-outgoing-destroy-callback.js | 64 +++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-http-incoming-outgoing-destroy-callback.js diff --git a/lib/_http_incoming.js b/lib/_http_incoming.js index 59136833c6b340..b848af997a11d9 100644 --- a/lib/_http_incoming.js +++ b/lib/_http_incoming.js @@ -118,11 +118,11 @@ IncomingMessage.prototype._read = function _read(n) { // It's possible that the socket will be destroyed, and removed from // any messages, before ever calling this. In that case, just skip // it, since something else is destroying this connection anyway. -IncomingMessage.prototype.destroy = function destroy(error) { +IncomingMessage.prototype.destroy = function destroy(error, cb) { // TODO(ronag): Implement in terms of _destroy this.destroyed = true; if (this.socket) - this.socket.destroy(error); + this.socket.destroy(error, cb); return this; }; diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index dade9a11014c01..a7432fbf70ac26 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -282,17 +282,17 @@ OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) { // It's possible that the socket will be destroyed, and removed from // any messages, before ever calling this. In that case, just skip // it, since something else is destroying this connection anyway. -OutgoingMessage.prototype.destroy = function destroy(error) { +OutgoingMessage.prototype.destroy = function destroy(error, cb) { if (this.destroyed) { return this; } this.destroyed = true; if (this.socket) { - this.socket.destroy(error); + this.socket.destroy(error, cb); } else { this.once('socket', function socketDestroyOnConnect(socket) { - socket.destroy(error); + socket.destroy(error, cb); }); } diff --git a/test/parallel/test-http-incoming-outgoing-destroy-callback.js b/test/parallel/test-http-incoming-outgoing-destroy-callback.js new file mode 100644 index 00000000000000..7e11872f4dfc50 --- /dev/null +++ b/test/parallel/test-http-incoming-outgoing-destroy-callback.js @@ -0,0 +1,64 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +{ + const server = http.createServer(common.mustCall((req, res) => { + res.destroy('xyz', common.mustCall((err) => { + assert.strictEqual(err, 'xyz'); + })); + server.close(); + })); + server.listen(0); + server.on('listening', common.mustCall(() => { + const clientRequest = http.request({ + port: server.address().port, + method: 'GET', + path: '/' + }); + clientRequest.on('error', common.mustCall()); + clientRequest.end(); + })); +} + +{ + const server = http.createServer(common.mustCall((req, res) => { + req.destroy('xyz', common.mustCall((err) => { + assert.strictEqual(err, 'xyz'); + })); + server.close(); + })); + server.listen(0); + server.on('listening', common.mustCall(() => { + const clientRequest = http.request({ + port: server.address().port, + method: 'GET', + path: '/' + }); + clientRequest.on('error', common.mustCall()); + clientRequest.end(); + })); +} + +{ + const server = http.createServer(common.mustCall((req, res) => { + res.end(); + })); + server.listen(0); + server.on('listening', common.mustCall(() => { + const clientRequest = http.request({ + port: server.address().port, + method: 'GET', + path: '/' + }); + clientRequest.on('response', common.mustCall((res) => { + res.destroy('zyx', common.mustCall((err) => { + assert.strictEqual(err, 'zyx'); + })); + server.close(); + })); + clientRequest.on('error', common.mustCall()); + clientRequest.end(); + })); +}