diff options
| author | akiyamn | 2023-09-24 23:22:21 +1000 |
|---|---|---|
| committer | akiyamn | 2023-09-24 23:22:21 +1000 |
| commit | 4e87195739f2a5d9a05451b48773c8afdc680765 (patch) | |
| tree | 9cba501844a4a11dcbdffc4050ed8189561c55ed /node_modules/wrangler/templates/pages-dev-util.ts | |
| download | price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.tar.gz price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.zip | |
Initial commit (by create-cloudflare CLI)
Diffstat (limited to 'node_modules/wrangler/templates/pages-dev-util.ts')
| -rw-r--r-- | node_modules/wrangler/templates/pages-dev-util.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/node_modules/wrangler/templates/pages-dev-util.ts b/node_modules/wrangler/templates/pages-dev-util.ts new file mode 100644 index 0000000..371e30f --- /dev/null +++ b/node_modules/wrangler/templates/pages-dev-util.ts @@ -0,0 +1,55 @@ +/** + * @param pathname A pathname string, such as `/foo` or `/foo/bar` + * @param routingRule The routing rule, such as `/foo/*` + * @returns True if pathname matches the routing rule + * + * / -> / + * /* -> /* + * /foo -> /foo + * /foo* -> /foo, /foo-bar, /foo/* + * /foo/* -> /foo, /foo/bar + */ +export function isRoutingRuleMatch( + pathname: string, + routingRule: string +): boolean { + // sanity checks + if (!pathname) { + throw new Error("Pathname is undefined."); + } + if (!routingRule) { + throw new Error("Routing rule is undefined."); + } + + const ruleRegExp = transformRoutingRuleToRegExp(routingRule); + return pathname.match(ruleRegExp) !== null; +} + +function transformRoutingRuleToRegExp(rule: string): RegExp { + let transformedRule; + + if (rule === "/" || rule === "/*") { + transformedRule = rule; + } else if (rule.endsWith("/*")) { + // make `/*` an optional group so we can match both /foo/* and /foo + // /foo/* => /foo(/*)? + transformedRule = `${rule.substring(0, rule.length - 2)}(/*)?`; + } else if (rule.endsWith("/")) { + // make `/` an optional group so we can match both /foo/ and /foo + // /foo/ => /foo(/)? + transformedRule = `${rule.substring(0, rule.length - 1)}(/)?`; + } else if (rule.endsWith("*")) { + transformedRule = rule; + } else { + transformedRule = `${rule}(/)?`; + } + + // /foo* => /foo.* => ^/foo.*$ + // /*.* => /*\.* => /.*\..* => ^/.*\..*$ + transformedRule = `^${transformedRule + .replaceAll(/\./g, "\\.") + .replaceAll(/\*/g, ".*")}$`; + + // ^/foo.*$ => /^\/foo.*$/ + return new RegExp(transformedRule); +} |
