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 | 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 2x 2x 2x 1x 1x 13x 12x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 3x 11x 2x 1x 11x 11x 11x 11x 1x | import { CallServiceConfig, getCallService } from './services/call-service'; import { ConnectionServiceConfig, getConnectionService } from './services/connection-service'; import { Connection, translateConnection } from './services/connection/connection'; import { getDefaultWebRTC } from './services/connection/web-rtc'; import { getHandleService, HandleServiceConfig } from './services/handle-service'; import { getAnswerCallHandler } from './services/handle/answer-call-handler'; import { getCloseCallHandler } from './services/handle/close-call-handler'; import { getDialCallHandler } from './services/handle/dial-call-handler'; import { getIceCallHandler } from './services/handle/ice-call-handler'; import { getOfferCallHandler } from './services/handle/offer-call-handler'; import { LoggerServiceConfig } from './services/logger-service'; import { getPushService, PushServiceConfig } from './services/push-service'; import { getSessionService, SessionServiceConfig } from './services/session-service'; import { getSignalRService, SignalRServiceConfig } from './services/signalr-service'; import { getTimeService } from './services/time-service'; import { getWorkerService, WorkerServiceConfig } from './services/worker-service'; import { getApiClient } from './utils/api-client'; import { getBase64 } from './utils/base64'; import { CryptoKeyPair, getCryptography } from './utils/cryptography'; import { Logger } from './utils/logger'; import { getUtf8 } from './utils/utf8'; import { urlBase64ToUint8Array } from './utils/web-push-helpers'; /** * Configuration interface for the Whisper messenger. * Combines configuration options from all services used by Whisper. */ export type WhisperConfig = { /** * Optional callback triggered when Whisper may work unstably. * Called when certain required features (like notifications) are unavailable. * * @param reason - Description of why the application may work unstably * @returns Promise that resolves when the warning has been acknowledged */ onMayWorkUnstably?: (reason: string) => Promise<void>; } & LoggerServiceConfig & SignalRServiceConfig & WorkerServiceConfig & PushServiceConfig & SessionServiceConfig & CallServiceConfig & ConnectionServiceConfig & HandleServiceConfig; /** * Main interface for the Whisper messenger. * Provides methods for managing connections and accessing messenger functionality. */ export interface Whisper { /** * Gets the public key of the current user. * This is the user's identity in the messenger. * * @returns The user's public key as a string, or undefined if not initialized */ get publicKey(): string | undefined; /** * Gets the current server time. * Used for synchronizing timestamps across the system. * * @returns Current server time in milliseconds */ get serverTime(): number; /** * Gets all active connections managed by the messenger. * * @returns Array of all connection objects */ get connections(): Connection[]; /** * Gets or creates a connection with a peer identified by their public key. * If a connection already exists, it is returned; otherwise, a new outgoing connection is created. * * @param publicKeyBase64 - Public key of the peer to connect to * @returns Connection object for communicating with the peer */ get(publicKeyBase64: string): Connection; /** * Terminates and removes a connection with a peer. * * @param publicKeyBase64 - Public key of the peer whose connection should be removed */ delete(publicKeyBase64: string): void; /** * Shows a browser notification to the user. * * @param title - Title of the notification * @param options - Additional notification options * @returns Boolean indicating if the notification was successfully shown */ showNotification(title: string, options?: NotificationOptions): boolean; } /** * Interface for initializing and creating Whisper messenger instances. * Provides factory methods for creating and configuring the messenger. */ export interface WhisperPrototype { /** * Initializes a new Whisper messenger instance with the provided configuration. * Sets up all required services and establishes necessary connections. * * @param config - Configuration options for all services * @returns Promise resolving to a configured Whisper instance */ initialize(config: WhisperConfig): Promise<Whisper>; /** * Generates a new signing key pair for user authentication. * * @returns A cryptographic key pair for signing operations */ generateSigningKeyPair(): CryptoKeyPair; } /** * Factory function that creates and returns an implementation of the WhisperPrototype interface. * Sets up the dependency injection chain for all required services. * * @param logger - Logger instance for error and debugging information * @returns An implementation of the WhisperPrototype interface */ export function getPrototype(logger: Logger): WhisperPrototype { const base64 = getBase64(); const utf8 = getUtf8(); const timeService = getTimeService(logger); const cryptography = getCryptography(logger); const apiClient = getApiClient(logger); const signalRService = getSignalRService(logger); const workerService = getWorkerService(logger); const sessionService = getSessionService(logger, base64); const pushService = getPushService(logger, workerService, base64); const callService = getCallService( logger, timeService, sessionService, apiClient, signalRService, base64, utf8, cryptography, ); const connectionService = getConnectionService( logger, timeService, callService, sessionService, base64, utf8, cryptography, ); const dialCallHandler = getDialCallHandler( logger, timeService, sessionService, base64, utf8, cryptography, connectionService, ); const offerCallHandler = getOfferCallHandler( logger, timeService, sessionService, base64, utf8, cryptography, connectionService, ); const answerCallHandler = getAnswerCallHandler( logger, timeService, sessionService, base64, utf8, cryptography, connectionService, ); const iceCallHandler = getIceCallHandler( logger, timeService, sessionService, base64, utf8, cryptography, connectionService, ); const closeCallHandler = getCloseCallHandler( logger, timeService, sessionService, base64, utf8, cryptography, connectionService, ); const handleService = getHandleService( logger, dialCallHandler, offerCallHandler, answerCallHandler, iceCallHandler, closeCallHandler, ); let busy = false; let initializationResolver: ((value: Whisper) => void) | undefined; let initializationPromise = new Promise<Whisper>((resolve) => { initializationResolver = resolve; }); const whisper: Whisper = { get publicKey(): string | undefined { return sessionService.signingPublicKeyBase64Safe; }, get serverTime(): number { return timeService.serverTime; }, get connections(): Connection[] { return connectionService.connections.map(translateConnection); }, get(publicKeyBase64: string): Connection { const connection = connectionService.getConnection(publicKeyBase64); return translateConnection(connection || connectionService.createOutgoing(publicKeyBase64)); }, delete(publicKeyBase64: string) { connectionService.deleteConnection(publicKeyBase64); }, showNotification(title: string, options?: NotificationOptions) { return pushService.showNotification(title, options); }, }; return { async initialize(config: WhisperConfig): Promise<Whisper> { if (busy) { logger.warn('[whisper] Parallel initialization attempt.'); await initializationPromise; return whisper; } busy = true; handleService.initialize(config); connectionService.initialize({ ...config, webRTC: getDefaultWebRTC(), }); callService.initialize({ ...config, navigator: window.navigator, }); await workerService.initialize({ ...config, navigator: window.navigator, }); await sessionService.initialize(config); await pushService.initialize({ ...config, onCall: handleService.call, notification: window.Notification, pushManager: window.PushManager, urlBase64ToUint8Array: urlBase64ToUint8Array, }); const subscription = await pushService.getSubscription(); await signalRService.initialize({ ...config, onCall: handleService.call, onReady: async () => { await callService.update(sessionService.signingPublicKeyBase64, subscription); }, }); workerService.controller?.postMessage({ type: 'CLIENT_READY' }); setInterval(async () => { await callService.update(sessionService.signingPublicKeyBase64, subscription); }, 60 * 1000); if (!subscription) { if (config.onMayWorkUnstably) { await config.onMayWorkUnstably( 'Due to notifications unavailable, Whisper Messenger may work unstably.', ); } } logger.log('[whisper] Initialized.'); initializationResolver?.call(this, whisper); busy = false; return whisper; }, generateSigningKeyPair(): CryptoKeyPair { return cryptography.generateSigningKeyPair(); }, }; } |