@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/esm/index.js
CHANGED
|
@@ -1182,73 +1182,24 @@ var ChannelInstance = class {
|
|
|
1182
1182
|
* Plays media on the channel
|
|
1183
1183
|
*/
|
|
1184
1184
|
async play(options, playbackId) {
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
this.channelData = await this.getDetails();
|
|
1192
|
-
}
|
|
1193
|
-
const playback = this.client.Playback(playbackId || v4_default());
|
|
1194
|
-
let playbackStarted = false;
|
|
1195
|
-
const failureListener = (event) => {
|
|
1196
|
-
if ("playback" in event && event.playback) {
|
|
1197
|
-
if (event.playback.state === "failed") {
|
|
1198
|
-
console.error("Playback falhou:", {
|
|
1199
|
-
playbackId: playback.id,
|
|
1200
|
-
channelId: this.id,
|
|
1201
|
-
state: event.playback.state
|
|
1202
|
-
});
|
|
1203
|
-
if (!playbackStarted) {
|
|
1204
|
-
reject(
|
|
1205
|
-
new Error(
|
|
1206
|
-
`Playback failed to start: ${event.playback.state}`
|
|
1207
|
-
)
|
|
1208
|
-
);
|
|
1209
|
-
}
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
};
|
|
1213
|
-
const startListener = (event) => {
|
|
1214
|
-
if ("playback" in event && event.playback) {
|
|
1215
|
-
console.log("Playback come\xE7ou:", {
|
|
1216
|
-
playbackId: playback.id,
|
|
1217
|
-
channelId: this.id,
|
|
1218
|
-
state: event.playback.state
|
|
1219
|
-
});
|
|
1220
|
-
playbackStarted = true;
|
|
1221
|
-
setTimeout(async () => {
|
|
1222
|
-
try {
|
|
1223
|
-
const status = await playback.get();
|
|
1224
|
-
if (status.state === "playing") {
|
|
1225
|
-
resolve(playback);
|
|
1226
|
-
} else {
|
|
1227
|
-
reject(
|
|
1228
|
-
new Error(`Playback started but state is ${status.state}`)
|
|
1229
|
-
);
|
|
1230
|
-
}
|
|
1231
|
-
} catch (_err) {
|
|
1232
|
-
reject(new Error("Failed to verify playback status"));
|
|
1233
|
-
}
|
|
1234
|
-
}, 500);
|
|
1235
|
-
}
|
|
1236
|
-
};
|
|
1237
|
-
playback.once("PlaybackStarted", startListener);
|
|
1238
|
-
playback.once("PlaybackFinished", failureListener);
|
|
1239
|
-
await this.baseClient.post(
|
|
1240
|
-
`/channels/${this.id}/play/${playback.id}`,
|
|
1241
|
-
options
|
|
1242
|
-
);
|
|
1243
|
-
setTimeout(() => {
|
|
1244
|
-
if (!playbackStarted) {
|
|
1245
|
-
reject(new Error("Playback timeout - n\xE3o iniciou"));
|
|
1246
|
-
}
|
|
1247
|
-
}, 5e3);
|
|
1248
|
-
} catch (error) {
|
|
1249
|
-
reject(error);
|
|
1185
|
+
if (!options.media) {
|
|
1186
|
+
throw new Error("Media URL is required");
|
|
1187
|
+
}
|
|
1188
|
+
try {
|
|
1189
|
+
if (!this.channelData) {
|
|
1190
|
+
this.channelData = await this.getDetails();
|
|
1250
1191
|
}
|
|
1251
|
-
|
|
1192
|
+
const playback = this.client.Playback(playbackId || v4_default());
|
|
1193
|
+
await this.baseClient.post(
|
|
1194
|
+
`/channels/${this.id}/play/${playback.id}`,
|
|
1195
|
+
options
|
|
1196
|
+
);
|
|
1197
|
+
return playback;
|
|
1198
|
+
} catch (error) {
|
|
1199
|
+
const message = getErrorMessage(error);
|
|
1200
|
+
console.error(`Error playing media on channel ${this.id}:`, message);
|
|
1201
|
+
throw new Error(`Failed to play media: ${message}`);
|
|
1202
|
+
}
|
|
1252
1203
|
}
|
|
1253
1204
|
/**
|
|
1254
1205
|
* Gets the current channel details
|
|
@@ -2369,17 +2320,12 @@ var DEFAULT_STARTING_DELAY = 500;
|
|
|
2369
2320
|
var DEFAULT_MAX_DELAY = 1e4;
|
|
2370
2321
|
var WebSocketClient = class extends EventEmitter3 {
|
|
2371
2322
|
/**
|
|
2372
|
-
* Creates a new
|
|
2373
|
-
*
|
|
2374
|
-
* This constructor initializes a WebSocketClient with the necessary dependencies and configuration.
|
|
2375
|
-
* It ensures that at least one application name is provided.
|
|
2323
|
+
* Creates a new WebSocket client instance.
|
|
2376
2324
|
*
|
|
2377
|
-
* @param baseClient - The
|
|
2378
|
-
* @param apps -
|
|
2379
|
-
* @param subscribedEvents - Optional
|
|
2380
|
-
* @param ariClient - Optional
|
|
2381
|
-
*
|
|
2382
|
-
* @throws {Error} Throws an error if the apps array is empty.
|
|
2325
|
+
* @param {BaseClient} baseClient - The base client containing connection details
|
|
2326
|
+
* @param {string[]} apps - List of applications to connect to
|
|
2327
|
+
* @param {WebSocketEventType[]} [subscribedEvents] - Optional list of events to subscribe to
|
|
2328
|
+
* @param {AriClient} [ariClient] - Optional ARI client for handling channel and playback events
|
|
2383
2329
|
*/
|
|
2384
2330
|
constructor(baseClient, apps, subscribedEvents, ariClient) {
|
|
2385
2331
|
super();
|
|
@@ -2394,8 +2340,6 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2394
2340
|
ws;
|
|
2395
2341
|
isReconnecting = false;
|
|
2396
2342
|
maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS;
|
|
2397
|
-
reconnectionAttempts = 0;
|
|
2398
|
-
lastWsUrl = "";
|
|
2399
2343
|
backOffOptions = {
|
|
2400
2344
|
numOfAttempts: DEFAULT_MAX_RECONNECT_ATTEMPTS,
|
|
2401
2345
|
startingDelay: DEFAULT_STARTING_DELAY,
|
|
@@ -2412,14 +2356,10 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2412
2356
|
}
|
|
2413
2357
|
};
|
|
2414
2358
|
/**
|
|
2415
|
-
* Establishes a WebSocket connection
|
|
2416
|
-
*
|
|
2417
|
-
* This method constructs the WebSocket URL using the base URL, credentials,
|
|
2418
|
-
* application names, and subscribed events. It then initiates the connection
|
|
2419
|
-
* using the constructed URL.
|
|
2359
|
+
* Establishes a WebSocket connection.
|
|
2420
2360
|
*
|
|
2421
|
-
* @returns
|
|
2422
|
-
* @throws
|
|
2361
|
+
* @returns {Promise<void>} Resolves when connection is established
|
|
2362
|
+
* @throws {Error} If connection fails
|
|
2423
2363
|
*/
|
|
2424
2364
|
async connect() {
|
|
2425
2365
|
const { baseUrl, username, password } = this.baseClient.getCredentials();
|
|
@@ -2434,24 +2374,15 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2434
2374
|
} else {
|
|
2435
2375
|
queryParams.append("subscribeAll", "true");
|
|
2436
2376
|
}
|
|
2437
|
-
|
|
2377
|
+
const wsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
|
|
2438
2378
|
console.log("Connecting to WebSocket...");
|
|
2439
|
-
return this.initializeWebSocket(
|
|
2379
|
+
return this.initializeWebSocket(wsUrl);
|
|
2440
2380
|
}
|
|
2441
2381
|
/**
|
|
2442
|
-
* Initializes
|
|
2443
|
-
*
|
|
2444
|
-
* This method attempts to establish a WebSocket connection to the specified URL.
|
|
2445
|
-
* It sets up event listeners for the WebSocket's 'open', 'message', 'close', and 'error' events.
|
|
2446
|
-
* If the connection is successful, it emits a 'connected' event. If it's a reconnection,
|
|
2447
|
-
* it also emits a 'reconnected' event with the current apps and subscribed events.
|
|
2448
|
-
* In case of connection failure, it uses an exponential backoff strategy to retry.
|
|
2382
|
+
* Initializes WebSocket connection with reconnection logic.
|
|
2449
2383
|
*
|
|
2450
|
-
* @param wsUrl - The WebSocket URL to connect to
|
|
2451
|
-
* @returns
|
|
2452
|
-
* or rejects if an error occurs during the connection process.
|
|
2453
|
-
* @throws Will throw an error if the WebSocket connection cannot be established
|
|
2454
|
-
* after the maximum number of retry attempts.
|
|
2384
|
+
* @param {string} wsUrl - The WebSocket URL to connect to
|
|
2385
|
+
* @returns {Promise<void>} Resolves when connection is established
|
|
2455
2386
|
*/
|
|
2456
2387
|
async initializeWebSocket(wsUrl) {
|
|
2457
2388
|
return (0, import_exponential_backoff.backOff)(async () => {
|
|
@@ -2460,14 +2391,7 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2460
2391
|
this.ws = new WebSocket(wsUrl);
|
|
2461
2392
|
this.ws.on("open", () => {
|
|
2462
2393
|
console.log("WebSocket connection established successfully");
|
|
2463
|
-
if (this.isReconnecting) {
|
|
2464
|
-
this.emit("reconnected", {
|
|
2465
|
-
apps: this.apps,
|
|
2466
|
-
subscribedEvents: this.subscribedEvents
|
|
2467
|
-
});
|
|
2468
|
-
}
|
|
2469
2394
|
this.isReconnecting = false;
|
|
2470
|
-
this.reconnectionAttempts = 0;
|
|
2471
2395
|
this.emit("connected");
|
|
2472
2396
|
resolve();
|
|
2473
2397
|
});
|
|
@@ -2477,13 +2401,13 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2477
2401
|
`WebSocket disconnected with code ${code}. Attempting to reconnect...`
|
|
2478
2402
|
);
|
|
2479
2403
|
if (!this.isReconnecting) {
|
|
2480
|
-
this.reconnect(
|
|
2404
|
+
this.reconnect(wsUrl);
|
|
2481
2405
|
}
|
|
2482
2406
|
});
|
|
2483
2407
|
this.ws.on("error", (err) => {
|
|
2484
2408
|
console.error("WebSocket error:", err.message);
|
|
2485
2409
|
if (!this.isReconnecting) {
|
|
2486
|
-
this.reconnect(
|
|
2410
|
+
this.reconnect(wsUrl);
|
|
2487
2411
|
}
|
|
2488
2412
|
reject(err);
|
|
2489
2413
|
});
|
|
@@ -2494,16 +2418,9 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2494
2418
|
}, this.backOffOptions);
|
|
2495
2419
|
}
|
|
2496
2420
|
/**
|
|
2497
|
-
*
|
|
2421
|
+
* Processes incoming WebSocket messages.
|
|
2498
2422
|
*
|
|
2499
|
-
*
|
|
2500
|
-
* subscribed events (if any), processes channel and playback events, and emits
|
|
2501
|
-
* the event to listeners. It also handles any errors that occur during processing.
|
|
2502
|
-
*
|
|
2503
|
-
* @param rawMessage - The raw message string received from the WebSocket connection.
|
|
2504
|
-
* @returns void This method doesn't return a value but emits events.
|
|
2505
|
-
*
|
|
2506
|
-
* @throws Will emit an 'error' event if the message cannot be parsed or processed.
|
|
2423
|
+
* @param {string} rawMessage - The raw message received from WebSocket
|
|
2507
2424
|
*/
|
|
2508
2425
|
handleMessage(rawMessage) {
|
|
2509
2426
|
try {
|
|
@@ -2531,27 +2448,18 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2531
2448
|
}
|
|
2532
2449
|
}
|
|
2533
2450
|
/**
|
|
2534
|
-
* Attempts to reconnect to the WebSocket
|
|
2535
|
-
*
|
|
2536
|
-
* This method is called when the WebSocket connection is closed unexpectedly.
|
|
2537
|
-
* It increments the reconnection attempt counter, logs the attempt, and uses
|
|
2538
|
-
* the backOff utility to retry the connection with exponential delays between attempts.
|
|
2451
|
+
* Attempts to reconnect to the WebSocket.
|
|
2539
2452
|
*
|
|
2540
|
-
* @param wsUrl - The WebSocket URL to reconnect to
|
|
2541
|
-
* @returns void - This method doesn't return a value.
|
|
2542
|
-
*
|
|
2543
|
-
* @emits reconnectFailed - Emitted if all reconnection attempts fail.
|
|
2453
|
+
* @param {string} wsUrl - The WebSocket URL to reconnect to
|
|
2544
2454
|
*/
|
|
2545
2455
|
reconnect(wsUrl) {
|
|
2546
2456
|
this.isReconnecting = true;
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
`Initiating reconnection attempt #${this.reconnectionAttempts}...`
|
|
2550
|
-
);
|
|
2457
|
+
console.log("Initiating reconnection attempt...");
|
|
2458
|
+
this.removeAllListeners();
|
|
2551
2459
|
(0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
|
|
2552
2460
|
(error) => {
|
|
2553
2461
|
console.error(
|
|
2554
|
-
|
|
2462
|
+
"Failed to reconnect after multiple attempts:",
|
|
2555
2463
|
error instanceof Error ? error.message : "Unknown error"
|
|
2556
2464
|
);
|
|
2557
2465
|
this.emit("reconnectFailed", error);
|
|
@@ -2559,13 +2467,7 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2559
2467
|
);
|
|
2560
2468
|
}
|
|
2561
2469
|
/**
|
|
2562
|
-
*
|
|
2563
|
-
*
|
|
2564
|
-
* This method attempts to gracefully close the WebSocket connection
|
|
2565
|
-
* and sets the WebSocket instance to undefined. If an error occurs
|
|
2566
|
-
* during the closing process, it will be caught and logged.
|
|
2567
|
-
*
|
|
2568
|
-
* @throws {Error} Logs an error message if closing the WebSocket fails.
|
|
2470
|
+
* Manually closes the WebSocket connection.
|
|
2569
2471
|
*/
|
|
2570
2472
|
close() {
|
|
2571
2473
|
try {
|
|
@@ -2582,30 +2484,17 @@ var WebSocketClient = class extends EventEmitter3 {
|
|
|
2582
2484
|
}
|
|
2583
2485
|
}
|
|
2584
2486
|
/**
|
|
2585
|
-
* Checks if the WebSocket
|
|
2487
|
+
* Checks if the WebSocket is currently connected.
|
|
2586
2488
|
*
|
|
2587
|
-
*
|
|
2588
|
-
* if the connection is established and ready for communication.
|
|
2589
|
-
*
|
|
2590
|
-
* @returns {boolean} True if the WebSocket connection is open and ready,
|
|
2591
|
-
* false otherwise.
|
|
2489
|
+
* @returns {boolean} True if connected, false otherwise
|
|
2592
2490
|
*/
|
|
2593
2491
|
isConnected() {
|
|
2594
2492
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
2595
2493
|
}
|
|
2596
2494
|
/**
|
|
2597
|
-
*
|
|
2598
|
-
*
|
|
2599
|
-
* This method returns the readyState of the WebSocket instance, which
|
|
2600
|
-
* indicates the current state of the connection. If no WebSocket instance
|
|
2601
|
-
* exists, it returns the CLOSED state.
|
|
2495
|
+
* Gets the current connection state.
|
|
2602
2496
|
*
|
|
2603
|
-
* @returns {number} The
|
|
2604
|
-
* Possible values are:
|
|
2605
|
-
* - WebSocket.CONNECTING (0): The connection is not yet open.
|
|
2606
|
-
* - WebSocket.OPEN (1): The connection is open and ready to communicate.
|
|
2607
|
-
* - WebSocket.CLOSING (2): The connection is in the process of closing.
|
|
2608
|
-
* - WebSocket.CLOSED (3): The connection is closed or couldn't be opened.
|
|
2497
|
+
* @returns {number} The WebSocket ready state
|
|
2609
2498
|
*/
|
|
2610
2499
|
getState() {
|
|
2611
2500
|
return this.ws?.readyState ?? WebSocket.CLOSED;
|