Skip to content

Commit

Permalink
perf: improve parse-url implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 24, 2023
1 parent 4013c4b commit 693d536
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
36 changes: 18 additions & 18 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions lib/fetch/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 693d536

Please sign in to comment.