From 693d536fa73cd468c3c51975385185f478dd6d7e Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 24 Sep 2023 17:33:29 -0400 Subject: [PATCH] perf: improve parse-url implementation --- lib/core/util.js | 36 ++++++++++++++++++------------------ lib/fetch/global.js | 8 -------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/core/util.js b/lib/core/util.js index 259ba7b38a6..769811f57f7 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -58,31 +58,31 @@ function parseURL (url) { throw new InvalidArgumentError('Invalid URL: The URL argument must be a non-null object.') } - if (url.port != null && url.port !== '' && !Number.isFinite(parseInt(url.port))) { - throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.') + if (!/^https?:/.test(url.origin || url.protocol)) { + throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.') } - if (url.path != null && typeof url.path !== 'string') { - throw new InvalidArgumentError('Invalid URL path: the path must be a string or null/undefined.') - } + if (!(url instanceof URL)) { + if (url.port != null && url.port !== '' && !Number.isFinite(parseInt(url.port))) { + throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.') + } - if (url.pathname != null && typeof url.pathname !== 'string') { - throw new InvalidArgumentError('Invalid URL pathname: the pathname must be a string or null/undefined.') - } + if (url.path != null && typeof url.path !== 'string') { + throw new InvalidArgumentError('Invalid URL path: the path must be a string or null/undefined.') + } - if (url.hostname != null && typeof url.hostname !== 'string') { - throw new InvalidArgumentError('Invalid URL hostname: the hostname must be a string or null/undefined.') - } + if (url.pathname != null && typeof url.pathname !== 'string') { + throw new InvalidArgumentError('Invalid URL pathname: the pathname must be a string or null/undefined.') + } - if (url.origin != null && typeof url.origin !== 'string') { - throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.') - } + if (url.hostname != null && typeof url.hostname !== 'string') { + throw new InvalidArgumentError('Invalid URL hostname: the hostname must be a string or null/undefined.') + } - if (!/^https?:/.test(url.origin || url.protocol)) { - throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.') - } + if (url.origin != null && typeof url.origin !== 'string') { + throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.') + } - if (!(url instanceof URL)) { const port = url.port != null ? url.port : (url.protocol === 'https:' ? 443 : 80) diff --git a/lib/fetch/global.js b/lib/fetch/global.js index 42282acdfe2..1df6f1227bc 100644 --- a/lib/fetch/global.js +++ b/lib/fetch/global.js @@ -9,14 +9,6 @@ function getGlobalOrigin () { } function setGlobalOrigin (newOrigin) { - if ( - newOrigin !== undefined && - typeof newOrigin !== 'string' && - !(newOrigin instanceof URL) - ) { - throw new Error('Invalid base url') - } - if (newOrigin === undefined) { Object.defineProperty(globalThis, globalOrigin, { value: undefined,