///
/* oxlint-disable no-console, unicorn/require-post-message-target-origin */
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching';
import { createPushNotifications } from './sw/pushNotification';
declare const self: ServiceWorkerGlobalScope;
let notificationSoundEnabled = true;
// Tracks whether a page client has reported itself as visible.
// The clients.matchAll() visibilityState is unreliable on iOS Safari PWA,
// so we use this explicit flag as a fallback.
let appIsVisible = false;
let showMessageContent = false;
let showEncryptedMessageContent = false;
let clearNotificationsOnRead = false;
const { handlePushNotificationPushData } = createPushNotifications(self, () => ({
showMessageContent,
showEncryptedMessageContent,
}));
/** Cache key used to persist notification settings across SW restarts (iOS kills the SW frequently). */
const SW_SETTINGS_CACHE = 'sable-sw-settings-v1';
const SW_SETTINGS_URL = '/sw-settings-meta';
/** Cache key used to persist the active session so push-event fetches work after SW restart. */
const SW_SESSION_CACHE = 'sable-sw-session-v1';
const SW_SESSION_URL = '/sw-session-meta';
async function persistSettings() {
try {
const cache = await self.caches.open(SW_SETTINGS_CACHE);
await cache.put(
SW_SETTINGS_URL,
new Response(
JSON.stringify({
notificationSoundEnabled,
showMessageContent,
showEncryptedMessageContent,
clearNotificationsOnRead,
}),
{ headers: { 'Content-Type': 'application/json' } }
)
);
} catch {
// Ignore — caches may be unavailable in some environments.
}
}
async function loadPersistedSettings() {
try {
const cache = await self.caches.open(SW_SETTINGS_CACHE);
const response = await cache.match(SW_SETTINGS_URL);
if (!response) return;
const s = await response.json();
if (typeof s.notificationSoundEnabled === 'boolean')
notificationSoundEnabled = s.notificationSoundEnabled;
if (typeof s.showMessageContent === 'boolean') showMessageContent = s.showMessageContent;
if (typeof s.showEncryptedMessageContent === 'boolean')
showEncryptedMessageContent = s.showEncryptedMessageContent;
if (typeof s.clearNotificationsOnRead === 'boolean')
clearNotificationsOnRead = s.clearNotificationsOnRead;
} catch {
// Ignore — stale or missing cache is fine; we fall back to defaults.
}
}
async function persistSession(session: SessionInfo): Promise {
try {
const cache = await self.caches.open(SW_SESSION_CACHE);
await cache.put(
SW_SESSION_URL,
new Response(JSON.stringify(session), {
headers: { 'Content-Type': 'application/json' },
})
);
} catch {
// Ignore — caches may be unavailable in some environments.
}
}
async function clearPersistedSession(): Promise {
try {
const cache = await self.caches.open(SW_SESSION_CACHE);
await cache.delete(SW_SESSION_URL);
} catch {
// Ignore.
}
}
async function loadPersistedSession(): Promise {
try {
const cache = await self.caches.open(SW_SESSION_CACHE);
const response = await cache.match(SW_SESSION_URL);
if (!response) return undefined;
const s = await response.json();
if (typeof s.accessToken === 'string' && typeof s.baseUrl === 'string') {
return {
accessToken: s.accessToken,
baseUrl: s.baseUrl,
userId: typeof s.userId === 'string' ? s.userId : undefined,
};
}
return undefined;
} catch {
return undefined;
}
}
type SessionInfo = {
accessToken: string;
baseUrl: string;
/** Matrix user ID of the account, used to identify which account a push belongs to. */
userId?: string;
};
/**
* Store session per client (tab)
*/
const sessions = new Map();
/**
* Session pre-loaded from cache on SW activation. Acts as an immediate
* fallback so media fetches don't 401 during the window between SW restart
* and the first live setSession message from the page.
* Cleared as soon as any real setSession call comes in.
*/
let preloadedSession: SessionInfo | undefined;
const clientToResolve = new Map void>();
const clientToSessionPromise = new Map>();
async function cleanupDeadClients() {
const activeClients = await self.clients.matchAll();
const activeIds = new Set(activeClients.map((c) => c.id));
Array.from(sessions.keys()).forEach((id) => {
if (!activeIds.has(id)) {
sessions.delete(id);
clientToResolve.delete(id);
clientToSessionPromise.delete(id);
}
});
}
function setSession(clientId: string, accessToken: unknown, baseUrl: unknown, userId?: unknown) {
if (typeof accessToken === 'string' && typeof baseUrl === 'string') {
const info: SessionInfo = {
accessToken,
baseUrl,
userId: typeof userId === 'string' ? userId : undefined,
};
sessions.set(clientId, info);
// A real session has arrived — discard the preloaded fallback.
preloadedSession = undefined;
console.debug('[SW] setSession: stored', clientId, baseUrl);
// Persist so push-event fetches work after iOS restarts the SW.
persistSession(info).catch(() => undefined);
} else {
// Logout or invalid session
sessions.delete(clientId);
preloadedSession = undefined;
console.debug('[SW] setSession: removed', clientId);
clearPersistedSession().catch(() => undefined);
}
const resolveSession = clientToResolve.get(clientId);
if (resolveSession) {
resolveSession(sessions.get(clientId));
clientToResolve.delete(clientId);
clientToSessionPromise.delete(clientId);
}
}
function requestSession(client: Client): Promise {
const promise =
clientToSessionPromise.get(client.id) ??
new Promise((resolve) => {
clientToResolve.set(client.id, resolve);
client.postMessage({ type: 'requestSession' });
});
if (!clientToSessionPromise.has(client.id)) {
clientToSessionPromise.set(client.id, promise);
}
return promise;
}
async function requestSessionWithTimeout(
clientId: string,
timeoutMs = 3000
): Promise {
const client = await self.clients.get(clientId);
if (!client) {
console.warn('[SW] requestSessionWithTimeout: client not found', clientId);
return undefined;
}
const sessionPromise = requestSession(client);
const timeout = new Promise((resolve) => {
setTimeout(() => {
console.warn('[SW] requestSessionWithTimeout: timed out after', timeoutMs, 'ms', clientId);
resolve(undefined);
}, timeoutMs);
});
return Promise.race([sessionPromise, timeout]);
}
// ---------------------------------------------------------------------------
// Encrypted push — decryption relay
// ---------------------------------------------------------------------------
/**
* The shape returned by the client tab after decrypting an encrypted push event.
* Also used as a partial pushData object for handlePushNotificationPushData.
*/
type DecryptionResult = {
eventId: string;
success: boolean;
eventType?: string;
content?: unknown;
sender_display_name?: string;
room_name?: string;
/** document.visibilityState reported by the responding app tab. */
visibilityState?: string;
};
/** Pending decryption requests keyed by event_id. */
const decryptionPendingMap = new Map void>();
/**
* Fetch a single raw Matrix event from the homeserver.
* Returns undefined on error (e.g. network failure, auth error, redacted event).
*/
async function fetchRawEvent(
baseUrl: string,
accessToken: string,
roomId: string,
eventId: string
): Promise | undefined> {
try {
const url = `${baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/event/${encodeURIComponent(eventId)}`;
const res = await fetch(url, {
headers: { Authorization: `Bearer ${accessToken}` },
});
if (!res.ok) {
console.warn('[SW fetchRawEvent] HTTP', res.status, 'for', eventId);
return undefined;
}
return (await res.json()) as Record;
} catch (err) {
console.warn('[SW fetchRawEvent] error', err);
return undefined;
}
}
/**
* Fetch the m.room.name state event from the homeserver.
* Returns undefined when not set (DMs and many encrypted rooms have no explicit name).
*/
async function fetchRoomName(
baseUrl: string,
accessToken: string,
roomId: string
): Promise {
try {
const url = `${baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/state/m.room.name`;
const res = await fetch(url, {
headers: { Authorization: `Bearer ${accessToken}` },
});
if (!res.ok) return undefined;
const data = (await res.json()) as Record;
const { name } = data;
return typeof name === 'string' && name.trim() ? name.trim() : undefined;
} catch {
return undefined;
}
}
type MemberInfo = {
displayname: string | undefined;
avatarUrl: string | undefined;
};
/**
* Fetch a room member's state from the homeserver.
* Returns displayname and avatar_url (both may be undefined).
*/
async function fetchMemberInfo(
baseUrl: string,
accessToken: string,
roomId: string,
userId: string
): Promise {
try {
const url = `${baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/state/m.room.member/${encodeURIComponent(userId)}`;
const res = await fetch(url, {
headers: { Authorization: `Bearer ${accessToken}` },
});
if (!res.ok) return { displayname: undefined, avatarUrl: undefined };
const data = (await res.json()) as Record;
const displayname =
typeof data.displayname === 'string' && data.displayname.trim()
? data.displayname.trim()
: undefined;
const avatarUrl =
typeof data.avatar_url === 'string' && data.avatar_url.trim()
? data.avatar_url.trim()
: undefined;
return { displayname, avatarUrl };
} catch {
return { displayname: undefined, avatarUrl: undefined };
}
}
/**
* Fetch the m.room.avatar state event URL from the homeserver.
* Returns undefined when the room has no avatar or the request fails.
*/
async function fetchRoomAvatar(
baseUrl: string,
accessToken: string,
roomId: string
): Promise {
try {
const url = `${baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/state/m.room.avatar`;
const res = await fetch(url, {
headers: { Authorization: `Bearer ${accessToken}` },
});
if (!res.ok) return undefined;
const data = (await res.json()) as Record;
const avatarUrl = data.url;
return typeof avatarUrl === 'string' && avatarUrl.trim() ? avatarUrl.trim() : undefined;
} catch {
return undefined;
}
}
/**
* Convert an mxc:// URL to a legacy unauthenticated thumbnail URL.
* Notification icons are fetched by the OS without auth headers, so we use
* the pre-MSC3916 media endpoint which most homeservers still serve publicly.
*/
function mxcToNotificationUrl(mxcUrl: string, baseUrl: string): string | undefined {
const match = mxcUrl.match(/^mxc:\/\/([^/]+)\/([^?#]+)/);
if (!match || !match[1] || !match[2]) return undefined;
const [, server, mediaId] = match;
return `${baseUrl}/_matrix/media/v3/thumbnail/${encodeURIComponent(server)}/${encodeURIComponent(mediaId)}?width=96&height=96&method=crop`;
}
/**
* Return the first any-session we have stored (used for push fetches where we
* don't have a client ID, e.g. when the app is backgrounded but still loaded).
*/
function getAnyStoredSession(): SessionInfo | undefined {
return sessions.values().next().value;
}
/**
* Extract the MXID localpart (@user:server → user) for fallback display names.
*/
function mxidLocalpart(userId: string): string {
return userId.match(/^@([^:]+):/)?.[1] ?? userId;
}
/**
* Post a decryptPushEvent request to one of the open window clients and wait
* up to 5 s for the pushDecryptResult reply.
*/
async function requestDecryptionFromClient(
windowClients: readonly Client[],
rawEvent: Record
): Promise {
const eventId = rawEvent.event_id as string;
// Chain clients sequentially using reduce to avoid await-in-loop and for-of.
return Array.from(windowClients).reduce(
async (prevPromise, client) => {
const prev = await prevPromise;
if (prev?.success) return prev;
const promise = new Promise((resolve) => {
decryptionPendingMap.set(eventId, resolve);
});
const timeout = new Promise((resolve) => {
setTimeout(() => {
decryptionPendingMap.delete(eventId);
console.warn('[SW decryptRelay] timed out waiting for client', client.id);
resolve(undefined);
}, 5000);
});
try {
(client as WindowClient).postMessage({
type: 'decryptPushEvent',
rawEvent,
});
} catch (err) {
decryptionPendingMap.delete(eventId);
console.warn('[SW decryptRelay] postMessage error', err);
return undefined;
}
return Promise.race([promise, timeout]);
},
Promise.resolve(undefined) as Promise
);
}
/**
* Handle a minimal push payload (event_id_only format).
* Fetches the event from the homeserver and shows a notification.
* For encrypted events, attempts to relay decryption to an open app tab.
*/
async function handleMinimalPushPayload(
roomId: string,
eventId: string,
windowClients: readonly Client[]
): Promise {
// On iOS the SW is killed and restarted for every push, clearing the in-memory sessions
// Map. Fall back to the Cache Storage copy that was written when the user last opened
// the app (same pattern as settings persistence).
const session = getAnyStoredSession() ?? (await loadPersistedSession());
if (!session) {
// No session anywhere — app was never opened since install, or the user logged out.
// Show a minimal actionable notification so the user can tap through to the room.
console.debug('[SW push] minimal payload: no session, showing generic notification');
await self.registration.showNotification('New Message', {
body: undefined,
icon: '/public/res/logo-maskable/logo-maskable-180x180.png',
badge: '/public/res/logo-maskable/logo-maskable-72x72.png',
tag: `room-${roomId}`,
renotify: true,
data: { room_id: roomId, event_id: eventId },
} as NotificationOptions);
return;
}
// Fetch the raw event, room name, and room avatar in parallel — all need only roomId.
const [rawEvent, roomNameFromState, roomAvatarMxc] = await Promise.all([
fetchRawEvent(session.baseUrl, session.accessToken, roomId, eventId),
fetchRoomName(session.baseUrl, session.accessToken, roomId),
fetchRoomAvatar(session.baseUrl, session.accessToken, roomId),
]);
if (!rawEvent) {
await self.registration.showNotification('New Message', {
body: undefined,
icon: '/public/res/logo-maskable/logo-maskable-180x180.png',
badge: '/public/res/logo-maskable/logo-maskable-72x72.png',
tag: `room-${roomId}`,
renotify: true,
data: { room_id: roomId, event_id: eventId, user_id: session.userId },
} as NotificationOptions);
return;
}
const eventType = rawEvent.type as string | undefined;
const sender = rawEvent.sender as string | undefined;
// Fetch sender's member state — gives us both display name and avatar URL.
const memberInfo = sender
? await fetchMemberInfo(session.baseUrl, session.accessToken, roomId, sender)
: { displayname: undefined, avatarUrl: undefined };
// Fall back to MXID localpart when the server returns no displayname.
const senderDisplay = memberInfo.displayname ?? (sender ? mxidLocalpart(sender) : 'Someone');
// For DMs (no m.room.name state), use the sender's display name as the room name.
const resolvedRoomName = roomNameFromState ?? senderDisplay;
// Room avatar takes priority (group rooms); for DMs fall back to sender's member avatar.
// Convert mxc:// to a legacy unauthenticated thumbnail URL so the OS can fetch it.
const notificationAvatarUrl =
(roomAvatarMxc ?? memberInfo.avatarUrl) !== undefined
? mxcToNotificationUrl((roomAvatarMxc ?? memberInfo.avatarUrl)!, session.baseUrl)
: undefined;
const baseData = {
room_id: roomId,
event_id: eventId,
user_id: session.userId,
};
if (eventType === 'm.room.encrypted') {
// Try to relay decryption to an open app tab.
const result =
windowClients.length > 0
? await requestDecryptionFromClient(windowClients, rawEvent)
: undefined;
// If the relay responded and the app is currently visible, the in-app UI is already
// displaying the message — skip the OS notification entirely.
if (result?.visibilityState === 'visible') return;
if (result?.success) {
// App was backgrounded but not frozen — decryption succeeded.
// Prefer the server-fetched display name (authoritative) over the relay's SDK cache
// value, which may be stale or missing if the SDK hasn't fully synced yet.
await handlePushNotificationPushData({
...baseData,
type: result.eventType,
content: result.content as { notification_type?: string; membership?: string } | undefined,
sender_display_name: senderDisplay,
// Prefer relay's room name (has m.direct / computed SDK name); fall back to state fetch.
room_name: result.room_name || resolvedRoomName,
room_avatar_url: notificationAvatarUrl,
});
} else {
// App is frozen or fully closed — show "Encrypted message" fallback.
await handlePushNotificationPushData({
...baseData,
type: 'm.room.encrypted',
content: {},
sender_display_name: senderDisplay,
room_name: resolvedRoomName,
room_avatar_url: notificationAvatarUrl,
});
}
} else {
// Unencrypted event — we have the plaintext, show it.
await handlePushNotificationPushData({
...baseData,
type: eventType,
content: rawEvent.content as { notification_type?: string; membership?: string } | undefined,
sender_display_name: senderDisplay,
room_name: resolvedRoomName,
room_avatar_url: notificationAvatarUrl,
});
}
}
self.addEventListener('install', (event: ExtendableEvent) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', (event: ExtendableEvent) => {
event.waitUntil(
(async () => {
await self.clients.claim();
await cleanupDeadClients();
// Pre-load the persisted session into memory so that media fetches arriving
// before the first setSession message from the page are immediately
// authenticated rather than falling through to a 3-second timeout.
preloadedSession = await loadPersistedSession();
// Proactively request sessions from all window clients so the sessions Map
// is pre-populated after a SW restart, rather than waiting for the first
// media fetch to trigger requestSessionWithTimeout.
const windowClients = await self.clients.matchAll({ type: 'window' });
windowClients.forEach((client) => client.postMessage({ type: 'requestSession' }));
})()
);
});
/**
* Receive session updates from clients
*/
self.addEventListener('message', (event: ExtendableMessageEvent) => {
const client = event.source as Client | null;
if (!client) return;
const { data } = event;
if (!data || typeof data !== 'object') return;
const { type, accessToken, baseUrl, userId } = data as Record;
if (type === 'setSession') {
setSession(client.id, accessToken, baseUrl, userId);
event.waitUntil(cleanupDeadClients());
}
if (type === 'pushDecryptResult') {
// Resolve a pending decryption request from handleMinimalPushPayload
const { eventId } = data as { eventId?: string };
if (typeof eventId === 'string') {
const resolve = decryptionPendingMap.get(eventId);
if (resolve) {
decryptionPendingMap.delete(eventId);
resolve(data as DecryptionResult);
}
}
}
if (type === 'setAppVisible') {
if (typeof (data as { visible?: unknown }).visible === 'boolean') {
appIsVisible = (data as { visible: boolean }).visible;
}
}
if (type === 'setNotificationSettings') {
if (
typeof (data as { notificationSoundEnabled?: unknown }).notificationSoundEnabled === 'boolean'
) {
notificationSoundEnabled = (data as { notificationSoundEnabled: boolean })
.notificationSoundEnabled;
}
if (typeof (data as { showMessageContent?: unknown }).showMessageContent === 'boolean') {
showMessageContent = (data as { showMessageContent: boolean }).showMessageContent;
}
if (
typeof (data as { showEncryptedMessageContent?: unknown }).showEncryptedMessageContent ===
'boolean'
) {
showEncryptedMessageContent = (data as { showEncryptedMessageContent: boolean })
.showEncryptedMessageContent;
}
if (
typeof (data as { clearNotificationsOnRead?: unknown }).clearNotificationsOnRead === 'boolean'
) {
clearNotificationsOnRead = (data as { clearNotificationsOnRead: boolean })
.clearNotificationsOnRead;
}
// Persist so settings survive SW restart (iOS kills the SW aggressively).
event.waitUntil(persistSettings());
}
});
const MEDIA_PATHS = [
'/_matrix/client/v1/media/download',
'/_matrix/client/v1/media/thumbnail',
// Legacy unauthenticated endpoints — servers that require auth return 404/403
// for these when no token is present, so intercept and add auth here too.
'/_matrix/media/v3/download',
'/_matrix/media/v3/thumbnail',
'/_matrix/media/r0/download',
'/_matrix/media/r0/thumbnail',
];
function mediaPath(url: string): boolean {
try {
const { pathname } = new URL(url);
return MEDIA_PATHS.some((p) => pathname.startsWith(p));
} catch {
return false;
}
}
function validMediaRequest(url: string, baseUrl: string): boolean {
return MEDIA_PATHS.some((p) => {
const validUrl = new URL(p, baseUrl);
return url.startsWith(validUrl.href);
});
}
function fetchConfig(token: string): RequestInit {
return {
headers: {
Authorization: `Bearer ${token}`,
},
cache: 'default',
};
}
self.addEventListener('message', (event: ExtendableMessageEvent) => {
if (event.data.type === 'togglePush') {
const token = event.data?.token;
const fetchOptions = fetchConfig(token);
event.waitUntil(
fetch(`${event.data.url}/_matrix/client/v3/pushers/set`, {
method: 'POST',
...fetchOptions,
body: JSON.stringify(event.data.pusherData),
})
);
}
});
self.addEventListener('fetch', (event: FetchEvent) => {
const { url, method } = event.request;
if (method !== 'GET' || !mediaPath(url)) return;
const { clientId } = event;
// For browser sub-resource loads (images, video, audio, etc.), 'follow' is
// the correct mode: the auth header is sent to the Matrix server which owns
// the first hop; any CDN redirect it issues is followed natively by the
// Fetch machinery. 'manual' would return an opaque-redirect Response that
// the browser cannot render as an
/