From fd8aa7de25d01074cf2234e8da07c5453aa92740 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Fri, 12 Jul 2024 08:41:41 -0400 Subject: [PATCH] fix parsing of port --- index.js | 6 +++++- test/parse.test.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2ca8173..f075141 100644 --- a/index.js +++ b/index.js @@ -221,7 +221,11 @@ function parse (uri, opts) { // fix port number if (isNaN(parsed.port)) { - parsed.port = matches[5] + if (matches[5] === undefined) { + parsed.port = parsed.scheme === 'http' ? 80 : 443 + } else { + parsed.port = matches[5] + } } if (parsed.host) { const ipv4result = normalizeIPv4(parsed.host) diff --git a/test/parse.test.js b/test/parse.test.js index ed29cc0..e74477c 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -314,5 +314,11 @@ test('URI parse', (t) => { components = URI.parse('urn:foo:|\\24fpl') t.equal(components.error, 'URN can not be parsed.') + + components = URI.parse('http://example.com/') + t.equal(components.port, 80) + components = URI.parse('https://example.com/') + t.equal(components.port, 443) + t.end() })