mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-21 12:01:35 +03:00
29 lines
957 B
TypeScript
29 lines
957 B
TypeScript
|
import { config, getEnvironment } from './config';
|
||
|
import { isValidIPAddress } from './is-valid-ip-address';
|
||
|
|
||
|
let prefixUrl = '/';
|
||
|
if (typeof window === 'undefined' || getEnvironment().isDesktop) {
|
||
|
// SSR or Desktop
|
||
|
const serverAPI = config.serverAPI;
|
||
|
if (isValidIPAddress(serverAPI.split(':')[0])) {
|
||
|
// This is for Server side rendering support
|
||
|
prefixUrl = new URL('http://' + config.serverAPI + '/').origin;
|
||
|
} else {
|
||
|
prefixUrl = serverAPI;
|
||
|
}
|
||
|
prefixUrl = prefixUrl.endsWith('/') ? prefixUrl : prefixUrl + '/';
|
||
|
} else {
|
||
|
const params = new URLSearchParams(window.location.search);
|
||
|
if (params.get('prefixUrl')) {
|
||
|
prefixUrl = params.get('prefixUrl') as string;
|
||
|
} else {
|
||
|
prefixUrl = window.location.origin + '/';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const apiUrl = new URL(prefixUrl);
|
||
|
const wsProtocol = apiUrl.protocol === 'https:' ? 'wss' : 'ws';
|
||
|
const websocketPrefixUrl = `${wsProtocol}://${apiUrl.host}`;
|
||
|
|
||
|
export { prefixUrl, websocketPrefixUrl };
|