Skip to content
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
48 changes: 48 additions & 0 deletions benchmark/url/urlpatternlist-exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
const common = require('../common.js');
const { URLPattern, URLPatternList } = require('url');
const assert = require('assert');

const bench = common.createBenchmark(main, {
routes: [10, 100, 1000],
input: ['first', 'last', 'none'],
method: ['URLPatternList', 'URLPattern'],
n: [1e4],
}, {
flags: ['--disable-warning=ExperimentalWarning'],
});

function main({ routes, input, method, n }) {
const patterns = [];
for (let i = 0; i < routes; i++) {
patterns.push(new URLPattern({ pathname: `/api/resource${i}/:id` }));
}
const url = {
first: 'https://example.com/api/resource0/42',
last: `https://example.com/api/resource${routes - 1}/42`,
none: 'https://example.com/api/missing/42',
}[input];

let exec;
if (method === 'URLPatternList') {
const list = new URLPatternList(patterns);
exec = (url) => list.exec(url);
} else {
// The loop that URLPatternList#exec() is specified as.
exec = (url) => {
for (const pattern of patterns) {
const result = pattern.exec(url);
if (result !== null) return result;
}
return null;
};
}

let result;
bench.start();
for (let i = 0; i < n; i++) {
result = exec(url);
}
bench.end(n);
assert.strictEqual(result === null, input === 'none');
}
175 changes: 175 additions & 0 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,8 @@ console.log(myPattern.test('https://nodejs.org/docs/latest/api/dns.html'));
// Prints: true
```

To find which of several patterns matches a URL, see [`URLPatternList`][].

#### `new URLPattern()`

Instantiate a new empty `URLPattern` object.
Expand Down Expand Up @@ -846,6 +848,175 @@ console.log(myPattern.test('https://nodejs.org/docs/latest/api/dns.html'));
// Prints: true
```

### Class: `URLPatternList`

<!-- YAML
added: REPLACEME
-->

> Stability: 1.0 - Early development

A `URLPatternList` matches a URL against an ordered list of {URLPattern}
objects and reports the first pattern that matches. `URLPatternList` is
not a standard API, and it is not yet clear whether it will become one.
It implements the interface proposed in [whatwg/urlpattern#166][].

The result is the same as calling [`urlPattern.exec()`][] on each pattern in
list order and keeping the first result that is not `null`. A `URLPatternList`
processes the input once per call instead of once per pattern, and uses an
index of the patterns' pathnames to skip patterns that cannot match.

```mjs
import { URLPattern, URLPatternList } from 'node:url';

const patterns = [
new URLPattern('https://example.com/'),
new URLPattern('https://example.com/about'),
new URLPattern('https://example.com/blog/:slug'),
];
const list = new URLPatternList(patterns);

const result = list.exec('https://example.com/blog/hello');
console.log(patterns.indexOf(result.pattern));
// Prints: 2
console.log(result.pathname.groups.slug);
// Prints: hello

console.log(list.test('https://example.com/contact'));
// Prints: false
```

```cjs
const { URLPattern, URLPatternList } = require('node:url');

const patterns = [
new URLPattern('https://example.com/'),
new URLPattern('https://example.com/about'),
new URLPattern('https://example.com/blog/:slug'),
];
const list = new URLPatternList(patterns);

const result = list.exec('https://example.com/blog/hello');
console.log(patterns.indexOf(result.pattern));
// Prints: 2
console.log(result.pathname.groups.slug);
// Prints: hello

console.log(list.test('https://example.com/contact'));
// Prints: false
```

To attach data such as a route handler to each pattern, keep it in a {Map}
keyed by the `URLPattern` and look it up with `result.pattern`.

#### `new URLPatternList(patterns)`

* `patterns` {Iterable} The {URLPattern} objects to match against, in the order
in which they are matched.

Creates a `URLPatternList` from the values of `patterns`, in iteration order.
Later changes to `patterns` do not affect the list.

The values must be `URLPattern` objects. Pattern strings, `URLPatternInit`
objects, and options such as `ignoreCase` are not accepted; pass them to the
{URLPattern} constructor instead. Each pattern keeps its own options, so a list
can mix case-sensitive and case-insensitive patterns.

A `TypeError` is thrown if `patterns` is not iterable, if one of its values is
not a `URLPattern`, if it contains the same `URLPattern` object more than once,
or if it is empty.

#### `urlPatternList.exec([input[, baseURL]])`

* `input` {string|Object} A URL string, or an object with the URL components
to match, as accepted by [`urlPattern.exec()`][]. **Default:** `{}`.
* `baseURL` {string} A base URL string used to resolve `input` when it is a
relative URL string.
* Returns: {Object|null}

Matches `input` against the patterns in list order. Returns `null` if no pattern
matches, which includes the case where `input` or `baseURL` is not a valid URL.
Otherwise, returns the result of [`urlPattern.exec()`][] for the first pattern
that matches, with an additional `pattern` property set to that `URLPattern`.

A `TypeError` is thrown if `baseURL` is passed and `input` is not a string.

The first pattern that matches wins, even if a later pattern is more specific:

```mjs
import { URLPattern, URLPatternList } from 'node:url';

const me = new URLPattern({ pathname: '/users/me' });
const user = new URLPattern({ pathname: '/users/:id' });

console.log(new URLPatternList([user, me]).exec({ pathname: '/users/me' })
.pattern === user);
// Prints: true
console.log(new URLPatternList([me, user]).exec({ pathname: '/users/me' })
.pattern === me);
// Prints: true
```

```cjs
const { URLPattern, URLPatternList } = require('node:url');

const me = new URLPattern({ pathname: '/users/me' });
const user = new URLPattern({ pathname: '/users/:id' });

console.log(new URLPatternList([user, me]).exec({ pathname: '/users/me' })
.pattern === user);
// Prints: true
console.log(new URLPatternList([me, user]).exec({ pathname: '/users/me' })
.pattern === me);
// Prints: true
```

#### `urlPatternList.test([input[, baseURL]])`

* `input` {string|Object} A URL string, or an object with the URL components
to match, as accepted by [`urlPattern.test()`][]. **Default:** `{}`.
* `baseURL` {string} A base URL string used to resolve `input` when it is a
relative URL string.
* Returns: {boolean}

Returns `true` if any pattern in the list matches `input`, and `false`
otherwise, which includes the case where `input` or `baseURL` is not a valid
URL.

A `TypeError` is thrown if `baseURL` is passed and `input` is not a string.

```mjs
import { URLPattern, URLPatternList } from 'node:url';

const list = new URLPatternList([
new URLPattern({ pathname: '/about' }),
new URLPattern({ pathname: '/blog/:slug' }),
]);

console.log(list.test('https://example.com/blog/hello'));
// Prints: true
console.log(list.test('/about', 'https://example.com'));
// Prints: true
console.log(list.test({ pathname: '/contact' }));
// Prints: false
```

```cjs
const { URLPattern, URLPatternList } = require('node:url');

const list = new URLPatternList([
new URLPattern({ pathname: '/about' }),
new URLPattern({ pathname: '/blog/:slug' }),
]);

console.log(list.test('https://example.com/blog/hello'));
// Prints: true
console.log(list.test('/about', 'https://example.com'));
// Prints: true
console.log(list.test({ pathname: '/contact' }));
// Prints: false
```

### Class: `URLSearchParams`

<!-- YAML
Expand Down Expand Up @@ -2103,6 +2274,7 @@ console.log(myURL.origin);
[`Error`]: errors.md#class-error
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[`TypeError`]: errors.md#class-typeerror
[`URLPatternList`]: #class-urlpatternlist
[`URLSearchParams`]: #class-urlsearchparams
[`array.toString()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
[`http.request()`]: http.md#httprequestoptions-callback
Expand All @@ -2118,6 +2290,8 @@ console.log(myURL.origin);
[`url.search`]: #urlsearch
[`url.toJSON()`]: #urltojson
[`url.toString()`]: #urltostring
[`urlPattern.exec()`]: #urlpatternexecinput-baseurl
[`urlPattern.test()`]: #urlpatterntestinput-baseurl
[`urlSearchParams.entries()`]: #urlsearchparamsentries
[`urlSearchParamsSymbol.iterator()`]: #urlsearchparamssymboliterator
[converted to a string]: https://tc39.es/ecma262/#sec-tostring
Expand All @@ -2126,3 +2300,4 @@ console.log(myURL.origin);
[legacy `urlObject`]: #legacy-urlobject
[percent-encoded]: #percent-encoding-in-urls
[stable sorting algorithm]: https://en.wikipedia.org/wiki/Sorting_algorithm#Stability
[whatwg/urlpattern#166]: https://github.com/whatwg/urlpattern/pull/166
2 changes: 2 additions & 0 deletions doc/type-map.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@
"TransformStreamDefaultController": "webstreams.html#class-transformstreamdefaultcontroller",
"TurboShakeParams": "webcrypto.html#class-turboshakeparams",
"URL": "url.html#the-whatwg-url-api",
"URLPattern": "url.html#class-urlpattern",
"URLPatternList": "url.html#class-urlpatternlist",
"URLSearchParams": "url.html#class-urlsearchparams",
"Verify": "crypto.html#class-verify",
"VirtualFileSystem": "vfs.html#class-virtualfilesystem",
Expand Down
3 changes: 2 additions & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {
decodeURIComponent,
} = primordials;

const { URLPattern } = internalBinding('url_pattern');
const { URLPattern, URLPatternList } = internalBinding('url_pattern');
const { toASCII } = internalBinding('encoding_binding');
const { encodeStr, hexTable } = require('internal/querystring');
const querystring = require('querystring');
Expand Down Expand Up @@ -1033,6 +1033,7 @@ module.exports = {
// WHATWG API
URL,
URLPattern,
URLPatternList,
URLSearchParams,
domainToASCII,
domainToUnicode,
Expand Down
7 changes: 7 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,12 @@ class Environment final : public MemoryRetainer {
return current_value;
}

inline bool EmitURLPatternListWarning() {
bool current_value = emit_url_pattern_list_warning_;
emit_url_pattern_list_warning_ = false;
return current_value;
}

// cb will be called as cb(env) on the next event loop iteration.
// Unlike the JS setImmediate() function, nested SetImmediate() calls will
// be run without returning control to the event loop, similar to nextTick().
Expand Down Expand Up @@ -1353,6 +1359,7 @@ class Environment final : public MemoryRetainer {
bool trace_sync_io_ = false;
bool emit_env_nonstring_warning_ = true;
bool emit_err_name_warning_ = true;
bool emit_url_pattern_list_warning_ = true;
bool source_maps_enabled_ = false;

size_t async_callback_scope_depth_ = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,10 @@
V(tlsa_record_template, v8::DictionaryTemplate) \
V(tty_constructor_template, v8::FunctionTemplate) \
V(txt_record_template, v8::DictionaryTemplate) \
V(urlpattern_constructor_template, v8::FunctionTemplate) \
V(urlpatterncomponentresult_template, v8::DictionaryTemplate) \
V(urlpatterninit_template, v8::DictionaryTemplate) \
V(urlpatternlistresult_template, v8::DictionaryTemplate) \
V(urlpatternresult_template, v8::DictionaryTemplate) \
V(write_wrap_template, v8::FunctionTemplate) \
V(worker_cpu_profile_taker_template, v8::ObjectTemplate) \
Expand Down
Loading
Loading