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

Code before:
 * Checks if the given URL's port matches the specified port. If no port is provided, it returns true.
 *
 * @param {URL} url - The URL object whose port will be checked.
 * @param {string} [port] - The port to match against the URL's port. Optional.
 * @return {boolean} Returns `true` if the URL's port matches the specified port or if no port is provided; otherwise, `false`.
 */
export function matchPort(url: URL, port?: string): boolean {
	return !port || port === url.port;
}

/**
 * Compares the protocol of the provided URL with a specified protocol.
 *
 * @param {URL} url - The URL object whose protocol needs to be checked.
 * @param {string} [protocol] - The protocol to compare against, without the trailing colon. If not provided, the method will always return true.
 * @return {boolean} Returns `true` if the protocol matches or if no protocol is specified; otherwise, `false`.
 */
export function matchProtocol(url: URL, protocol?: string): boolean {
	return !protocol || protocol === url.protocol.slice(0, -1);
}

/**
 * Matches a given URL's hostname against a specified hostname, with optional support for wildcard patterns.
 *
 * @param {URL} url - The URL object whose hostname is to be matched.
 * @param {string} [hostname] - The hostname to match against. Supports wildcard patterns if `allowWildcard` is true.
 * @param {boolean} [allowWildcard] - Indicates whether wildcard patterns in the `hostname` parameter are allowed.
 * @return {boolean} - Returns `true` if the URL's hostname matches the given hostname criteria; otherwise, `false`.
 */
export function matchHostname(url: URL, hostname?: string, allowWildcard?: boolean): boolean {
	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.

Review comment (suggestion):
```suggestion
 * @param {string} [hostname] - The hostname to match against. Supports wildcard patterns if `allowWildcard` is `true`.
```

Code after:
 * Checks if the given URL's port matches the specified port. If no port is provided, it returns `true`.
 *
 * @param {URL} url - The URL object whose port will be checked.
 * @param {string} [port=] - The port to match against the URL's port. Optional.
 * @return {boolean} Returns `true` if the URL's port matches the specified port or if no port is provided; otherwise, `false`.
 */
export function matchPort(url: URL, port?: string): boolean {
	return !port || port === url.port;
}

/**
 * Compares the protocol of the provided URL with a specified protocol.
 *
 * @param {URL} url - The URL object whose protocol needs to be checked.
 * @param {string} [protocol] - The protocol to compare against, without the trailing colon. If not provided, the method will always return `true`.
 * @return {boolean} Returns `true` if the protocol matches or if no protocol is specified; otherwise, `false`.
 */
export function matchProtocol(url: URL, protocol?: string): boolean {
	return !protocol || protocol === url.protocol.slice(0, -1);
}

/**
 * Matches a given URL's hostname against a specified hostname, with optional support for wildcard patterns.
 *
 * @param {URL} url - The URL object whose hostname is to be matched.
 * @param {string} [hostname] - The hostname to match against. Supports wildcard patterns if `allowWildcard` is `true`.
 * @param {boolean} [allowWildcard=false] - Indicates whether wildcard patterns in the `hostname` parameter are allowed.
 * @return {boolean} - Returns `true` if the URL's hostname matches the given hostname criteria; otherwise, `false`.
 */
export function matchHostname(url: URL, hostname?: string, allowWildcard = false): boolean {
	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.