Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | 2x 55x 41x 1x 1x 40x 1x 1x 39x 39x 39x 7x 7x 2x 2x 5x 5x 2x 2x 3x 1x 1x 2x 2x 1x 39x 9x 7x 7x 39x 39x 2x 2x 37x 37x 3x 3x 34x 34x 34x 3x 3x 34x 30x 30x 30x 2x 2x 2x 2x 2x 2x 34x 11x 1x 1x 2x 2x 1x 1x 10x 10x 1x 1x 9x 1x 1x 8x 8x 8x 8x 7x 7x 2x 2x 8x 1x 1x 14x 14x 1x 13x 1x 1x 12x 1x 1x 11x 11x 4x 7x 1x 1x 6x 6x 1x 1x 5x 5x 1x 1x 4x 4x 4x 5x 2x 2x 3x 2x 2x 1x 1x 5x 5x 4x 4x 3x 3x 1x 1x | import { CallPayload } from '../models/infrasctructure/call-payload'; import { Base64 } from '../utils/base64'; import { Logger } from '../utils/logger'; import { WorkerService } from './worker-service'; /** * Configuration options for the push notification service. * Defines optional settings for controlling service behavior and permission callbacks. */ export type PushServiceConfig = { /** * When true, disables the push notification service entirely. */ disablePushService?: boolean; /** * VAPID key required for push service authentication. */ vapidKey?: string; /** * Callback triggered when notification permission is in 'default' state. */ onPermissionDefault?: () => Promise<void>; /** * Callback triggered when notification permission is granted by the user. */ onPermissionGranted?: () => Promise<void>; /** * Callback triggered when notification permission is denied by the user. */ onPermissionDenied?: () => Promise<void>; /** * Optional function to convert base64 string to Uint8Array (for testability). */ urlBase64ToUint8Array?: (base64String: string) => Uint8Array; }; /** * Internal configuration type that extends PushServiceConfig with required * callback handlers and optional browser API references. */ type PushServiceEffectiveConfig = { /** * Callback triggered when a push notification with call payload is received. * * @param payload - The call payload containing connection information */ onCall: (payload: CallPayload) => Promise<void>; /** * Optional Notification class reference for environments where global access is restricted. */ notification?: typeof Notification; /** * Optional PushManager class reference for environments where global access is restricted. */ pushManager?: typeof PushManager; /** * Optional function to convert url-safe base64 string to Uint8Array (for testability). */ urlBase64ToUint8Array?: (base64String: string) => Uint8Array; } & PushServiceConfig; /** * Represents a push notification subscription with endpoint and encryption keys. * Contains all the necessary information to deliver push messages to this device. */ export interface Subscription { /** * URL endpoint for the push service provider. */ endpoint: string; /** * Optional timestamp when this subscription expires, or null if it does not expire. */ expirationTime: number | null; /** * Cryptographic keys required for securing push notification payloads. */ keys: { /** * P-256 Diffie-Hellman public key for message encryption. */ p256dh: string; /** * Authentication secret for the subscription. */ auth: string; }; } /** * Service interface for managing push notifications. * Provides methods for initialization, subscription management, and displaying notifications. */ export interface PushService { /** * Initializes the push notification service with the provided configuration. * Sets up event listeners, requests permissions if needed, and registers with push service. * * @param config - Configuration with callbacks and settings * @returns Promise that resolves when initialization is complete */ initialize(config: PushServiceEffectiveConfig): Promise<void>; /** * Retrieves the current push notification subscription if available. * * @returns Promise resolving to the current subscription or undefined if not subscribed */ getSubscription(): Promise<Subscription | undefined>; /** * Displays a notification to the user, either through the service worker or directly. * * @param title - Title text of the notification * @param options - Additional notification options like body text, icon, etc. * @returns Boolean indicating if the notification was successfully displayed */ showNotification(title: string, options?: NotificationOptions): boolean; } /** * Factory function that creates and returns an implementation of the PushService interface. * Manages push notification subscriptions, permission handling, and notification display. * * @param logger - Logger instance for output of service events and errors * @param workerService - Service for accessing the service worker registration * @param base64 - Utility for Base64 encoding/decoding of subscription keys * @returns An implementation of the PushService interface */ export function getPushService(logger: Logger, workerService: WorkerService, base64: Base64): PushService { let subscription: Subscription | undefined; let vapidKey: string | undefined; let notification: typeof Notification | undefined; let pushManager: typeof PushManager | undefined; let urlBase64ToUint8Array: ((base64String: string) => Uint8Array) | undefined; return { async initialize(config: PushServiceEffectiveConfig): Promise<void> { if (config.disablePushService) { logger.warn('[push-service] Disabled.'); return; } if (!workerService.container) { logger.warn('[push-service] Service Worker is not available.'); return; } notification = config.notification; pushManager = config.pushManager; urlBase64ToUint8Array = config.urlBase64ToUint8Array; async function processMessage(payload: any) { const { data } = payload; if (!data) { logger.error('[push-service] Invalid payload data.'); return; } const callPayload = data as CallPayload; if (!callPayload || !callPayload.a) { logger.error('[push-service] Invalid payload data.'); return; } if (!config.onCall) { logger.warn('[push-service] Message callback is not initialized.'); return; } try { await config.onCall(callPayload); } catch (error) { logger.error(`[push-service] Error while processing push call.`, error); } } workerService.container.addEventListener('message', async (event: any) => { if (event.data?.payload && event.data.type === 'PUSH_NOTIFICATION') { logger.debug('[push-service] Message received from Service Worker:', event.data.payload); await processMessage(event.data.payload); } }); vapidKey = config.vapidKey; if (!config.vapidKey) { logger.warn('[push-service] No vapid key found.'); return; } const Notification = notification; if (!Notification) { logger.warn('[push-service] Notifications are not supported.'); return; } let permission: NotificationPermission = Notification.permission; const { onPermissionDefault, onPermissionGranted, onPermissionDenied } = config; if (permission === 'default') { if (onPermissionDefault) await onPermissionDefault(); permission = await Notification.requestPermission(); } switch (permission) { case 'granted': logger.debug('[push-service] Notification permission granted.'); if (onPermissionGranted) await onPermissionGranted(); break; case 'denied': logger.error('[push-service] Notification permission denied.'); if (onPermissionDenied) await onPermissionDenied(); break; case 'default': logger.error('[push-service] Notification permission is still default.'); if (onPermissionDefault) await onPermissionDefault(); break; } logger.debug('[push-service] Initialized.'); }, async getSubscription(): Promise<Subscription | undefined> { async function getPushSubscription( urlBase64ToUint8Array: (base64String: string) => Uint8Array, ): Promise<PushSubscription | undefined> { if (!pushManager) { logger.warn('[push-service] PushManager is not available.'); return undefined; } async function unsubscribePushSubscription(subscription: PushSubscription): Promise<void> { try { await subscription.unsubscribe(); logger.debug(`[push-service] Unsubscribed.`); } catch (err) { logger.error('[push-service] An error occurred while cancelling the subscription.', err); } } const registration = workerService.registration; if (!registration) { logger.warn('[push-service] Service Worker registration is not available.'); return undefined; } if (!vapidKey) { logger.warn('[push-service] Vapid Key is not initialized.'); return undefined; } try { const applicationServerKey = urlBase64ToUint8Array(vapidKey); const currentSubscription = await registration.pushManager.getSubscription(); if (currentSubscription?.options?.applicationServerKey) { const currentSubscriptionApplicationServerKey = new Uint8Array( currentSubscription.options.applicationServerKey, ); if ( base64.encode(currentSubscriptionApplicationServerKey) !== base64.encode(applicationServerKey) ) { logger.debug('[push-service] Another subscription found. Unsubscribing.'); await unsubscribePushSubscription(currentSubscription); } } return await registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: applicationServerKey, }); } catch (err) { logger.error('[push-service] An error occurred while obtaining the subscription.', err); return undefined; } } const Notification = notification; if (!Notification) { return undefined; } if (Notification.permission !== 'granted') { logger.warn('[push-service] Notification permission was not granted.'); return undefined; } if (!urlBase64ToUint8Array) { logger.warn('[push-service] UrlBase64ToUint8Array is not initialized.'); return undefined; } const pushSubscription = await getPushSubscription(urlBase64ToUint8Array); if (!pushSubscription) { return undefined; } if (!pushSubscription.endpoint) { logger.warn('[push-service] Invalid subscription endpoint.'); return undefined; } const p256dhKey = pushSubscription.getKey('p256dh'); if (!p256dhKey || p256dhKey.byteLength === 0) { logger.warn('[push-service] Invalid p256dh key.'); return undefined; } const authKey = pushSubscription.getKey('auth'); if (!authKey || authKey.byteLength === 0) { logger.warn('[push-service] Invalid auth key.'); return undefined; } subscription = { endpoint: pushSubscription.endpoint, expirationTime: pushSubscription.expirationTime, keys: { p256dh: base64.encode(new Uint8Array(p256dhKey)), auth: base64.encode(new Uint8Array(authKey)), }, }; logger.debug('[push-service] Subscription:', JSON.stringify(subscription)); return subscription; }, showNotification(title: string, options?: NotificationOptions) { function showInternal() { if (!workerService.controller?.postMessage) { logger.warn('[push-service] Unable to show notification. Service worker is not initialized.'); return false; } if (!subscription) { logger.warn('[push-service] Unable to show notification. Notifications unavailable.'); return false; } workerService.controller.postMessage({ type: 'SHOW_NOTIFICATION', title: title, options: options }); return true; } const shown = showInternal(); if (!shown) { const Notification = notification; if (Notification) { new Notification(title, options); return true; } return false; } return shown; }, }; } |