Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: implement captureRejections for async handlers #27867

Closed
wants to merge 7 commits into from
Prev Previous commit
Next Next commit
tls: implement capture rejections for 'secureConnection' event
  • Loading branch information
mcollina committed Dec 1, 2019
commit f283ec6b19cc28821d9d2cb475a873a314968f55
14 changes: 14 additions & 0 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ assertCrypto();
const { setImmediate } = require('timers');
const assert = require('internal/assert');
const crypto = require('crypto');
const EE = require('events');
const net = require('net');
const tls = require('tls');
const common = require('_tls_common');
Expand Down Expand Up @@ -1282,6 +1283,19 @@ Server.prototype.addContext = function(servername, context) {
this._contexts.push([re, tls.createSecureContext(context).context]);
};

Server.prototype[EE.captureRejectionSymbol] = function(
err, event, sock) {

switch (event) {
case 'secureConnection':
sock.destroy(err);
break;
default:
net.Server.prototype[Symbol.for('nodejs.rejection')]
.call(this, err, event, sock);
}
};

function SNICallback(servername, callback) {
const contexts = this.server._contexts;

Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-tls-server-capture-rejection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const events = require('events');
const fixtures = require('../common/fixtures');
const { createServer, connect } = require('tls');
const cert = fixtures.readKey('rsa_cert.crt');
const key = fixtures.readKey('rsa_private.pem');

events.captureRejections = true;

const server = createServer({ cert, key }, common.mustCall(async (sock) => {
server.close();

const _err = new Error('kaboom');
sock.on('error', common.mustCall((err) => {
assert.strictEqual(err, _err);
}));
throw _err;
}));

server.listen(0, common.mustCall(() => {
const sock = connect({
port: server.address().port,
host: server.address().host,
rejectUnauthorized: false
});

sock.on('close', common.mustCall());
}));