mirror of
https://github.com/JonasunderscoreJones/api-worker.git
synced 2025-10-23 18:39:19 +02:00
Initial commit (by create-cloudflare CLI)
This commit is contained in:
commit
58a42872a0
1745 changed files with 741893 additions and 0 deletions
55
node_modules/wrangler/templates/pages-dev-util.ts
generated
vendored
Normal file
55
node_modules/wrangler/templates/pages-dev-util.ts
generated
vendored
Normal file
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue