Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve parse-url implementation #2286

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
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