The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/lib/absoluteUrl.ts

58 lines
1.4 KiB

// There is a good chance this module may be promoted to root lib/ in the future
import { baseURI } from './baseURI';
type AbsoluteUrlOptions = {
rootUrl?: string;
secure?: boolean;
replaceLocalhost?: boolean;
};
export function absoluteUrl(path?: string, options?: AbsoluteUrlOptions): string {
if (!options && typeof path === 'object') {
options = path;
path = undefined;
}
options = { ...absoluteUrl.defaultOptions, ...options };
let { rootUrl } = options;
if (!rootUrl) throw Error('Must pass options.rootUrl or set ROOT_URL in the server environment');
if (!/^http[s]?:\/\//i.test(rootUrl)) {
rootUrl = `http://${rootUrl}`;
}
if (!rootUrl.endsWith('/')) {
rootUrl += '/';
}
if (path) {
while (path.startsWith('/')) path = path.slice(1);
rootUrl += path;
}
if (options.secure && /^http:/.test(rootUrl) && !/http:\/\/localhost[:/]/.test(rootUrl) && !/http:\/\/127\.0\.0\.1[:/]/.test(rootUrl)) {
rootUrl = rootUrl.replace(/^http:/, 'https:');
}
if (options.replaceLocalhost) {
rootUrl = rootUrl.replace(/^http:\/\/localhost([:/].*)/, 'http://127.0.0.1$1');
}
return rootUrl;
}
absoluteUrl.defaultOptions = {
rootUrl: baseURI,
secure: window.isSecureContext,
} as AbsoluteUrlOptions;
export function _relativeToSiteRootUrl(link: string): string {
if (typeof __meteor_runtime_config__ === 'object' && link.slice(0, 1) === '/') {
link = (__meteor_runtime_config__.ROOT_URL_PATH_PREFIX || '') + link;
}
return link;
}