first
This commit is contained in:
commit
b053d27321
1660 files changed
+329972
No files matched your search
+19
@@ -0,0 +1,19 @@
|
||||
Copyright 2020 Intel Corporation
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
[](https://github.com/kenchris/urlpattern-polyfill/actions/workflows/workflow.yml)
|
||||
[](https://www.npmjs.com/package/urlpattern-polyfill)
|
||||

|
||||
|
||||
URLPattern polyfills
|
||||
===
|
||||
|
||||
URLPattern is a new web API for matching URLs. Its intended to both provide a convenient API for web developers and to be usable in other web APIs that need to match URLs; e.g. service workers. The [explainer](https://github.com/wanderview/service-worker-scope-pattern-matching/blob/master/explainer.md) discusses the motivating use cases.
|
||||
|
||||
This is a polyfill for the [URLPattern API](https://wicg.github.io/urlpattern/) so that the feature is available in browsers that don't support it natively. This polyfill passes
|
||||
the same web platform test suite.
|
||||
|
||||
How to load the polyfill
|
||||
---
|
||||
The polyfill works in browsers (ESM module) and in Node.js either via import (ESM module) or via require (CJS module).
|
||||
|
||||
The polyfill will only be loaded if the URLPattern doesn't already exist on the global object, and in that case it will add it to the global object.
|
||||
|
||||
## loading as ESM module
|
||||
|
||||
```javascript
|
||||
// Conditional ESM module loading (Node.js and browser)
|
||||
// @ts-ignore: Property 'UrlPattern' does not exist
|
||||
if (!globalThis.URLPattern) {
|
||||
await import("urlpattern-polyfill");
|
||||
}
|
||||
/**
|
||||
* The above is the recommended way to load the ESM module, as it only
|
||||
* loads it on demand, thus when not natively supported by the runtime or
|
||||
* already polyfilled.
|
||||
*/
|
||||
import "urlpattern-polyfill";
|
||||
|
||||
/**
|
||||
* In case you want to replace an existing implementation with the polyfill:
|
||||
*/
|
||||
import {URLPattern} from "urlpattern-polyfill";
|
||||
globalThis.URLPattern = URLPattern
|
||||
```
|
||||
|
||||
> ## Note:
|
||||
> The line with `// @ts-ignore: Property 'UrlPattern' does not exist ` is needed in some environments because before you load the polyfill it might not be available, and the feature-check in the if statement gives an TypeScript error. The whole idea is that it loads when its not there.
|
||||
|
||||
## loading as CommonJs module
|
||||
|
||||
```javascript
|
||||
// Conditional CJS module loading (Node.js)
|
||||
if (!globalThis.URLPattern) {
|
||||
require("urlpattern-polyfill");
|
||||
}
|
||||
/**
|
||||
* The above is the recommended way to load the CommonJs module, as it only
|
||||
* loads it on demand, thus when not natively supported by the runtime or
|
||||
* already polyfilled.
|
||||
*/
|
||||
require("urlpattern-polyfill");
|
||||
|
||||
/**
|
||||
* In case you want to replace an existing implementation with the polyfill:
|
||||
*/
|
||||
const {URLPattern} = require("urlpattern-polyfill");;
|
||||
globalThis.URLPattern = URLPattern
|
||||
```
|
||||
|
||||
> ## Note:
|
||||
> No matter how you load the polyfill, when there is no implementation in your environment, it will _always_ add it to the global object.
|
||||
|
||||
Basic example
|
||||
---
|
||||
|
||||
```javascript
|
||||
let p = new URLPattern({ pathname: '/foo/:name' });
|
||||
|
||||
let r = p.exec('https://example.com/foo/bar');
|
||||
console.log(r.pathname.input); // "/foo/bar"
|
||||
console.log(r.pathname.groups.name); // "bar"
|
||||
|
||||
let r2 = p.exec({ pathname: '/foo/baz' });
|
||||
console.log(r2.pathname.groups.name); // "baz"
|
||||
```
|
||||
|
||||
Example of matching same-origin JPG or PNG requests
|
||||
---
|
||||
|
||||
```javascript
|
||||
// Match same-origin jpg or png URLs.
|
||||
// Note: This uses a named group to make it easier to access
|
||||
// the result later.
|
||||
const p = new URLPattern({
|
||||
pathname: '/*.:filetype(jpg|png)',
|
||||
baseURL: self.location
|
||||
});
|
||||
|
||||
for (let url in url_list) {
|
||||
const r = p.exec(url);
|
||||
|
||||
// skip non-matches
|
||||
if (!r) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (r.pathname.groups['filetype'] === 'jpg') {
|
||||
// process jpg
|
||||
} else if (r.pathname.groups['filetype'] === 'png') {
|
||||
// process png
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The pattern in this case can be made simpler without the origin check by leaving off the baseURL.
|
||||
|
||||
```javascript
|
||||
// Match any URL ending with 'jpg' or 'png'.
|
||||
const p = new URLPattern({ pathname: '/*.:filetype(jpg|png)' });
|
||||
```
|
||||
|
||||
Example of Short Form Support
|
||||
---
|
||||
We are planning to also support a "short form" for initializing URLPattern objects.
|
||||
This is supported by the polyfill but not yet by the Chromium implementation.
|
||||
|
||||
For example:
|
||||
|
||||
```javascript
|
||||
const p = new URLPattern("https://*.example.com/foo/*");
|
||||
```
|
||||
|
||||
Or:
|
||||
|
||||
```javascript
|
||||
const p = new URLPattern("foo/*", self.location);
|
||||
```
|
||||
|
||||
API reference
|
||||
===
|
||||
|
||||
API overview with typeScript type annotations is found below. Associated browser Web IDL can be found [here](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/modules/url_pattern/).
|
||||
|
||||
```ts
|
||||
type URLPatternInput = URLPatternInit | string;
|
||||
|
||||
class URLPattern {
|
||||
constructor(init?: URLPatternInput, baseURL?: string);
|
||||
|
||||
test(input?: URLPatternInput, baseURL?: string): boolean;
|
||||
|
||||
exec(input?: URLPatternInput, baseURL?: string): URLPatternResult | null;
|
||||
|
||||
readonly protocol: string;
|
||||
readonly username: string;
|
||||
readonly password: string;
|
||||
readonly hostname: string;
|
||||
readonly port: string;
|
||||
readonly pathname: string;
|
||||
readonly search: string;
|
||||
readonly hash: string;
|
||||
}
|
||||
|
||||
interface URLPatternInit {
|
||||
baseURL?: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
protocol?: string;
|
||||
hostname?: string;
|
||||
port?: string;
|
||||
pathname?: string;
|
||||
search?: string;
|
||||
hash?: string;
|
||||
}
|
||||
|
||||
interface URLPatternResult {
|
||||
inputs: [URLPatternInput];
|
||||
protocol: URLPatternComponentResult;
|
||||
username: URLPatternComponentResult;
|
||||
password: URLPatternComponentResult;
|
||||
hostname: URLPatternComponentResult;
|
||||
port: URLPatternComponentResult;
|
||||
pathname: URLPatternComponentResult;
|
||||
search: URLPatternComponentResult;
|
||||
hash: URLPatternComponentResult;
|
||||
}
|
||||
|
||||
interface URLPatternComponentResult {
|
||||
input: string;
|
||||
groups: {
|
||||
[key: string]: string | undefined;
|
||||
};
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Pattern syntax
|
||||
===
|
||||
The pattern syntax here is based on what is used in the popular path-to-regexp library.
|
||||
|
||||
* An understanding of a "divider" that separates segments of the string. For the pathname this is typically the `"/"` character.
|
||||
* A regex group defined by an enclosed set of parentheses. Inside of the parentheses a general regex may be defined.
|
||||
* A named group that matches characters until the next divider. The named group begins with a `":"` character and then a name. For example, `"/:foo/:bar"` has two named groups.
|
||||
* A custom regex for a named group. In this case a set of parentheses with a regex immediately follows the named group; e.g. `"/:foo(.*)"` will override the default of matching to the next divider.
|
||||
* A modifier may optionally follow a regex or named group. A modifier is a `"?"`, `"*"`, or `"+"` functions just as they do in regular expressions. When a group is optional or repeated and it's preceded by a divider then the divider is also optional or repeated. For example, `"/foo/:bar?"` will match `"/foo"`, `"/foo/"`, or `"/foo/baz"`. Escaping the divider will make it required instead.
|
||||
* A way to greedily match characters, even across dividers, by using `"(.*)"` (so-called unnamed groups).
|
||||
|
||||
|
||||
Currently we plan to have these known differences with path-to-regexp:
|
||||
|
||||
* No support for custom prefixes and suffixes.
|
||||
|
||||
|
||||
Canonicalization
|
||||
===
|
||||
|
||||
URLs have a canonical form that is based on ASCII, meaning that [internationalized domain names](https://en.wikipedia.org/wiki/Internationalized_domain_name) (hostnames) also have a canonical ASCII based representation, and that other components such as `hash`, `search` and `pathname` are encoded using [percent encoding](https://en.wikipedia.org/wiki/Percent-encoding).
|
||||
|
||||
Currently `URLPattern` does not perform any encoding or normalization of the patterns. So a developer would need to URL encode unicode characters before passing the pattern into the constructor. Similarly, the constructor does not do things like flattening pathnames such as /foo/../bar to /bar. Currently the pattern must be written to target canonical URL output manually.
|
||||
|
||||
It does, however, perform these operations for `test()` and `exec()` input.
|
||||
|
||||
Encoding components can easily be done manually, but do not encoding the pattern syntax:
|
||||
|
||||
```javascript
|
||||
encodeURIComponent("?q=æøå")
|
||||
// "%3Fq%3D%C3%A6%C3%B8%C3%A5"
|
||||
```
|
||||
|
||||
```javascript
|
||||
new URL("https://ølerlækkernårdetermit.dk").hostname
|
||||
// "xn--lerlkkernrdetermit-dubo78a.dk"
|
||||
```
|
||||
|
||||
Breaking changes
|
||||
===
|
||||
- V9.0.0 drops support for NodeJS 14 and lower. NodeJS 15 or higher is required. This is due to using private class fields, so we can have better optimalizations. There is _No_ change in functionality, but we were able to reduce the size of the polyfill by ~2.5KB (~13%), thanks to a pr #118 from @jimmywarting.
|
||||
|
||||
Learn more
|
||||
===
|
||||
|
||||
- [Explainer](https://github.com/wanderview/service-worker-scope-pattern-matching/blob/master/explainer.md)
|
||||
- [Design Document](https://docs.google.com/document/d/17L6b3zlTHtyxQvOAvbK55gQOi5rrJLERwjt_sKXpzqc/edit#)
|
||||
|
||||
Reporting a security issue
|
||||
===
|
||||
If you have information about a security issue or vulnerability with an Intel-maintained open source project on https://github.com/intel, please send an e-mail to secure@intel.com. Encrypt sensitive information using our PGP public key. For issues related to Intel products, please visit https://security-center.intel.com.
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import type * as Types from "./types.js";
|
||||
export { URLPattern } from "./types.js";
|
||||
|
||||
declare global {
|
||||
class URLPattern extends Types.URLPattern {}
|
||||
type URLPatternInit = Types.URLPatternInit;
|
||||
type URLPatternResult = Types.URLPatternResult;
|
||||
type URLPatternComponentResult = Types.URLPatternComponentResult;
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
export type URLPatternInput = URLPatternInit | string;
|
||||
|
||||
export declare class URLPattern {
|
||||
constructor(init?: URLPatternInput, baseURL?: string);
|
||||
|
||||
test(input?: URLPatternInput, baseURL?: string): boolean;
|
||||
|
||||
exec(input?: URLPatternInput, baseURL?: string): URLPatternResult | null;
|
||||
|
||||
readonly protocol: string;
|
||||
readonly username: string;
|
||||
readonly password: string;
|
||||
readonly hostname: string;
|
||||
readonly port: string;
|
||||
readonly pathname: string;
|
||||
readonly search: string;
|
||||
readonly hash: string;
|
||||
}
|
||||
|
||||
interface URLPatternInit {
|
||||
baseURL?: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
protocol?: string;
|
||||
hostname?: string;
|
||||
port?: string;
|
||||
pathname?: string;
|
||||
search?: string;
|
||||
hash?: string;
|
||||
}
|
||||
|
||||
export interface URLPatternResult {
|
||||
inputs: [URLPatternInput];
|
||||
protocol: URLPatternComponentResult;
|
||||
username: URLPatternComponentResult;
|
||||
password: URLPatternComponentResult;
|
||||
hostname: URLPatternComponentResult;
|
||||
port: URLPatternComponentResult;
|
||||
pathname: URLPatternComponentResult;
|
||||
search: URLPatternComponentResult;
|
||||
hash: URLPatternComponentResult;
|
||||
}
|
||||
|
||||
export interface URLPatternComponentResult {
|
||||
input: string;
|
||||
groups: {
|
||||
[key: string]: string | undefined;
|
||||
};
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
"use strict";var U=Object.defineProperty;var Re=Object.getOwnPropertyDescriptor;var Ee=Object.getOwnPropertyNames;var Oe=Object.prototype.hasOwnProperty;var a=(e,t)=>U(e,"name",{value:t,configurable:!0});var ke=(e,t)=>{for(var r in t)U(e,r,{get:t[r],enumerable:!0})},Te=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ee(t))!Oe.call(e,o)&&o!==r&&U(e,o,{get:()=>t[o],enumerable:!(n=Re(t,o))||n.enumerable});return e};var Ae=e=>Te(U({},"__esModule",{value:!0}),e);var He={};ke(He,{URLPattern:()=>M});module.exports=Ae(He);var P=class{type=3;name="";prefix="";value="";suffix="";modifier=3;constructor(t,r,n,o,l,f){this.type=t,this.name=r,this.prefix=n,this.value=o,this.suffix=l,this.modifier=f}hasCustomName(){return this.name!==""&&typeof this.name!="number"}};a(P,"Part");var ye=/[$_\p{ID_Start}]/u,we=/[$_\u200C\u200D\p{ID_Continue}]/u,F=".*";function Ce(e,t){return(t?/^[\x00-\xFF]*$/:/^[\x00-\x7F]*$/).test(e)}a(Ce,"isASCII");function W(e,t=!1){let r=[],n=0;for(;n<e.length;){let o=e[n],l=a(function(f){if(!t)throw new TypeError(f);r.push({type:"INVALID_CHAR",index:n,value:e[n++]})},"ErrorOrInvalid");if(o==="*"){r.push({type:"ASTERISK",index:n,value:e[n++]});continue}if(o==="+"||o==="?"){r.push({type:"OTHER_MODIFIER",index:n,value:e[n++]});continue}if(o==="\\"){r.push({type:"ESCAPED_CHAR",index:n++,value:e[n++]});continue}if(o==="{"){r.push({type:"OPEN",index:n,value:e[n++]});continue}if(o==="}"){r.push({type:"CLOSE",index:n,value:e[n++]});continue}if(o===":"){let f="",s=n+1;for(;s<e.length;){let i=e.substr(s,1);if(s===n+1&&ye.test(i)||s!==n+1&&we.test(i)){f+=e[s++];continue}break}if(!f){l(`Missing parameter name at ${n}`);continue}r.push({type:"NAME",index:n,value:f}),n=s;continue}if(o==="("){let f=1,s="",i=n+1,c=!1;if(e[i]==="?"){l(`Pattern cannot start with "?" at ${i}`);continue}for(;i<e.length;){if(!Ce(e[i],!1)){l(`Invalid character '${e[i]}' at ${i}.`),c=!0;break}if(e[i]==="\\"){s+=e[i++]+e[i++];continue}if(e[i]===")"){if(f--,f===0){i++;break}}else if(e[i]==="("&&(f++,e[i+1]!=="?")){l(`Capturing groups are not allowed at ${i}`),c=!0;break}s+=e[i++]}if(c)continue;if(f){l(`Unbalanced pattern at ${n}`);continue}if(!s){l(`Missing pattern at ${n}`);continue}r.push({type:"REGEX",index:n,value:s}),n=i;continue}r.push({type:"CHAR",index:n,value:e[n++]})}return r.push({type:"END",index:n,value:""}),r}a(W,"lexer");function _(e,t={}){let r=W(e);t.delimiter??="/#?",t.prefixes??="./";let n=`[^${x(t.delimiter)}]+?`,o=[],l=0,f=0,s="",i=new Set,c=a(u=>{if(f<r.length&&r[f].type===u)return r[f++].value},"tryConsume"),h=a(()=>c("OTHER_MODIFIER")??c("ASTERISK"),"tryConsumeModifier"),p=a(u=>{let d=c(u);if(d!==void 0)return d;let{type:g,index:y}=r[f];throw new TypeError(`Unexpected ${g} at ${y}, expected ${u}`)},"mustConsume"),A=a(()=>{let u="",d;for(;d=c("CHAR")??c("ESCAPED_CHAR");)u+=d;return u},"consumeText"),be=a(u=>u,"DefaultEncodePart"),N=t.encodePart||be,H="",v=a(u=>{H+=u},"appendToPendingFixedValue"),D=a(()=>{H.length&&(o.push(new P(3,"","",N(H),"",3)),H="")},"maybeAddPartFromPendingFixedValue"),Z=a((u,d,g,y,B)=>{let m=3;switch(B){case"?":m=1;break;case"*":m=0;break;case"+":m=2;break}if(!d&&!g&&m===3){v(u);return}if(D(),!d&&!g){if(!u)return;o.push(new P(3,"","",N(u),"",m));return}let S;g?g==="*"?S=F:S=g:S=n;let k=2;S===n?(k=1,S=""):S===F&&(k=0,S="");let E;if(d?E=d:g&&(E=l++),i.has(E))throw new TypeError(`Duplicate name '${E}'.`);i.add(E),o.push(new P(k,E,N(u),S,N(y),m))},"addPart");for(;f<r.length;){let u=c("CHAR"),d=c("NAME"),g=c("REGEX");if(!d&&!g&&(g=c("ASTERISK")),d||g){let m=u??"";t.prefixes.indexOf(m)===-1&&(v(m),m=""),D();let S=h();Z(m,d,g,"",S);continue}let y=u??c("ESCAPED_CHAR");if(y){v(y);continue}if(c("OPEN")){let m=A(),S=c("NAME"),k=c("REGEX");!S&&!k&&(k=c("ASTERISK"));let E=A();p("CLOSE");let Pe=h();Z(m,S,k,E,Pe);continue}D(),p("END")}return o}a(_,"parse");function x(e){return e.replace(/([.+*?^${}()[\]|/\\])/g,"\\$1")}a(x,"escapeString");function q(e){return e&&e.ignoreCase?"ui":"u"}a(q,"flags");function J(e,t,r){return z(_(e,r),t,r)}a(J,"stringToRegexp");function T(e){switch(e){case 0:return"*";case 1:return"?";case 2:return"+";case 3:return""}}a(T,"modifierToString");function z(e,t,r={}){r.delimiter??="/#?",r.prefixes??="./",r.sensitive??=!1,r.strict??=!1,r.end??=!0,r.start??=!0,r.endsWith="";let n=r.start?"^":"";for(let s of e){if(s.type===3){s.modifier===3?n+=x(s.value):n+=`(?:${x(s.value)})${T(s.modifier)}`;continue}t&&t.push(s.name);let i=`[^${x(r.delimiter)}]+?`,c=s.value;if(s.type===1?c=i:s.type===0&&(c=F),!s.prefix.length&&!s.suffix.length){s.modifier===3||s.modifier===1?n+=`(${c})${T(s.modifier)}`:n+=`((?:${c})${T(s.modifier)})`;continue}if(s.modifier===3||s.modifier===1){n+=`(?:${x(s.prefix)}(${c})${x(s.suffix)})`,n+=T(s.modifier);continue}n+=`(?:${x(s.prefix)}`,n+=`((?:${c})(?:`,n+=x(s.suffix),n+=x(s.prefix),n+=`(?:${c}))*)${x(s.suffix)})`,s.modifier===0&&(n+="?")}let o=`[${x(r.endsWith)}]|$`,l=`[${x(r.delimiter)}]`;if(r.end)return r.strict||(n+=`${l}?`),r.endsWith.length?n+=`Line truncated
|
||||
+1
@@ -0,0 +1 @@
|
||||
var Pe=Object.defineProperty;var a=(e,t)=>Pe(e,"name",{value:t,configurable:!0});var P=class{type=3;name="";prefix="";value="";suffix="";modifier=3;constructor(t,r,n,c,l,f){this.type=t,this.name=r,this.prefix=n,this.value=c,this.suffix=l,this.modifier=f}hasCustomName(){return this.name!==""&&typeof this.name!="number"}};a(P,"Part");var Re=/[$_\p{ID_Start}]/u,Ee=/[$_\u200C\u200D\p{ID_Continue}]/u,v=".*";function Oe(e,t){return(t?/^[\x00-\xFF]*$/:/^[\x00-\x7F]*$/).test(e)}a(Oe,"isASCII");function D(e,t=!1){let r=[],n=0;for(;n<e.length;){let c=e[n],l=a(function(f){if(!t)throw new TypeError(f);r.push({type:"INVALID_CHAR",index:n,value:e[n++]})},"ErrorOrInvalid");if(c==="*"){r.push({type:"ASTERISK",index:n,value:e[n++]});continue}if(c==="+"||c==="?"){r.push({type:"OTHER_MODIFIER",index:n,value:e[n++]});continue}if(c==="\\"){r.push({type:"ESCAPED_CHAR",index:n++,value:e[n++]});continue}if(c==="{"){r.push({type:"OPEN",index:n,value:e[n++]});continue}if(c==="}"){r.push({type:"CLOSE",index:n,value:e[n++]});continue}if(c===":"){let f="",s=n+1;for(;s<e.length;){let i=e.substr(s,1);if(s===n+1&&Re.test(i)||s!==n+1&&Ee.test(i)){f+=e[s++];continue}break}if(!f){l(`Missing parameter name at ${n}`);continue}r.push({type:"NAME",index:n,value:f}),n=s;continue}if(c==="("){let f=1,s="",i=n+1,o=!1;if(e[i]==="?"){l(`Pattern cannot start with "?" at ${i}`);continue}for(;i<e.length;){if(!Oe(e[i],!1)){l(`Invalid character '${e[i]}' at ${i}.`),o=!0;break}if(e[i]==="\\"){s+=e[i++]+e[i++];continue}if(e[i]===")"){if(f--,f===0){i++;break}}else if(e[i]==="("&&(f++,e[i+1]!=="?")){l(`Capturing groups are not allowed at ${i}`),o=!0;break}s+=e[i++]}if(o)continue;if(f){l(`Unbalanced pattern at ${n}`);continue}if(!s){l(`Missing pattern at ${n}`);continue}r.push({type:"REGEX",index:n,value:s}),n=i;continue}r.push({type:"CHAR",index:n,value:e[n++]})}return r.push({type:"END",index:n,value:""}),r}a(D,"lexer");function F(e,t={}){let r=D(e);t.delimiter??="/#?",t.prefixes??="./";let n=`[^${x(t.delimiter)}]+?`,c=[],l=0,f=0,s="",i=new Set,o=a(u=>{if(f<r.length&&r[f].type===u)return r[f++].value},"tryConsume"),h=a(()=>o("OTHER_MODIFIER")??o("ASTERISK"),"tryConsumeModifier"),p=a(u=>{let d=o(u);if(d!==void 0)return d;let{type:g,index:y}=r[f];throw new TypeError(`Unexpected ${g} at ${y}, expected ${u}`)},"mustConsume"),A=a(()=>{let u="",d;for(;d=o("CHAR")??o("ESCAPED_CHAR");)u+=d;return u},"consumeText"),xe=a(u=>u,"DefaultEncodePart"),N=t.encodePart||xe,H="",$=a(u=>{H+=u},"appendToPendingFixedValue"),M=a(()=>{H.length&&(c.push(new P(3,"","",N(H),"",3)),H="")},"maybeAddPartFromPendingFixedValue"),X=a((u,d,g,y,Z)=>{let m=3;switch(Z){case"?":m=1;break;case"*":m=0;break;case"+":m=2;break}if(!d&&!g&&m===3){$(u);return}if(M(),!d&&!g){if(!u)return;c.push(new P(3,"","",N(u),"",m));return}let S;g?g==="*"?S=v:S=g:S=n;let k=2;S===n?(k=1,S=""):S===v&&(k=0,S="");let E;if(d?E=d:g&&(E=l++),i.has(E))throw new TypeError(`Duplicate name '${E}'.`);i.add(E),c.push(new P(k,E,N(u),S,N(y),m))},"addPart");for(;f<r.length;){let u=o("CHAR"),d=o("NAME"),g=o("REGEX");if(!d&&!g&&(g=o("ASTERISK")),d||g){let m=u??"";t.prefixes.indexOf(m)===-1&&($(m),m=""),M();let S=h();X(m,d,g,"",S);continue}let y=u??o("ESCAPED_CHAR");if(y){$(y);continue}if(o("OPEN")){let m=A(),S=o("NAME"),k=o("REGEX");!S&&!k&&(k=o("ASTERISK"));let E=A();p("CLOSE");let be=h();X(m,S,k,E,be);continue}M(),p("END")}return c}a(F,"parse");function x(e){return e.replace(/([.+*?^${}()[\]|/\\])/g,"\\$1")}a(x,"escapeString");function B(e){return e&&e.ignoreCase?"ui":"u"}a(B,"flags");function q(e,t,r){return W(F(e,r),t,r)}a(q,"stringToRegexp");function T(e){switch(e){case 0:return"*";case 1:return"?";case 2:return"+";case 3:return""}}a(T,"modifierToString");function W(e,t,r={}){r.delimiter??="/#?",r.prefixes??="./",r.sensitive??=!1,r.strict??=!1,r.end??=!0,r.start??=!0,r.endsWith="";let n=r.start?"^":"";for(let s of e){if(s.type===3){s.modifier===3?n+=x(s.value):n+=`(?:${x(s.value)})${T(s.modifier)}`;continue}t&&t.push(s.name);let i=`[^${x(r.delimiter)}]+?`,o=s.value;if(s.type===1?o=i:s.type===0&&(o=v),!s.prefix.length&&!s.suffix.length){s.modifier===3||s.modifier===1?n+=`(${o})${T(s.modifier)}`:n+=`((?:${o})${T(s.modifier)})`;continue}if(s.modifier===3||s.modifier===1){n+=`(?:${x(s.prefix)}(${o})${x(s.suffix)})`,n+=T(s.modifier);continue}n+=`(?:${x(s.prefix)}`,n+=`((?:${o})(?:`,n+=x(s.suffix),n+=x(s.prefix),n+=`(?:${o}))*)${x(s.suffix)})`,s.modifier===0&&(n+="?")}let c=`[${x(r.endsWith)}]|$`,l=`[${x(r.delimiter)}]`;if(r.end)return r.strict||(n+=`${l}?`),r.endsWith.length?n+=`(?=${c})`:n+="$",new RegExp(n,B(r));r.strict||(n+=`(?:${l}(?=${c}))?`);let f=!1;if(e.length){let s=e[e.length-1];s.type===3&&s.modifier===3&&(f=r.delimiter.indexOf(s)>-1)}return f||(n+=`(?=${l}|${c})`),new RegExp(n,B(r))}a(W,"partsToRegexp");var b={delimiter:"",prefixes:"",sensitive:!0,strict:!0},J={delimiter:".",prefixes:"",sensitive:!0,strict:!0},Q={delimiter:"/",prefixes:"/",sensitive:!0,strict:!0};function ee(e,t){return e.length?e[0]==="/"?!0:!t||e.lengtLine truncated
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
const { URLPattern } = require("./dist/urlpattern.cjs");
|
||||
|
||||
module.exports = { URLPattern };
|
||||
|
||||
if (!globalThis.URLPattern) {
|
||||
globalThis.URLPattern = URLPattern;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { URLPattern } from "./dist/urlpattern.js";
|
||||
|
||||
export { URLPattern };
|
||||
|
||||
if (!globalThis.URLPattern) {
|
||||
globalThis.URLPattern = URLPattern;
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
{
|
||||
"name": "urlpattern-polyfill",
|
||||
"version": "10.1.0",
|
||||
"description": "Polyfill for the URLPattern API",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/kenchris/urlpattern-polyfill"
|
||||
},
|
||||
"type": "module",
|
||||
"main": "./index.cjs",
|
||||
"module": "./index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
"./urlpattern": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/urlpattern.js",
|
||||
"require": "./dist/urlpattern.cjs"
|
||||
},
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./index.js",
|
||||
"require": "./index.cjs"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
"url",
|
||||
"urlpattern",
|
||||
"url-pattern",
|
||||
"browser",
|
||||
"node",
|
||||
"polyfill",
|
||||
"w3c",
|
||||
"wicg"
|
||||
],
|
||||
"files": [
|
||||
"dist",
|
||||
"index.js",
|
||||
"index.cjs"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@ava/typescript": "^4.0.0",
|
||||
"ava": "^5.3.0",
|
||||
"esbuild": "^0.17.19",
|
||||
"rimraf": "^5.0.1",
|
||||
"typescript": "^5.1.3",
|
||||
"wireit": "^0.9.5"
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"test/**/*",
|
||||
"!test/wpt"
|
||||
],
|
||||
"typescript": {
|
||||
"rewritePaths": {
|
||||
"src/": "dist/"
|
||||
},
|
||||
"compile": false
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "wireit",
|
||||
"sync-wpt": "wireit",
|
||||
"test": "wireit",
|
||||
"manual-test": "wireit",
|
||||
"publish-dev": "wireit",
|
||||
"publish-patch": "wireit",
|
||||
"publish-major": "wireit"
|
||||
},
|
||||
"wireit": {
|
||||
"build:esm": {
|
||||
"command": "esbuild --bundle --format=esm src/url-pattern.ts --outfile=dist/urlpattern.js --minify --keep-names --target=es2022",
|
||||
"output": [
|
||||
"dist/urlpattern.js"
|
||||
],
|
||||
"files": [
|
||||
"src/**/*"
|
||||
]
|
||||
},
|
||||
"build:cjs": {
|
||||
"command": "esbuild --bundle --format=cjs src/url-pattern.ts --outfile=dist/urlpattern.cjs --minify --keep-names --target=es2022",
|
||||
"output": [
|
||||
"dist/urlpattern.cjs"
|
||||
],
|
||||
"files": [
|
||||
"src/**/*"
|
||||
]
|
||||
},
|
||||
"copyTypeFiles": {
|
||||
"command": "cp ./src/index.d.ts ./src/types.d.ts ./dist",
|
||||
"output": [
|
||||
"dist/index.d.ts",
|
||||
"dist/types.d.ts"
|
||||
],
|
||||
"dependencies": [
|
||||
"build:esm",
|
||||
"build:cjs"
|
||||
]
|
||||
},
|
||||
"build": {
|
||||
"dependencies": [
|
||||
"copyTypeFiles"
|
||||
]
|
||||
},
|
||||
"prepFakeNodeModules": {
|
||||
"command": "rm -rf node_modules/urlpattern-polyfill; ln -s $(pwd) node_modules/urlpattern-polyfill"
|
||||
},
|
||||
"sync-wpt": {
|
||||
"command": "cd test && wget http://wpt.live/urlpattern/resources/urlpatterntestdata.json && wget http://wpt.live/urlpattern/resources/urlpattern-compare-test-data.json"
|
||||
},
|
||||
"test": {
|
||||
"command": "ava --timeout=60s",
|
||||
"watch": "test/**/*",
|
||||
"files": [
|
||||
"test/**/*"
|
||||
],
|
||||
"dependencies": [
|
||||
"prepFakeNodeModules",
|
||||
"build"
|
||||
]
|
||||
},
|
||||
"manual-test": {
|
||||
"command": "npx http-server -o /index.html -p 4203",
|
||||
"dependencies": [
|
||||
"test"
|
||||
]
|
||||
},
|
||||
"publish-dev": {
|
||||
"command": "VERSION=${npm_package_version%-*}-dev.`git rev-parse --short HEAD` && npm version --no-git-tag-version $VERSION && npm publish --tag dev",
|
||||
"dependencies": [
|
||||
"test"
|
||||
]
|
||||
},
|
||||
"publish-patch": {
|
||||
"command": "npm version patch && npm publish",
|
||||
"dependencies": [
|
||||
"test"
|
||||
]
|
||||
},
|
||||
"publish-major": {
|
||||
"command": "npm version major && npm publish",
|
||||
"dependencies": [
|
||||
"test"
|
||||
]
|
||||
}
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {}
|
||||
}
|
||||
Reference in new issue
Block a user