summaryrefslogtreecommitdiff
path: root/node_modules/wrangler/templates/pages-dev-util.ts
blob: 371e30fe68f0b278dae611ac5fc45b265cbfe799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
}