north-star / files / src / features / session / session.ts
session.ts
| 1 | import { generateRoomKey, exportRoomKey, importRoomKey } from '../../core/crypto'; |
| 2 | import { buildRoomHash, parseRoomFromHash } from '../../core/room'; |
| 3 | import { renderInviteQrCard } from './qr'; |
| 4 | import { PUBLIC_SETTINGS } from '../../public_settings'; |
| 5 | |
| 6 | /** |
| 7 | * Parses an existing room from the URL hash, or generates a new one. |
| 8 | * Sets room UI elements and renders invite QR cards. |
| 9 | * Returns { roomId, roomKey, roomKeyBase64 }. |
| 10 | */ |
| 11 | export async function initSession({ |
| 12 | roomCurrent, |
| 13 | createRoomLink, |
| 14 | inviteRoomQrList, |
| 15 | isLocalCommit, |
| 16 | BASE_ROOM_URL, |
| 17 | CREATE_ROOM_URL, |
| 18 | GITLAB_PAGES_BASE_URL |
| 19 | }: { |
| 20 | roomCurrent: HTMLElement | null; |
| 21 | createRoomLink: HTMLAnchorElement | null; |
| 22 | inviteRoomQrList: HTMLElement | null; |
| 23 | isLocalCommit: boolean; |
| 24 | BASE_ROOM_URL: string; |
| 25 | CREATE_ROOM_URL: string; |
| 26 | GITLAB_PAGES_BASE_URL: string; |
| 27 | }) { |
| 28 | let roomKey: CryptoKey | null = null; |
| 29 | let roomKeyBase64: string | null = null; |
| 30 | let roomId: string; |
| 31 | |
| 32 | const parsedRoom = parseRoomFromHash(location.hash); |
| 33 | if (parsedRoom) { |
| 34 | roomId = parsedRoom.roomId; |
| 35 | if (parsedRoom.roomKeyBase64) { |
| 36 | try { |
| 37 | roomKey = await importRoomKey(parsedRoom.roomKeyBase64); |
| 38 | roomKeyBase64 = parsedRoom.roomKeyBase64; |
| 39 | } catch (err) { |
| 40 | console.error('Failed to import room key from URL — running without encryption', err); |
| 41 | } |
| 42 | } |
| 43 | } else { |
| 44 | roomId = crypto.randomUUID().replace(/-/g, '').slice(0, PUBLIC_SETTINGS.room.idLength); |
| 45 | roomKey = await generateRoomKey(); |
| 46 | roomKeyBase64 = await exportRoomKey(roomKey); |
| 47 | location.hash = buildRoomHash(roomId, roomKeyBase64); |
| 48 | } |
| 49 | |
| 50 | if (roomCurrent) roomCurrent.textContent = `You are currently in room ${roomId}`; |
| 51 | if (createRoomLink) createRoomLink.href = CREATE_ROOM_URL; |
| 52 | |
| 53 | if (inviteRoomQrList) { |
| 54 | const inviteRoomUrl = `${BASE_ROOM_URL}#${buildRoomHash(roomId, roomKeyBase64)}`; |
| 55 | inviteRoomQrList.textContent = ''; |
| 56 | const isLocalRun = isLocalCommit || ['localhost', '127.0.0.1', '::1'].includes(location.hostname); |
| 57 | const roomHash = buildRoomHash(roomId, roomKeyBase64); |
| 58 | |
| 59 | await renderInviteQrCard({ |
| 60 | container: inviteRoomQrList, |
| 61 | title: isLocalRun ? 'This Device URL' : '', |
| 62 | url: inviteRoomUrl, |
| 63 | statusText: 'Scan to join this room', |
| 64 | roomId |
| 65 | }); |
| 66 | |
| 67 | if (isLocalRun) { |
| 68 | await renderInviteQrCard({ |
| 69 | container: inviteRoomQrList, |
| 70 | title: 'App URL', |
| 71 | url: `${GITLAB_PAGES_BASE_URL}#${roomHash}`, |
| 72 | statusText: 'Scan to join on hosted app', |
| 73 | roomId |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return { roomId, roomKey, roomKeyBase64 }; |
| 79 | } |