Skip to content

Commit

Permalink
http: allow passing callbacks for [Incoming,Outgoing]Message#destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
rexagod committed Jun 16, 2020
1 parent b371213 commit eea97ab
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
6 changes: 3 additions & 3 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
64 changes: 64 additions & 0 deletions test/parallel/test-http-incoming-outgoing-destroy-callback.js
Original file line number Diff line number Diff line change
@@ -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();
}));
}

0 comments on commit eea97ab

Please sign in to comment.