GeezerVison is showing github.com — url: add URLPattern implementation · nodejs/node@bc97a90
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/errors.md +7Lines changed: 7 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2116,6 +2116,13 @@ constructor][`new URL(input)`] or the legacy [`url.parse()`][] to be parsed. | 2116 | 2116 | The thrown error object typically has an additional property `'input'` that | 2117 | 2117 | contains the URL that failed to parse. | 2118 | 2118 |
| | 2119 | +<a id="ERR_INVALID_URL_PATTERN"></a> | | 2120 | + | | 2121 | +### `ERR_INVALID_URL_PATTERN` | | 2122 | + | | 2123 | +An invalid URLPattern was passed to the [WHATWG][WHATWG URL API] \[`URLPattern` | | 2124 | +constructor]\[`new URLPattern(input)`] to be parsed. | | 2125 | + | 2119 | 2126 | <a id="ERR_INVALID_URL_SCHEME"></a> | 2120 | 2127 |
| 2121 | 2128 | ### `ERR_INVALID_URL_SCHEME` | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/url.md +123Lines changed: 123 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -714,6 +714,129 @@ Parses a string as a URL. If `base` is provided, it will be used as the base | 714 | 714 | URL for the purpose of resolving non-absolute `input` URLs. Returns `null` | 715 | 715 | if `input` is not a valid. | 716 | 716 |
| | 717 | +### Class: `URLPattern` | | 718 | + | | 719 | +> Stability: 1 - Experimental | | 720 | +
| | 721 | +<!-- YAML | | 722 | +added: REPLACEME | | 723 | +--> | | 724 | + | | 725 | +The `URLPattern` API provides an interface to match URLs or parts of URLs | | 726 | +against a pattern. | | 727 | + | | 728 | +```js | | 729 | +const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html'); | | 730 | +console.log(myPattern.exec('https://nodejs.org/docs/latest/api/dns.html')); | | 731 | +// Prints: | | 732 | +// { | | 733 | +// "hash": { "groups": { "0": "" }, "input": "" }, | | 734 | +// "hostname": { "groups": {}, "input": "nodejs.org" }, | | 735 | +// "inputs": [ | | 736 | +// "https://nodejs.org/docs/latest/api/dns.html" | | 737 | +// ], | | 738 | +// "password": { "groups": { "0": "" }, "input": "" }, | | 739 | +// "pathname": { "groups": { "0": "dns" }, "input": "/docs/latest/api/dns.html" }, | | 740 | +// "port": { "groups": {}, "input": "" }, | | 741 | +// "protocol": { "groups": {}, "input": "https" }, | | 742 | +// "search": { "groups": { "0": "" }, "input": "" }, | | 743 | +// "username": { "groups": { "0": "" }, "input": "" } | | 744 | +// } | | 745 | + | | 746 | +console.log(myPattern.test('https://nodejs.org/docs/latest/api/dns.html')); | | 747 | +// Prints: true | | 748 | +``` | | 749 | + | | 750 | +#### `new URLPattern()` | | 751 | + | | 752 | +Instantiate a new empty `URLPattern` object. | | 753 | + | | 754 | +#### `new URLPattern(string[, baseURL][, options])` | | 755 | + | | 756 | +* `string` {string} A URL string | | 757 | +* `baseURL` {string | undefined} A base URL string | | 758 | +* `options` {Object} Options | | 759 | + | | 760 | +Parse the `string` as a URL, and use it to instantiate a new | | 761 | +`URLPattern` object. | | 762 | + | | 763 | +If `baseURL` is not specified, it defaults to `undefined`. | | 764 | + | | 765 | +An option can have `ignoreCase` boolean attribute which enables | | 766 | +case-insensitive matching if set to true. | | 767 | + | | 768 | +The constructor can throw a `TypeError` to indicate parsing failure. | | 769 | + | | 770 | +#### `new URLPattern(objg[, baseURL][, options])` | | 771 | + | | 772 | +* `obj` {Object} An input pattern | | 773 | +* `baseURL` {string | undefined} A base URL string | | 774 | +* `options` {Object} Options | | 775 | + | | 776 | +Parse the `Object` as an input pattern, and use it to instantiate a new | | 777 | +`URLPattern` object. The object members can be any of `protocol`, `username`, | | 778 | +`password`, `hostname`, `port`, `pathname`, `search`, `hash` or `baseURL`. | | 779 | + | | 780 | +If `baseURL` is not specified, it defaults to `undefined`. | | 781 | + | | 782 | +An option can have `ignoreCase` boolean attribute which enables | | 783 | +case-insensitive matching if set to true. | | 784 | + | | 785 | +The constructor can throw a `TypeError` to indicate parsing failure. | | 786 | + | | 787 | +#### `urlPattern.exec(input[, baseURL])` | | 788 | + | | 789 | +* `input` {string | Object} A URL or URL parts | | 790 | +* `baseURL` {string | undefined} A base URL string | | 791 | + | | 792 | +Input can be a string or an object providing the individual URL parts. The | | 793 | +object members can be any of `protocol`, `username`, `password`, `hostname`, | | 794 | +`port`, `pathname`, `search`, `hash` or `baseURL`. | | 795 | + | | 796 | +If `baseURL` is not specified, it will default to `undefined`. | | 797 | + | | 798 | +Returns an object with an `inputs` key containing the array of arguments | | 799 | +passed into the function and keys of the URL components which contains the | | 800 | +matched input and matched groups. | | 801 | + | | 802 | +```js | | 803 | +const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html'); | | 804 | +console.log(myPattern.exec('https://nodejs.org/docs/latest/api/dns.html')); | | 805 | +// Prints: | | 806 | +// { | | 807 | +// "hash": { "groups": { "0": "" }, "input": "" }, | | 808 | +// "hostname": { "groups": {}, "input": "nodejs.org" }, | | 809 | +// "inputs": [ | | 810 | +// "https://nodejs.org/docs/latest/api/dns.html" | | 811 | +// ], | | 812 | +// "password": { "groups": { "0": "" }, "input": "" }, | | 813 | +// "pathname": { "groups": { "0": "dns" }, "input": "/docs/latest/api/dns.html" }, | | 814 | +// "port": { "groups": {}, "input": "" }, | | 815 | +// "protocol": { "groups": {}, "input": "https" }, | | 816 | +// "search": { "groups": { "0": "" }, "input": "" }, | | 817 | +// "username": { "groups": { "0": "" }, "input": "" } | | 818 | +// } | | 819 | +``` | | 820 | + | | 821 | +#### `urlPattern.test(input[, baseURL])` | | 822 | + | | 823 | +* `input` {string | Object} A URL or URL parts | | 824 | +* `baseURL` {string | undefined} A base URL string | | 825 | + | | 826 | +Input can be a string or an object providing the individual URL parts. The | | 827 | +object members can be any of `protocol`, `username`, `password`, `hostname`, | | 828 | +`port`, `pathname`, `search`, `hash` or `baseURL`. | | 829 | + | | 830 | +If `baseURL` is not specified, it will default to `undefined`. | | 831 | + | | 832 | +Returns a boolean indicating if the input matches the current pattern. | | 833 | + | | 834 | +```js | | 835 | +const myPattern = new URLPattern('https://nodejs.org/docs/latest/api/*.html'); | | 836 | +console.log(myPattern.test('https://nodejs.org/docs/latest/api/dns.html')); | | 837 | +// Prints: true | | 838 | +``` | | 839 | + | 717 | 840 | ### Class: `URLSearchParams` | 718 | 841 |
| 719 | 842 | <!-- YAML | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ const { | 32 | 32 | decodeURIComponent, | 33 | 33 | } = primordials; | 34 | 34 |
| | 35 | +const { URLPattern } = internalBinding('url_pattern'); | 35 | 36 | const { inspect } = require('internal/util/inspect'); | 36 | 37 | const { | 37 | 38 | encodeStr, | @@ -1574,6 +1575,7 @@ module.exports = { | 1574 | 1575 | toPathIfFileURL, | 1575 | 1576 | installObjectURLMethods, | 1576 | 1577 | URL, | | 1578 | + URLPattern, | 1577 | 1579 | URLSearchParams, | 1578 | 1580 | URLParse: URL.parse, | 1579 | 1581 | domainToASCII, | |
Collapse file Copy file name to clipboardExpand all lines: lib/url.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,7 @@ const { | 30 | 30 | decodeURIComponent, | 31 | 31 | } = primordials; | 32 | 32 |
| | 33 | +const { URLPattern } = internalBinding('url_pattern'); | 33 | 34 | const { toASCII } = internalBinding('encoding_binding'); | 34 | 35 | const { encodeStr, hexTable } = require('internal/querystring'); | 35 | 36 | const querystring = require('querystring'); | @@ -1030,6 +1031,7 @@ module.exports = { | 1030 | 1031 |
| 1031 | 1032 | // WHATWG API | 1032 | 1033 | URL, | | 1034 | + URLPattern, | 1033 | 1035 | URLSearchParams, | 1034 | 1036 | domainToASCII, | 1035 | 1037 | domainToUnicode, | |
Collapse file Copy file name to clipboardExpand all lines: node.gyp +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 146 | 146 | 'src/node_trace_events.cc', | 147 | 147 | 'src/node_types.cc', | 148 | 148 | 'src/node_url.cc', | | 149 | + 'src/node_url_pattern.cc', | 149 | 150 | 'src/node_util.cc', | 150 | 151 | 'src/node_v8.cc', | 151 | 152 | 'src/node_wasi.cc', | | 275 | 276 | 'src/node_stat_watcher.h', | 276 | 277 | 'src/node_union_bytes.h', | 277 | 278 | 'src/node_url.h', | | 279 | + 'src/node_url_pattern.h', | 278 | 280 | 'src/node_version.h', | 279 | 281 | 'src/node_v8.h', | 280 | 282 | 'src/node_v8_platform-inl.h', | |
Collapse file Copy file name to clipboardExpand all lines: src/env_properties.h +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 78 | 78 | V(async_ids_stack_string, "async_ids_stack") \ | 79 | 79 | V(attributes_string, "attributes") \ | 80 | 80 | V(base_string, "base") \ | | 81 | + V(base_url_string, "baseURL") \ | 81 | 82 | V(bits_string, "bits") \ | 82 | 83 | V(block_list_string, "blockList") \ | 83 | 84 | V(buffer_string, "buffer") \ | | 179 | 180 | V(get_data_clone_error_string, "_getDataCloneError") \ | 180 | 181 | V(get_shared_array_buffer_id_string, "_getSharedArrayBufferId") \ | 181 | 182 | V(gid_string, "gid") \ | | 183 | + V(groups_string, "groups") \ | | 184 | + V(has_regexp_groups_string, "hasRegExpGroups") \ | | 185 | + V(hash_string, "hash") \ | 182 | 186 | V(h2_string, "h2") \ | 183 | 187 | V(handle_string, "handle") \ | 184 | 188 | V(hash_algorithm_string, "hashAlgorithm") \ | 185 | 189 | V(help_text_string, "helpText") \ | 186 | 190 | V(homedir_string, "homedir") \ | 187 | 191 | V(host_string, "host") \ | 188 | 192 | V(hostmaster_string, "hostmaster") \ | | 193 | + V(hostname_string, "hostname") \ | 189 | 194 | V(http_1_1_string, "http/1.1") \ | 190 | 195 | V(id_string, "id") \ | 191 | 196 | V(identity_string, "identity") \ | | 197 | + V(ignore_case_string, "ignoreCase") \ | 192 | 198 | V(ignore_string, "ignore") \ | 193 | 199 | V(infoaccess_string, "infoAccess") \ | 194 | 200 | V(inherit_string, "inherit") \ | 195 | 201 | V(input_string, "input") \ | | 202 | + V(inputs_string, "inputs") \ | 196 | 203 | V(internal_binding_string, "internalBinding") \ | 197 | 204 | V(internal_string, "internal") \ | 198 | 205 | V(ipv4_string, "IPv4") \ | | 280 | 287 | V(parse_error_string, "Parse Error") \ | 281 | 288 | V(password_string, "password") \ | 282 | 289 | V(path_string, "path") \ | | 290 | + V(pathname_string, "pathname") \ | 283 | 291 | V(pending_handle_string, "pendingHandle") \ | 284 | 292 | V(permission_string, "permission") \ | 285 | 293 | V(pid_string, "pid") \ | | 295 | 303 | V(priority_string, "priority") \ | 296 | 304 | V(process_string, "process") \ | 297 | 305 | V(promise_string, "promise") \ | | 306 | + V(protocol_string, "protocol") \ | 298 | 307 | V(prototype_string, "prototype") \ | 299 | 308 | V(psk_string, "psk") \ | 300 | 309 | V(pubkey_string, "pubkey") \ | | 323 | 332 | V(scopeid_string, "scopeid") \ | 324 | 333 | V(script_id_string, "scriptId") \ | 325 | 334 | V(script_name_string, "scriptName") \ | | 335 | + V(search_string, "search") \ | 326 | 336 | V(serial_number_string, "serialNumber") \ | 327 | 337 | V(serial_string, "serial") \ | 328 | 338 | V(servername_string, "servername") \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_binding.cc +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | #include "node_builtins.h" | 5 | 5 | #include "node_errors.h" | 6 | 6 | #include "node_external_reference.h" | | 7 | +#include "node_url_pattern.h" | 7 | 8 | #include "util.h" | 8 | 9 |
| 9 | 10 | #include <string> | | 87 | 88 | V(types) \ | 88 | 89 | V(udp_wrap) \ | 89 | 90 | V(url) \ | | 91 | + V(url_pattern) \ | 90 | 92 | V(util) \ | 91 | 93 | V(uv) \ | 92 | 94 | V(v8) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.h +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,6 +90,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); | 90 | 90 | V(ERR_INVALID_STATE, Error) \ | 91 | 91 | V(ERR_INVALID_THIS, TypeError) \ | 92 | 92 | V(ERR_INVALID_URL, TypeError) \ | | 93 | + V(ERR_INVALID_URL_PATTERN, TypeError) \ | 93 | 94 | V(ERR_INVALID_URL_SCHEME, TypeError) \ | 94 | 95 | V(ERR_LOAD_SQLITE_EXTENSION, Error) \ | 95 | 96 | V(ERR_MEMORY_ALLOCATION_FAILED, Error) \ | @@ -99,6 +100,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); | 99 | 100 | V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \ | 100 | 101 | V(ERR_MODULE_NOT_FOUND, Error) \ | 101 | 102 | V(ERR_NON_CONTEXT_AWARE_DISABLED, Error) \ | | 103 | + V(ERR_OPERATION_FAILED, TypeError) \ | 102 | 104 | V(ERR_OUT_OF_RANGE, RangeError) \ | 103 | 105 | V(ERR_REQUIRE_ASYNC_MODULE, Error) \ | 104 | 106 | V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_external_reference.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -180,6 +180,7 @@ class ExternalReferenceRegistry { | 180 | 180 | V(tty_wrap) \ | 181 | 181 | V(udp_wrap) \ | 182 | 182 | V(url) \ | | 183 | + V(url_pattern) \ | 183 | 184 | V(util) \ | 184 | 185 | V(pipe_wrap) \ | 185 | 186 | V(sea) \ | |
|