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

lib: support promise reject for http2.connect promisify integration #53475

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -3380,8 +3380,10 @@ function connect(authority, options, listener) {
ObjectDefineProperty(connect, promisify.custom, {
__proto__: null,
value: (authority, options) => {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const server = connect(authority, options, () => resolve(server));

server.once('error', reject);
pimterry marked this conversation as resolved.
Show resolved Hide resolved
});
},
});
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-http2-client-promisify-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const assert = require('assert');
const http2 = require('http2');
const util = require('util');

// Successful connection test
const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
stream.respond();
Expand All @@ -27,6 +28,21 @@ server.listen(0, common.mustCall(() => {
assert.strictEqual(data, 'ok');
client.close();
server.close();
testConnectionError();
}));
}));
}));

// Error connection test
function testConnectionError() {
const connect = util.promisify(http2.connect);

// Attempt to connect to an invalid port
connect('http://localhost:9999')
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
.then(common.mustNotCall('Promise should not be resolved'))
.catch(common.mustCall((err) => {
assert(err instanceof Error);
assert.strictEqual(err.code, 'ECONNREFUSED');
console.log('Error test passed.');
pimterry marked this conversation as resolved.
Show resolved Hide resolved
}));
}