fuzzyworld

north-star / files / src / core / room.ts

room.ts

Blame: the commit that last changed each line · file · history

32796e53 2026-03-16 Pavel Safronov1import { PUBLIC_SETTINGS } from '../public_settings';
9fda5271 2026-03-16 Pavel Safronov2
36d64b37 2026-03-15 Pavel Safronov3const ROOM_KEY_PATTERN = /^[A-Za-z0-9_-]+$/;
4
b4e7f682 2026-03-16 Pavel Safronov5export const buildRoomHash = (nextRoomId: string, nextRoomKeyBase64: string | null = null) => {
36d64b37 2026-03-15 Pavel Safronov6 const keyPart = nextRoomKeyBase64 ? `.${nextRoomKeyBase64}` : '';
7 return `${nextRoomId}${keyPart}`;
8};
9
b4e7f682 2026-03-16 Pavel Safronov10export const parseRoomFromHash = (hashValue: string) => {
36d64b37 2026-03-15 Pavel Safronov11 const normalizedHash = (hashValue || '').startsWith('#') ? hashValue.slice(1) : (hashValue || '');
9fda5271 2026-03-16 Pavel Safronov12 const compactMatch = normalizedHash.match(new RegExp(`^([a-z0-9]{${PUBLIC_SETTINGS.room.idLength}})(?:\\.([A-Za-z0-9_-]+))?$`));
36d64b37 2026-03-15 Pavel Safronov13 if (!compactMatch) return null;
14
15 const roomValue = compactMatch[1];
16 const keyValue = compactMatch[2] || null;
17 if (keyValue && !ROOM_KEY_PATTERN.test(keyValue)) return null;
18 return { roomId: roomValue, roomKeyBase64: keyValue };
19};