From e36956300b9c4cf0771af6229672ca45c34227cd Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 18:42:40 +0100 Subject: [PATCH 1/4] perf(index): convert unused capture groups to non-capture groups --- lib/utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 0716ab9..0fff880 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -138,10 +138,10 @@ function findToken (str, token) { return ind } -const RDS1 = /^\.\.?\// -const RDS2 = /^\/\.(\/|$)/ -const RDS3 = /^\/\.\.(\/|$)/ -const RDS5 = /^\/?(?:.|\n)*?(?=\/|$)/ +const RDS1 = /^\.\.?\//u +const RDS2 = /^\/\.(?:\/|$)/u +const RDS3 = /^\/\.\.(?:\/|$)/u +const RDS5 = /^\/?(?:.|\n)*?(?=\/|$)/u function removeDotSegments (input) { const output = [] From 6ce48f57e0a88e0065fa92e2116ad9f863c02dc0 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 18:49:52 +0100 Subject: [PATCH 2/4] refactor(lib/schemes): use `i` case-insensitive flag over `[a-zA-Z]` --- lib/schemes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/schemes.js b/lib/schemes.js index c5cd478..45d06fe 100644 --- a/lib/schemes.js +++ b/lib/schemes.js @@ -1,7 +1,7 @@ 'use strict' -const UUID_REG = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/ -const URN_REG = /([A-Za-z0-9][A-Za-z0-9-]{0,31}):((?:[A-Za-z0-9()+,\-.:=@;$_!*']|%[0-9A-Fa-f]{2})+)/ +const UUID_REG = /^[\da-f]{8}\b-[\da-f]{4}\b-[\da-f]{4}\b-[\da-f]{4}\b-[\da-f]{12}$/iu +const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu function isSecure (wsComponents) { return typeof wsComponents.secure === 'boolean' ? wsComponents.secure : String(wsComponents.scheme).toLowerCase() === 'wss' From b5a1cb9cccad5003de8d5d07144881bfbf1e4e36 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 18:50:48 +0100 Subject: [PATCH 3/4] refactor(lib/utils): use `\d` over `[0-9]` --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 0fff880..78134c6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,7 +4,7 @@ const { HEX } = require('./scopedChars') function normalizeIPv4 (host) { if (findToken(host, '.') < 3) { return { host, isIPV4: false } } - const matches = host.match(/^(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) || [] + const matches = host.match(/^(\b[01]?\d{1,2}|\b2[0-4]\d|\b25[0-5])(\.([01]?\d{1,2}|2[0-4]\d|25[0-5])){3}$/u) || [] const [address] = matches if (address) { return { host: stripLeadingZeros(address, '.'), isIPV4: true } From 13003ab84e05ca564892c20d9cdee24efba60162 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 18:51:07 +0100 Subject: [PATCH 4/4] refactor(index): add `u` unicode flag to regex --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 134dec9..ba9979e 100644 --- a/index.js +++ b/index.js @@ -160,7 +160,7 @@ function serialize (cmpts, opts) { } if (authority === undefined) { - s = s.replace(/^\/\//, '/%2F') // don't allow the path to start with "//" + s = s.replace(/^\/\//u, '/%2F') // don't allow the path to start with "//" } uriTokens.push(s) @@ -178,7 +178,7 @@ function serialize (cmpts, opts) { return uriTokens.join('') } -const hexLookUp = Array.from({ length: 127 }, (v, k) => /[^!"$&'()*+,.;=_`a-z{}~-]/.test(String.fromCharCode(k))) +const hexLookUp = Array.from({ length: 127 }, (v, k) => /[^!"$&'()*+,\-.;=_`a-z{}~]/u.test(String.fromCharCode(k))) function nonSimpleDomain (value) { let code = 0 @@ -191,7 +191,7 @@ function nonSimpleDomain (value) { return false } -const URI_PARSE = /^(?:([^:/?#]+):)?(?:\/\/((?:([^/?#@]*)@)?(\[[^/?#\]]+\]|[^/?#:]*)(?::(\d*))?))?([^?#]*)(?:\?([^#]*))?(?:#((?:.|\n|\r)*))?/i +const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u function parse (uri, opts) { const options = Object.assign({}, opts)