From 1448ec77ad3d0c3547653664411da2298b547798 Mon Sep 17 00:00:00 2001 From: ehsankhfr Date: Sun, 16 Jun 2024 17:47:48 +0100 Subject: [PATCH 1/6] lib: fix typos --- lib/internal/http2/core.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index eda587bc51ef45..98b2c73ed9a75a 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -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'); } From 1c59c563dad6bacd5b2fe0c0a57d430d600b8091 Mon Sep 17 00:00:00 2001 From: ehsankhfr Date: Sun, 16 Jun 2024 18:41:10 +0100 Subject: [PATCH 2/6] lib: support reject promise for http2.connect promisify --- lib/internal/http2/core.js | 4 +++- .../test-http2-client-promisify-connect.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 98b2c73ed9a75a..86e5be3ae6edf5 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -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)); + + session.once('error', reject); }); }, }); diff --git a/test/parallel/test-http2-client-promisify-connect.js b/test/parallel/test-http2-client-promisify-connect.js index 3e41bee49bb5c3..ae15900c3a49b6 100644 --- a/test/parallel/test-http2-client-promisify-connect.js +++ b/test/parallel/test-http2-client-promisify-connect.js @@ -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(); @@ -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') + .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.'); + })); +} From f14cf6636e07cabbad95984ffe3aa22dff70f267 Mon Sep 17 00:00:00 2001 From: ehsankhfr Date: Sun, 16 Jun 2024 20:29:05 +0100 Subject: [PATCH 3/6] fix typo --- lib/internal/http2/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 86e5be3ae6edf5..660cfbfc29f8dd 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -3383,7 +3383,7 @@ ObjectDefineProperty(connect, promisify.custom, { return new Promise((resolve, reject) => { const server = connect(authority, options, () => resolve(server)); - session.once('error', reject); + server.once('error', reject); }); }, }); From 312d9e401fdd8cefe2352fa1abfe0cf80bb8cef2 Mon Sep 17 00:00:00 2001 From: ehsankhfr Date: Mon, 17 Jun 2024 19:58:34 +0100 Subject: [PATCH 4/6] add removeListener for error --- lib/internal/http2/core.js | 5 ++++- test/parallel/test-http2-client-promisify-connect.js | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 660cfbfc29f8dd..9d3f9b84b7dc3a 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -3381,7 +3381,10 @@ ObjectDefineProperty(connect, promisify.custom, { __proto__: null, value: (authority, options) => { return new Promise((resolve, reject) => { - const server = connect(authority, options, () => resolve(server)); + const server = connect(authority, options, () => { + server.removeListener('error', reject); + return resolve(server); + }); server.once('error', reject); }); diff --git a/test/parallel/test-http2-client-promisify-connect.js b/test/parallel/test-http2-client-promisify-connect.js index ae15900c3a49b6..780ea4100a422b 100644 --- a/test/parallel/test-http2-client-promisify-connect.js +++ b/test/parallel/test-http2-client-promisify-connect.js @@ -43,6 +43,5 @@ function testConnectionError() { .catch(common.mustCall((err) => { assert(err instanceof Error); assert.strictEqual(err.code, 'ECONNREFUSED'); - console.log('Error test passed.'); })); } From 0248828fdae77a3b3a9f40ff2212696e0f7d5583 Mon Sep 17 00:00:00 2001 From: ehsankhfr Date: Mon, 17 Jun 2024 20:41:24 +0100 Subject: [PATCH 5/6] add new test file --- ...st-http2-client-promisify-connect-error.js | 28 +++++++++++++++++++ .../test-http2-client-promisify-connect.js | 15 ---------- 2 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 test/parallel/test-http2-client-promisify-connect-error.js diff --git a/test/parallel/test-http2-client-promisify-connect-error.js b/test/parallel/test-http2-client-promisify-connect-error.js new file mode 100644 index 00000000000000..0b9750739bc964 --- /dev/null +++ b/test/parallel/test-http2-client-promisify-connect-error.js @@ -0,0 +1,28 @@ +'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.on('stream', common.mustCall((stream) => { + stream.respond(); + stream.end('ok'); +})); + +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'); + })); + }); +})); diff --git a/test/parallel/test-http2-client-promisify-connect.js b/test/parallel/test-http2-client-promisify-connect.js index 780ea4100a422b..3e41bee49bb5c3 100644 --- a/test/parallel/test-http2-client-promisify-connect.js +++ b/test/parallel/test-http2-client-promisify-connect.js @@ -8,7 +8,6 @@ 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(); @@ -28,20 +27,6 @@ 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') - .then(common.mustNotCall('Promise should not be resolved')) - .catch(common.mustCall((err) => { - assert(err instanceof Error); - assert.strictEqual(err.code, 'ECONNREFUSED'); - })); -} From 6bab53ddd54f023f6a0177af0b7bc65027c12fb7 Mon Sep 17 00:00:00 2001 From: ehsankhfr Date: Tue, 18 Jun 2024 09:18:23 +0100 Subject: [PATCH 6/6] fix http2.connect error test --- test/parallel/test-http2-client-promisify-connect-error.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/parallel/test-http2-client-promisify-connect-error.js b/test/parallel/test-http2-client-promisify-connect-error.js index 0b9750739bc964..44ff4a281812f6 100644 --- a/test/parallel/test-http2-client-promisify-connect-error.js +++ b/test/parallel/test-http2-client-promisify-connect-error.js @@ -9,10 +9,6 @@ const http2 = require('http2'); const util = require('util'); const server = http2.createServer(); -server.on('stream', common.mustCall((stream) => { - stream.respond(); - stream.end('ok'); -})); server.listen(0, common.mustCall(() => { const port = server.address().port;