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

Code before:
	} 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.
 *
 * @param {string} src - The source URL of the remote resource to be validated.
 * @param {Object} options - The configuration options for domain and pattern matching.
 * @param {string[]} options.domains - A list of allowed domain names.
 * @param {RemotePattern[]} options.remotePatterns - A list of allowed remote patterns for matching.
 * @return {boolean} Returns true if the source URL matches any of the specified domains or remote patterns; otherwise, false.
 */
export function isRemoteAllowed(
	src: string,
	{
		domains,
		remotePatterns,
	}: {
		domains: string[];
		remotePatterns: RemotePattern[];
	},
): boolean {
	if (!URL.canParse(src)) {
		return false;
	}

	const url = new URL(src);
	return (
		domains.some((domain) => matchHostname(url, domain)) ||
		remotePatterns.some((remotePattern) => matchPattern(url, remotePattern))
	);
}


Review comment (suggestion):
```suggestion
 * @return {boolean} Returns `true` if the source URL matches any of the specified domains or remote patterns; otherwise, `false`.
```

Code after:
	} 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.
 *
 * @param {string} src - The source URL of the remote resource to be validated.
 * @param {Object} options - The configuration options for domain and pattern matching.
 * @param {string[]} options.domains - A list of allowed domain names.
 * @param {RemotePattern[]} options.remotePatterns - A list of allowed remote patterns for matching.
 * @return {boolean} Returns `true` if the source URL matches any of the specified domains or remote patterns; otherwise, `false`.
 */
export function isRemoteAllowed(
	src: string,
	{
		domains,
		remotePatterns,
	}: {
		domains: string[];
		remotePatterns: RemotePattern[];
	},
): boolean {
	if (!URL.canParse(src)) {
		return false;
	}

	const url = new URL(src);
	return (
		domains.some((domain) => matchHostname(url, domain)) ||
		remotePatterns.some((remotePattern) => matchPattern(url, remotePattern))
	);
}