http2: reject failed http2.connect when used with promisify

PR-URL: https://github.com/nodejs/node/pull/53475
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
pull/53522/head
ehsankhfr 2024-06-16 17:47:48 +01:00 committed by Tim Perry
parent b952ee1e12
commit 42bce05ef6
No known key found for this signature in database
GPG Key ID: 0DDE38AFD3AF94F0
2 changed files with 33 additions and 4 deletions

View File

@ -1034,7 +1034,7 @@ function setupHandle(socket, type, options) {
// If the session has been destroyed, go ahead and emit 'connect',
// but do nothing else. The various on('connect') handlers set by
// core will check for session.destroyed before progressing, this
// ensures that those at l`east get cleared out.
// ensures that those at least get cleared out.
if (this.destroyed) {
process.nextTick(emit, this, 'connect', this, socket);
return;
@ -2126,7 +2126,7 @@ class Http2Stream extends Duplex {
}
[kProceed]() {
assert.fail('Implementors MUST implement this. Please report this as a ' +
assert.fail('Implementers MUST implement this. Please report this as a ' +
'bug in Node.js');
}
@ -3380,8 +3380,13 @@ function connect(authority, options, listener) {
ObjectDefineProperty(connect, promisify.custom, {
__proto__: null,
value: (authority, options) => {
return new Promise((resolve) => {
const server = connect(authority, options, () => resolve(server));
return new Promise((resolve, reject) => {
const server = connect(authority, options, () => {
server.removeListener('error', reject);
return resolve(server);
});
server.once('error', reject);
});
},
});

View File

@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const util = require('util');
const server = http2.createServer();
server.listen(0, common.mustCall(() => {
const port = server.address().port;
server.close(() => {
const connect = util.promisify(http2.connect);
connect(`http://localhost:${port}`)
.then(common.mustNotCall('Promise should not be resolved'))
.catch(common.mustCall((err) => {
assert(err instanceof Error);
assert.strictEqual(err.code, 'ECONNREFUSED');
}));
});
}));