Files
tech-log-frontend/src/adapters/web-push/notification-registry.ts
T

123 lines
3.6 KiB
TypeScript

import type {
NotificationClickDataV1,
NotificationRouteIntentId,
NotificationTypeId,
} from "../../contracts/web-push.ts";
export type SafeNotification = Readonly<{
title: string;
options: Readonly<{
body: string;
data: NotificationClickDataV1;
requireInteraction: false;
tag: string;
}>;
}>;
export type WebPushNotificationDefinition = Readonly<{
notificationType: NotificationTypeId;
routeIntent: NotificationRouteIntentId;
title: string;
body: string;
path: string;
}>;
export interface WebPushNotificationRegistry {
resolve(
notificationType: NotificationTypeId,
routeIntent: NotificationRouteIntentId,
): WebPushNotificationDefinition | null;
routePath(routeIntent: NotificationRouteIntentId): string | null;
}
const REGISTRY_ID = /^[A-Z][A-Z0-9_]{0,63}$/u;
export function createWebPushNotificationRegistry(
definitions: readonly WebPushNotificationDefinition[],
): WebPushNotificationRegistry {
if (
definitions.length === 0 ||
definitions.length > 32 ||
definitions.some(
(definition) =>
!REGISTRY_ID.test(definition.notificationType) ||
!REGISTRY_ID.test(definition.routeIntent) ||
definition.title.length < 1 ||
definition.title.length > 80 ||
definition.body.length < 1 ||
definition.body.length > 160 ||
!safePath(definition.path),
)
) {
throw new TypeError("Web Push notification registry is invalid.");
}
const byType = new Map<string, WebPushNotificationDefinition>();
const byRoute = new Map<string, string>();
for (const definition of definitions) {
if (
byType.has(definition.notificationType) ||
(byRoute.has(definition.routeIntent) &&
byRoute.get(definition.routeIntent) !== definition.path)
) {
throw new TypeError("Web Push notification registry is ambiguous.");
}
const snapshot = Object.freeze({ ...definition });
byType.set(definition.notificationType, snapshot);
byRoute.set(definition.routeIntent, definition.path);
}
const registry: WebPushNotificationRegistry = {
resolve(notificationType, routeIntent) {
const definition = byType.get(notificationType);
return definition && definition.routeIntent === routeIntent
? definition
: null;
},
routePath(routeIntent) {
return byRoute.get(routeIntent) ?? null;
},
};
return Object.freeze(registry);
}
export type AssociationNotificationTagDigest = (
algorithm: "SHA-256",
data: Uint8Array<ArrayBuffer>,
) => Promise<ArrayBuffer>;
export async function createAssociationNotificationTag(
associationEpoch: string,
notificationType: NotificationTypeId,
digest: AssociationNotificationTagDigest = (algorithm, data) =>
globalThis.crypto.subtle.digest(algorithm, data),
): Promise<string> {
const encoded = new TextEncoder().encode(
`PUSH_ASSOCIATION_TAG_V1\0${associationEpoch}`,
);
const hashed = new Uint8Array(await digest("SHA-256", encoded));
if (hashed.byteLength !== 32) {
throw new TypeError("Web Push notification tag digest is invalid.");
}
const prefix = [...hashed.subarray(0, 12)]
.map((value) => value.toString(16).padStart(2, "0"))
.join("");
return `ca-push-v1-${prefix}-${notificationType.toLowerCase()}`;
}
function safePath(path: string): boolean {
if (
!path.startsWith("/") ||
path.startsWith("//") ||
path.includes("\\") ||
path.includes("#") ||
path.length > 512
) {
return false;
}
try {
const parsed = new URL(path, "https://registry.invalid");
return parsed.origin === "https://registry.invalid";
} catch {
return false;
}
}