HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: /var/www/indoadvisory_new/webapp/node_modules/wrangler/templates/pages-dev-util.ts
/**
 * @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);
}