@metamask-previews/core-backend 2.0.0-preview-a2a9f78c → 2.1.0-preview-468843ab

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -216,39 +216,45 @@ class BackendWebSocketService {
216
216
  return;
217
217
  }
218
218
  // If already connecting, wait for the existing connection attempt to complete
219
- if (__classPrivateFieldGet(this, _BackendWebSocketService_state, "f") === WebSocketState.CONNECTING && __classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f")) {
219
+ if (__classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f")) {
220
220
  await __classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f");
221
221
  return;
222
222
  }
223
- // Priority 2: Check authentication requirements (signed in)
224
- let bearerToken;
225
- try {
226
- const token = await __classPrivateFieldGet(this, _BackendWebSocketService_messenger, "f").call('AuthenticationController:getBearerToken');
227
- if (!token) {
223
+ // Create and store the connection promise IMMEDIATELY (before any async operations)
224
+ // This ensures subsequent connect() calls will wait for this promise instead of creating new connections
225
+ __classPrivateFieldSet(this, _BackendWebSocketService_connectionPromise, (async () => {
226
+ // Priority 2: Check authentication requirements (signed in)
227
+ let bearerToken;
228
+ try {
229
+ const token = await __classPrivateFieldGet(this, _BackendWebSocketService_messenger, "f").call('AuthenticationController:getBearerToken');
230
+ if (!token) {
231
+ __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_scheduleReconnect).call(this);
232
+ throw new Error('Authentication required: user not signed in');
233
+ }
234
+ bearerToken = token;
235
+ }
236
+ catch (error) {
237
+ log('Failed to check authentication requirements', { error });
238
+ // Can't connect - schedule retry
228
239
  __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_scheduleReconnect).call(this);
229
- return;
240
+ throw error;
230
241
  }
231
- bearerToken = token;
232
- }
233
- catch (error) {
234
- log('Failed to check authentication requirements', { error });
235
- // Can't connect - schedule retry
236
- __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_scheduleReconnect).call(this);
237
- return;
238
- }
239
- __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.CONNECTING);
240
- // Create and store the connection promise
241
- __classPrivateFieldSet(this, _BackendWebSocketService_connectionPromise, __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_establishConnection).call(this, bearerToken), "f");
242
+ __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.CONNECTING);
243
+ // Establish the actual WebSocket connection
244
+ try {
245
+ await __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_establishConnection).call(this, bearerToken);
246
+ }
247
+ catch (error) {
248
+ const errorMessage = (0, utils_1.getErrorMessage)(error);
249
+ log('Connection attempt failed', { errorMessage, error });
250
+ __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.ERROR);
251
+ // Rethrow to propagate error to caller
252
+ throw error;
253
+ }
254
+ })(), "f");
242
255
  try {
243
256
  await __classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f");
244
257
  }
245
- catch (error) {
246
- const errorMessage = (0, utils_1.getErrorMessage)(error);
247
- log('Connection attempt failed', { errorMessage, error });
248
- __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.ERROR);
249
- // Rethrow to propagate error to caller
250
- throw error;
251
- }
252
258
  finally {
253
259
  // Clear the connection promise when done (success or failure)
254
260
  __classPrivateFieldSet(this, _BackendWebSocketService_connectionPromise, null, "f");
@@ -786,13 +792,11 @@ async function _BackendWebSocketService_establishConnection(bearerToken) {
786
792
  __classPrivateFieldGet(this, _BackendWebSocketService_trace, "f").call(this, {
787
793
  name: `${SERVICE_NAME} Channel Message`,
788
794
  data: {
789
- channel: message.channel,
790
795
  latency_ms: latency,
791
796
  event: message.event,
792
797
  },
793
798
  tags: {
794
799
  service: SERVICE_NAME,
795
- channel_type: message.channel,
796
800
  },
797
801
  }, () => {
798
802
  // Direct lookup for exact channel match
@@ -809,7 +813,7 @@ async function _BackendWebSocketService_establishConnection(bearerToken) {
809
813
  // Calculate notification latency: time from server sent to client received
810
814
  const receivedAt = Date.now();
811
815
  const latency = receivedAt - timestamp;
812
- // Trace notification processing with latency data
816
+ // Trace notification processing wi th latency data
813
817
  // Use stored channelType instead of parsing each time
814
818
  __classPrivateFieldGet(this, _BackendWebSocketService_trace, "f").call(this, {
815
819
  name: `${SERVICE_NAME} Notification`,
@@ -1 +1 @@
1
- {"version":3,"file":"BackendWebSocketService.cjs","sourceRoot":"","sources":["../src/BackendWebSocketService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,2CAAkD;AAClD,+BAAoC;AAGpC,yCAA6D;AAE7D,MAAM,YAAY,GAAG,yBAAkC,CAAC;AAExD,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,yBAAyB,GAAG;IAChC,SAAS;IACT,YAAY;IACZ,aAAa;IACb,aAAa;IACb,WAAW;IACX,mBAAmB;IACnB,2BAA2B;IAC3B,wBAAwB;IACxB,kCAAkC;IAClC,oBAAoB;IACpB,uBAAuB;IACvB,qBAAqB;CACb,CAAC;AAEX;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,IAAY;IACzC,QAAQ,IAAI,EAAE;QACZ,KAAK,IAAI;YACP,OAAO,gBAAgB,CAAC;QAC1B,KAAK,IAAI;YACP,OAAO,YAAY,CAAC;QACtB,KAAK,IAAI;YACP,OAAO,gBAAgB,CAAC;QAC1B,KAAK,IAAI;YACP,OAAO,kBAAkB,CAAC;QAC5B,KAAK,IAAI;YACP,OAAO,UAAU,CAAC;QACpB,KAAK,IAAI;YACP,OAAO,oBAAoB,CAAC;QAC9B,KAAK,IAAI;YACP,OAAO,kBAAkB,CAAC;QAC5B,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CAAsB,IAAI,MAAA,CAAC;KAChC;AACH,CAAC,yGAOqB,KAAY;IAChC,KAAK,MAAM,CAAC,EAAE,OAAO,CAAC,IAAI,uBAAA,IAAI,gDAAiB,EAAE;QAC/C,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;KACvB;IACD,uBAAA,IAAI,gDAAiB,CAAC,KAAK,EAAE,CAAC;AAChC,CAAC;IAMC,uBAAA,IAAI,8CAAe,CAAC,KAAK,EAAE,CAAC;AAC9B,CAAC,iFAOS,QAAwB;IAChC,MAAM,QAAQ,GAAG,uBAAA,IAAI,sCAAO,CAAC;IAC7B,uBAAA,IAAI,kCAAU,QAAQ,MAAA,CAAC;IAEvB,IAAI,QAAQ,KAAK,QAAQ,EAAE;QACzB,wCAAwC;QACxC,sEAAsE;QACtE,uBAAA,IAAI,0CAAW,CAAC,OAAO,CACrB,gDAAgD,EAChD,IAAI,CAAC,iBAAiB,EAAE,CACzB,CAAC;KACH;AACH,CAAC","sourcesContent":["import type { RestrictedMessenger } from '@metamask/base-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport { getErrorMessage } from '@metamask/utils';\nimport { v4 as uuidV4 } from 'uuid';\n\nimport type { BackendWebSocketServiceMethodActions } from './BackendWebSocketService-method-action-types';\nimport { projectLogger, createModuleLogger } from './logger';\n\nconst SERVICE_NAME = 'BackendWebSocketService' as const;\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'connect',\n 'disconnect',\n 'sendMessage',\n 'sendRequest',\n 'subscribe',\n 'getConnectionInfo',\n 'getSubscriptionsByChannel',\n 'channelHasSubscription',\n 'findSubscriptionsByChannelPrefix',\n 'addChannelCallback',\n 'removeChannelCallback',\n 'getChannelCallbacks',\n] as const;\n\n/**\n * Gets human-readable close reason from RFC 6455 close code\n *\n * @param code - WebSocket close code\n * @returns Human-readable close reason\n */\nexport function getCloseReason(code: number): string {\n switch (code) {\n case 1000:\n return 'Normal Closure';\n case 1001:\n return 'Going Away';\n case 1002:\n return 'Protocol Error';\n case 1003:\n return 'Unsupported Data';\n case 1004:\n return 'Reserved';\n case 1005:\n return 'No Status Received';\n case 1006:\n return 'Abnormal Closure';\n case 1007:\n return 'Invalid frame payload data';\n case 1008:\n return 'Policy Violation';\n case 1009:\n return 'Message Too Big';\n case 1010:\n return 'Mandatory Extension';\n case 1011:\n return 'Internal Server Error';\n case 1012:\n return 'Service Restart';\n case 1013:\n return 'Try Again Later';\n case 1014:\n return 'Bad Gateway';\n case 1015:\n return 'TLS Handshake';\n default:\n if (code >= 3000 && code <= 3999) {\n return 'Library/Framework Error';\n }\n if (code >= 4000 && code <= 4999) {\n return 'Application Error';\n }\n return 'Unknown';\n }\n}\n\n/**\n * WebSocket connection states\n */\nexport enum WebSocketState {\n CONNECTING = 'connecting',\n CONNECTED = 'connected',\n DISCONNECTING = 'disconnecting',\n DISCONNECTED = 'disconnected',\n ERROR = 'error',\n}\n\n/**\n * WebSocket event types\n */\nexport enum WebSocketEventType {\n CONNECTED = 'connected',\n DISCONNECTED = 'disconnected',\n MESSAGE = 'message',\n ERROR = 'error',\n RECONNECTING = 'reconnecting',\n RECONNECTED = 'reconnected',\n}\n\n/**\n * Configuration options for the WebSocket service\n */\nexport type BackendWebSocketServiceOptions = {\n /** The WebSocket URL to connect to */\n url: string;\n\n /** The messenger for inter-service communication */\n messenger: BackendWebSocketServiceMessenger;\n\n /** Connection timeout in milliseconds (default: 10000) */\n timeout?: number;\n\n /** Initial reconnection delay in milliseconds (default: 500) */\n reconnectDelay?: number;\n\n /** Maximum reconnection delay in milliseconds (default: 5000) */\n maxReconnectDelay?: number;\n\n /** Request timeout in milliseconds (default: 30000) */\n requestTimeout?: number;\n\n /** Optional callback to determine if connection should be enabled (default: always enabled) */\n isEnabled?: () => boolean;\n\n /** Optional callback to trace performance of WebSocket operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n/**\n * Client Request message\n * Used when client sends a request to the server\n */\nexport type ClientRequestMessage = {\n event: string;\n data: {\n requestId: string;\n channels?: string[];\n [key: string]: unknown;\n };\n};\n\n/**\n * Server Response message\n * Used when server responds to a client request\n */\nexport type ServerResponseMessage = {\n event: string;\n data: {\n requestId: string;\n subscriptionId?: string;\n succeeded?: string[];\n failed?: string[];\n [key: string]: unknown;\n };\n};\n\n/**\n * Server Notification message\n * Used when server sends unsolicited data to client\n * subscriptionId is optional for system-wide notifications\n */\nexport type ServerNotificationMessage = {\n event: string;\n subscriptionId?: string;\n channel: string;\n data: Record<string, unknown>;\n timestamp: number;\n};\n\n/**\n * Union type for all WebSocket messages\n */\nexport type WebSocketMessage =\n | ClientRequestMessage\n | ServerResponseMessage\n | ServerNotificationMessage;\n\n/**\n * Channel-based callback configuration\n */\nexport type ChannelCallback = {\n /** Channel name to match (also serves as the unique identifier) */\n channelName: string;\n /** Callback function */\n callback: (notification: ServerNotificationMessage) => void;\n};\n\n/**\n * Unified WebSocket subscription object used for both internal storage and external API\n */\nexport type WebSocketSubscription = {\n /** The subscription ID from the server */\n subscriptionId: string;\n /** Channel names for this subscription */\n channels: string[];\n /** Channel type with version (e.g., 'account-activity.v1') extracted from first channel */\n channelType: string;\n /** Callback function for handling notifications (optional for external use) */\n callback?: (notification: ServerNotificationMessage) => void;\n /** Function to unsubscribe and clean up */\n unsubscribe: (requestId?: string) => Promise<void>;\n};\n\n/**\n * WebSocket connection info\n */\nexport type WebSocketConnectionInfo = {\n state: WebSocketState;\n url: string;\n reconnectAttempts: number;\n timeout: number;\n reconnectDelay: number;\n maxReconnectDelay: number;\n requestTimeout: number;\n connectedAt?: number;\n};\n\n// Action types for the messaging system - using generated method actions\nexport type BackendWebSocketServiceActions =\n BackendWebSocketServiceMethodActions;\n\nexport type BackendWebSocketServiceAllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type BackendWebSocketServiceAllowedEvents =\n | AuthenticationController.AuthenticationControllerStateChangeEvent\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent;\n\n// Event types for WebSocket connection state changes\nexport type BackendWebSocketServiceConnectionStateChangedEvent = {\n type: 'BackendWebSocketService:connectionStateChanged';\n payload: [WebSocketConnectionInfo];\n};\n\nexport type BackendWebSocketServiceEvents =\n BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type BackendWebSocketServiceMessenger = RestrictedMessenger<\n typeof SERVICE_NAME,\n BackendWebSocketServiceActions | BackendWebSocketServiceAllowedActions,\n BackendWebSocketServiceEvents | BackendWebSocketServiceAllowedEvents,\n BackendWebSocketServiceAllowedActions['type'],\n BackendWebSocketServiceAllowedEvents['type']\n>;\n\n/**\n * WebSocket Service with automatic reconnection, session management and direct callback routing\n *\n * Connection Management:\n * - Automatically subscribes to AuthenticationController:stateChange (sign in/out)\n * - Automatically subscribes to KeyringController:lock/unlock events\n * - Idempotent connect() function safe for multiple rapid calls\n * - Auto-reconnects on unexpected disconnects (manualDisconnect = false)\n *\n * Platform Responsibilities:\n * - Call connect() when app opens/foregrounds\n * - Call disconnect() when app closes/backgrounds\n * - Provide isEnabled() callback (feature flag)\n * - Call destroy() on app termination\n *\n * Real-Time Performance Optimizations:\n * - Fast path message routing (zero allocations)\n * - Production mode removes try-catch overhead\n * - Optimized JSON parsing with fail-fast\n * - Direct callback routing bypasses event emitters\n * - Memory cleanup and resource management\n */\nexport class BackendWebSocketService {\n /**\n * The name of the service.\n */\n readonly name = SERVICE_NAME;\n\n readonly #messenger: BackendWebSocketServiceMessenger;\n\n readonly #options: Required<\n Omit<BackendWebSocketServiceOptions, 'messenger' | 'isEnabled' | 'traceFn'>\n >;\n\n readonly #isEnabled: (() => boolean) | undefined;\n\n readonly #trace: TraceCallback;\n\n #ws: WebSocket | undefined;\n\n #state: WebSocketState = WebSocketState.DISCONNECTED;\n\n #reconnectAttempts = 0;\n\n #reconnectTimer: NodeJS.Timeout | null = null;\n\n #connectionTimeout: NodeJS.Timeout | null = null;\n\n // Track the current connection promise to handle concurrent connection attempts\n #connectionPromise: Promise<void> | null = null;\n\n readonly #pendingRequests = new Map<\n string,\n {\n resolve: (value: unknown) => void;\n reject: (error: Error) => void;\n timeout: NodeJS.Timeout;\n }\n >();\n\n #connectedAt: number = 0;\n\n // Track manual disconnects to prevent automatic reconnection\n #manualDisconnect = false;\n\n // Simplified subscription storage (single flat map)\n // Key: subscription ID string (e.g., 'sub_abc123def456')\n // Value: WebSocketSubscription object with channels, callback and metadata\n readonly #subscriptions = new Map<string, WebSocketSubscription>();\n\n // Channel-based callback storage\n // Key: channel name (serves as unique identifier)\n // Value: ChannelCallback configuration\n readonly #channelCallbacks = new Map<string, ChannelCallback>();\n\n // =============================================================================\n // 1. CONSTRUCTOR & INITIALIZATION\n // =============================================================================\n\n /**\n * Creates a new WebSocket service instance\n *\n * @param options - Configuration options for the WebSocket service\n */\n constructor(options: BackendWebSocketServiceOptions) {\n this.#messenger = options.messenger;\n this.#isEnabled = options.isEnabled;\n // Default to no-op trace function to keep core platform-agnostic\n this.#trace =\n options.traceFn ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (((_request: any, fn?: any) => fn?.()) as TraceCallback);\n\n this.#options = {\n url: options.url,\n timeout: options.timeout ?? 10000,\n reconnectDelay: options.reconnectDelay ?? 500,\n maxReconnectDelay: options.maxReconnectDelay ?? 5000,\n requestTimeout: options.requestTimeout ?? 30000,\n };\n\n // Subscribe to authentication and keyring controller events\n this.#subscribeEvents();\n\n // Register action handlers using the method actions pattern\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Setup event handling for authentication and wallet lock state\n *\n * Three event sources trigger connection/disconnection:\n * 1. AuthenticationController:stateChange (sign in/out)\n * 2. KeyringController:unlock (wallet unlocked)\n * 3. KeyringController:lock (wallet locked)\n *\n * All connect() calls are idempotent and validate all requirements.\n */\n #subscribeEvents(): void {\n // Subscribe to authentication state changes (sign in/out)\n this.#messenger.subscribe(\n 'AuthenticationController:stateChange',\n (state: AuthenticationController.AuthenticationControllerState) => {\n if (state.isSignedIn) {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.connect();\n } else {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.disconnect();\n }\n },\n (state) => ({ isSignedIn: state.isSignedIn }),\n );\n\n // Subscribe to wallet unlock event\n this.#messenger.subscribe('KeyringController:unlock', () => {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.connect();\n });\n\n // Subscribe to wallet lock event\n this.#messenger.subscribe('KeyringController:lock', () => {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.disconnect();\n });\n }\n\n // =============================================================================\n // 2. PUBLIC API METHODS\n // =============================================================================\n\n /**\n * Establishes WebSocket connection with smart reconnection behavior\n *\n * Connection Requirements (all must be true):\n * 1. Feature enabled (isEnabled() = true)\n * 2. Wallet unlocked (checked by getBearerToken)\n * 3. User signed in (checked by getBearerToken)\n *\n * Platform code should call this when app opens/foregrounds.\n * Automatically called on KeyringController:unlock event.\n *\n * @returns Promise that resolves when connection is established\n */\n async connect(): Promise<void> {\n // Reset manual disconnect flag when explicitly connecting\n this.#manualDisconnect = false;\n\n // Priority 1: Check if feature is enabled via callback (feature flag check)\n // If feature is disabled, stop all connection attempts\n if (this.#isEnabled && !this.#isEnabled()) {\n // Clear any pending reconnection attempts since feature is disabled\n this.#clearTimers();\n this.#reconnectAttempts = 0;\n return;\n }\n\n // If already connected, return immediately\n if (this.#state === WebSocketState.CONNECTED) {\n return;\n }\n\n // If already connecting, wait for the existing connection attempt to complete\n if (this.#state === WebSocketState.CONNECTING && this.#connectionPromise) {\n await this.#connectionPromise;\n return;\n }\n\n // Priority 2: Check authentication requirements (signed in)\n let bearerToken: string;\n try {\n const token = await this.#messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!token) {\n this.#scheduleReconnect();\n return;\n }\n bearerToken = token;\n } catch (error) {\n log('Failed to check authentication requirements', { error });\n\n // Can't connect - schedule retry\n this.#scheduleReconnect();\n return;\n }\n\n this.#setState(WebSocketState.CONNECTING);\n\n // Create and store the connection promise\n this.#connectionPromise = this.#establishConnection(bearerToken);\n\n try {\n await this.#connectionPromise;\n } catch (error) {\n const errorMessage = getErrorMessage(error);\n log('Connection attempt failed', { errorMessage, error });\n this.#setState(WebSocketState.ERROR);\n\n // Rethrow to propagate error to caller\n throw error;\n } finally {\n // Clear the connection promise when done (success or failure)\n this.#connectionPromise = null;\n }\n }\n\n /**\n * Closes WebSocket connection\n *\n * @returns Promise that resolves when disconnection is complete\n */\n async disconnect(): Promise<void> {\n if (\n this.#state === WebSocketState.DISCONNECTED ||\n this.#state === WebSocketState.DISCONNECTING\n ) {\n return;\n }\n\n // Mark this as a manual disconnect to prevent automatic reconnection\n this.#manualDisconnect = true;\n\n this.#setState(WebSocketState.DISCONNECTING);\n this.#clearTimers();\n this.#clearPendingRequests(new Error('WebSocket disconnected'));\n\n // Clear any pending connection promise\n this.#connectionPromise = null;\n\n if (this.#ws) {\n this.#ws.close(1000, 'Normal closure');\n }\n\n log('WebSocket manually disconnected');\n }\n\n /**\n * Sends a message through the WebSocket (fire-and-forget, no response expected)\n *\n * This is a low-level method for sending messages without waiting for a response.\n * Most consumers should use `sendRequest()` instead, which handles request-response\n * correlation and provides proper error handling with timeouts.\n *\n * Use this method only when:\n * - You don't need a response from the server\n * - You're implementing custom message protocols\n * - You need fine-grained control over message timing\n *\n * @param message - The message to send\n * @throws Error if WebSocket is not connected or send fails\n *\n * @see sendRequest for request-response pattern with automatic correlation\n */\n sendMessage(message: ClientRequestMessage): void {\n if (this.#state !== WebSocketState.CONNECTED || !this.#ws) {\n throw new Error(`Cannot send message: WebSocket is ${this.#state}`);\n }\n\n try {\n this.#ws.send(JSON.stringify(message));\n } catch (error) {\n const errorMessage = getErrorMessage(error);\n this.#handleError(new Error(errorMessage));\n throw new Error(errorMessage);\n }\n }\n\n /**\n * Sends a request and waits for a correlated response (recommended for most use cases)\n *\n * This is the recommended high-level method for request-response communication.\n * It automatically handles:\n * - Request ID generation and correlation\n * - Response matching with timeout protection\n * - Automatic reconnection on timeout\n * - Proper cleanup of pending requests\n *\n * @param message - The request message (can include optional requestId for testing)\n * @returns Promise that resolves with the response data\n * @throws Error if WebSocket is not connected, request times out, or response indicates failure\n *\n * @see sendMessage for fire-and-forget messaging without response handling\n */\n async sendRequest<T = ServerResponseMessage['data']>(\n message: Omit<ClientRequestMessage, 'data'> & {\n data?: Omit<ClientRequestMessage['data'], 'requestId'> & {\n requestId?: string;\n };\n },\n ): Promise<T> {\n if (this.#state !== WebSocketState.CONNECTED) {\n throw new Error(`Cannot send request: WebSocket is ${this.#state}`);\n }\n\n // Use provided requestId if available, otherwise generate a new one\n const requestId = message.data?.requestId ?? uuidV4();\n const requestMessage: ClientRequestMessage = {\n event: message.event,\n data: {\n ...message.data,\n requestId, // Set after spread to ensure it's not overwritten by undefined\n },\n };\n\n return new Promise((resolve, reject) => {\n const timeout = setTimeout(() => {\n this.#pendingRequests.delete(requestId);\n log('Request timeout - triggering reconnection', {\n timeout: this.#options.requestTimeout,\n });\n\n // Trigger reconnection on request timeout as it may indicate stale connection\n if (this.#state === WebSocketState.CONNECTED && this.#ws) {\n // Force close the current connection to trigger reconnection logic\n this.#ws.close(3000, 'Request timeout - forcing reconnect');\n }\n\n reject(\n new Error(`Request timeout after ${this.#options.requestTimeout}ms`),\n );\n }, this.#options.requestTimeout);\n\n // Store in pending requests for response correlation\n this.#pendingRequests.set(requestId, {\n resolve: resolve as (value: unknown) => void,\n reject,\n timeout,\n });\n\n // Send the request\n try {\n this.sendMessage(requestMessage);\n } catch (error) {\n this.#pendingRequests.delete(requestId);\n clearTimeout(timeout);\n reject(error instanceof Error ? error : new Error(String(error)));\n }\n });\n }\n\n /**\n * Gets current connection information\n *\n * @returns Current connection status and details\n */\n getConnectionInfo(): WebSocketConnectionInfo {\n return {\n state: this.#state,\n url: this.#options.url,\n timeout: this.#options.timeout,\n reconnectDelay: this.#options.reconnectDelay,\n maxReconnectDelay: this.#options.maxReconnectDelay,\n requestTimeout: this.#options.requestTimeout,\n reconnectAttempts: this.#reconnectAttempts,\n connectedAt: this.#connectedAt,\n };\n }\n\n /**\n * Gets all subscription information for a specific channel\n *\n * @param channel - The channel name to look up\n * @returns Array of subscription details for all subscriptions containing the channel\n */\n getSubscriptionsByChannel(channel: string): WebSocketSubscription[] {\n const matchingSubscriptions: WebSocketSubscription[] = [];\n for (const [subscriptionId, subscription] of this.#subscriptions) {\n if (subscription.channels.includes(channel)) {\n matchingSubscriptions.push({\n subscriptionId,\n channels: subscription.channels,\n channelType: subscription.channelType,\n unsubscribe: subscription.unsubscribe,\n });\n }\n }\n return matchingSubscriptions;\n }\n\n /**\n * Checks if a channel has a subscription\n *\n * @param channel - The channel name to check\n * @returns True if the channel has a subscription, false otherwise\n */\n channelHasSubscription(channel: string): boolean {\n for (const subscription of this.#subscriptions.values()) {\n if (subscription.channels.includes(channel)) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * Finds all subscriptions that have channels starting with the specified prefix\n *\n * @param channelPrefix - The channel prefix to search for (e.g., \"account-activity.v1\")\n * @returns Array of subscription info for matching subscriptions\n */\n findSubscriptionsByChannelPrefix(\n channelPrefix: string,\n ): WebSocketSubscription[] {\n const matchingSubscriptions: WebSocketSubscription[] = [];\n\n for (const [subscriptionId, subscription] of this.#subscriptions) {\n // Check if any channel in this subscription starts with the prefix\n const hasMatchingChannel = subscription.channels.some((channel) =>\n channel.startsWith(channelPrefix),\n );\n\n if (hasMatchingChannel) {\n matchingSubscriptions.push({\n subscriptionId,\n channels: subscription.channels,\n channelType: subscription.channelType,\n unsubscribe: subscription.unsubscribe,\n });\n }\n }\n\n return matchingSubscriptions;\n }\n\n /**\n * Register a callback for specific channels (local callback only, no server subscription)\n *\n * **Key Difference from `subscribe()`:**\n * - `addChannelCallback()`: Registers a local callback without creating a server-side subscription.\n * The callback triggers on ANY message matching the channel name, regardless of subscriptionId.\n * Useful for system-wide notifications or when you don't control the subscription lifecycle.\n *\n * - `subscribe()`: Creates a proper server-side subscription with a subscriptionId.\n * The callback only triggers for messages with the matching subscriptionId.\n * Includes proper lifecycle management (unsubscribe, automatic cleanup on disconnect).\n *\n * **When to use `addChannelCallback()`:**\n * - Listening to system-wide notifications (e.g., 'system-notifications.v1')\n * - Monitoring channels where subscriptions are managed elsewhere\n * - Debug/logging scenarios where you want to observe all channel messages\n *\n * **When to use `subscribe()` instead:**\n * - Creating new subscriptions that need server-side registration\n * - When you need proper cleanup via unsubscribe\n * - Most application use cases (recommended approach)\n *\n * @param options - Channel callback configuration\n * @param options.channelName - Channel name to match exactly\n * @param options.callback - Function to call when channel matches\n *\n * @example\n * ```typescript\n * // Listen to system notifications (no server subscription needed)\n * webSocketService.addChannelCallback({\n * channelName: 'system-notifications.v1',\n * callback: (notification) => {\n * console.log('System notification:', notification.data);\n * }\n * });\n *\n * // For account-specific subscriptions, use subscribe() instead:\n * // const sub = await webSocketService.subscribe({\n * // channels: ['account-activity.v1.eip155:0:0x1234...'],\n * // callback: (notification) => { ... }\n * // });\n * ```\n *\n * @see subscribe for creating proper server-side subscriptions with lifecycle management\n */\n addChannelCallback(options: {\n channelName: string;\n callback: (notification: ServerNotificationMessage) => void;\n }): void {\n const channelCallback: ChannelCallback = {\n channelName: options.channelName,\n callback: options.callback,\n };\n\n // Check if callback already exists for this channel\n if (this.#channelCallbacks.has(options.channelName)) {\n return;\n }\n\n this.#channelCallbacks.set(options.channelName, channelCallback);\n }\n\n /**\n * Remove a channel callback\n *\n * @param channelName - The channel name returned from addChannelCallback\n * @returns True if callback was found and removed, false otherwise\n */\n removeChannelCallback(channelName: string): boolean {\n return this.#channelCallbacks.delete(channelName);\n }\n\n /**\n * Get all registered channel callbacks (for debugging)\n *\n * @returns Array of all registered channel callbacks\n */\n getChannelCallbacks(): ChannelCallback[] {\n return Array.from(this.#channelCallbacks.values());\n }\n\n /**\n * Destroy the service and clean up resources\n * Called when service is being destroyed or app is terminating\n */\n destroy(): void {\n this.#clearTimers();\n this.#clearSubscriptions();\n\n // Clear any pending connection promise\n this.#connectionPromise = null;\n\n // Clear all pending requests\n this.#clearPendingRequests(new Error('Service cleanup'));\n\n if (this.#ws && this.#ws.readyState === WebSocket.OPEN) {\n this.#ws.close(1000, 'Service cleanup');\n }\n\n // Set state to disconnected immediately\n this.#setState(WebSocketState.DISCONNECTED);\n }\n\n /**\n * Create and manage a subscription with server-side registration (recommended for most use cases)\n *\n * This is the recommended subscription API for high-level services. It creates a proper\n * server-side subscription and routes notifications based on subscriptionId.\n *\n * **Key Features:**\n * - Creates server-side subscription with unique subscriptionId\n * - Callback triggered only for messages with matching subscriptionId\n * - Automatic lifecycle management (cleanup on disconnect)\n * - Includes unsubscribe method for proper cleanup\n * - Request-response pattern with error handling\n *\n * **When to use `subscribe()`:**\n * - Creating new subscriptions (account activity, price updates, etc.)\n * - When you need proper cleanup/unsubscribe functionality\n * - Most application use cases\n *\n * **When to use `addChannelCallback()` instead:**\n * - System-wide notifications without server-side subscription\n * - Observing channels managed elsewhere\n * - Debug/logging scenarios\n *\n * @param options - Subscription configuration\n * @param options.channels - Array of channel names to subscribe to\n * @param options.callback - Callback function for handling notifications\n * @param options.requestId - Optional request ID for testing (will generate UUID if not provided)\n * @param options.channelType - Channel type identifier\n * @returns Subscription object with unsubscribe method\n *\n * @example\n * ```typescript\n * // AccountActivityService usage\n * const subscription = await webSocketService.subscribe({\n * channels: ['account-activity.v1.eip155:0:0x1234...'],\n * callback: (notification) => {\n * this.handleAccountActivity(notification.data);\n * }\n * });\n *\n * // Later, clean up\n * await subscription.unsubscribe();\n * ```\n *\n * @see addChannelCallback for local callbacks without server-side subscription\n */\n async subscribe(options: {\n /** Channel names to subscribe to */\n channels: string[];\n /** Channel type with version (e.g., 'account-activity.v1') for tracing and monitoring */\n channelType: string;\n /** Handler for incoming notifications */\n callback: (notification: ServerNotificationMessage) => void;\n /** Optional request ID for testing (will generate UUID if not provided) */\n requestId?: string;\n }): Promise<WebSocketSubscription> {\n const { channels, channelType, callback, requestId } = options;\n\n if (this.#state !== WebSocketState.CONNECTED) {\n throw new Error(\n `Cannot create subscription(s) ${channels.join(', ')}: WebSocket is ${this.#state}`,\n );\n }\n\n // Send subscription request and wait for response\n const subscriptionResponse = await this.sendRequest({\n event: 'subscribe',\n data: { channels, requestId },\n });\n\n if (!subscriptionResponse?.subscriptionId) {\n throw new Error('Invalid subscription response: missing subscription ID');\n }\n\n const { subscriptionId } = subscriptionResponse;\n\n // Create unsubscribe function\n const unsubscribe = async (unsubRequestId?: string): Promise<void> => {\n // Send unsubscribe request first\n await this.sendRequest({\n event: 'unsubscribe',\n data: {\n subscription: subscriptionId,\n channels,\n requestId: unsubRequestId,\n },\n });\n\n // Clean up subscription mapping\n this.#subscriptions.delete(subscriptionId);\n };\n\n const subscription = {\n subscriptionId,\n channels: [...channels],\n channelType,\n unsubscribe,\n };\n\n // Store subscription with subscription ID as key\n this.#subscriptions.set(subscriptionId, {\n subscriptionId,\n channels: [...channels], // Store copy of channels\n channelType,\n callback,\n unsubscribe,\n });\n\n return subscription;\n }\n\n // =============================================================================\n // 3. CONNECTION MANAGEMENT (PRIVATE)\n // =============================================================================\n\n /**\n * Builds an authenticated WebSocket URL with bearer token as query parameter.\n * Uses query parameter for WebSocket authentication since native WebSocket\n * doesn't support custom headers during handshake.\n *\n * @param bearerToken - The bearer token to use for authentication\n * @returns The authenticated WebSocket URL\n */\n #buildAuthenticatedUrl(bearerToken: string): string {\n const baseUrl = this.#options.url;\n\n // Add token as query parameter to the WebSocket URL\n const url = new URL(baseUrl);\n url.searchParams.set('token', bearerToken);\n\n return url.toString();\n }\n\n /**\n * Establishes the actual WebSocket connection\n *\n * @param bearerToken - The bearer token to use for authentication\n * @returns Promise that resolves when connection is established\n */\n async #establishConnection(bearerToken: string): Promise<void> {\n const wsUrl = this.#buildAuthenticatedUrl(bearerToken);\n const connectionStartTime = Date.now();\n\n return new Promise<void>((resolve, reject) => {\n const ws = new WebSocket(wsUrl);\n this.#connectionTimeout = setTimeout(() => {\n log('WebSocket connection timeout - forcing close', {\n timeout: this.#options.timeout,\n });\n ws.close();\n reject(\n new Error(\n `Failed to connect to WebSocket: Connection timeout after ${this.#options.timeout}ms`,\n ),\n );\n }, this.#options.timeout);\n\n ws.onopen = () => {\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n\n // Calculate connection latency\n const connectionLatency = Date.now() - connectionStartTime;\n\n // Trace successful connection with latency\n this.#trace(\n {\n name: `${SERVICE_NAME} Connection`,\n data: {\n reconnectAttempt: this.#reconnectAttempts,\n latency_ms: connectionLatency,\n },\n tags: {\n service: SERVICE_NAME,\n },\n },\n () => {\n this.#ws = ws;\n this.#setState(WebSocketState.CONNECTED);\n this.#connectedAt = Date.now();\n\n // Reset reconnect attempts on successful connection\n this.#reconnectAttempts = 0;\n\n resolve();\n },\n );\n };\n\n ws.onerror = (event: Event) => {\n log('WebSocket onerror event triggered', { event });\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n const error = new Error(`WebSocket connection error to ${wsUrl}`);\n reject(error);\n };\n\n ws.onclose = (event: CloseEvent) => {\n log('WebSocket onclose event triggered', {\n code: event.code,\n reason: event.reason,\n wasClean: event.wasClean,\n });\n if (this.#state === WebSocketState.CONNECTING) {\n // Handle connection-phase close events\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n reject(\n new Error(\n `WebSocket connection closed during connection: ${event.code} ${event.reason}`,\n ),\n );\n } else {\n this.#handleClose(event);\n }\n };\n\n // Set up message handler immediately - no need to wait for connection\n ws.onmessage = (event: MessageEvent) => {\n try {\n const message = this.#parseMessage(event.data);\n this.#handleMessage(message);\n } catch {\n // Silently ignore invalid JSON messages\n }\n };\n });\n }\n\n // =============================================================================\n // 4. MESSAGE HANDLING (PRIVATE)\n // =============================================================================\n\n /**\n * Handles incoming WebSocket messages\n *\n * @param message - The WebSocket message to handle\n */\n #handleMessage(message: WebSocketMessage): void {\n // Handle server responses (correlated with requests) first\n if (this.#isServerResponse(message)) {\n this.#handleServerResponse(message);\n return;\n }\n\n // Handle subscription notifications with valid subscriptionId\n if (this.#isSubscriptionNotification(message)) {\n const notificationMsg = message as ServerNotificationMessage;\n const handled = this.#handleSubscriptionNotification(notificationMsg);\n // If subscription notification wasn't handled (falsy subscriptionId), fall through to channel handling\n if (handled) {\n return;\n }\n }\n\n // Trigger channel callbacks for any message with a channel property\n if (this.#isChannelMessage(message)) {\n const channelMsg = message as ServerNotificationMessage;\n this.#handleChannelMessage(channelMsg);\n }\n }\n\n /**\n * Checks if a message is a server response (correlated with client requests)\n *\n * @param message - The message to check\n * @returns True if the message is a server response\n */\n #isServerResponse(\n message: WebSocketMessage,\n ): message is ServerResponseMessage {\n return (\n 'data' in message &&\n message.data &&\n typeof message.data === 'object' &&\n 'requestId' in message.data\n );\n }\n\n /**\n * Checks if a message is a subscription notification (has subscriptionId)\n *\n * @param message - The message to check\n * @returns True if the message is a subscription notification with subscriptionId\n */\n #isSubscriptionNotification(message: WebSocketMessage): boolean {\n return 'subscriptionId' in message && !this.#isServerResponse(message);\n }\n\n /**\n * Checks if a message has a channel property (system or subscription notification)\n *\n * @param message - The message to check\n * @returns True if the message has a channel property\n */\n #isChannelMessage(\n message: WebSocketMessage,\n ): message is ServerNotificationMessage {\n return 'channel' in message;\n }\n\n /**\n * Handles server response messages (correlated with client requests)\n *\n * @param message - The server response message to handle\n */\n #handleServerResponse(message: ServerResponseMessage): void {\n const { requestId } = message.data;\n\n const request = this.#pendingRequests.get(requestId);\n if (!request) {\n return;\n }\n\n this.#pendingRequests.delete(requestId);\n clearTimeout(request.timeout);\n\n // Check if the response indicates failure\n if (message.data.failed && message.data.failed.length > 0) {\n request.reject(\n new Error(`Request failed: ${message.data.failed.join(', ')}`),\n );\n } else {\n request.resolve(message.data);\n }\n }\n\n /**\n * Handles messages with channel properties by triggering channel callbacks\n *\n * @param message - The message with channel property to handle\n */\n #handleChannelMessage(message: ServerNotificationMessage): void {\n if (this.#channelCallbacks.size === 0) {\n return;\n }\n\n // Calculate notification latency: time from server sent to client received\n const receivedAt = Date.now();\n const latency = receivedAt - message.timestamp;\n\n // Trace channel message processing with latency data\n this.#trace(\n {\n name: `${SERVICE_NAME} Channel Message`,\n data: {\n channel: message.channel,\n latency_ms: latency,\n event: message.event,\n },\n tags: {\n service: SERVICE_NAME,\n channel_type: message.channel,\n },\n },\n () => {\n // Direct lookup for exact channel match\n this.#channelCallbacks.get(message.channel)?.callback(message);\n },\n );\n }\n\n /**\n * Handles server notifications with subscription IDs\n *\n * @param message - The server notification message to handle\n * @returns True if the message was handled, false if it should fall through to channel handling\n */\n #handleSubscriptionNotification(message: ServerNotificationMessage): boolean {\n const { subscriptionId, timestamp, channel } = message;\n\n // Only handle if subscriptionId is defined and not null (allows \"0\" as valid ID)\n if (subscriptionId !== null && subscriptionId !== undefined) {\n const subscription = this.#subscriptions.get(subscriptionId);\n if (!subscription) {\n return false;\n }\n\n // Calculate notification latency: time from server sent to client received\n const receivedAt = Date.now();\n const latency = receivedAt - timestamp;\n\n // Trace notification processing with latency data\n // Use stored channelType instead of parsing each time\n this.#trace(\n {\n name: `${SERVICE_NAME} Notification`,\n data: {\n channel,\n latency_ms: latency,\n subscriptionId,\n },\n tags: {\n service: SERVICE_NAME,\n notification_type: subscription.channelType,\n },\n },\n () => {\n subscription.callback?.(message);\n },\n );\n return true;\n }\n\n return false;\n }\n\n /**\n * Parse WebSocket message data\n *\n * @param data - The raw message data to parse\n * @returns Parsed message\n */\n #parseMessage(data: string): WebSocketMessage {\n return JSON.parse(data);\n }\n\n // =============================================================================\n // 5. EVENT HANDLERS (PRIVATE)\n // =============================================================================\n\n /**\n * Handles WebSocket close events (mobile optimized)\n *\n * @param event - The WebSocket close event\n */\n #handleClose(event: CloseEvent): void {\n // Calculate connection duration before we clear state\n const connectionDuration = Date.now() - this.#connectedAt;\n\n this.#clearTimers();\n this.#connectedAt = 0;\n\n // Clear any pending connection promise\n this.#connectionPromise = null;\n\n // Clear subscriptions and pending requests on any disconnect\n // This ensures clean state for reconnection\n this.#clearPendingRequests(new Error('WebSocket connection closed'));\n this.#clearSubscriptions();\n\n // Update state to disconnected\n this.#setState(WebSocketState.DISCONNECTED);\n\n // Check if this was a manual disconnect\n if (this.#manualDisconnect) {\n // Manual disconnect - don't reconnect\n return;\n }\n\n // Trace unexpected disconnect with details\n this.#trace(\n {\n name: `${SERVICE_NAME} Disconnect`,\n data: {\n code: event.code,\n reason: event.reason || getCloseReason(event.code),\n connectionDuration_ms: connectionDuration,\n },\n tags: {\n service: SERVICE_NAME,\n disconnect_type: 'unexpected',\n },\n },\n () => {\n // Empty trace callback - just measuring the event\n },\n );\n\n // For any unexpected disconnects, attempt reconnection\n // The manualDisconnect flag is the only gate - if it's false, we reconnect\n this.#scheduleReconnect();\n }\n\n /**\n * Handles WebSocket errors\n *\n * @param _error - Error that occurred (unused)\n */\n #handleError(_error: Error): void {\n // Placeholder for future error handling logic\n }\n\n // =============================================================================\n // 6. STATE MANAGEMENT (PRIVATE)\n // =============================================================================\n\n /**\n * Schedules a reconnection attempt with exponential backoff\n */\n #scheduleReconnect(): void {\n this.#reconnectAttempts += 1;\n\n const rawDelay =\n this.#options.reconnectDelay * Math.pow(1.5, this.#reconnectAttempts - 1);\n const delay = Math.min(rawDelay, this.#options.maxReconnectDelay);\n\n this.#reconnectTimer = setTimeout(() => {\n // Clear timer reference first\n this.#reconnectTimer = null;\n\n // Check if connection is still enabled before reconnecting\n if (this.#isEnabled && !this.#isEnabled()) {\n this.#reconnectAttempts = 0;\n return;\n }\n\n // Attempt to reconnect - if it fails, schedule another attempt\n this.connect().catch(() => {\n this.#scheduleReconnect();\n });\n }, delay);\n }\n\n /**\n * Clears all active timers\n */\n #clearTimers(): void {\n if (this.#reconnectTimer) {\n clearTimeout(this.#reconnectTimer);\n this.#reconnectTimer = null;\n }\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n }\n\n /**\n * Clears all pending requests and rejects them with the given error\n *\n * @param error - Error to reject with\n */\n #clearPendingRequests(error: Error): void {\n for (const [, request] of this.#pendingRequests) {\n clearTimeout(request.timeout);\n request.reject(error);\n }\n this.#pendingRequests.clear();\n }\n\n /**\n * Clears all active subscriptions\n */\n #clearSubscriptions(): void {\n this.#subscriptions.clear();\n }\n\n /**\n * Sets the connection state and emits state change events\n *\n * @param newState - The new WebSocket state\n */\n #setState(newState: WebSocketState): void {\n const oldState = this.#state;\n this.#state = newState;\n\n if (oldState !== newState) {\n // Publish connection state change event\n // Messenger handles listener errors internally, no need for try-catch\n this.#messenger.publish(\n 'BackendWebSocketService:connectionStateChanged',\n this.getConnectionInfo(),\n );\n }\n }\n}\n"]}
1
+ {"version":3,"file":"BackendWebSocketService.cjs","sourceRoot":"","sources":["../src/BackendWebSocketService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,2CAAkD;AAClD,+BAAoC;AAGpC,yCAA6D;AAE7D,MAAM,YAAY,GAAG,yBAAkC,CAAC;AAExD,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,yBAAyB,GAAG;IAChC,SAAS;IACT,YAAY;IACZ,aAAa;IACb,aAAa;IACb,WAAW;IACX,mBAAmB;IACnB,2BAA2B;IAC3B,wBAAwB;IACxB,kCAAkC;IAClC,oBAAoB;IACpB,uBAAuB;IACvB,qBAAqB;CACb,CAAC;AAEX;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,IAAY;IACzC,QAAQ,IAAI,EAAE;QACZ,KAAK,IAAI;YACP,OAAO,gBAAgB,CAAC;QAC1B,KAAK,IAAI;YAC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,EAAE,KAAK,CAAC,MAAA,CAAC;AACZ,CAAC;IAMC,IAAI,uBAAA,IAAI,+CAAgB,EAAE;QACxB,YAAY,CAAC,uBAAA,IAAI,+CAAgB,CAAC,CAAC;QACnC,uBAAA,IAAI,2CAAmB,IAAI,MAAA,CAAC;KAC7B;IACD,IAAI,uBAAA,IAAI,kDAAmB,EAAE;QAC3B,YAAY,CAAC,uBAAA,IAAI,kDAAmB,CAAC,CAAC;QACtC,uBAAA,IAAI,8CAAsB,IAAI,MAAA,CAAC;KAChC;AACH,CAAC,yGAOqB,KAAY;IAChC,KAAK,MAAM,CAAC,EAAE,OAAO,CAAC,IAAI,uBAAA,IAAI,gDAAiB,EAAE;QAC/C,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC9B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;KACvB;IACD,uBAAA,IAAI,gDAAiB,CAAC,KAAK,EAAE,CAAC;AAChC,CAAC;IAMC,uBAAA,IAAI,8CAAe,CAAC,KAAK,EAAE,CAAC;AAC9B,CAAC,iFAOS,QAAwB;IAChC,MAAM,QAAQ,GAAG,uBAAA,IAAI,sCAAO,CAAC;IAC7B,uBAAA,IAAI,kCAAU,QAAQ,MAAA,CAAC;IAEvB,IAAI,QAAQ,KAAK,QAAQ,EAAE;QACzB,wCAAwC;QACxC,sEAAsE;QACtE,uBAAA,IAAI,0CAAW,CAAC,OAAO,CACrB,gDAAgD,EAChD,IAAI,CAAC,iBAAiB,EAAE,CACzB,CAAC;KACH;AACH,CAAC","sourcesContent":["import type { RestrictedMessenger } from '@metamask/base-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\nimport { getErrorMessage } from '@metamask/utils';\nimport { v4 as uuidV4 } from 'uuid';\n\nimport type { BackendWebSocketServiceMethodActions } from './BackendWebSocketService-method-action-types';\nimport { projectLogger, createModuleLogger } from './logger';\n\nconst SERVICE_NAME = 'BackendWebSocketService' as const;\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'connect',\n 'disconnect',\n 'sendMessage',\n 'sendRequest',\n 'subscribe',\n 'getConnectionInfo',\n 'getSubscriptionsByChannel',\n 'channelHasSubscription',\n 'findSubscriptionsByChannelPrefix',\n 'addChannelCallback',\n 'removeChannelCallback',\n 'getChannelCallbacks',\n] as const;\n\n/**\n * Gets human-readable close reason from RFC 6455 close code\n *\n * @param code - WebSocket close code\n * @returns Human-readable close reason\n */\nexport function getCloseReason(code: number): string {\n switch (code) {\n case 1000:\n return 'Normal Closure';\n case 1001:\n return 'Going Away';\n case 1002:\n return 'Protocol Error';\n case 1003:\n return 'Unsupported Data';\n case 1004:\n return 'Reserved';\n case 1005:\n return 'No Status Received';\n case 1006:\n return 'Abnormal Closure';\n case 1007:\n return 'Invalid frame payload data';\n case 1008:\n return 'Policy Violation';\n case 1009:\n return 'Message Too Big';\n case 1010:\n return 'Mandatory Extension';\n case 1011:\n return 'Internal Server Error';\n case 1012:\n return 'Service Restart';\n case 1013:\n return 'Try Again Later';\n case 1014:\n return 'Bad Gateway';\n case 1015:\n return 'TLS Handshake';\n default:\n if (code >= 3000 && code <= 3999) {\n return 'Library/Framework Error';\n }\n if (code >= 4000 && code <= 4999) {\n return 'Application Error';\n }\n return 'Unknown';\n }\n}\n\n/**\n * WebSocket connection states\n */\nexport enum WebSocketState {\n CONNECTING = 'connecting',\n CONNECTED = 'connected',\n DISCONNECTING = 'disconnecting',\n DISCONNECTED = 'disconnected',\n ERROR = 'error',\n}\n\n/**\n * WebSocket event types\n */\nexport enum WebSocketEventType {\n CONNECTED = 'connected',\n DISCONNECTED = 'disconnected',\n MESSAGE = 'message',\n ERROR = 'error',\n RECONNECTING = 'reconnecting',\n RECONNECTED = 'reconnected',\n}\n\n/**\n * Configuration options for the WebSocket service\n */\nexport type BackendWebSocketServiceOptions = {\n /** The WebSocket URL to connect to */\n url: string;\n\n /** The messenger for inter-service communication */\n messenger: BackendWebSocketServiceMessenger;\n\n /** Connection timeout in milliseconds (default: 10000) */\n timeout?: number;\n\n /** Initial reconnection delay in milliseconds (default: 500) */\n reconnectDelay?: number;\n\n /** Maximum reconnection delay in milliseconds (default: 5000) */\n maxReconnectDelay?: number;\n\n /** Request timeout in milliseconds (default: 30000) */\n requestTimeout?: number;\n\n /** Optional callback to determine if connection should be enabled (default: always enabled) */\n isEnabled?: () => boolean;\n\n /** Optional callback to trace performance of WebSocket operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n/**\n * Client Request message\n * Used when client sends a request to the server\n */\nexport type ClientRequestMessage = {\n event: string;\n data: {\n requestId: string;\n channels?: string[];\n [key: string]: unknown;\n };\n};\n\n/**\n * Server Response message\n * Used when server responds to a client request\n */\nexport type ServerResponseMessage = {\n event: string;\n data: {\n requestId: string;\n subscriptionId?: string;\n succeeded?: string[];\n failed?: string[];\n [key: string]: unknown;\n };\n};\n\n/**\n * Server Notification message\n * Used when server sends unsolicited data to client\n * subscriptionId is optional for system-wide notifications\n */\nexport type ServerNotificationMessage = {\n event: string;\n subscriptionId?: string;\n channel: string;\n data: Record<string, unknown>;\n timestamp: number;\n};\n\n/**\n * Union type for all WebSocket messages\n */\nexport type WebSocketMessage =\n | ClientRequestMessage\n | ServerResponseMessage\n | ServerNotificationMessage;\n\n/**\n * Channel-based callback configuration\n */\nexport type ChannelCallback = {\n /** Channel name to match (also serves as the unique identifier) */\n channelName: string;\n /** Callback function */\n callback: (notification: ServerNotificationMessage) => void;\n};\n\n/**\n * Unified WebSocket subscription object used for both internal storage and external API\n */\nexport type WebSocketSubscription = {\n /** The subscription ID from the server */\n subscriptionId: string;\n /** Channel names for this subscription */\n channels: string[];\n /** Channel type with version (e.g., 'account-activity.v1') extracted from first channel */\n channelType: string;\n /** Callback function for handling notifications (optional for external use) */\n callback?: (notification: ServerNotificationMessage) => void;\n /** Function to unsubscribe and clean up */\n unsubscribe: (requestId?: string) => Promise<void>;\n};\n\n/**\n * WebSocket connection info\n */\nexport type WebSocketConnectionInfo = {\n state: WebSocketState;\n url: string;\n reconnectAttempts: number;\n timeout: number;\n reconnectDelay: number;\n maxReconnectDelay: number;\n requestTimeout: number;\n connectedAt?: number;\n};\n\n// Action types for the messaging system - using generated method actions\nexport type BackendWebSocketServiceActions =\n BackendWebSocketServiceMethodActions;\n\nexport type BackendWebSocketServiceAllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\nexport type BackendWebSocketServiceAllowedEvents =\n | AuthenticationController.AuthenticationControllerStateChangeEvent\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent;\n\n// Event types for WebSocket connection state changes\nexport type BackendWebSocketServiceConnectionStateChangedEvent = {\n type: 'BackendWebSocketService:connectionStateChanged';\n payload: [WebSocketConnectionInfo];\n};\n\nexport type BackendWebSocketServiceEvents =\n BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type BackendWebSocketServiceMessenger = RestrictedMessenger<\n typeof SERVICE_NAME,\n BackendWebSocketServiceActions | BackendWebSocketServiceAllowedActions,\n BackendWebSocketServiceEvents | BackendWebSocketServiceAllowedEvents,\n BackendWebSocketServiceAllowedActions['type'],\n BackendWebSocketServiceAllowedEvents['type']\n>;\n\n/**\n * WebSocket Service with automatic reconnection, session management and direct callback routing\n *\n * Connection Management:\n * - Automatically subscribes to AuthenticationController:stateChange (sign in/out)\n * - Automatically subscribes to KeyringController:lock/unlock events\n * - Idempotent connect() function safe for multiple rapid calls\n * - Auto-reconnects on unexpected disconnects (manualDisconnect = false)\n *\n * Platform Responsibilities:\n * - Call connect() when app opens/foregrounds\n * - Call disconnect() when app closes/backgrounds\n * - Provide isEnabled() callback (feature flag)\n * - Call destroy() on app termination\n *\n * Real-Time Performance Optimizations:\n * - Fast path message routing (zero allocations)\n * - Production mode removes try-catch overhead\n * - Optimized JSON parsing with fail-fast\n * - Direct callback routing bypasses event emitters\n * - Memory cleanup and resource management\n */\nexport class BackendWebSocketService {\n /**\n * The name of the service.\n */\n readonly name = SERVICE_NAME;\n\n readonly #messenger: BackendWebSocketServiceMessenger;\n\n readonly #options: Required<\n Omit<BackendWebSocketServiceOptions, 'messenger' | 'isEnabled' | 'traceFn'>\n >;\n\n readonly #isEnabled: (() => boolean) | undefined;\n\n readonly #trace: TraceCallback;\n\n #ws: WebSocket | undefined;\n\n #state: WebSocketState = WebSocketState.DISCONNECTED;\n\n #reconnectAttempts = 0;\n\n #reconnectTimer: NodeJS.Timeout | null = null;\n\n #connectionTimeout: NodeJS.Timeout | null = null;\n\n // Track the current connection promise to handle concurrent connection attempts\n #connectionPromise: Promise<void> | null = null;\n\n readonly #pendingRequests = new Map<\n string,\n {\n resolve: (value: unknown) => void;\n reject: (error: Error) => void;\n timeout: NodeJS.Timeout;\n }\n >();\n\n #connectedAt: number = 0;\n\n // Track manual disconnects to prevent automatic reconnection\n #manualDisconnect = false;\n\n // Simplified subscription storage (single flat map)\n // Key: subscription ID string (e.g., 'sub_abc123def456')\n // Value: WebSocketSubscription object with channels, callback and metadata\n readonly #subscriptions = new Map<string, WebSocketSubscription>();\n\n // Channel-based callback storage\n // Key: channel name (serves as unique identifier)\n // Value: ChannelCallback configuration\n readonly #channelCallbacks = new Map<string, ChannelCallback>();\n\n // =============================================================================\n // 1. CONSTRUCTOR & INITIALIZATION\n // =============================================================================\n\n /**\n * Creates a new WebSocket service instance\n *\n * @param options - Configuration options for the WebSocket service\n */\n constructor(options: BackendWebSocketServiceOptions) {\n this.#messenger = options.messenger;\n this.#isEnabled = options.isEnabled;\n // Default to no-op trace function to keep core platform-agnostic\n this.#trace =\n options.traceFn ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (((_request: any, fn?: any) => fn?.()) as TraceCallback);\n\n this.#options = {\n url: options.url,\n timeout: options.timeout ?? 10000,\n reconnectDelay: options.reconnectDelay ?? 500,\n maxReconnectDelay: options.maxReconnectDelay ?? 5000,\n requestTimeout: options.requestTimeout ?? 30000,\n };\n\n // Subscribe to authentication and keyring controller events\n this.#subscribeEvents();\n\n // Register action handlers using the method actions pattern\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Setup event handling for authentication and wallet lock state\n *\n * Three event sources trigger connection/disconnection:\n * 1. AuthenticationController:stateChange (sign in/out)\n * 2. KeyringController:unlock (wallet unlocked)\n * 3. KeyringController:lock (wallet locked)\n *\n * All connect() calls are idempotent and validate all requirements.\n */\n #subscribeEvents(): void {\n // Subscribe to authentication state changes (sign in/out)\n this.#messenger.subscribe(\n 'AuthenticationController:stateChange',\n (state: AuthenticationController.AuthenticationControllerState) => {\n if (state.isSignedIn) {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.connect();\n } else {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.disconnect();\n }\n },\n (state) => ({ isSignedIn: state.isSignedIn }),\n );\n\n // Subscribe to wallet unlock event\n this.#messenger.subscribe('KeyringController:unlock', () => {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.connect();\n });\n\n // Subscribe to wallet lock event\n this.#messenger.subscribe('KeyringController:lock', () => {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.disconnect();\n });\n }\n\n // =============================================================================\n // 2. PUBLIC API METHODS\n // =============================================================================\n\n /**\n * Establishes WebSocket connection with smart reconnection behavior\n *\n * Connection Requirements (all must be true):\n * 1. Feature enabled (isEnabled() = true)\n * 2. Wallet unlocked (checked by getBearerToken)\n * 3. User signed in (checked by getBearerToken)\n *\n * Platform code should call this when app opens/foregrounds.\n * Automatically called on KeyringController:unlock event.\n *\n * @returns Promise that resolves when connection is established\n */\n async connect(): Promise<void> {\n // Reset manual disconnect flag when explicitly connecting\n this.#manualDisconnect = false;\n\n // Priority 1: Check if feature is enabled via callback (feature flag check)\n // If feature is disabled, stop all connection attempts\n if (this.#isEnabled && !this.#isEnabled()) {\n // Clear any pending reconnection attempts since feature is disabled\n this.#clearTimers();\n this.#reconnectAttempts = 0;\n return;\n }\n\n // If already connected, return immediately\n if (this.#state === WebSocketState.CONNECTED) {\n return;\n }\n\n // If already connecting, wait for the existing connection attempt to complete\n if (this.#connectionPromise) {\n await this.#connectionPromise;\n return;\n }\n\n // Create and store the connection promise IMMEDIATELY (before any async operations)\n // This ensures subsequent connect() calls will wait for this promise instead of creating new connections\n this.#connectionPromise = (async () => {\n // Priority 2: Check authentication requirements (signed in)\n let bearerToken: string;\n try {\n const token = await this.#messenger.call(\n 'AuthenticationController:getBearerToken',\n );\n if (!token) {\n this.#scheduleReconnect();\n throw new Error('Authentication required: user not signed in');\n }\n bearerToken = token;\n } catch (error) {\n log('Failed to check authentication requirements', { error });\n\n // Can't connect - schedule retry\n this.#scheduleReconnect();\n throw error;\n }\n\n this.#setState(WebSocketState.CONNECTING);\n\n // Establish the actual WebSocket connection\n try {\n await this.#establishConnection(bearerToken);\n } catch (error) {\n const errorMessage = getErrorMessage(error);\n log('Connection attempt failed', { errorMessage, error });\n this.#setState(WebSocketState.ERROR);\n\n // Rethrow to propagate error to caller\n throw error;\n }\n })();\n\n try {\n await this.#connectionPromise;\n } finally {\n // Clear the connection promise when done (success or failure)\n this.#connectionPromise = null;\n }\n }\n\n /**\n * Closes WebSocket connection\n *\n * @returns Promise that resolves when disconnection is complete\n */\n async disconnect(): Promise<void> {\n if (\n this.#state === WebSocketState.DISCONNECTED ||\n this.#state === WebSocketState.DISCONNECTING\n ) {\n return;\n }\n\n // Mark this as a manual disconnect to prevent automatic reconnection\n this.#manualDisconnect = true;\n\n this.#setState(WebSocketState.DISCONNECTING);\n this.#clearTimers();\n this.#clearPendingRequests(new Error('WebSocket disconnected'));\n\n // Clear any pending connection promise\n this.#connectionPromise = null;\n\n if (this.#ws) {\n this.#ws.close(1000, 'Normal closure');\n }\n\n log('WebSocket manually disconnected');\n }\n\n /**\n * Sends a message through the WebSocket (fire-and-forget, no response expected)\n *\n * This is a low-level method for sending messages without waiting for a response.\n * Most consumers should use `sendRequest()` instead, which handles request-response\n * correlation and provides proper error handling with timeouts.\n *\n * Use this method only when:\n * - You don't need a response from the server\n * - You're implementing custom message protocols\n * - You need fine-grained control over message timing\n *\n * @param message - The message to send\n * @throws Error if WebSocket is not connected or send fails\n *\n * @see sendRequest for request-response pattern with automatic correlation\n */\n sendMessage(message: ClientRequestMessage): void {\n if (this.#state !== WebSocketState.CONNECTED || !this.#ws) {\n throw new Error(`Cannot send message: WebSocket is ${this.#state}`);\n }\n\n try {\n this.#ws.send(JSON.stringify(message));\n } catch (error) {\n const errorMessage = getErrorMessage(error);\n this.#handleError(new Error(errorMessage));\n throw new Error(errorMessage);\n }\n }\n\n /**\n * Sends a request and waits for a correlated response (recommended for most use cases)\n *\n * This is the recommended high-level method for request-response communication.\n * It automatically handles:\n * - Request ID generation and correlation\n * - Response matching with timeout protection\n * - Automatic reconnection on timeout\n * - Proper cleanup of pending requests\n *\n * @param message - The request message (can include optional requestId for testing)\n * @returns Promise that resolves with the response data\n * @throws Error if WebSocket is not connected, request times out, or response indicates failure\n *\n * @see sendMessage for fire-and-forget messaging without response handling\n */\n async sendRequest<T = ServerResponseMessage['data']>(\n message: Omit<ClientRequestMessage, 'data'> & {\n data?: Omit<ClientRequestMessage['data'], 'requestId'> & {\n requestId?: string;\n };\n },\n ): Promise<T> {\n if (this.#state !== WebSocketState.CONNECTED) {\n throw new Error(`Cannot send request: WebSocket is ${this.#state}`);\n }\n\n // Use provided requestId if available, otherwise generate a new one\n const requestId = message.data?.requestId ?? uuidV4();\n const requestMessage: ClientRequestMessage = {\n event: message.event,\n data: {\n ...message.data,\n requestId, // Set after spread to ensure it's not overwritten by undefined\n },\n };\n\n return new Promise((resolve, reject) => {\n const timeout = setTimeout(() => {\n this.#pendingRequests.delete(requestId);\n log('Request timeout - triggering reconnection', {\n timeout: this.#options.requestTimeout,\n });\n\n // Trigger reconnection on request timeout as it may indicate stale connection\n if (this.#state === WebSocketState.CONNECTED && this.#ws) {\n // Force close the current connection to trigger reconnection logic\n this.#ws.close(3000, 'Request timeout - forcing reconnect');\n }\n\n reject(\n new Error(`Request timeout after ${this.#options.requestTimeout}ms`),\n );\n }, this.#options.requestTimeout);\n\n // Store in pending requests for response correlation\n this.#pendingRequests.set(requestId, {\n resolve: resolve as (value: unknown) => void,\n reject,\n timeout,\n });\n\n // Send the request\n try {\n this.sendMessage(requestMessage);\n } catch (error) {\n this.#pendingRequests.delete(requestId);\n clearTimeout(timeout);\n reject(error instanceof Error ? error : new Error(String(error)));\n }\n });\n }\n\n /**\n * Gets current connection information\n *\n * @returns Current connection status and details\n */\n getConnectionInfo(): WebSocketConnectionInfo {\n return {\n state: this.#state,\n url: this.#options.url,\n timeout: this.#options.timeout,\n reconnectDelay: this.#options.reconnectDelay,\n maxReconnectDelay: this.#options.maxReconnectDelay,\n requestTimeout: this.#options.requestTimeout,\n reconnectAttempts: this.#reconnectAttempts,\n connectedAt: this.#connectedAt,\n };\n }\n\n /**\n * Gets all subscription information for a specific channel\n *\n * @param channel - The channel name to look up\n * @returns Array of subscription details for all subscriptions containing the channel\n */\n getSubscriptionsByChannel(channel: string): WebSocketSubscription[] {\n const matchingSubscriptions: WebSocketSubscription[] = [];\n for (const [subscriptionId, subscription] of this.#subscriptions) {\n if (subscription.channels.includes(channel)) {\n matchingSubscriptions.push({\n subscriptionId,\n channels: subscription.channels,\n channelType: subscription.channelType,\n unsubscribe: subscription.unsubscribe,\n });\n }\n }\n return matchingSubscriptions;\n }\n\n /**\n * Checks if a channel has a subscription\n *\n * @param channel - The channel name to check\n * @returns True if the channel has a subscription, false otherwise\n */\n channelHasSubscription(channel: string): boolean {\n for (const subscription of this.#subscriptions.values()) {\n if (subscription.channels.includes(channel)) {\n return true;\n }\n }\n return false;\n }\n\n /**\n * Finds all subscriptions that have channels starting with the specified prefix\n *\n * @param channelPrefix - The channel prefix to search for (e.g., \"account-activity.v1\")\n * @returns Array of subscription info for matching subscriptions\n */\n findSubscriptionsByChannelPrefix(\n channelPrefix: string,\n ): WebSocketSubscription[] {\n const matchingSubscriptions: WebSocketSubscription[] = [];\n\n for (const [subscriptionId, subscription] of this.#subscriptions) {\n // Check if any channel in this subscription starts with the prefix\n const hasMatchingChannel = subscription.channels.some((channel) =>\n channel.startsWith(channelPrefix),\n );\n\n if (hasMatchingChannel) {\n matchingSubscriptions.push({\n subscriptionId,\n channels: subscription.channels,\n channelType: subscription.channelType,\n unsubscribe: subscription.unsubscribe,\n });\n }\n }\n\n return matchingSubscriptions;\n }\n\n /**\n * Register a callback for specific channels (local callback only, no server subscription)\n *\n * **Key Difference from `subscribe()`:**\n * - `addChannelCallback()`: Registers a local callback without creating a server-side subscription.\n * The callback triggers on ANY message matching the channel name, regardless of subscriptionId.\n * Useful for system-wide notifications or when you don't control the subscription lifecycle.\n *\n * - `subscribe()`: Creates a proper server-side subscription with a subscriptionId.\n * The callback only triggers for messages with the matching subscriptionId.\n * Includes proper lifecycle management (unsubscribe, automatic cleanup on disconnect).\n *\n * **When to use `addChannelCallback()`:**\n * - Listening to system-wide notifications (e.g., 'system-notifications.v1')\n * - Monitoring channels where subscriptions are managed elsewhere\n * - Debug/logging scenarios where you want to observe all channel messages\n *\n * **When to use `subscribe()` instead:**\n * - Creating new subscriptions that need server-side registration\n * - When you need proper cleanup via unsubscribe\n * - Most application use cases (recommended approach)\n *\n * @param options - Channel callback configuration\n * @param options.channelName - Channel name to match exactly\n * @param options.callback - Function to call when channel matches\n *\n * @example\n * ```typescript\n * // Listen to system notifications (no server subscription needed)\n * webSocketService.addChannelCallback({\n * channelName: 'system-notifications.v1',\n * callback: (notification) => {\n * console.log('System notification:', notification.data);\n * }\n * });\n *\n * // For account-specific subscriptions, use subscribe() instead:\n * // const sub = await webSocketService.subscribe({\n * // channels: ['account-activity.v1.eip155:0:0x1234...'],\n * // callback: (notification) => { ... }\n * // });\n * ```\n *\n * @see subscribe for creating proper server-side subscriptions with lifecycle management\n */\n addChannelCallback(options: {\n channelName: string;\n callback: (notification: ServerNotificationMessage) => void;\n }): void {\n const channelCallback: ChannelCallback = {\n channelName: options.channelName,\n callback: options.callback,\n };\n\n // Check if callback already exists for this channel\n if (this.#channelCallbacks.has(options.channelName)) {\n return;\n }\n\n this.#channelCallbacks.set(options.channelName, channelCallback);\n }\n\n /**\n * Remove a channel callback\n *\n * @param channelName - The channel name returned from addChannelCallback\n * @returns True if callback was found and removed, false otherwise\n */\n removeChannelCallback(channelName: string): boolean {\n return this.#channelCallbacks.delete(channelName);\n }\n\n /**\n * Get all registered channel callbacks (for debugging)\n *\n * @returns Array of all registered channel callbacks\n */\n getChannelCallbacks(): ChannelCallback[] {\n return Array.from(this.#channelCallbacks.values());\n }\n\n /**\n * Destroy the service and clean up resources\n * Called when service is being destroyed or app is terminating\n */\n destroy(): void {\n this.#clearTimers();\n this.#clearSubscriptions();\n\n // Clear any pending connection promise\n this.#connectionPromise = null;\n\n // Clear all pending requests\n this.#clearPendingRequests(new Error('Service cleanup'));\n\n if (this.#ws && this.#ws.readyState === WebSocket.OPEN) {\n this.#ws.close(1000, 'Service cleanup');\n }\n\n // Set state to disconnected immediately\n this.#setState(WebSocketState.DISCONNECTED);\n }\n\n /**\n * Create and manage a subscription with server-side registration (recommended for most use cases)\n *\n * This is the recommended subscription API for high-level services. It creates a proper\n * server-side subscription and routes notifications based on subscriptionId.\n *\n * **Key Features:**\n * - Creates server-side subscription with unique subscriptionId\n * - Callback triggered only for messages with matching subscriptionId\n * - Automatic lifecycle management (cleanup on disconnect)\n * - Includes unsubscribe method for proper cleanup\n * - Request-response pattern with error handling\n *\n * **When to use `subscribe()`:**\n * - Creating new subscriptions (account activity, price updates, etc.)\n * - When you need proper cleanup/unsubscribe functionality\n * - Most application use cases\n *\n * **When to use `addChannelCallback()` instead:**\n * - System-wide notifications without server-side subscription\n * - Observing channels managed elsewhere\n * - Debug/logging scenarios\n *\n * @param options - Subscription configuration\n * @param options.channels - Array of channel names to subscribe to\n * @param options.callback - Callback function for handling notifications\n * @param options.requestId - Optional request ID for testing (will generate UUID if not provided)\n * @param options.channelType - Channel type identifier\n * @returns Subscription object with unsubscribe method\n *\n * @example\n * ```typescript\n * // AccountActivityService usage\n * const subscription = await webSocketService.subscribe({\n * channels: ['account-activity.v1.eip155:0:0x1234...'],\n * callback: (notification) => {\n * this.handleAccountActivity(notification.data);\n * }\n * });\n *\n * // Later, clean up\n * await subscription.unsubscribe();\n * ```\n *\n * @see addChannelCallback for local callbacks without server-side subscription\n */\n async subscribe(options: {\n /** Channel names to subscribe to */\n channels: string[];\n /** Channel type with version (e.g., 'account-activity.v1') for tracing and monitoring */\n channelType: string;\n /** Handler for incoming notifications */\n callback: (notification: ServerNotificationMessage) => void;\n /** Optional request ID for testing (will generate UUID if not provided) */\n requestId?: string;\n }): Promise<WebSocketSubscription> {\n const { channels, channelType, callback, requestId } = options;\n\n if (this.#state !== WebSocketState.CONNECTED) {\n throw new Error(\n `Cannot create subscription(s) ${channels.join(', ')}: WebSocket is ${this.#state}`,\n );\n }\n\n // Send subscription request and wait for response\n const subscriptionResponse = await this.sendRequest({\n event: 'subscribe',\n data: { channels, requestId },\n });\n\n if (!subscriptionResponse?.subscriptionId) {\n throw new Error('Invalid subscription response: missing subscription ID');\n }\n\n const { subscriptionId } = subscriptionResponse;\n\n // Create unsubscribe function\n const unsubscribe = async (unsubRequestId?: string): Promise<void> => {\n // Send unsubscribe request first\n await this.sendRequest({\n event: 'unsubscribe',\n data: {\n subscription: subscriptionId,\n channels,\n requestId: unsubRequestId,\n },\n });\n\n // Clean up subscription mapping\n this.#subscriptions.delete(subscriptionId);\n };\n\n const subscription = {\n subscriptionId,\n channels: [...channels],\n channelType,\n unsubscribe,\n };\n\n // Store subscription with subscription ID as key\n this.#subscriptions.set(subscriptionId, {\n subscriptionId,\n channels: [...channels], // Store copy of channels\n channelType,\n callback,\n unsubscribe,\n });\n\n return subscription;\n }\n\n // =============================================================================\n // 3. CONNECTION MANAGEMENT (PRIVATE)\n // =============================================================================\n\n /**\n * Builds an authenticated WebSocket URL with bearer token as query parameter.\n * Uses query parameter for WebSocket authentication since native WebSocket\n * doesn't support custom headers during handshake.\n *\n * @param bearerToken - The bearer token to use for authentication\n * @returns The authenticated WebSocket URL\n */\n #buildAuthenticatedUrl(bearerToken: string): string {\n const baseUrl = this.#options.url;\n\n // Add token as query parameter to the WebSocket URL\n const url = new URL(baseUrl);\n url.searchParams.set('token', bearerToken);\n\n return url.toString();\n }\n\n /**\n * Establishes the actual WebSocket connection\n *\n * @param bearerToken - The bearer token to use for authentication\n * @returns Promise that resolves when connection is established\n */\n async #establishConnection(bearerToken: string): Promise<void> {\n const wsUrl = this.#buildAuthenticatedUrl(bearerToken);\n const connectionStartTime = Date.now();\n\n return new Promise<void>((resolve, reject) => {\n const ws = new WebSocket(wsUrl);\n this.#connectionTimeout = setTimeout(() => {\n log('WebSocket connection timeout - forcing close', {\n timeout: this.#options.timeout,\n });\n ws.close();\n reject(\n new Error(\n `Failed to connect to WebSocket: Connection timeout after ${this.#options.timeout}ms`,\n ),\n );\n }, this.#options.timeout);\n\n ws.onopen = () => {\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n\n // Calculate connection latency\n const connectionLatency = Date.now() - connectionStartTime;\n\n // Trace successful connection with latency\n this.#trace(\n {\n name: `${SERVICE_NAME} Connection`,\n data: {\n reconnectAttempt: this.#reconnectAttempts,\n latency_ms: connectionLatency,\n },\n tags: {\n service: SERVICE_NAME,\n },\n },\n () => {\n this.#ws = ws;\n this.#setState(WebSocketState.CONNECTED);\n this.#connectedAt = Date.now();\n\n // Reset reconnect attempts on successful connection\n this.#reconnectAttempts = 0;\n\n resolve();\n },\n );\n };\n\n ws.onerror = (event: Event) => {\n log('WebSocket onerror event triggered', { event });\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n const error = new Error(`WebSocket connection error to ${wsUrl}`);\n reject(error);\n };\n\n ws.onclose = (event: CloseEvent) => {\n log('WebSocket onclose event triggered', {\n code: event.code,\n reason: event.reason,\n wasClean: event.wasClean,\n });\n if (this.#state === WebSocketState.CONNECTING) {\n // Handle connection-phase close events\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n reject(\n new Error(\n `WebSocket connection closed during connection: ${event.code} ${event.reason}`,\n ),\n );\n } else {\n this.#handleClose(event);\n }\n };\n\n // Set up message handler immediately - no need to wait for connection\n ws.onmessage = (event: MessageEvent) => {\n try {\n const message = this.#parseMessage(event.data);\n this.#handleMessage(message);\n } catch {\n // Silently ignore invalid JSON messages\n }\n };\n });\n }\n\n // =============================================================================\n // 4. MESSAGE HANDLING (PRIVATE)\n // =============================================================================\n\n /**\n * Handles incoming WebSocket messages\n *\n * @param message - The WebSocket message to handle\n */\n #handleMessage(message: WebSocketMessage): void {\n // Handle server responses (correlated with requests) first\n if (this.#isServerResponse(message)) {\n this.#handleServerResponse(message);\n return;\n }\n\n // Handle subscription notifications with valid subscriptionId\n if (this.#isSubscriptionNotification(message)) {\n const notificationMsg = message as ServerNotificationMessage;\n const handled = this.#handleSubscriptionNotification(notificationMsg);\n // If subscription notification wasn't handled (falsy subscriptionId), fall through to channel handling\n if (handled) {\n return;\n }\n }\n\n // Trigger channel callbacks for any message with a channel property\n if (this.#isChannelMessage(message)) {\n const channelMsg = message as ServerNotificationMessage;\n this.#handleChannelMessage(channelMsg);\n }\n }\n\n /**\n * Checks if a message is a server response (correlated with client requests)\n *\n * @param message - The message to check\n * @returns True if the message is a server response\n */\n #isServerResponse(\n message: WebSocketMessage,\n ): message is ServerResponseMessage {\n return (\n 'data' in message &&\n message.data &&\n typeof message.data === 'object' &&\n 'requestId' in message.data\n );\n }\n\n /**\n * Checks if a message is a subscription notification (has subscriptionId)\n *\n * @param message - The message to check\n * @returns True if the message is a subscription notification with subscriptionId\n */\n #isSubscriptionNotification(message: WebSocketMessage): boolean {\n return 'subscriptionId' in message && !this.#isServerResponse(message);\n }\n\n /**\n * Checks if a message has a channel property (system or subscription notification)\n *\n * @param message - The message to check\n * @returns True if the message has a channel property\n */\n #isChannelMessage(\n message: WebSocketMessage,\n ): message is ServerNotificationMessage {\n return 'channel' in message;\n }\n\n /**\n * Handles server response messages (correlated with client requests)\n *\n * @param message - The server response message to handle\n */\n #handleServerResponse(message: ServerResponseMessage): void {\n const { requestId } = message.data;\n\n const request = this.#pendingRequests.get(requestId);\n if (!request) {\n return;\n }\n\n this.#pendingRequests.delete(requestId);\n clearTimeout(request.timeout);\n\n // Check if the response indicates failure\n if (message.data.failed && message.data.failed.length > 0) {\n request.reject(\n new Error(`Request failed: ${message.data.failed.join(', ')}`),\n );\n } else {\n request.resolve(message.data);\n }\n }\n\n /**\n * Handles messages with channel properties by triggering channel callbacks\n *\n * @param message - The message with channel property to handle\n */\n #handleChannelMessage(message: ServerNotificationMessage): void {\n if (this.#channelCallbacks.size === 0) {\n return;\n }\n\n // Calculate notification latency: time from server sent to client received\n const receivedAt = Date.now();\n const latency = receivedAt - message.timestamp;\n\n // Trace channel message processing with latency data\n this.#trace(\n {\n name: `${SERVICE_NAME} Channel Message`,\n data: {\n latency_ms: latency,\n event: message.event,\n },\n tags: {\n service: SERVICE_NAME,\n },\n },\n () => {\n // Direct lookup for exact channel match\n this.#channelCallbacks.get(message.channel)?.callback(message);\n },\n );\n }\n\n /**\n * Handles server notifications with subscription IDs\n *\n * @param message - The server notification message to handle\n * @returns True if the message was handled, false if it should fall through to channel handling\n */\n #handleSubscriptionNotification(message: ServerNotificationMessage): boolean {\n const { subscriptionId, timestamp, channel } = message;\n\n // Only handle if subscriptionId is defined and not null (allows \"0\" as valid ID)\n if (subscriptionId !== null && subscriptionId !== undefined) {\n const subscription = this.#subscriptions.get(subscriptionId);\n if (!subscription) {\n return false;\n }\n\n // Calculate notification latency: time from server sent to client received\n const receivedAt = Date.now();\n const latency = receivedAt - timestamp;\n\n // Trace notification processing wi th latency data\n // Use stored channelType instead of parsing each time\n this.#trace(\n {\n name: `${SERVICE_NAME} Notification`,\n data: {\n channel,\n latency_ms: latency,\n subscriptionId,\n },\n tags: {\n service: SERVICE_NAME,\n notification_type: subscription.channelType,\n },\n },\n () => {\n subscription.callback?.(message);\n },\n );\n return true;\n }\n\n return false;\n }\n\n /**\n * Parse WebSocket message data\n *\n * @param data - The raw message data to parse\n * @returns Parsed message\n */\n #parseMessage(data: string): WebSocketMessage {\n return JSON.parse(data);\n }\n\n // =============================================================================\n // 5. EVENT HANDLERS (PRIVATE)\n // =============================================================================\n\n /**\n * Handles WebSocket close events (mobile optimized)\n *\n * @param event - The WebSocket close event\n */\n #handleClose(event: CloseEvent): void {\n // Calculate connection duration before we clear state\n const connectionDuration = Date.now() - this.#connectedAt;\n\n this.#clearTimers();\n this.#connectedAt = 0;\n\n // Clear any pending connection promise\n this.#connectionPromise = null;\n\n // Clear subscriptions and pending requests on any disconnect\n // This ensures clean state for reconnection\n this.#clearPendingRequests(new Error('WebSocket connection closed'));\n this.#clearSubscriptions();\n\n // Update state to disconnected\n this.#setState(WebSocketState.DISCONNECTED);\n\n // Check if this was a manual disconnect\n if (this.#manualDisconnect) {\n // Manual disconnect - don't reconnect\n return;\n }\n\n // Trace unexpected disconnect with details\n this.#trace(\n {\n name: `${SERVICE_NAME} Disconnect`,\n data: {\n code: event.code,\n reason: event.reason || getCloseReason(event.code),\n connectionDuration_ms: connectionDuration,\n },\n tags: {\n service: SERVICE_NAME,\n disconnect_type: 'unexpected',\n },\n },\n () => {\n // Empty trace callback - just measuring the event\n },\n );\n\n // For any unexpected disconnects, attempt reconnection\n // The manualDisconnect flag is the only gate - if it's false, we reconnect\n this.#scheduleReconnect();\n }\n\n /**\n * Handles WebSocket errors\n *\n * @param _error - Error that occurred (unused)\n */\n #handleError(_error: Error): void {\n // Placeholder for future error handling logic\n }\n\n // =============================================================================\n // 6. STATE MANAGEMENT (PRIVATE)\n // =============================================================================\n\n /**\n * Schedules a reconnection attempt with exponential backoff\n */\n #scheduleReconnect(): void {\n this.#reconnectAttempts += 1;\n\n const rawDelay =\n this.#options.reconnectDelay * Math.pow(1.5, this.#reconnectAttempts - 1);\n const delay = Math.min(rawDelay, this.#options.maxReconnectDelay);\n\n this.#reconnectTimer = setTimeout(() => {\n // Clear timer reference first\n this.#reconnectTimer = null;\n\n // Check if connection is still enabled before reconnecting\n if (this.#isEnabled && !this.#isEnabled()) {\n this.#reconnectAttempts = 0;\n return;\n }\n\n // Attempt to reconnect - if it fails, schedule another attempt\n this.connect().catch(() => {\n this.#scheduleReconnect();\n });\n }, delay);\n }\n\n /**\n * Clears all active timers\n */\n #clearTimers(): void {\n if (this.#reconnectTimer) {\n clearTimeout(this.#reconnectTimer);\n this.#reconnectTimer = null;\n }\n if (this.#connectionTimeout) {\n clearTimeout(this.#connectionTimeout);\n this.#connectionTimeout = null;\n }\n }\n\n /**\n * Clears all pending requests and rejects them with the given error\n *\n * @param error - Error to reject with\n */\n #clearPendingRequests(error: Error): void {\n for (const [, request] of this.#pendingRequests) {\n clearTimeout(request.timeout);\n request.reject(error);\n }\n this.#pendingRequests.clear();\n }\n\n /**\n * Clears all active subscriptions\n */\n #clearSubscriptions(): void {\n this.#subscriptions.clear();\n }\n\n /**\n * Sets the connection state and emits state change events\n *\n * @param newState - The new WebSocket state\n */\n #setState(newState: WebSocketState): void {\n const oldState = this.#state;\n this.#state = newState;\n\n if (oldState !== newState) {\n // Publish connection state change event\n // Messenger handles listener errors internally, no need for try-catch\n this.#messenger.publish(\n 'BackendWebSocketService:connectionStateChanged',\n this.getConnectionInfo(),\n );\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"BackendWebSocketService.d.cts","sourceRoot":"","sources":["../src/BackendWebSocketService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAAkC;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EACV,0BAA0B,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;;;;OAIG;IACH,iBAAiB,IAAI,uBAAuB;IAa5C;;;;;OAKG;IACH,yBAAyB,CAAC,OAAO,EAAE,MAAM,GAAG,qBAAqB,EAAE;IAenE;;;;;OAKG;IACH,sBAAsB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAShD;;;;;OAKG;IACH,gCAAgC,CAC9B,aAAa,EAAE,MAAM,GACpB,qBAAqB,EAAE;IAsB1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IACH,kBAAkB,CAAC,OAAO,EAAE;QAC1B,WAAW,EAAE,MAAM,CAAC;QACpB,QAAQ,EAAE,CAAC,YAAY,EAAE,yBAAyB,KAAK,IAAI,CAAC;KAC7D,GAAG,IAAI;IAcR;;;;;OAKG;IACH,qBAAqB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO;IAInD;;;;OAIG;IACH,mBAAmB,IAAI,eAAe,EAAE;IAIxC;;;OAGG;IACH,OAAO,IAAI,IAAI;IAkBf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6CG;IACG,SAAS,CAAC,OAAO,EAAE;QACvB,oCAAoC;QACpC,QAAQ,EAAE,MAAM,EAAE,CAAC;QACnB,yFAAyF;QACzF,WAAW,EAAE,MAAM,CAAC;QACpB,yCAAyC;QACzC,QAAQ,EAAE,CAAC,YAAY,EAAE,yBAAyB,KAAK,IAAI,CAAC;QAC5D,2EAA2E;QAC3E,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,qBAAqB,CAAC;CAmgBnC"}
@@ -212,39 +212,45 @@ export class BackendWebSocketService {
212
212
  return;
213
213
  }
214
214
  // If already connecting, wait for the existing connection attempt to complete
215
- if (__classPrivateFieldGet(this, _BackendWebSocketService_state, "f") === WebSocketState.CONNECTING && __classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f")) {
215
+ if (__classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f")) {
216
216
  await __classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f");
217
217
  return;
218
218
  }
219
- // Priority 2: Check authentication requirements (signed in)
220
- let bearerToken;
221
- try {
222
- const token = await __classPrivateFieldGet(this, _BackendWebSocketService_messenger, "f").call('AuthenticationController:getBearerToken');
223
- if (!token) {
219
+ // Create and store the connection promise IMMEDIATELY (before any async operations)
220
+ // This ensures subsequent connect() calls will wait for this promise instead of creating new connections
221
+ __classPrivateFieldSet(this, _BackendWebSocketService_connectionPromise, (async () => {
222
+ // Priority 2: Check authentication requirements (signed in)
223
+ let bearerToken;
224
+ try {
225
+ const token = await __classPrivateFieldGet(this, _BackendWebSocketService_messenger, "f").call('AuthenticationController:getBearerToken');
226
+ if (!token) {
227
+ __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_scheduleReconnect).call(this);
228
+ throw new Error('Authentication required: user not signed in');
229
+ }
230
+ bearerToken = token;
231
+ }
232
+ catch (error) {
233
+ log('Failed to check authentication requirements', { error });
234
+ // Can't connect - schedule retry
224
235
  __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_scheduleReconnect).call(this);
225
- return;
236
+ throw error;
226
237
  }
227
- bearerToken = token;
228
- }
229
- catch (error) {
230
- log('Failed to check authentication requirements', { error });
231
- // Can't connect - schedule retry
232
- __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_scheduleReconnect).call(this);
233
- return;
234
- }
235
- __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.CONNECTING);
236
- // Create and store the connection promise
237
- __classPrivateFieldSet(this, _BackendWebSocketService_connectionPromise, __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_establishConnection).call(this, bearerToken), "f");
238
+ __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.CONNECTING);
239
+ // Establish the actual WebSocket connection
240
+ try {
241
+ await __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_establishConnection).call(this, bearerToken);
242
+ }
243
+ catch (error) {
244
+ const errorMessage = getErrorMessage(error);
245
+ log('Connection attempt failed', { errorMessage, error });
246
+ __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.ERROR);
247
+ // Rethrow to propagate error to caller
248
+ throw error;
249
+ }
250
+ })(), "f");
238
251
  try {
239
252
  await __classPrivateFieldGet(this, _BackendWebSocketService_connectionPromise, "f");
240
253
  }
241
- catch (error) {
242
- const errorMessage = getErrorMessage(error);
243
- log('Connection attempt failed', { errorMessage, error });
244
- __classPrivateFieldGet(this, _BackendWebSocketService_instances, "m", _BackendWebSocketService_setState).call(this, WebSocketState.ERROR);
245
- // Rethrow to propagate error to caller
246
- throw error;
247
- }
248
254
  finally {
249
255
  // Clear the connection promise when done (success or failure)
250
256
  __classPrivateFieldSet(this, _BackendWebSocketService_connectionPromise, null, "f");
@@ -781,13 +787,11 @@ async function _BackendWebSocketService_establishConnection(bearerToken) {
781
787
  __classPrivateFieldGet(this, _BackendWebSocketService_trace, "f").call(this, {
782
788
  name: `${SERVICE_NAME} Channel Message`,
783
789
  data: {
784
- channel: message.channel,
785
790
  latency_ms: latency,
786
791
  event: message.event,
787
792
  },
788
793
  tags: {
789
794
  service: SERVICE_NAME,
790
- channel_type: message.channel,
791
795
  },
792
796
  }, () => {
793
797
  // Direct lookup for exact channel match
@@ -804,7 +808,7 @@ async function _BackendWebSocketService_establishConnection(bearerToken) {
804
808
  // Calculate notification latency: time from server sent to client received
805
809
  const receivedAt = Date.now();
806
810
  const latency = receivedAt - timestamp;
807
- // Trace notification processing with latency data
811
+ // Trace notification processing wi th latency data
808
812
  // Use stored channelType instead of parsing each time
809
813
  __classPrivateFieldGet(this, _BackendWebSocketService_trace, "f").call(this, {
810
814
  name: `${SERVICE_NAME} Notification`,