@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.
package/dist/esm/index.js
CHANGED
|
@@ -2315,17 +2315,22 @@ var Sounds = class {
|
|
|
2315
2315
|
var import_exponential_backoff = __toESM(require_backoff(), 1);
|
|
2316
2316
|
import { EventEmitter as EventEmitter3 } from "events";
|
|
2317
2317
|
import WebSocket from "ws";
|
|
2318
|
-
var DEFAULT_MAX_RECONNECT_ATTEMPTS =
|
|
2318
|
+
var DEFAULT_MAX_RECONNECT_ATTEMPTS = 30;
|
|
2319
2319
|
var DEFAULT_STARTING_DELAY = 500;
|
|
2320
2320
|
var DEFAULT_MAX_DELAY = 1e4;
|
|
2321
2321
|
var WebSocketClient = class extends EventEmitter3 {
|
|
2322
2322
|
/**
|
|
2323
|
-
* Creates a new
|
|
2323
|
+
* Creates a new WebSocketClient instance.
|
|
2324
2324
|
*
|
|
2325
|
-
*
|
|
2326
|
-
*
|
|
2327
|
-
*
|
|
2328
|
-
* @param
|
|
2325
|
+
* This constructor initializes a WebSocketClient with the necessary dependencies and configuration.
|
|
2326
|
+
* It ensures that at least one application name is provided.
|
|
2327
|
+
*
|
|
2328
|
+
* @param baseClient - The BaseClient instance used for basic ARI operations and authentication.
|
|
2329
|
+
* @param apps - An array of application names to connect to via the WebSocket.
|
|
2330
|
+
* @param subscribedEvents - Optional. An array of WebSocketEventTypes to subscribe to. If not provided, all events will be subscribed.
|
|
2331
|
+
* @param ariClient - Optional. The AriClient instance, used for creating Channel and Playback instances when processing events.
|
|
2332
|
+
*
|
|
2333
|
+
* @throws {Error} Throws an error if the apps array is empty.
|
|
2329
2334
|
*/
|
|
2330
2335
|
constructor(baseClient, apps, subscribedEvents, ariClient) {
|
|
2331
2336
|
super();
|
|
@@ -2340,6 +2345,8 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2340
2345
|
ws;
|
|
2341
2346
|
isReconnecting = false;
|
|
2342
2347
|
maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS;
|
|
2348
|
+
reconnectionAttempts = 0;
|
|
2349
|
+
lastWsUrl = "";
|
|
2343
2350
|
backOffOptions = {
|
|
2344
2351
|
numOfAttempts: DEFAULT_MAX_RECONNECT_ATTEMPTS,
|
|
2345
2352
|
startingDelay: DEFAULT_STARTING_DELAY,
|
|
@@ -2356,10 +2363,14 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2356
2363
|
}
|
|
2357
2364
|
};
|
|
2358
2365
|
/**
|
|
2359
|
-
* Establishes a WebSocket connection.
|
|
2366
|
+
* Establishes a WebSocket connection to the Asterisk server.
|
|
2360
2367
|
*
|
|
2361
|
-
*
|
|
2362
|
-
*
|
|
2368
|
+
* This method constructs the WebSocket URL using the base URL, credentials,
|
|
2369
|
+
* application names, and subscribed events. It then initiates the connection
|
|
2370
|
+
* using the constructed URL.
|
|
2371
|
+
*
|
|
2372
|
+
* @returns A Promise that resolves when the WebSocket connection is successfully established.
|
|
2373
|
+
* @throws Will throw an error if the connection cannot be established.
|
|
2363
2374
|
*/
|
|
2364
2375
|
async connect() {
|
|
2365
2376
|
const { baseUrl, username, password } = this.baseClient.getCredentials();
|
|
@@ -2374,15 +2385,24 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2374
2385
|
} else {
|
|
2375
2386
|
queryParams.append("subscribeAll", "true");
|
|
2376
2387
|
}
|
|
2377
|
-
|
|
2388
|
+
this.lastWsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
|
|
2378
2389
|
console.log("Connecting to WebSocket...");
|
|
2379
|
-
return this.initializeWebSocket(
|
|
2390
|
+
return this.initializeWebSocket(this.lastWsUrl);
|
|
2380
2391
|
}
|
|
2381
2392
|
/**
|
|
2382
|
-
* Initializes WebSocket connection with
|
|
2393
|
+
* Initializes a WebSocket connection with exponential backoff retry mechanism.
|
|
2383
2394
|
*
|
|
2384
|
-
*
|
|
2385
|
-
*
|
|
2395
|
+
* This method attempts to establish a WebSocket connection to the specified URL.
|
|
2396
|
+
* It sets up event listeners for the WebSocket's 'open', 'message', 'close', and 'error' events.
|
|
2397
|
+
* If the connection is successful, it emits a 'connected' event. If it's a reconnection,
|
|
2398
|
+
* it also emits a 'reconnected' event with the current apps and subscribed events.
|
|
2399
|
+
* In case of connection failure, it uses an exponential backoff strategy to retry.
|
|
2400
|
+
*
|
|
2401
|
+
* @param wsUrl - The WebSocket URL to connect to.
|
|
2402
|
+
* @returns A Promise that resolves when the connection is successfully established,
|
|
2403
|
+
* or rejects if an error occurs during the connection process.
|
|
2404
|
+
* @throws Will throw an error if the WebSocket connection cannot be established
|
|
2405
|
+
* after the maximum number of retry attempts.
|
|
2386
2406
|
*/
|
|
2387
2407
|
async initializeWebSocket(wsUrl) {
|
|
2388
2408
|
return (0, import_exponential_backoff.backOff)(async () => {
|
|
@@ -2391,7 +2411,14 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2391
2411
|
this.ws = new WebSocket(wsUrl);
|
|
2392
2412
|
this.ws.on("open", () => {
|
|
2393
2413
|
console.log("WebSocket connection established successfully");
|
|
2414
|
+
if (this.isReconnecting) {
|
|
2415
|
+
this.emit("reconnected", {
|
|
2416
|
+
apps: this.apps,
|
|
2417
|
+
subscribedEvents: this.subscribedEvents
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2394
2420
|
this.isReconnecting = false;
|
|
2421
|
+
this.reconnectionAttempts = 0;
|
|
2395
2422
|
this.emit("connected");
|
|
2396
2423
|
resolve();
|
|
2397
2424
|
});
|
|
@@ -2401,13 +2428,13 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2401
2428
|
`WebSocket disconnected with code ${code}. Attempting to reconnect...`
|
|
2402
2429
|
);
|
|
2403
2430
|
if (!this.isReconnecting) {
|
|
2404
|
-
this.reconnect(
|
|
2431
|
+
this.reconnect(this.lastWsUrl);
|
|
2405
2432
|
}
|
|
2406
2433
|
});
|
|
2407
2434
|
this.ws.on("error", (err) => {
|
|
2408
2435
|
console.error("WebSocket error:", err.message);
|
|
2409
2436
|
if (!this.isReconnecting) {
|
|
2410
|
-
this.reconnect(
|
|
2437
|
+
this.reconnect(this.lastWsUrl);
|
|
2411
2438
|
}
|
|
2412
2439
|
reject(err);
|
|
2413
2440
|
});
|
|
@@ -2418,9 +2445,16 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2418
2445
|
}, this.backOffOptions);
|
|
2419
2446
|
}
|
|
2420
2447
|
/**
|
|
2421
|
-
*
|
|
2448
|
+
* Handles incoming WebSocket messages by parsing and processing events.
|
|
2449
|
+
*
|
|
2450
|
+
* This method parses the raw message into a WebSocketEvent, filters it based on
|
|
2451
|
+
* subscribed events (if any), processes channel and playback events, and emits
|
|
2452
|
+
* the event to listeners. It also handles any errors that occur during processing.
|
|
2453
|
+
*
|
|
2454
|
+
* @param rawMessage - The raw message string received from the WebSocket connection.
|
|
2455
|
+
* @returns void This method doesn't return a value but emits events.
|
|
2422
2456
|
*
|
|
2423
|
-
* @
|
|
2457
|
+
* @throws Will emit an 'error' event if the message cannot be parsed or processed.
|
|
2424
2458
|
*/
|
|
2425
2459
|
handleMessage(rawMessage) {
|
|
2426
2460
|
try {
|
|
@@ -2448,18 +2482,27 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2448
2482
|
}
|
|
2449
2483
|
}
|
|
2450
2484
|
/**
|
|
2451
|
-
* Attempts to reconnect to the WebSocket.
|
|
2485
|
+
* Attempts to reconnect to the WebSocket server using an exponential backoff strategy.
|
|
2452
2486
|
*
|
|
2453
|
-
*
|
|
2487
|
+
* This method is called when the WebSocket connection is closed unexpectedly.
|
|
2488
|
+
* It increments the reconnection attempt counter, logs the attempt, and uses
|
|
2489
|
+
* the backOff utility to retry the connection with exponential delays between attempts.
|
|
2490
|
+
*
|
|
2491
|
+
* @param wsUrl - The WebSocket URL to reconnect to.
|
|
2492
|
+
* @returns void - This method doesn't return a value.
|
|
2493
|
+
*
|
|
2494
|
+
* @emits reconnectFailed - Emitted if all reconnection attempts fail.
|
|
2454
2495
|
*/
|
|
2455
2496
|
reconnect(wsUrl) {
|
|
2456
2497
|
this.isReconnecting = true;
|
|
2457
|
-
|
|
2458
|
-
|
|
2498
|
+
this.reconnectionAttempts++;
|
|
2499
|
+
console.log(
|
|
2500
|
+
`Initiating reconnection attempt #${this.reconnectionAttempts}...`
|
|
2501
|
+
);
|
|
2459
2502
|
(0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
|
|
2460
2503
|
(error) => {
|
|
2461
2504
|
console.error(
|
|
2462
|
-
|
|
2505
|
+
`Failed to reconnect after ${this.reconnectionAttempts} attempts:`,
|
|
2463
2506
|
error instanceof Error ? error.message : "Unknown error"
|
|
2464
2507
|
);
|
|
2465
2508
|
this.emit("reconnectFailed", error);
|
|
@@ -2467,7 +2510,13 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2467
2510
|
);
|
|
2468
2511
|
}
|
|
2469
2512
|
/**
|
|
2470
|
-
*
|
|
2513
|
+
* Closes the WebSocket connection if it exists.
|
|
2514
|
+
*
|
|
2515
|
+
* This method attempts to gracefully close the WebSocket connection
|
|
2516
|
+
* and sets the WebSocket instance to undefined. If an error occurs
|
|
2517
|
+
* during the closing process, it will be caught and logged.
|
|
2518
|
+
*
|
|
2519
|
+
* @throws {Error} Logs an error message if closing the WebSocket fails.
|
|
2471
2520
|
*/
|
|
2472
2521
|
close() {
|
|
2473
2522
|
try {
|
|
@@ -2484,17 +2533,30 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2484
2533
|
}
|
|
2485
2534
|
}
|
|
2486
2535
|
/**
|
|
2487
|
-
* Checks if the WebSocket is currently
|
|
2536
|
+
* Checks if the WebSocket connection is currently open and active.
|
|
2537
|
+
*
|
|
2538
|
+
* This method provides a way to determine the current state of the WebSocket connection.
|
|
2539
|
+
* It checks if the WebSocket's readyState property is equal to WebSocket.OPEN,
|
|
2540
|
+
* which indicates an active connection.
|
|
2488
2541
|
*
|
|
2489
|
-
* @returns {boolean} True if
|
|
2542
|
+
* @returns {boolean} True if the WebSocket connection is open and active, false otherwise.
|
|
2490
2543
|
*/
|
|
2491
2544
|
isConnected() {
|
|
2492
2545
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
2493
2546
|
}
|
|
2494
2547
|
/**
|
|
2495
|
-
*
|
|
2548
|
+
* Retrieves the current state of the WebSocket connection.
|
|
2549
|
+
*
|
|
2550
|
+
* This method provides a way to check the current state of the WebSocket connection.
|
|
2551
|
+
* It returns a number corresponding to one of the WebSocket readyState values:
|
|
2552
|
+
* - 0 (CONNECTING): The connection is not yet open.
|
|
2553
|
+
* - 1 (OPEN): The connection is open and ready to communicate.
|
|
2554
|
+
* - 2 (CLOSING): The connection is in the process of closing.
|
|
2555
|
+
* - 3 (CLOSED): The connection is closed or couldn't be opened.
|
|
2556
|
+
*
|
|
2557
|
+
* If the WebSocket instance doesn't exist, it returns WebSocket.CLOSED (3).
|
|
2496
2558
|
*
|
|
2497
|
-
* @returns {number}
|
|
2559
|
+
* @returns {number} A number representing the current state of the WebSocket connection.
|
|
2498
2560
|
*/
|
|
2499
2561
|
getState() {
|
|
2500
2562
|
return this.ws?.readyState ?? WebSocket.CLOSED;
|