north-star / main_old / files / src / core / room.ts
Branch main_old · default branch · all branches
room.ts
32796e53 2026-03-16 Pavel Safronov | 1 | import { PUBLIC_SETTINGS } from '../public_settings'; |
9fda5271 2026-03-16 Pavel Safronov | 2 | |
36d64b37 2026-03-15 Pavel Safronov | 3 | const ROOM_KEY_PATTERN = /^[A-Za-z0-9_-]+$/; |
| 4 | ||
b4e7f682 2026-03-16 Pavel Safronov | 5 | export const buildRoomHash = (nextRoomId: string, nextRoomKeyBase64: string | null = null) => { |
36d64b37 2026-03-15 Pavel Safronov | 6 | const keyPart = nextRoomKeyBase64 ? `.${nextRoomKeyBase64}` : ''; |
| 7 | return `${nextRoomId}${keyPart}`; | |
| 8 | }; | |
| 9 | ||
b4e7f682 2026-03-16 Pavel Safronov | 10 | export const parseRoomFromHash = (hashValue: string) => { |
36d64b37 2026-03-15 Pavel Safronov | 11 | const normalizedHash = (hashValue || '').startsWith('#') ? hashValue.slice(1) : (hashValue || ''); |
9fda5271 2026-03-16 Pavel Safronov | 12 | const compactMatch = normalizedHash.match(new RegExp(`^([a-z0-9]{${PUBLIC_SETTINGS.room.idLength}})(?:\\.([A-Za-z0-9_-]+))?$`)); |
36d64b37 2026-03-15 Pavel Safronov | 13 | 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 | }; |