|
| 1 | +/** |
| 2 | + * pushSubscriptionService.ts – JSON-backed persistence for browser push |
| 3 | + * notification subscriptions. |
| 4 | + * |
| 5 | + * Subscriptions are keyed by their endpoint URL so that re-subscribing with |
| 6 | + * the same endpoint is idempotent. The store is a simple JSON file mirroring |
| 7 | + * the pattern used by store.ts for bounties. |
| 8 | + */ |
| 9 | + |
| 10 | +import fs from "fs"; |
| 11 | +import path from "path"; |
| 12 | + |
| 13 | +export interface PushSubscription { |
| 14 | + endpoint: string; |
| 15 | + keys: { |
| 16 | + p256dh: string; |
| 17 | + auth: string; |
| 18 | + }; |
| 19 | + createdAt: number; |
| 20 | +} |
| 21 | + |
| 22 | +export function resolvePushStorePath(): string { |
| 23 | + return ( |
| 24 | + process.env.PUSH_SUBSCRIPTION_STORE_PATH ?? |
| 25 | + path.join(__dirname, "../data/push-subscriptions.json") |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function tryParse<T>(filePath: string): T | null { |
| 30 | + try { |
| 31 | + const raw = fs.readFileSync(filePath, "utf8"); |
| 32 | + return JSON.parse(raw) as T; |
| 33 | + } catch { |
| 34 | + return null; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function loadSubscriptions(storePath?: string): PushSubscription[] { |
| 39 | + const store = storePath ?? resolvePushStorePath(); |
| 40 | + const primary = tryParse<PushSubscription[]>(store); |
| 41 | + if (primary !== null) return primary; |
| 42 | + return []; |
| 43 | +} |
| 44 | + |
| 45 | +function saveSubscriptions(subscriptions: PushSubscription[], storePath?: string): void { |
| 46 | + const store = storePath ?? resolvePushStorePath(); |
| 47 | + fs.mkdirSync(path.dirname(store), { recursive: true }); |
| 48 | + fs.writeFileSync(store, JSON.stringify(subscriptions, null, 2), "utf8"); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Register (or update) a push subscription. Returns the stored subscription. |
| 53 | + */ |
| 54 | +export function registerPushSubscription( |
| 55 | + subscription: PushSubscription, |
| 56 | + storePath?: string, |
| 57 | +): PushSubscription { |
| 58 | + const subscriptions = loadSubscriptions(storePath); |
| 59 | + const existing = subscriptions.find((s) => s.endpoint === subscription.endpoint); |
| 60 | + |
| 61 | + if (existing) { |
| 62 | + existing.keys = subscription.keys; |
| 63 | + return existing; |
| 64 | + } |
| 65 | + |
| 66 | + const stored: PushSubscription = { |
| 67 | + endpoint: subscription.endpoint, |
| 68 | + keys: subscription.keys, |
| 69 | + createdAt: subscription.createdAt ?? Date.now(), |
| 70 | + }; |
| 71 | + subscriptions.push(stored); |
| 72 | + saveSubscriptions(subscriptions, storePath); |
| 73 | + return stored; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Remove a push subscription by endpoint. Returns true if one was removed. |
| 78 | + */ |
| 79 | +export function unregisterPushSubscription( |
| 80 | + endpoint: string, |
| 81 | + storePath?: string, |
| 82 | +): boolean { |
| 83 | + const subscriptions = loadSubscriptions(storePath); |
| 84 | + const next = subscriptions.filter((s) => s.endpoint !== endpoint); |
| 85 | + if (next.length === subscriptions.length) return false; |
| 86 | + saveSubscriptions(next, storePath); |
| 87 | + return true; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * List all registered push subscriptions. |
| 92 | + */ |
| 93 | +export function listPushSubscriptions(storePath?: string): PushSubscription[] { |
| 94 | + return loadSubscriptions(storePath); |
| 95 | +} |
0 commit comments