Add Prettier and ESLint tooling (Refs #77)
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
const origin = 'https://electric-cherry.invalid';
|
||||
const pagePath = /^\/(?:$|activity|operations|autoscaling|account|capacity|metrics|server-keys|settings(?:\/users)?|servers\/[^/]+|hosts(?:\/[^/]+)?|collections(?:\/launches\/[^/]+)?|(?:scripts|configs|plugins|betas|campaigns|maintenance|provisioning|matches)(?:\/[^/]+)?|game-files|leaderboard(?:\/players\/[^/]+(?:\/[^/]+)?)?)$/;
|
||||
const pagePath =
|
||||
/^\/(?:$|activity|operations|autoscaling|account|capacity|metrics|server-keys|settings(?:\/users)?|servers\/[^/]+|hosts(?:\/[^/]+)?|collections(?:\/launches\/[^/]+)?|(?:scripts|configs|plugins|betas|campaigns|maintenance|provisioning|matches)(?:\/[^/]+)?|game-files|leaderboard(?:\/players\/[^/]+(?:\/[^/]+)?)?)$/;
|
||||
|
||||
/** Only return to application pages, never an external URL or a mutation/API endpoint. */
|
||||
export function safeReturnHref(value: string | null | undefined, fallback: string): string {
|
||||
if (!value || value.length > 12000 || !value.startsWith('/') || value.startsWith('//') || /[\\\x00-\x1f\x7f]/.test(value)) return fallback;
|
||||
if (
|
||||
!value ||
|
||||
value.length > 12000 ||
|
||||
!value.startsWith('/') ||
|
||||
value.startsWith('//') ||
|
||||
// eslint-disable-next-line no-control-regex -- Reject control characters in redirect paths.
|
||||
/[\\\x00-\x1f\x7f]/.test(value)
|
||||
)
|
||||
return fallback;
|
||||
try {
|
||||
const url = new URL(value, origin);
|
||||
if (url.origin !== origin || !pagePath.test(url.pathname)) return fallback;
|
||||
return url.pathname + url.search + url.hash;
|
||||
} catch { return fallback; }
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
export function contextHref(destination: string, source: URL | string): string {
|
||||
@@ -25,11 +36,32 @@ export function returnLabel(href: string): string {
|
||||
if (path.startsWith('/servers/')) return 'Back to server';
|
||||
if (path.startsWith('/leaderboard/players/')) return 'Back to player';
|
||||
if (path.startsWith('/collections/launches/')) return 'Back to collection launch';
|
||||
if (path === '/collections' && url.searchParams.get('view') === 'active') return 'Back to active launches';
|
||||
if (path === '/collections' && url.searchParams.get('view') === 'history') return 'Back to launch history';
|
||||
if (path === '/collections' && url.searchParams.get('view') === 'active')
|
||||
return 'Back to active launches';
|
||||
if (path === '/collections' && url.searchParams.get('view') === 'history')
|
||||
return 'Back to launch history';
|
||||
if (path.startsWith('/matches/')) return 'Back to match';
|
||||
if (path === '/hosts/operations') return 'Back to host operation history';
|
||||
if (path.startsWith('/hosts/')) return 'Back to host';
|
||||
const names: Record<string, string> = { '': 'Servers', activity: 'Activity', operations: 'Activity', autoscaling: 'Autoscaling', collections: 'Collections', hosts: 'Hosts', capacity: 'Capacity', scripts: 'Scripts', configs: 'Configs', plugins: 'Plugins', 'game-files': 'Game Files', betas: 'Betas', campaigns: 'Campaigns', maintenance: 'Maintenance', provisioning: 'Server queues', leaderboard: 'Leaderboard', matches: 'Match History', metrics: 'Metrics' };
|
||||
const names: Record<string, string> = {
|
||||
'': 'Servers',
|
||||
activity: 'Activity',
|
||||
operations: 'Activity',
|
||||
autoscaling: 'Autoscaling',
|
||||
collections: 'Collections',
|
||||
hosts: 'Hosts',
|
||||
capacity: 'Capacity',
|
||||
scripts: 'Scripts',
|
||||
configs: 'Configs',
|
||||
plugins: 'Plugins',
|
||||
'game-files': 'Game Files',
|
||||
betas: 'Betas',
|
||||
campaigns: 'Campaigns',
|
||||
maintenance: 'Maintenance',
|
||||
provisioning: 'Server queues',
|
||||
leaderboard: 'Leaderboard',
|
||||
matches: 'Match History',
|
||||
metrics: 'Metrics'
|
||||
};
|
||||
return `Back to ${names[path.split('/')[1]] ?? 'previous page'}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user