Skip to content

Commit

Permalink
perf: improve parse-url implementation (nodejs#2286)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored and crysmags committed Feb 27, 2024
1 parent 50aece8 commit b38eb71
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 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
4 changes: 2 additions & 2 deletions test/client-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ errorAndChunkedEncodingPipelining(consts.ASYNC_ITERATOR)

test('invalid options throws', (t) => {
try {
new Client({ port: 'foobar' }) // eslint-disable-line
new Client({ port: 'foobar', protocol: 'https:' }) // eslint-disable-line
t.fail()
} catch (err) {
t.type(err, errors.InvalidArgumentError)
Expand Down Expand Up @@ -374,7 +374,7 @@ test('invalid options throws', (t) => {
t.fail()
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'Invalid URL hostname: the hostname must be a string or null/undefined.')
t.equal(err.message, 'Invalid URL protocol: the URL must start with `http:` or `https:`.')
}

try {
Expand Down

0 comments on commit b38eb71

Please sign in to comment.