@ipcom/asterisk-ari 0.0.153 → 0.0.154

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.
@@ -14,56 +14,109 @@ export declare class WebSocketClient extends EventEmitter {
14
14
  private ws?;
15
15
  private isReconnecting;
16
16
  private readonly maxReconnectAttempts;
17
+ private reconnectionAttempts;
18
+ private lastWsUrl;
17
19
  private readonly backOffOptions;
18
20
  /**
19
- * Creates a new WebSocket client instance.
21
+ * Creates a new WebSocketClient instance.
20
22
  *
21
- * @param {BaseClient} baseClient - The base client containing connection details
22
- * @param {string[]} apps - List of applications to connect to
23
- * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of events to subscribe to
24
- * @param {AriClient} [ariClient] - Optional ARI client for handling channel and playback events
23
+ * This constructor initializes a WebSocketClient with the necessary dependencies and configuration.
24
+ * It ensures that at least one application name is provided.
25
+ *
26
+ * @param baseClient - The BaseClient instance used for basic ARI operations and authentication.
27
+ * @param apps - An array of application names to connect to via the WebSocket.
28
+ * @param subscribedEvents - Optional. An array of WebSocketEventTypes to subscribe to. If not provided, all events will be subscribed.
29
+ * @param ariClient - Optional. The AriClient instance, used for creating Channel and Playback instances when processing events.
30
+ *
31
+ * @throws {Error} Throws an error if the apps array is empty.
25
32
  */
26
33
  constructor(baseClient: BaseClient, apps: string[], subscribedEvents?: WebSocketEventType[] | undefined, ariClient?: AriClient | undefined);
27
34
  /**
28
- * Establishes a WebSocket connection.
35
+ * Establishes a WebSocket connection to the Asterisk server.
29
36
  *
30
- * @returns {Promise<void>} Resolves when connection is established
31
- * @throws {Error} If connection fails
37
+ * This method constructs the WebSocket URL using the base URL, credentials,
38
+ * application names, and subscribed events. It then initiates the connection
39
+ * using the constructed URL.
40
+ *
41
+ * @returns A Promise that resolves when the WebSocket connection is successfully established.
42
+ * @throws Will throw an error if the connection cannot be established.
32
43
  */
33
44
  connect(): Promise<void>;
34
45
  /**
35
- * Initializes WebSocket connection with reconnection logic.
46
+ * Initializes a WebSocket connection with exponential backoff retry mechanism.
47
+ *
48
+ * This method attempts to establish a WebSocket connection to the specified URL.
49
+ * It sets up event listeners for the WebSocket's 'open', 'message', 'close', and 'error' events.
50
+ * If the connection is successful, it emits a 'connected' event. If it's a reconnection,
51
+ * it also emits a 'reconnected' event with the current apps and subscribed events.
52
+ * In case of connection failure, it uses an exponential backoff strategy to retry.
36
53
  *
37
- * @param {string} wsUrl - The WebSocket URL to connect to
38
- * @returns {Promise<void>} Resolves when connection is established
54
+ * @param wsUrl - The WebSocket URL to connect to.
55
+ * @returns A Promise that resolves when the connection is successfully established,
56
+ * or rejects if an error occurs during the connection process.
57
+ * @throws Will throw an error if the WebSocket connection cannot be established
58
+ * after the maximum number of retry attempts.
39
59
  */
40
60
  private initializeWebSocket;
41
61
  /**
42
- * Processes incoming WebSocket messages.
62
+ * Handles incoming WebSocket messages by parsing and processing events.
43
63
  *
44
- * @param {string} rawMessage - The raw message received from WebSocket
64
+ * This method parses the raw message into a WebSocketEvent, filters it based on
65
+ * subscribed events (if any), processes channel and playback events, and emits
66
+ * the event to listeners. It also handles any errors that occur during processing.
67
+ *
68
+ * @param rawMessage - The raw message string received from the WebSocket connection.
69
+ * @returns void This method doesn't return a value but emits events.
70
+ *
71
+ * @throws Will emit an 'error' event if the message cannot be parsed or processed.
45
72
  */
46
73
  private handleMessage;
47
74
  /**
48
- * Attempts to reconnect to the WebSocket.
75
+ * Attempts to reconnect to the WebSocket server using an exponential backoff strategy.
76
+ *
77
+ * This method is called when the WebSocket connection is closed unexpectedly.
78
+ * It increments the reconnection attempt counter, logs the attempt, and uses
79
+ * the backOff utility to retry the connection with exponential delays between attempts.
49
80
  *
50
- * @param {string} wsUrl - The WebSocket URL to reconnect to
81
+ * @param wsUrl - The WebSocket URL to reconnect to.
82
+ * @returns void - This method doesn't return a value.
83
+ *
84
+ * @emits reconnectFailed - Emitted if all reconnection attempts fail.
51
85
  */
52
86
  private reconnect;
53
87
  /**
54
- * Manually closes the WebSocket connection.
88
+ * Closes the WebSocket connection if it exists.
89
+ *
90
+ * This method attempts to gracefully close the WebSocket connection
91
+ * and sets the WebSocket instance to undefined. If an error occurs
92
+ * during the closing process, it will be caught and logged.
93
+ *
94
+ * @throws {Error} Logs an error message if closing the WebSocket fails.
55
95
  */
56
96
  close(): void;
57
97
  /**
58
- * Checks if the WebSocket is currently connected.
98
+ * Checks if the WebSocket connection is currently open and active.
59
99
  *
60
- * @returns {boolean} True if connected, false otherwise
100
+ * This method provides a way to determine the current state of the WebSocket connection.
101
+ * It checks if the WebSocket's readyState property is equal to WebSocket.OPEN,
102
+ * which indicates an active connection.
103
+ *
104
+ * @returns {boolean} True if the WebSocket connection is open and active, false otherwise.
61
105
  */
62
106
  isConnected(): boolean;
63
107
  /**
64
- * Gets the current connection state.
108
+ * Retrieves the current state of the WebSocket connection.
109
+ *
110
+ * This method provides a way to check the current state of the WebSocket connection.
111
+ * It returns a number corresponding to one of the WebSocket readyState values:
112
+ * - 0 (CONNECTING): The connection is not yet open.
113
+ * - 1 (OPEN): The connection is open and ready to communicate.
114
+ * - 2 (CLOSING): The connection is in the process of closing.
115
+ * - 3 (CLOSED): The connection is closed or couldn't be opened.
116
+ *
117
+ * If the WebSocket instance doesn't exist, it returns WebSocket.CLOSED (3).
65
118
  *
66
- * @returns {number} The WebSocket ready state
119
+ * @returns {number} A number representing the current state of the WebSocket connection.
67
120
  */
68
121
  getState(): number;
69
122
  }
@@ -1 +1 @@
1
- {"version":3,"file":"websocketClient.d.ts","sourceRoot":"","sources":["../../../src/ari-client/websocketClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,KAAK,EAAkB,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAMvE;;;GAGG;AACH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,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAC;IAClC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC;IAvC7B,OAAO,CAAC,EAAE,CAAC,CAAY;IACvB,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAkC;IACvE,OAAO,CAAC,oBAAoB,CAAK;IACjC,OAAO,CAAC,SAAS,CAAc;IAE/B,OAAO,CAAC,QAAQ,CAAC,cAAc,CAc7B;IAEF;;;;;;;;;;;;OAYG;gBAEgB,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,MAAM,EAAE,EACd,gBAAgB,CAAC,EAAE,kBAAkB,EAAE,YAAA,EACvC,SAAS,CAAC,EAAE,SAAS,YAAA;IASxC;;;;;;;;;OASG;IACU,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAyBrC;;;;;;;;;;;;;;OAcG;YACW,mBAAmB;IA6CjC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,aAAa;IAiCrB;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,SAAS;IAkBjB;;;;;;;;OAQG;IACI,KAAK,IAAI,IAAI;IAepB;;;;;;;;OAQG;IACI,WAAW,IAAI,OAAO;IAI7B;;;;;;;;;;;;;OAaG;IACI,QAAQ,IAAI,MAAM;CAG1B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ipcom/asterisk-ari",
3
- "version": "0.0.153",
3
+ "version": "0.0.154",
4
4
  "type": "module",
5
5
  "description": "JavaScript client for Asterisk REST Interface.",
6
6
  "homepage": "https://github.com/fabiotheo/ipcom-asterisk-ari",