34 lines
867 B
JavaScript
34 lines
867 B
JavaScript
export function normalizeServer(input) {
|
|
const value = input.trim();
|
|
if (
|
|
!/^https:\/\//i.test(value) ||
|
|
[...value].some(
|
|
(character) =>
|
|
character.charCodeAt(0) < 32 ||
|
|
character.charCodeAt(0) === 127 ||
|
|
character === '\\',
|
|
)
|
|
) {
|
|
throw new Error('invalidServer');
|
|
}
|
|
const url = new URL(value);
|
|
if (
|
|
url.protocol !== 'https:' ||
|
|
!url.hostname ||
|
|
url.username ||
|
|
url.password ||
|
|
value.includes('?') ||
|
|
value.includes('#')
|
|
) {
|
|
throw new Error('invalidServer');
|
|
}
|
|
if (!url.pathname.endsWith('/')) {
|
|
url.pathname += '/';
|
|
}
|
|
return url.href;
|
|
}
|
|
|
|
export function needsConfirmation(activeServer, nextServer) {
|
|
return activeServer !== null && activeServer !== nextServer;
|
|
}
|