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