@ipcom/asterisk-ari 0.0.157 → 0.0.159

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
@@ -1782,7 +1782,7 @@ var ChannelInstance = class {
1782
1782
  * Otherwise, all listeners for the given event type are removed.
1783
1783
  *
1784
1784
  * @param {T} event - The type of WebSocket event to remove listener(s) for
1785
- * @param {Function} [listener] - Optional specific listener to remove
1785
+ * @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
1786
1786
  * @throws {Error} If no event type is provided
1787
1787
  */
1788
1788
  off(event, listener) {
@@ -2744,7 +2744,7 @@ var PlaybackInstance = class {
2744
2744
  * Registers an event listener for a specific WebSocket event type.
2745
2745
  *
2746
2746
  * @param {T} event - Event type to listen for
2747
- * @param {Function} listener - Callback function for the event
2747
+ * @param {(data: WebSocketEvent) => void} listener - Callback function for the event
2748
2748
  */
2749
2749
  on(event, listener) {
2750
2750
  if (!event) {
@@ -2772,7 +2772,7 @@ var PlaybackInstance = class {
2772
2772
  * Registers a one-time event listener for a specific WebSocket event type.
2773
2773
  *
2774
2774
  * @param {T} event - Event type to listen for
2775
- * @param {Function} listener - Callback function for the event
2775
+ * @param {(data: WebSocketEvent) => void} listener - Callback function for the event
2776
2776
  */
2777
2777
  once(event, listener) {
2778
2778
  if (!event) {
@@ -2802,7 +2802,7 @@ var PlaybackInstance = class {
2802
2802
  * Removes event listener(s) for a specific WebSocket event type.
2803
2803
  *
2804
2804
  * @param {T} event - Event type to remove listener(s) for
2805
- * @param {Function} [listener] - Optional specific listener to remove
2805
+ * @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
2806
2806
  */
2807
2807
  off(event, listener) {
2808
2808
  if (!event) {
@@ -2983,11 +2983,11 @@ var Playbacks = class {
2983
2983
  * This method performs the following cleanup operations:
2984
2984
  * 1. Clears all pending timeouts in the event queue.
2985
2985
  * 2. Removes all playback instances.
2986
- *
2986
+ *
2987
2987
  * @remarks
2988
2988
  * This method should be called when the Playbacks instance is no longer needed
2989
2989
  * to ensure proper resource management and prevent memory leaks.
2990
- *
2990
+ *
2991
2991
  * @returns {void} This method doesn't return a value.
2992
2992
  */
2993
2993
  cleanup() {
@@ -3297,13 +3297,64 @@ var WebSocketClient = class extends EventEmitter4 {
3297
3297
  (event) => queryParams.append("event", event)
3298
3298
  );
3299
3299
  this.lastWsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
3300
- console.log("Connecting to WebSocket...");
3301
3300
  try {
3302
3301
  await this.initializeWebSocket(this.lastWsUrl);
3303
3302
  } finally {
3304
3303
  this.isConnecting = false;
3305
3304
  }
3306
3305
  }
3306
+ /**
3307
+ * Reconecta o WebSocket com uma lista atualizada de aplicações.
3308
+ *
3309
+ * @param {string[]} newApps - Lista de aplicações para reconectar
3310
+ * @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3311
+ * @returns {Promise<void>} Promise resolvida quando reconectado com sucesso
3312
+ */
3313
+ async reconnectWithApps(newApps, subscribedEvents) {
3314
+ if (!newApps.length) {
3315
+ throw new Error("At least one application name is required");
3316
+ }
3317
+ const uniqueApps = Array.from(/* @__PURE__ */ new Set([...this.apps, ...newApps]));
3318
+ if (uniqueApps.length === this.apps.length && uniqueApps.every((app) => this.apps.includes(app))) {
3319
+ console.log(
3320
+ "No changes in applications list, maintaining current connection"
3321
+ );
3322
+ return;
3323
+ }
3324
+ console.log(
3325
+ `Reconnecting WebSocket with updated applications: ${uniqueApps.join(", ")}`
3326
+ );
3327
+ this.apps = uniqueApps;
3328
+ if (subscribedEvents) {
3329
+ this.subscribedEvents = subscribedEvents;
3330
+ }
3331
+ if (this.ws) {
3332
+ await new Promise((resolve) => {
3333
+ this.once("disconnected", () => resolve());
3334
+ this.close();
3335
+ });
3336
+ }
3337
+ await this.connect();
3338
+ console.log("WebSocket reconnected successfully with updated applications");
3339
+ }
3340
+ /**
3341
+ * Adiciona novas aplicações à conexão WebSocket existente.
3342
+ *
3343
+ * @param {string[]} newApps - Lista de novas aplicações para adicionar
3344
+ * @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3345
+ * @returns {Promise<void>} Promise resolvida quando as aplicações são adicionadas com sucesso
3346
+ */
3347
+ async addApps(newApps, subscribedEvents) {
3348
+ if (!newApps.length) {
3349
+ throw new Error("At least one application name is required");
3350
+ }
3351
+ const appsToAdd = newApps.filter((app) => !this.apps.includes(app));
3352
+ if (appsToAdd.length === 0) {
3353
+ console.log("All applications are already registered");
3354
+ return;
3355
+ }
3356
+ await this.reconnectWithApps(appsToAdd, subscribedEvents);
3357
+ }
3307
3358
  /**
3308
3359
  * Initializes a WebSocket connection with exponential backoff retry mechanism.
3309
3360
  *
@@ -3325,7 +3376,6 @@ var WebSocketClient = class extends EventEmitter4 {
3325
3376
  try {
3326
3377
  this.ws = new WebSocket(wsUrl);
3327
3378
  this.ws.once("open", () => {
3328
- console.log("WebSocket connection established successfully");
3329
3379
  this.setupHeartbeat();
3330
3380
  if (this.isReconnecting) {
3331
3381
  this.emit("reconnected", {
@@ -3576,7 +3626,6 @@ var AriClient = class {
3576
3626
  baseClient;
3577
3627
  webSocketClient;
3578
3628
  eventListeners = /* @__PURE__ */ new Map();
3579
- // Armazena os listeners para limpeza
3580
3629
  channels;
3581
3630
  endpoints;
3582
3631
  applications;
@@ -3634,16 +3683,19 @@ var AriClient = class {
3634
3683
  * @param {string[]} apps - List of application names to subscribe to
3635
3684
  * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
3636
3685
  * @returns {Promise<void>} Resolves when connection is established
3637
- * @throws {Error} If connection fails or if WebSocket is already connected
3686
+ * @throws {Error} If connection fails
3638
3687
  */
3639
3688
  async connectWebSocket(apps, subscribedEvents) {
3640
3689
  try {
3641
3690
  if (!apps.length) {
3642
3691
  throw new Error("At least one application name is required.");
3643
3692
  }
3644
- if (this.webSocketClient) {
3645
- await this.closeWebSocket();
3646
- await new Promise((resolve) => setTimeout(resolve, 1e3));
3693
+ if (this.webSocketClient && this.webSocketClient.isConnected()) {
3694
+ console.log(
3695
+ "WebSocket already connected. Reconnecting with updated apps..."
3696
+ );
3697
+ await this.webSocketClient.reconnectWithApps(apps, subscribedEvents);
3698
+ return;
3647
3699
  }
3648
3700
  this.webSocketClient = new WebSocketClient(
3649
3701
  this.baseClient,
@@ -3652,17 +3704,31 @@ var AriClient = class {
3652
3704
  this
3653
3705
  );
3654
3706
  await this.webSocketClient.connect();
3655
- console.log("WebSocket connection established successfully.");
3656
3707
  } catch (error) {
3657
3708
  console.error("Failed to establish WebSocket connection:", error);
3658
- this.webSocketClient = void 0;
3659
3709
  throw error;
3660
3710
  }
3661
3711
  }
3712
+ /**
3713
+ * Adds applications to the existing WebSocket connection.
3714
+ *
3715
+ * @param {string[]} apps - Additional applications to subscribe to
3716
+ * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
3717
+ * @returns {Promise<void>} Resolves when applications are added successfully
3718
+ * @throws {Error} If no WebSocket connection exists or if the operation fails
3719
+ */
3720
+ async addApplicationsToWebSocket(apps, subscribedEvents) {
3721
+ if (!this.webSocketClient || !this.webSocketClient.isConnected()) {
3722
+ throw new Error(
3723
+ "No active WebSocket connection. Create one first with connectWebSocket()."
3724
+ );
3725
+ }
3726
+ await this.webSocketClient.addApps(apps, subscribedEvents);
3727
+ }
3662
3728
  /**
3663
3729
  * Destroys the ARI Client instance, cleaning up all resources and removing circular references.
3664
3730
  * This method should be called when the ARI Client is no longer needed to ensure proper cleanup.
3665
- *
3731
+ *
3666
3732
  * @returns {Promise<void>} A promise that resolves when the destruction process is complete.
3667
3733
  * @throws {Error} If an error occurs during the destruction process.
3668
3734
  */
@@ -3684,6 +3750,13 @@ var AriClient = class {
3684
3750
  throw error;
3685
3751
  }
3686
3752
  }
3753
+ /**
3754
+ * Registers an event listener for WebSocket events.
3755
+ *
3756
+ * @param {T} event - The event type to listen for
3757
+ * @param {Function} listener - Callback function for handling the event
3758
+ * @throws {Error} If WebSocket is not connected
3759
+ */
3687
3760
  /**
3688
3761
  * Registers an event listener for WebSocket events.
3689
3762
  *
@@ -3728,7 +3801,10 @@ var AriClient = class {
3728
3801
  this.off(event, wrappedListener);
3729
3802
  };
3730
3803
  this.webSocketClient.once(event, wrappedListener);
3731
- this.eventListeners.set(event, [...existingListeners, wrappedListener]);
3804
+ this.eventListeners.set(event, [
3805
+ ...existingListeners,
3806
+ wrappedListener
3807
+ ]);
3732
3808
  console.log(`One-time event listener registered for ${event}`);
3733
3809
  }
3734
3810
  /**
@@ -3746,7 +3822,9 @@ var AriClient = class {
3746
3822
  const existingListeners = this.eventListeners.get(event) || [];
3747
3823
  this.eventListeners.set(
3748
3824
  event,
3749
- existingListeners.filter((l) => l !== listener)
3825
+ existingListeners.filter(
3826
+ (l) => l !== listener
3827
+ )
3750
3828
  );
3751
3829
  console.log(`Event listener removed for ${event}`);
3752
3830
  }