happy-dom
Version:
Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.
28 lines (24 loc) • 751 B
text/typescript
import { URL } from 'url';
/**
* Fetch CORS utility.
*/
export default class FetchCORSUtility {
/**
* Validates request headers.
*
* @param originURL Origin URL.
* @param targetURL Target URL.
*/
public static isCORS(originURL: URL | string, targetURL: URL | string): boolean {
originURL = typeof originURL === 'string' ? new URL(<string>originURL) : <URL>originURL;
targetURL = typeof targetURL === 'string' ? new URL(<string>targetURL) : <URL>targetURL;
if (targetURL.protocol === 'about:' || targetURL.protocol === 'javascript:') {
return false;
}
return (
(originURL.hostname !== targetURL.hostname &&
!originURL.hostname.endsWith(targetURL.hostname)) ||
originURL.protocol !== targetURL.protocol
);
}
}