From 43c593c428cfdec44f64018a6cb23762251e8dbb Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 30 Jan 2025 14:38:19 -0500 Subject: [PATCH] url: add URLPattern implementation Co-authored-by: Daniel Lemire PR-URL: https://github.com/nodejs/node/pull/56452 Reviewed-By: Daniel Lemire Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Benjamin Gruenbaum Reviewed-By: Jordan Harband Reviewed-By: Stephen Belanger --- doc/api/errors.md | 7 + doc/api/url.md | 123 ++++ lib/internal/url.js | 2 + lib/url.js | 2 + node.gyp | 2 + src/env_properties.h | 10 + src/node_binding.cc | 2 + src/node_errors.h | 2 + src/node_external_reference.h | 1 + src/node_url_pattern.cc | 787 +++++++++++++++++++++++ src/node_url_pattern.h | 112 ++++ test/parallel/test-bootstrap-modules.js | 1 + typings/globals.d.ts | 2 + typings/internalBinding/url_pattern.d.ts | 20 + 14 files changed, 1073 insertions(+) create mode 100644 src/node_url_pattern.cc create mode 100644 src/node_url_pattern.h create mode 100644 typings/internalBinding/url_pattern.d.ts diff --git a/doc/api/errors.md b/doc/api/errors.md index 8009e75d07..e02624f367 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2118,6 +2118,13 @@ constructor][`new URL(input)`] or the legacy [`url.parse()`][] to be parsed. The thrown error object typically has an additional property `'input'` that contains the URL that failed to parse. + + +### `ERR_INVALID_URL_PATTERN` + +An invalid URLPattern was passed to the [WHATWG][WHATWG URL API] \[`URLPattern` +constructor]\[`new URLPattern(input)`] to be parsed. + ### `ERR_INVALID_URL_SCHEME` diff --git a/doc/api/url.md b/doc/api/url.md index 56fb57aa68..5ef961a5e7 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -714,6 +714,129 @@ Parses a string as a URL. If `base` is provided, it will be used as the base URL for the purpose of resolving non-absolute `input` URLs. Returns `null` if `input` is not a valid. +### Class: `URLPattern` + +> Stability: 1 - Experimental + + + +The `URLPattern` API provides an interface to match URLs or parts of URLs +against a pattern. + +```js +const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html'); +console.log(myPattern.exec('https://nodejs.org/docs/latest/api/dns.html')); +// Prints: +// { +// "hash": { "groups": { "0": "" }, "input": "" }, +// "hostname": { "groups": {}, "input": "nodejs.org" }, +// "inputs": [ +// "https://nodejs.org/docs/latest/api/dns.html" +// ], +// "password": { "groups": { "0": "" }, "input": "" }, +// "pathname": { "groups": { "0": "dns" }, "input": "/docs/latest/api/dns.html" }, +// "port": { "groups": {}, "input": "" }, +// "protocol": { "groups": {}, "input": "https" }, +// "search": { "groups": { "0": "" }, "input": "" }, +// "username": { "groups": { "0": "" }, "input": "" } +// } + +console.log(myPattern.test('https://nodejs.org/docs/latest/api/dns.html')); +// Prints: true +``` + +#### `new URLPattern()` + +Instantiate a new empty `URLPattern` object. + +#### `new URLPattern(string[, baseURL][, options])` + +* `string` {string} A URL string +* `baseURL` {string | undefined} A base URL string +* `options` {Object} Options + +Parse the `string` as a URL, and use it to instantiate a new +`URLPattern` object. + +If `baseURL` is not specified, it defaults to `undefined`. + +An option can have `ignoreCase` boolean attribute which enables +case-insensitive matching if set to true. + +The constructor can throw a `TypeError` to indicate parsing failure. + +#### `new URLPattern(objg[, baseURL][, options])` + +* `obj` {Object} An input pattern +* `baseURL` {string | undefined} A base URL string +* `options` {Object} Options + +Parse the `Object` as an input pattern, and use it to instantiate a new +`URLPattern` object. The object members can be any of `protocol`, `username`, +`password`, `hostname`, `port`, `pathname`, `search`, `hash` or `baseURL`. + +If `baseURL` is not specified, it defaults to `undefined`. + +An option can have `ignoreCase` boolean attribute which enables +case-insensitive matching if set to true. + +The constructor can throw a `TypeError` to indicate parsing failure. + +#### `urlPattern.exec(input[, baseURL])` + +* `input` {string | Object} A URL or URL parts +* `baseURL` {string | undefined} A base URL string + +Input can be a string or an object providing the individual URL parts. The +object members can be any of `protocol`, `username`, `password`, `hostname`, +`port`, `pathname`, `search`, `hash` or `baseURL`. + +If `baseURL` is not specified, it will default to `undefined`. + +Returns an object with an `inputs` key containing the array of arguments +passed into the function and keys of the URL components which contains the +matched input and matched groups. + +```js +const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html'); +console.log(myPattern.exec('https://nodejs.org/docs/latest/api/dns.html')); +// Prints: +// { +// "hash": { "groups": { "0": "" }, "input": "" }, +// "hostname": { "groups": {}, "input": "nodejs.org" }, +// "inputs": [ +// "https://nodejs.org/docs/latest/api/dns.html" +// ], +// "password": { "groups": { "0": "" }, "input": "" }, +// "pathname": { "groups": { "0": "dns" }, "input": "/docs/latest/api/dns.html" }, +// "port": { "groups": {}, "input": "" }, +// "protocol": { "groups": {}, "input": "https" }, +// "search": { "groups": { "0": "" }, "input": "" }, +// "username": { "groups": { "0": "" }, "input": "" } +// } +``` + +#### `urlPattern.test(input[, baseURL])` + +* `input` {string | Object} A URL or URL parts +* `baseURL` {string | undefined} A base URL string + +Input can be a string or an object providing the individual URL parts. The +object members can be any of `protocol`, `username`, `password`, `hostname`, +`port`, `pathname`, `search`, `hash` or `baseURL`. + +If `baseURL` is not specified, it will default to `undefined`. + +Returns a boolean indicating if the input matches the current pattern. + +```js +const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html'); +console.log(myPattern.test('https://nodejs.org/docs/latest/api/dns.html')); +// Prints: true +``` + ### Class: `URLSearchParams`