Skip to content

Commit

Permalink
http: add captureRejection support to OutgoingMessage
Browse files Browse the repository at this point in the history
PR-URL: #27867
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
mcollina authored and targos committed Dec 9, 2019
1 parent 33a6bf3 commit 334d4f6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const {

const { getDefaultHighWaterMark } = require('internal/streams/state');
const assert = require('internal/assert');
const EE = require('events');
const Stream = require('stream');
const internalUtil = require('internal/util');
const { kOutHeaders, utcDate, kNeedDrain } = require('internal/http');
Expand Down Expand Up @@ -884,6 +885,11 @@ OutgoingMessage.prototype.pipe = function pipe() {
this.emit('error', new ERR_STREAM_CANNOT_PIPE());
};

OutgoingMessage.prototype[EE.captureRejectionSymbol] =
function(err, event) {
this.destroy(err);
};

module.exports = {
OutgoingMessage
};
89 changes: 89 additions & 0 deletions test/parallel/test-http-outgoing-message-capture-rejection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const events = require('events');
const { createServer, request } = require('http');

events.captureRejections = true;

{
const server = createServer(common.mustCall((req, res) => {
const _err = new Error('kaboom');
res.on('drain', common.mustCall(async () => {
throw _err;
}));

res.socket.on('error', common.mustCall((err) => {
assert.strictEqual(err, _err);
}));

// Write until there is space in the buffer
while (res.write('hello')) {}
}));

server.listen(0, common.mustCall(() => {
const req = request({
method: 'GET',
host: server.address().host,
port: server.address().port
});

req.end();

req.on('response', common.mustCall((res) => {
res.on('aborted', common.mustCall());
res.resume();
server.close();
}));
}));
}

{
let _res;
let shouldEnd = false;
// Not using mustCall here, because it is OS-dependant.
const server = createServer((req, res) => {
// So that we cleanly stop
_res = res;

if (shouldEnd) {
res.end();
}
});

server.listen(0, common.mustCall(() => {
const _err = new Error('kaboom');

const req = request({
method: 'POST',
host: server.address().host,
port: server.address().port
});

req.on('response', common.mustNotCall((res) => {
// So that we cleanly stop
res.resume();
server.close();
}));

req.on('error', common.mustCall((err) => {
server.close();
// On some variants of Windows, this can happen before
// the server has received the request.
if (_res) {
_res.end();
} else {
shouldEnd = true;
}
assert.strictEqual(err, _err);
}));

req.on('drain', common.mustCall(async () => {
throw _err;
}));

// Write until there is space in the buffer
while (req.write('hello')) {}
}));
}

0 comments on commit 334d4f6

Please sign in to comment.