From 96ef89d1d20de9a9f6447ede43c084c79e27af27 Mon Sep 17 00:00:00 2001 From: Vincent Le Goff Date: Thu, 2 Dec 2021 16:41:13 +0100 Subject: [PATCH] feat: minor improvements --- index.js | 5 +---- lib/utils.js | 23 ++--------------------- test/compatibility.test.js | 8 ++++++-- test/parse.test.js | 9 +++++++++ 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 0978392..e36a19c 100644 --- a/index.js +++ b/index.js @@ -226,7 +226,7 @@ function parse (uri, opts) { if (parsed.host) { const ipv4result = normalizeIPv4(parsed.host) if (ipv4result.isIPV4 === false) { - parsed.host = normalizeIPv6(ipv4result.host, { isIPV4: false }).host + parsed.host = normalizeIPv6(ipv4result.host, { isIPV4: false }).host.toLowerCase() } else { parsed.host = ipv4result.host } @@ -288,9 +288,6 @@ function parse (uri, opts) { if (schemeHandler && schemeHandler.parse) { schemeHandler.parse(parsed, options) } - if (parsed.host && parsed.host.length && parsed.host[0] === '[' && parsed.host[parsed.host.length - 1] === ']') { - parsed.host = parsed.host.substr(1).slice(0, -1) - } } else { parsed.error = parsed.error || 'URI can not be parsed.' } diff --git a/lib/utils.js b/lib/utils.js index 73a95a6..71017e4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -107,46 +107,27 @@ function normalizeIPv6 (host, opts = {}) { } } -function stripLeadingZeros (str, token, outputArray = false) { - const outArray = [] +function stripLeadingZeros (str, token) { let out = '' - let temp = '' let skip = true const l = str.length for (let i = 0; i < l; i++) { const c = str[i] if (c === '0' && skip) { if ((i + 1 <= l && str[i + 1] === token) || i + 1 === l) { - if (outputArray) { - temp += c - } else { out += c - } skip = false } } else { if (c === token) { skip = true - if (temp.length) { - outArray.push(temp) - temp = '' - } } else { skip = false } - if (outputArray) { - if (c !== token) { - temp += c - } - } else { out += c - } } } - if (temp.length) { - outArray.push(temp) - } - return outputArray ? outArray : out + return out } function findToken (str, token) { diff --git a/test/compatibility.test.js b/test/compatibility.test.js index 7aab2e8..84b11e7 100644 --- a/test/compatibility.test.js +++ b/test/compatibility.test.js @@ -12,6 +12,7 @@ test('compatibility Parse', (t) => { '//10.10.10.10', '//10.10.000.10', '//[2001:db8::7%en0]', + '//[2001:dbZ::1]:80', '//[2001:db8::1]:80', '//[2001:db8::001]:80', 'uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body', @@ -27,8 +28,11 @@ test('compatibility Parse', (t) => { 'wss://example.com/?bar=baz', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6', - 'urn:example:%D0%B0123,z456' - // 'mailto:chris@example.com', + 'urn:example:%D0%B0123,z456', + '//[2606:2800:220:1:248:1893:25c8:1946:43209]', + 'http://foo.bar', + 'http://' + // 'mailto:chris@example.com'-203845, // 'mailto:infobot@example.com?subject=current-issue', // 'mailto:infobot@example.com?body=send%20current-issue', // 'mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index', diff --git a/test/parse.test.js b/test/parse.test.js index 0f20904..d8e8e38 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -196,6 +196,10 @@ test('URI parse', (t) => { t.equal(components.query, undefined, 'query') t.equal(components.fragment, undefined, 'fragment') + // invalid IPv6 + components = URI.parse('//[2001:dbZ::7]') + t.equal(components.host,'[2001:dbz::7]') + // mixed IPv4address & IPv6address components = URI.parse('//[::ffff:129.144.52.38]') t.equal(components.error, undefined, 'IPv4address errors') @@ -297,5 +301,10 @@ test('URI parse', (t) => { t.equal(components.nid, 'foo', 'nid') t.equal(components.nss, 'a123,456', 'nss') + components = URI.parse('//[2606:2800:220:1:248:1893:25c8:1946:43209]') + t.equal(components.host,"[2606:2800:220:1:248:1893:25c8:1946:43209]") + + components = URI.parse('urn:foo:|\\24fpl') + t.equal(components.error,'URN can not be parsed.') t.end() })