Repository: withastro/astro
File: packages/internal-helpers/src/remote.ts (TypeScript)

Code before:
	if (!hostname) {
		return true;
	} else if (!allowWildcard || !hostname.startsWith('*')) {
		return hostname === url.hostname;
	} else if (hostname.startsWith('**.')) {
		const slicedHostname = hostname.slice(2); // ** length
		return slicedHostname !== url.hostname && url.hostname.endsWith(slicedHostname);
	} else if (hostname.startsWith('*.')) {
		const slicedHostname = hostname.slice(1); // * length
		const additionalSubdomains = url.hostname
			.replace(slicedHostname, '')
			.split('.')
			.filter(Boolean);
		return additionalSubdomains.length === 1;
	}

	return false;
}

/**
 * Matches a given URL's pathname against a specified pattern, with optional support for wildcards.
 *
 * @param {URL} url - The URL object containing the pathname to be matched.
 * @param {string} [pathname] - The pathname pattern to match the URL against.
 * @param {boolean} [allowWildcard] - Determines whether wildcard matching is allowed.
 * @return {boolean} - Returns `true` if the URL's pathname matches the specified pattern, otherwise `false`.
 */
export function matchPathname(url: URL, pathname?: string, allowWildcard?: boolean): boolean {
	if (!pathname) {
		return true;
	} else if (!allowWildcard || !pathname.endsWith('*')) {
		return pathname === url.pathname;
	} else if (pathname.endsWith('/**')) {
		const slicedPathname = pathname.slice(0, -2); // ** length
		return slicedPathname !== url.pathname && url.pathname.startsWith(slicedPathname);
	} else if (pathname.endsWith('/*')) {
		const slicedPathname = pathname.slice(0, -1); // * length
		const additionalPathChunks = url.pathname
			.replace(slicedPathname, '')
			.split('/')
			.filter(Boolean);
		return additionalPathChunks.length === 1;
	}

	return false;
}

/**
 * Determines whether a given remote resource, identified by its source URL,
 * is allowed based on specified domains and remote patterns.
 *

Review comment (suggestion):
```suggestion
 * @return {boolean} - Returns `true` if the URL's pathname matches the specified pattern; otherwise, `false`.
```

Code after:
	if (!hostname) {
		return true;
	} else if (!allowWildcard || !hostname.startsWith('*')) {
		return hostname === url.hostname;
	} else if (hostname.startsWith('**.')) {
		const slicedHostname = hostname.slice(2); // ** length
		return slicedHostname !== url.hostname && url.hostname.endsWith(slicedHostname);
	} else if (hostname.startsWith('*.')) {
		const slicedHostname = hostname.slice(1); // * length
		const additionalSubdomains = url.hostname
			.replace(slicedHostname, '')
			.split('.')
			.filter(Boolean);
		return additionalSubdomains.length === 1;
	}

	return false;
}

/**
 * Matches a given URL's pathname against a specified pattern, with optional support for wildcards.
 *
 * @param {URL} url - The URL object containing the pathname to be matched.
 * @param {string} [pathname] - The pathname pattern to match the URL against.
 * @param {boolean} [allowWildcard=false] - Determines whether wildcard matching is allowed.
 * @return {boolean} - Returns `true` if the URL's pathname matches the specified pattern; otherwise, `false`.
 */
export function matchPathname(url: URL, pathname?: string, allowWildcard = false): boolean {
	if (!pathname) {
		return true;
	} else if (!allowWildcard || !pathname.endsWith('*')) {
		return pathname === url.pathname;
	} else if (pathname.endsWith('/**')) {
		const slicedPathname = pathname.slice(0, -2); // ** length
		return slicedPathname !== url.pathname && url.pathname.startsWith(slicedPathname);
	} else if (pathname.endsWith('/*')) {
		const slicedPathname = pathname.slice(0, -1); // * length
		const additionalPathChunks = url.pathname
			.replace(slicedPathname, '')
			.split('/')
			.filter(Boolean);
		return additionalPathChunks.length === 1;
	}

	return false;
}

/**
 * Determines whether a given remote resource, identified by its source URL,
 * is allowed based on specified domains and remote patterns.
 *