@ipcom/asterisk-ari 0.0.151 → 0.0.153
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/cjs/index.cjs +44 -155
- package/dist/cjs/index.cjs.map +2 -2
- package/dist/esm/index.js +44 -155
- package/dist/esm/index.js.map +2 -2
- package/dist/types/ari-client/resources/channels.d.ts.map +1 -1
- package/dist/types/ari-client/websocketClient.d.ts +20 -73
- package/dist/types/ari-client/websocketClient.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -1202,73 +1202,24 @@ var ChannelInstance = class {
|
|
|
1202
1202
|
* Plays media on the channel
|
|
1203
1203
|
*/
|
|
1204
1204
|
async play(options, playbackId) {
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
this.channelData = await this.getDetails();
|
|
1212
|
-
}
|
|
1213
|
-
const playback = this.client.Playback(playbackId || v4_default());
|
|
1214
|
-
let playbackStarted = false;
|
|
1215
|
-
const failureListener = (event) => {
|
|
1216
|
-
if ("playback" in event && event.playback) {
|
|
1217
|
-
if (event.playback.state === "failed") {
|
|
1218
|
-
console.error("Playback falhou:", {
|
|
1219
|
-
playbackId: playback.id,
|
|
1220
|
-
channelId: this.id,
|
|
1221
|
-
state: event.playback.state
|
|
1222
|
-
});
|
|
1223
|
-
if (!playbackStarted) {
|
|
1224
|
-
reject(
|
|
1225
|
-
new Error(
|
|
1226
|
-
`Playback failed to start: ${event.playback.state}`
|
|
1227
|
-
)
|
|
1228
|
-
);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
};
|
|
1233
|
-
const startListener = (event) => {
|
|
1234
|
-
if ("playback" in event && event.playback) {
|
|
1235
|
-
console.log("Playback come\xE7ou:", {
|
|
1236
|
-
playbackId: playback.id,
|
|
1237
|
-
channelId: this.id,
|
|
1238
|
-
state: event.playback.state
|
|
1239
|
-
});
|
|
1240
|
-
playbackStarted = true;
|
|
1241
|
-
setTimeout(async () => {
|
|
1242
|
-
try {
|
|
1243
|
-
const status = await playback.get();
|
|
1244
|
-
if (status.state === "playing") {
|
|
1245
|
-
resolve(playback);
|
|
1246
|
-
} else {
|
|
1247
|
-
reject(
|
|
1248
|
-
new Error(`Playback started but state is ${status.state}`)
|
|
1249
|
-
);
|
|
1250
|
-
}
|
|
1251
|
-
} catch (_err) {
|
|
1252
|
-
reject(new Error("Failed to verify playback status"));
|
|
1253
|
-
}
|
|
1254
|
-
}, 500);
|
|
1255
|
-
}
|
|
1256
|
-
};
|
|
1257
|
-
playback.once("PlaybackStarted", startListener);
|
|
1258
|
-
playback.once("PlaybackFinished", failureListener);
|
|
1259
|
-
await this.baseClient.post(
|
|
1260
|
-
`/channels/${this.id}/play/${playback.id}`,
|
|
1261
|
-
options
|
|
1262
|
-
);
|
|
1263
|
-
setTimeout(() => {
|
|
1264
|
-
if (!playbackStarted) {
|
|
1265
|
-
reject(new Error("Playback timeout - n\xE3o iniciou"));
|
|
1266
|
-
}
|
|
1267
|
-
}, 5e3);
|
|
1268
|
-
} catch (error) {
|
|
1269
|
-
reject(error);
|
|
1205
|
+
if (!options.media) {
|
|
1206
|
+
throw new Error("Media URL is required");
|
|
1207
|
+
}
|
|
1208
|
+
try {
|
|
1209
|
+
if (!this.channelData) {
|
|
1210
|
+
this.channelData = await this.getDetails();
|
|
1270
1211
|
}
|
|
1271
|
-
|
|
1212
|
+
const playback = this.client.Playback(playbackId || v4_default());
|
|
1213
|
+
await this.baseClient.post(
|
|
1214
|
+
`/channels/${this.id}/play/${playback.id}`,
|
|
1215
|
+
options
|
|
1216
|
+
);
|
|
1217
|
+
return playback;
|
|
1218
|
+
} catch (error) {
|
|
1219
|
+
const message = getErrorMessage(error);
|
|
1220
|
+
console.error(`Error playing media on channel ${this.id}:`, message);
|
|
1221
|
+
throw new Error(`Failed to play media: ${message}`);
|
|
1222
|
+
}
|
|
1272
1223
|
}
|
|
1273
1224
|
/**
|
|
1274
1225
|
* Gets the current channel details
|
|
@@ -2389,17 +2340,12 @@ var DEFAULT_STARTING_DELAY = 500;
|
|
|
2389
2340
|
var DEFAULT_MAX_DELAY = 1e4;
|
|
2390
2341
|
var WebSocketClient = class extends import_events3.EventEmitter {
|
|
2391
2342
|
/**
|
|
2392
|
-
* Creates a new
|
|
2393
|
-
*
|
|
2394
|
-
* This constructor initializes a WebSocketClient with the necessary dependencies and configuration.
|
|
2395
|
-
* It ensures that at least one application name is provided.
|
|
2343
|
+
* Creates a new WebSocket client instance.
|
|
2396
2344
|
*
|
|
2397
|
-
* @param baseClient - The
|
|
2398
|
-
* @param apps -
|
|
2399
|
-
* @param subscribedEvents - Optional
|
|
2400
|
-
* @param ariClient - Optional
|
|
2401
|
-
*
|
|
2402
|
-
* @throws {Error} Throws an error if the apps array is empty.
|
|
2345
|
+
* @param {BaseClient} baseClient - The base client containing connection details
|
|
2346
|
+
* @param {string[]} apps - List of applications to connect to
|
|
2347
|
+
* @param {WebSocketEventType[]} [subscribedEvents] - Optional list of events to subscribe to
|
|
2348
|
+
* @param {AriClient} [ariClient] - Optional ARI client for handling channel and playback events
|
|
2403
2349
|
*/
|
|
2404
2350
|
constructor(baseClient, apps, subscribedEvents, ariClient) {
|
|
2405
2351
|
super();
|
|
@@ -2414,8 +2360,6 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2414
2360
|
ws;
|
|
2415
2361
|
isReconnecting = false;
|
|
2416
2362
|
maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS;
|
|
2417
|
-
reconnectionAttempts = 0;
|
|
2418
|
-
lastWsUrl = "";
|
|
2419
2363
|
backOffOptions = {
|
|
2420
2364
|
numOfAttempts: DEFAULT_MAX_RECONNECT_ATTEMPTS,
|
|
2421
2365
|
startingDelay: DEFAULT_STARTING_DELAY,
|
|
@@ -2432,14 +2376,10 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2432
2376
|
}
|
|
2433
2377
|
};
|
|
2434
2378
|
/**
|
|
2435
|
-
* Establishes a WebSocket connection
|
|
2436
|
-
*
|
|
2437
|
-
* This method constructs the WebSocket URL using the base URL, credentials,
|
|
2438
|
-
* application names, and subscribed events. It then initiates the connection
|
|
2439
|
-
* using the constructed URL.
|
|
2379
|
+
* Establishes a WebSocket connection.
|
|
2440
2380
|
*
|
|
2441
|
-
* @returns
|
|
2442
|
-
* @throws
|
|
2381
|
+
* @returns {Promise<void>} Resolves when connection is established
|
|
2382
|
+
* @throws {Error} If connection fails
|
|
2443
2383
|
*/
|
|
2444
2384
|
async connect() {
|
|
2445
2385
|
const { baseUrl, username, password } = this.baseClient.getCredentials();
|
|
@@ -2454,24 +2394,15 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2454
2394
|
} else {
|
|
2455
2395
|
queryParams.append("subscribeAll", "true");
|
|
2456
2396
|
}
|
|
2457
|
-
|
|
2397
|
+
const wsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
|
|
2458
2398
|
console.log("Connecting to WebSocket...");
|
|
2459
|
-
return this.initializeWebSocket(
|
|
2399
|
+
return this.initializeWebSocket(wsUrl);
|
|
2460
2400
|
}
|
|
2461
2401
|
/**
|
|
2462
|
-
* Initializes
|
|
2463
|
-
*
|
|
2464
|
-
* This method attempts to establish a WebSocket connection to the specified URL.
|
|
2465
|
-
* It sets up event listeners for the WebSocket's 'open', 'message', 'close', and 'error' events.
|
|
2466
|
-
* If the connection is successful, it emits a 'connected' event. If it's a reconnection,
|
|
2467
|
-
* it also emits a 'reconnected' event with the current apps and subscribed events.
|
|
2468
|
-
* In case of connection failure, it uses an exponential backoff strategy to retry.
|
|
2402
|
+
* Initializes WebSocket connection with reconnection logic.
|
|
2469
2403
|
*
|
|
2470
|
-
* @param wsUrl - The WebSocket URL to connect to
|
|
2471
|
-
* @returns
|
|
2472
|
-
* or rejects if an error occurs during the connection process.
|
|
2473
|
-
* @throws Will throw an error if the WebSocket connection cannot be established
|
|
2474
|
-
* after the maximum number of retry attempts.
|
|
2404
|
+
* @param {string} wsUrl - The WebSocket URL to connect to
|
|
2405
|
+
* @returns {Promise<void>} Resolves when connection is established
|
|
2475
2406
|
*/
|
|
2476
2407
|
async initializeWebSocket(wsUrl) {
|
|
2477
2408
|
return (0, import_exponential_backoff.backOff)(async () => {
|
|
@@ -2480,14 +2411,7 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2480
2411
|
this.ws = new import_ws.default(wsUrl);
|
|
2481
2412
|
this.ws.on("open", () => {
|
|
2482
2413
|
console.log("WebSocket connection established successfully");
|
|
2483
|
-
if (this.isReconnecting) {
|
|
2484
|
-
this.emit("reconnected", {
|
|
2485
|
-
apps: this.apps,
|
|
2486
|
-
subscribedEvents: this.subscribedEvents
|
|
2487
|
-
});
|
|
2488
|
-
}
|
|
2489
2414
|
this.isReconnecting = false;
|
|
2490
|
-
this.reconnectionAttempts = 0;
|
|
2491
2415
|
this.emit("connected");
|
|
2492
2416
|
resolve();
|
|
2493
2417
|
});
|
|
@@ -2497,13 +2421,13 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2497
2421
|
`WebSocket disconnected with code ${code}. Attempting to reconnect...`
|
|
2498
2422
|
);
|
|
2499
2423
|
if (!this.isReconnecting) {
|
|
2500
|
-
this.reconnect(
|
|
2424
|
+
this.reconnect(wsUrl);
|
|
2501
2425
|
}
|
|
2502
2426
|
});
|
|
2503
2427
|
this.ws.on("error", (err) => {
|
|
2504
2428
|
console.error("WebSocket error:", err.message);
|
|
2505
2429
|
if (!this.isReconnecting) {
|
|
2506
|
-
this.reconnect(
|
|
2430
|
+
this.reconnect(wsUrl);
|
|
2507
2431
|
}
|
|
2508
2432
|
reject(err);
|
|
2509
2433
|
});
|
|
@@ -2514,16 +2438,9 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2514
2438
|
}, this.backOffOptions);
|
|
2515
2439
|
}
|
|
2516
2440
|
/**
|
|
2517
|
-
*
|
|
2441
|
+
* Processes incoming WebSocket messages.
|
|
2518
2442
|
*
|
|
2519
|
-
*
|
|
2520
|
-
* subscribed events (if any), processes channel and playback events, and emits
|
|
2521
|
-
* the event to listeners. It also handles any errors that occur during processing.
|
|
2522
|
-
*
|
|
2523
|
-
* @param rawMessage - The raw message string received from the WebSocket connection.
|
|
2524
|
-
* @returns void This method doesn't return a value but emits events.
|
|
2525
|
-
*
|
|
2526
|
-
* @throws Will emit an 'error' event if the message cannot be parsed or processed.
|
|
2443
|
+
* @param {string} rawMessage - The raw message received from WebSocket
|
|
2527
2444
|
*/
|
|
2528
2445
|
handleMessage(rawMessage) {
|
|
2529
2446
|
try {
|
|
@@ -2551,27 +2468,18 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2551
2468
|
}
|
|
2552
2469
|
}
|
|
2553
2470
|
/**
|
|
2554
|
-
* Attempts to reconnect to the WebSocket
|
|
2555
|
-
*
|
|
2556
|
-
* This method is called when the WebSocket connection is closed unexpectedly.
|
|
2557
|
-
* It increments the reconnection attempt counter, logs the attempt, and uses
|
|
2558
|
-
* the backOff utility to retry the connection with exponential delays between attempts.
|
|
2471
|
+
* Attempts to reconnect to the WebSocket.
|
|
2559
2472
|
*
|
|
2560
|
-
* @param wsUrl - The WebSocket URL to reconnect to
|
|
2561
|
-
* @returns void - This method doesn't return a value.
|
|
2562
|
-
*
|
|
2563
|
-
* @emits reconnectFailed - Emitted if all reconnection attempts fail.
|
|
2473
|
+
* @param {string} wsUrl - The WebSocket URL to reconnect to
|
|
2564
2474
|
*/
|
|
2565
2475
|
reconnect(wsUrl) {
|
|
2566
2476
|
this.isReconnecting = true;
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
`Initiating reconnection attempt #${this.reconnectionAttempts}...`
|
|
2570
|
-
);
|
|
2477
|
+
console.log("Initiating reconnection attempt...");
|
|
2478
|
+
this.removeAllListeners();
|
|
2571
2479
|
(0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
|
|
2572
2480
|
(error) => {
|
|
2573
2481
|
console.error(
|
|
2574
|
-
|
|
2482
|
+
"Failed to reconnect after multiple attempts:",
|
|
2575
2483
|
error instanceof Error ? error.message : "Unknown error"
|
|
2576
2484
|
);
|
|
2577
2485
|
this.emit("reconnectFailed", error);
|
|
@@ -2579,13 +2487,7 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2579
2487
|
);
|
|
2580
2488
|
}
|
|
2581
2489
|
/**
|
|
2582
|
-
*
|
|
2583
|
-
*
|
|
2584
|
-
* This method attempts to gracefully close the WebSocket connection
|
|
2585
|
-
* and sets the WebSocket instance to undefined. If an error occurs
|
|
2586
|
-
* during the closing process, it will be caught and logged.
|
|
2587
|
-
*
|
|
2588
|
-
* @throws {Error} Logs an error message if closing the WebSocket fails.
|
|
2490
|
+
* Manually closes the WebSocket connection.
|
|
2589
2491
|
*/
|
|
2590
2492
|
close() {
|
|
2591
2493
|
try {
|
|
@@ -2602,30 +2504,17 @@ var WebSocketClient = class extends import_events3.EventEmitter {
|
|
|
2602
2504
|
}
|
|
2603
2505
|
}
|
|
2604
2506
|
/**
|
|
2605
|
-
* Checks if the WebSocket
|
|
2507
|
+
* Checks if the WebSocket is currently connected.
|
|
2606
2508
|
*
|
|
2607
|
-
*
|
|
2608
|
-
* if the connection is established and ready for communication.
|
|
2609
|
-
*
|
|
2610
|
-
* @returns {boolean} True if the WebSocket connection is open and ready,
|
|
2611
|
-
* false otherwise.
|
|
2509
|
+
* @returns {boolean} True if connected, false otherwise
|
|
2612
2510
|
*/
|
|
2613
2511
|
isConnected() {
|
|
2614
2512
|
return this.ws?.readyState === import_ws.default.OPEN;
|
|
2615
2513
|
}
|
|
2616
2514
|
/**
|
|
2617
|
-
*
|
|
2618
|
-
*
|
|
2619
|
-
* This method returns the readyState of the WebSocket instance, which
|
|
2620
|
-
* indicates the current state of the connection. If no WebSocket instance
|
|
2621
|
-
* exists, it returns the CLOSED state.
|
|
2515
|
+
* Gets the current connection state.
|
|
2622
2516
|
*
|
|
2623
|
-
* @returns {number} The
|
|
2624
|
-
* Possible values are:
|
|
2625
|
-
* - WebSocket.CONNECTING (0): The connection is not yet open.
|
|
2626
|
-
* - WebSocket.OPEN (1): The connection is open and ready to communicate.
|
|
2627
|
-
* - WebSocket.CLOSING (2): The connection is in the process of closing.
|
|
2628
|
-
* - WebSocket.CLOSED (3): The connection is closed or couldn't be opened.
|
|
2517
|
+
* @returns {number} The WebSocket ready state
|
|
2629
2518
|
*/
|
|
2630
2519
|
getState() {
|
|
2631
2520
|
return this.ws?.readyState ?? import_ws.default.CLOSED;
|