Skip to content

Commit 79415ed

Browse files
committed
url: improve invalid url performance
1 parent 7e12d0e commit 79415ed

File tree

9 files changed

+35
-19
lines changed

9 files changed

+35
-19
lines changed

benchmark/url/whatwg-url-validity.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const url = require('url');
4+
const URL = url.URL;
5+
6+
const bench = common.createBenchmark(main, {
7+
type: ['valid', 'invalid'],
8+
e: [1e5],
9+
});
10+
11+
// This benchmark is used to compare the `Invalid URL` path of the URL parser
12+
function main({ type, e }) {
13+
const url = type === 'valid' ? 'https://www.nodejs.org' : 'www.nodejs.org';
14+
bench.start();
15+
for (let i = 0; i < e; i++) {
16+
try {
17+
new URL(url);
18+
} catch {
19+
// do nothing
20+
}
21+
}
22+
bench.end(e);
23+
}

lib/internal/errors.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,8 +1370,7 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
13701370
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
13711371
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
13721372
E('ERR_INVALID_URI', 'URI malformed', URIError);
1373-
E('ERR_INVALID_URL', function(input) {
1374-
this.input = input;
1373+
E('ERR_INVALID_URL', function() {
13751374
// Don't include URL in message.
13761375
// (See https://github.com/nodejs/node/pull/38614)
13771376
return 'Invalid URL';

lib/internal/modules/esm/load.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function getSource(url, context) {
4545
} else if (protocol === 'data:') {
4646
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
4747
if (!match) {
48-
throw new ERR_INVALID_URL(responseURL);
48+
throw new ERR_INVALID_URL();
4949
}
5050
const { 1: base64, 2: body } = match;
5151
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
@@ -84,7 +84,7 @@ function getSourceSync(url, context) {
8484
} else if (protocol === 'data:') {
8585
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
8686
if (!match) {
87-
throw new ERR_INVALID_URL(responseURL);
87+
throw new ERR_INVALID_URL();
8888
}
8989
const { 1: base64, 2: body } = match;
9090
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');

lib/internal/url.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,7 @@ class URL {
780780
base = `${base}`;
781781
}
782782

783-
const href = bindingUrl.parse(input, base);
784-
785-
if (!href) {
786-
throw new ERR_INVALID_URL(input);
787-
}
788-
789-
this.#updateContext(href);
783+
this.#updateContext(bindingUrl.parse(input, base));
790784
}
791785

792786
[inspect.custom](depth, opts) {
@@ -861,7 +855,7 @@ class URL {
861855
set href(value) {
862856
value = `${value}`;
863857
const href = bindingUrl.update(this.#context.href, updateActions.kHref, value);
864-
if (!href) { throw ERR_INVALID_URL(value); }
858+
if (!href) { throw ERR_INVALID_URL(); }
865859
this.#updateContext(href);
866860
}
867861

lib/url.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
394394
if (this.hostname !== '') {
395395
if (ipv6Hostname) {
396396
if (forbiddenHostCharsIpv6.test(this.hostname)) {
397-
throw new ERR_INVALID_URL(url);
397+
throw new ERR_INVALID_URL();
398398
}
399399
} else {
400400
// IDNA Support: Returns a punycoded representation of "domain".
@@ -413,7 +413,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
413413
// the pathname as we've done in getHostname, throw an exception to
414414
// convey the severity of this issue.
415415
if (this.hostname === '' || forbiddenHostChars.test(this.hostname)) {
416-
throw new ERR_INVALID_URL(url);
416+
throw new ERR_INVALID_URL();
417417
}
418418
}
419419
}

src/node_url.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
243243
base =
244244
ada::parse<ada::url_aggregator>(Utf8Value(isolate, args[1]).ToString());
245245
if (!base) {
246-
return args.GetReturnValue().Set(false);
246+
return THROW_ERR_INVALID_URL(realm->env(), "Invalid URL");
247247
}
248248
base_pointer = &base.value();
249249
}
250250
auto out =
251251
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
252252

253253
if (!out) {
254-
return args.GetReturnValue().Set(false);
254+
return THROW_ERR_INVALID_URL(realm->env(), "Invalid URL");
255255
}
256256

257257
binding_data->UpdateComponents(out->get_components(), out->type);

test/parallel/test-url-null-char.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ const assert = require('assert');
44

55
assert.throws(
66
() => { new URL('a\0b'); },
7-
{ input: 'a\0b' }
7+
{ code: 'ERR_INVALID_URL' }
88
);

test/parallel/test-url-parse-invalid-input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ assert.throws(() => { url.parse('http://%E0%A4%A@fail'); },
3838
});
3939

4040
assert.throws(() => { url.parse('http://[127.0.0.1\x00c8763]:8000/'); },
41-
{ code: 'ERR_INVALID_URL', input: 'http://[127.0.0.1\x00c8763]:8000/' }
41+
{ code: 'ERR_INVALID_URL' }
4242
);
4343

4444
if (common.hasIntl) {

test/parallel/test-whatwg-url-custom-parsing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ for (const test of failureTests) {
5555
() => new URL(test.input, test.base),
5656
(error) => {
5757
assert.throws(() => { throw error; }, expectedError);
58-
assert.strictEqual(`${error}`, 'TypeError [ERR_INVALID_URL]: Invalid URL');
58+
assert.strictEqual(`${error}`, 'TypeError: Invalid URL');
5959
assert.strictEqual(error.message, 'Invalid URL');
6060
return true;
6161
});

0 commit comments

Comments
 (0)