@ipcom/asterisk-ari 0.0.146-beta → 0.0.146
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/README.md +32 -117
- package/dist/cjs/index.cjs +166 -767
- package/dist/cjs/index.cjs.map +4 -4
- package/dist/esm/index.js +166 -766
- package/dist/esm/index.js.map +4 -4
- package/dist/types/ari-client/ariClient.d.ts +1 -13
- package/dist/types/ari-client/ariClient.d.ts.map +1 -1
- package/dist/types/ari-client/baseClient.d.ts.map +1 -1
- package/dist/types/ari-client/interfaces/events.types.d.ts +0 -5
- package/dist/types/ari-client/interfaces/events.types.d.ts.map +1 -1
- package/dist/types/ari-client/interfaces/index.d.ts +1 -1
- package/dist/types/ari-client/interfaces/index.d.ts.map +1 -1
- package/dist/types/ari-client/resources/bridges.d.ts +14 -365
- package/dist/types/ari-client/resources/bridges.d.ts.map +1 -1
- package/dist/types/ari-client/resources/channels.d.ts.map +1 -1
- package/dist/types/ari-client/resources/playbacks.d.ts +1 -1
- package/dist/types/ari-client/resources/playbacks.d.ts.map +1 -1
- package/dist/types/ari-client/websocketClient.d.ts +73 -20
- package/dist/types/ari-client/websocketClient.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/esm/index.js
CHANGED
|
@@ -611,7 +611,6 @@ var BaseClient = class {
|
|
|
611
611
|
}
|
|
612
612
|
});
|
|
613
613
|
this.addInterceptors();
|
|
614
|
-
console.log(`BaseClient initialized for ${baseUrl}`);
|
|
615
614
|
}
|
|
616
615
|
client;
|
|
617
616
|
/**
|
|
@@ -636,7 +635,6 @@ var BaseClient = class {
|
|
|
636
635
|
addInterceptors() {
|
|
637
636
|
this.client.interceptors.request.use(
|
|
638
637
|
(config) => {
|
|
639
|
-
console.log(`[Request] ${config.method?.toUpperCase()} ${config.url}`);
|
|
640
638
|
return config;
|
|
641
639
|
},
|
|
642
640
|
(error) => {
|
|
@@ -647,7 +645,6 @@ var BaseClient = class {
|
|
|
647
645
|
);
|
|
648
646
|
this.client.interceptors.response.use(
|
|
649
647
|
(response) => {
|
|
650
|
-
console.log(`[Response] ${response.status} ${response.config.url}`);
|
|
651
648
|
return response;
|
|
652
649
|
},
|
|
653
650
|
(error) => {
|
|
@@ -770,7 +767,6 @@ var BaseClient = class {
|
|
|
770
767
|
...this.client.defaults.headers.common,
|
|
771
768
|
...headers
|
|
772
769
|
};
|
|
773
|
-
console.log("Updated client headers");
|
|
774
770
|
}
|
|
775
771
|
/**
|
|
776
772
|
* Gets the current request timeout setting.
|
|
@@ -783,7 +779,6 @@ var BaseClient = class {
|
|
|
783
779
|
*/
|
|
784
780
|
setTimeout(timeout) {
|
|
785
781
|
this.client.defaults.timeout = timeout;
|
|
786
|
-
console.log(`Updated timeout to ${timeout}ms`);
|
|
787
782
|
}
|
|
788
783
|
};
|
|
789
784
|
|
|
@@ -794,7 +789,7 @@ var Applications = class {
|
|
|
794
789
|
}
|
|
795
790
|
/**
|
|
796
791
|
* Lists all applications.
|
|
797
|
-
*
|
|
792
|
+
*
|
|
798
793
|
* @returns A promise that resolves to an array of Application objects.
|
|
799
794
|
* @throws {Error} If the API response is not an array.
|
|
800
795
|
*/
|
|
@@ -807,7 +802,7 @@ var Applications = class {
|
|
|
807
802
|
}
|
|
808
803
|
/**
|
|
809
804
|
* Retrieves details of a specific application.
|
|
810
|
-
*
|
|
805
|
+
*
|
|
811
806
|
* @param appName - The name of the application to retrieve details for.
|
|
812
807
|
* @returns A promise that resolves to an ApplicationDetails object.
|
|
813
808
|
* @throws {Error} If there's an error fetching the application details.
|
|
@@ -824,7 +819,7 @@ var Applications = class {
|
|
|
824
819
|
}
|
|
825
820
|
/**
|
|
826
821
|
* Sends a message to a specific application.
|
|
827
|
-
*
|
|
822
|
+
*
|
|
828
823
|
* @param appName - The name of the application to send the message to.
|
|
829
824
|
* @param body - The message to be sent, containing an event and optional data.
|
|
830
825
|
* @returns A promise that resolves when the message is successfully sent.
|
|
@@ -917,681 +912,97 @@ var Asterisk = class {
|
|
|
917
912
|
};
|
|
918
913
|
|
|
919
914
|
// src/ari-client/resources/bridges.ts
|
|
920
|
-
import { EventEmitter } from "events";
|
|
921
|
-
import { isAxiosError as isAxiosError2 } from "axios";
|
|
922
|
-
|
|
923
|
-
// src/ari-client/interfaces/events.types.ts
|
|
924
|
-
var bridgeEvents = [
|
|
925
|
-
"BridgeCreated",
|
|
926
|
-
"BridgeDestroyed",
|
|
927
|
-
"BridgeMerged",
|
|
928
|
-
"BridgeBlindTransfer",
|
|
929
|
-
"BridgeAttendedTransfer",
|
|
930
|
-
"BridgeVideoSourceChanged"
|
|
931
|
-
];
|
|
932
|
-
|
|
933
|
-
// src/ari-client/utils.ts
|
|
934
|
-
function toQueryParams2(options) {
|
|
935
|
-
return new URLSearchParams(
|
|
936
|
-
Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
|
|
937
|
-
).toString();
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
// src/ari-client/resources/bridges.ts
|
|
941
|
-
var getErrorMessage = (error) => {
|
|
942
|
-
if (isAxiosError2(error)) {
|
|
943
|
-
return error.response?.data?.message || error.message || "Um erro do axios ocorreu";
|
|
944
|
-
}
|
|
945
|
-
if (error instanceof Error) {
|
|
946
|
-
return error.message;
|
|
947
|
-
}
|
|
948
|
-
return "Um erro desconhecido ocorreu";
|
|
949
|
-
};
|
|
950
|
-
var BridgeInstance = class {
|
|
951
|
-
/**
|
|
952
|
-
* Creates a new BridgeInstance.
|
|
953
|
-
*
|
|
954
|
-
* @param client - The AriClient instance for making API calls.
|
|
955
|
-
* @param baseClient - The BaseClient instance for making HTTP requests.
|
|
956
|
-
* @param bridgeId - Optional. The ID of the bridge. If not provided, a new ID will be generated.
|
|
957
|
-
*/
|
|
958
|
-
constructor(client, baseClient, bridgeId) {
|
|
959
|
-
this.client = client;
|
|
960
|
-
this.baseClient = baseClient;
|
|
961
|
-
this.id = bridgeId || `bridge-${Date.now()}`;
|
|
962
|
-
console.log(`BridgeInstance inicializada com ID: ${this.id}`);
|
|
963
|
-
}
|
|
964
|
-
eventEmitter = new EventEmitter();
|
|
965
|
-
bridgeData = null;
|
|
966
|
-
id;
|
|
967
|
-
/**
|
|
968
|
-
* Registers a listener for specific bridge events.
|
|
969
|
-
*
|
|
970
|
-
* @param event - The type of event to listen for.
|
|
971
|
-
* @param listener - The callback function to be called when the event occurs.
|
|
972
|
-
*/
|
|
973
|
-
/**
|
|
974
|
-
* Registers a listener for specific bridge events.
|
|
975
|
-
*
|
|
976
|
-
* This method allows you to attach an event listener to the bridge instance for a specific event type.
|
|
977
|
-
* When the specified event occurs, the provided listener function will be called with the event data.
|
|
978
|
-
*
|
|
979
|
-
* @template T - The specific type of WebSocketEvent to listen for.
|
|
980
|
-
* It receives the event data as its parameter.
|
|
981
|
-
* @returns {void}
|
|
982
|
-
*
|
|
983
|
-
* @example
|
|
984
|
-
* bridge.on('BridgeCreated', (event) => {
|
|
985
|
-
* console.log('Bridge created:', event.bridge.id);
|
|
986
|
-
* });
|
|
987
|
-
* @param event
|
|
988
|
-
* @param listener
|
|
989
|
-
*/
|
|
990
|
-
on(event, listener) {
|
|
991
|
-
if (!event) {
|
|
992
|
-
throw new Error("Event type is required");
|
|
993
|
-
}
|
|
994
|
-
const wrappedListener = (data) => {
|
|
995
|
-
if ("bridge" in data && data.bridge?.id === this.id) {
|
|
996
|
-
listener(data);
|
|
997
|
-
}
|
|
998
|
-
};
|
|
999
|
-
this.eventEmitter.on(event, wrappedListener);
|
|
1000
|
-
console.log(`Event listener registered for ${event} on bridge ${this.id}`);
|
|
1001
|
-
}
|
|
1002
|
-
/**
|
|
1003
|
-
* Registers a one-time listener for specific bridge events.
|
|
1004
|
-
*
|
|
1005
|
-
* @param event - The type of event to listen for.
|
|
1006
|
-
* @param listener - The callback function to be called when the event occurs.
|
|
1007
|
-
*/
|
|
1008
|
-
once(event, listener) {
|
|
1009
|
-
if (!event) {
|
|
1010
|
-
throw new Error("Event type is required");
|
|
1011
|
-
}
|
|
1012
|
-
const wrappedListener = (data) => {
|
|
1013
|
-
if ("bridge" in data && data.bridge?.id === this.id) {
|
|
1014
|
-
listener(data);
|
|
1015
|
-
}
|
|
1016
|
-
};
|
|
1017
|
-
this.eventEmitter.once(event, wrappedListener);
|
|
1018
|
-
console.log(
|
|
1019
|
-
`One-time listener registered for ${event} on bridge ${this.id}`
|
|
1020
|
-
);
|
|
1021
|
-
}
|
|
1022
|
-
/**
|
|
1023
|
-
* Removes event listener(s) from the bridge.
|
|
1024
|
-
*
|
|
1025
|
-
* @param event - The type of event to remove listeners for.
|
|
1026
|
-
* @param listener - Optional. The specific listener to remove. If not provided, all listeners for the event will be removed.
|
|
1027
|
-
*/
|
|
1028
|
-
off(event, listener) {
|
|
1029
|
-
if (!event) {
|
|
1030
|
-
throw new Error("Event type is required");
|
|
1031
|
-
}
|
|
1032
|
-
if (listener) {
|
|
1033
|
-
this.eventEmitter.off(event, listener);
|
|
1034
|
-
console.log(
|
|
1035
|
-
`Specific listener removed for ${event} on bridge ${this.id}`
|
|
1036
|
-
);
|
|
1037
|
-
} else {
|
|
1038
|
-
this.eventEmitter.removeAllListeners(event);
|
|
1039
|
-
console.log(`All listeners removed for ${event} on bridge ${this.id}`);
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
/**
|
|
1043
|
-
* Emits an event if it corresponds to the current bridge.
|
|
1044
|
-
*
|
|
1045
|
-
* @param event - The WebSocketEvent to emit.
|
|
1046
|
-
*/
|
|
1047
|
-
emitEvent(event) {
|
|
1048
|
-
if (!event) {
|
|
1049
|
-
console.warn("Invalid event received");
|
|
1050
|
-
return;
|
|
1051
|
-
}
|
|
1052
|
-
if ("bridge" in event && event.bridge?.id === this.id) {
|
|
1053
|
-
this.eventEmitter.emit(event.type, event);
|
|
1054
|
-
console.log(`Event ${event.type} emitted for bridge ${this.id}`);
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
/**
|
|
1058
|
-
* Removes all event listeners from this bridge instance.
|
|
1059
|
-
*/
|
|
1060
|
-
removeAllListeners() {
|
|
1061
|
-
this.eventEmitter.removeAllListeners();
|
|
1062
|
-
console.log(`All listeners removed from bridge ${this.id}`);
|
|
1063
|
-
}
|
|
1064
|
-
/**
|
|
1065
|
-
* Retrieves the current details of the bridge.
|
|
1066
|
-
*
|
|
1067
|
-
* @returns A Promise that resolves to the Bridge object containing the current details.
|
|
1068
|
-
* @throws An error if the retrieval fails.
|
|
1069
|
-
*/
|
|
1070
|
-
async get() {
|
|
1071
|
-
try {
|
|
1072
|
-
if (!this.id) {
|
|
1073
|
-
throw new Error("No bridge associated with this instance");
|
|
1074
|
-
}
|
|
1075
|
-
this.bridgeData = await this.baseClient.get(
|
|
1076
|
-
`/bridges/${this.id}`
|
|
1077
|
-
);
|
|
1078
|
-
console.log(`Details retrieved for bridge ${this.id}`);
|
|
1079
|
-
return this.bridgeData;
|
|
1080
|
-
} catch (error) {
|
|
1081
|
-
const message = getErrorMessage(error);
|
|
1082
|
-
console.error(`Error retrieving details for bridge ${this.id}:`, message);
|
|
1083
|
-
throw new Error(`Failed to get bridge details: ${message}`);
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
/**
|
|
1087
|
-
* Adds channels to the bridge.
|
|
1088
|
-
*
|
|
1089
|
-
* @param request - The AddChannelRequest object containing the channels to add.
|
|
1090
|
-
* @throws An error if the operation fails.
|
|
1091
|
-
*/
|
|
1092
|
-
async add(request) {
|
|
1093
|
-
try {
|
|
1094
|
-
const queryParams = toQueryParams2({
|
|
1095
|
-
channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
|
|
1096
|
-
...request.role && { role: request.role }
|
|
1097
|
-
});
|
|
1098
|
-
await this.baseClient.post(
|
|
1099
|
-
`/bridges/${this.id}/addChannel?${queryParams}`
|
|
1100
|
-
);
|
|
1101
|
-
console.log(`Channels added to bridge ${this.id}`);
|
|
1102
|
-
} catch (error) {
|
|
1103
|
-
const message = getErrorMessage(error);
|
|
1104
|
-
console.error(`Error adding channels to bridge ${this.id}:`, message);
|
|
1105
|
-
throw new Error(`Failed to add channels: ${message}`);
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
/**
|
|
1109
|
-
* Removes channels from the bridge.
|
|
1110
|
-
*
|
|
1111
|
-
* @param request - The RemoveChannelRequest object containing the channels to remove.
|
|
1112
|
-
* @throws An error if the operation fails.
|
|
1113
|
-
*/
|
|
1114
|
-
async remove(request) {
|
|
1115
|
-
try {
|
|
1116
|
-
const queryParams = toQueryParams2({
|
|
1117
|
-
channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
|
|
1118
|
-
});
|
|
1119
|
-
await this.baseClient.post(
|
|
1120
|
-
`/bridges/${this.id}/removeChannel?${queryParams}`
|
|
1121
|
-
);
|
|
1122
|
-
console.log(`Channels removed from bridge ${this.id}`);
|
|
1123
|
-
} catch (error) {
|
|
1124
|
-
const message = getErrorMessage(error);
|
|
1125
|
-
console.error(`Error removing channels from bridge ${this.id}:`, message);
|
|
1126
|
-
throw new Error(`Failed to remove channels: ${message}`);
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
/**
|
|
1130
|
-
* Plays media on the bridge.
|
|
1131
|
-
*
|
|
1132
|
-
* @param request - The PlayMediaRequest object containing the media details to play.
|
|
1133
|
-
* @returns A Promise that resolves to a BridgePlayback object.
|
|
1134
|
-
* @throws An error if the operation fails.
|
|
1135
|
-
*/
|
|
1136
|
-
async playMedia(request) {
|
|
1137
|
-
try {
|
|
1138
|
-
const queryParams = new URLSearchParams({
|
|
1139
|
-
...request.lang && { lang: request.lang },
|
|
1140
|
-
...request.offsetms && { offsetms: request.offsetms.toString() },
|
|
1141
|
-
...request.skipms && { skipms: request.skipms.toString() },
|
|
1142
|
-
...request.playbackId && { playbackId: request.playbackId }
|
|
1143
|
-
}).toString();
|
|
1144
|
-
const result = await this.baseClient.post(
|
|
1145
|
-
`/bridges/${this.id}/play?${queryParams}`,
|
|
1146
|
-
{ media: request.media }
|
|
1147
|
-
);
|
|
1148
|
-
console.log(`Media playback started on bridge ${this.id}`);
|
|
1149
|
-
return result;
|
|
1150
|
-
} catch (error) {
|
|
1151
|
-
const message = getErrorMessage(error);
|
|
1152
|
-
console.error(`Error playing media on bridge ${this.id}:`, message);
|
|
1153
|
-
throw new Error(`Failed to play media: ${message}`);
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
/**
|
|
1157
|
-
* Stops media playback on the bridge.
|
|
1158
|
-
*
|
|
1159
|
-
* @param playbackId - The ID of the playback to stop.
|
|
1160
|
-
* @throws An error if the operation fails.
|
|
1161
|
-
*/
|
|
1162
|
-
async stopPlayback(playbackId) {
|
|
1163
|
-
try {
|
|
1164
|
-
await this.baseClient.delete(
|
|
1165
|
-
`/bridges/${this.id}/play/${playbackId}`
|
|
1166
|
-
);
|
|
1167
|
-
console.log(`Playback ${playbackId} stopped on bridge ${this.id}`);
|
|
1168
|
-
} catch (error) {
|
|
1169
|
-
const message = getErrorMessage(error);
|
|
1170
|
-
console.error(`Error stopping playback on bridge ${this.id}:`, message);
|
|
1171
|
-
throw new Error(`Failed to stop playback: ${message}`);
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
/**
|
|
1175
|
-
* Sets the video source for the bridge.
|
|
1176
|
-
*
|
|
1177
|
-
* @param channelId - The ID of the channel to set as the video source.
|
|
1178
|
-
* @throws An error if the operation fails.
|
|
1179
|
-
*/
|
|
1180
|
-
async setVideoSource(channelId) {
|
|
1181
|
-
try {
|
|
1182
|
-
await this.baseClient.post(
|
|
1183
|
-
`/bridges/${this.id}/videoSource/${channelId}`
|
|
1184
|
-
);
|
|
1185
|
-
console.log(`Video source set for bridge ${this.id}`);
|
|
1186
|
-
} catch (error) {
|
|
1187
|
-
const message = getErrorMessage(error);
|
|
1188
|
-
console.error(
|
|
1189
|
-
`Error setting video source for bridge ${this.id}:`,
|
|
1190
|
-
message
|
|
1191
|
-
);
|
|
1192
|
-
throw new Error(`Failed to set video source: ${message}`);
|
|
1193
|
-
}
|
|
1194
|
-
}
|
|
1195
|
-
/**
|
|
1196
|
-
* Removes the video source from the bridge.
|
|
1197
|
-
*
|
|
1198
|
-
* @throws An error if the operation fails.
|
|
1199
|
-
*/
|
|
1200
|
-
async clearVideoSource() {
|
|
1201
|
-
try {
|
|
1202
|
-
await this.baseClient.delete(`/bridges/${this.id}/videoSource`);
|
|
1203
|
-
console.log(`Video source removed from bridge ${this.id}`);
|
|
1204
|
-
} catch (error) {
|
|
1205
|
-
const message = getErrorMessage(error);
|
|
1206
|
-
console.error(
|
|
1207
|
-
`Error removing video source from bridge ${this.id}:`,
|
|
1208
|
-
message
|
|
1209
|
-
);
|
|
1210
|
-
throw new Error(`Failed to remove video source: ${message}`);
|
|
1211
|
-
}
|
|
1212
|
-
}
|
|
1213
|
-
/**
|
|
1214
|
-
* Checks if the bridge has listeners for a specific event.
|
|
1215
|
-
*
|
|
1216
|
-
* @param event - The event type to check for listeners.
|
|
1217
|
-
* @returns A boolean indicating whether there are listeners for the event.
|
|
1218
|
-
*/
|
|
1219
|
-
hasListeners(event) {
|
|
1220
|
-
return this.eventEmitter.listenerCount(event) > 0;
|
|
1221
|
-
}
|
|
1222
|
-
/**
|
|
1223
|
-
* Retrieves the current bridge data without making an API call.
|
|
1224
|
-
*
|
|
1225
|
-
* @returns The current Bridge object or null if no data is available.
|
|
1226
|
-
*/
|
|
1227
|
-
getCurrentData() {
|
|
1228
|
-
return this.bridgeData;
|
|
1229
|
-
}
|
|
1230
|
-
};
|
|
1231
915
|
var Bridges = class {
|
|
1232
|
-
constructor(
|
|
1233
|
-
this.baseClient = baseClient;
|
|
916
|
+
constructor(client) {
|
|
1234
917
|
this.client = client;
|
|
1235
918
|
}
|
|
1236
|
-
bridgeInstances = /* @__PURE__ */ new Map();
|
|
1237
|
-
/**
|
|
1238
|
-
* Creates or retrieves a Bridge instance.
|
|
1239
|
-
*
|
|
1240
|
-
* This method manages the creation and retrieval of BridgeInstance objects.
|
|
1241
|
-
* If an ID is provided and an instance with that ID already exists, it returns the existing instance.
|
|
1242
|
-
* If an ID is provided but no instance exists, it creates a new instance with that ID.
|
|
1243
|
-
* If no ID is provided, it creates a new instance with a generated ID.
|
|
1244
|
-
*
|
|
1245
|
-
* @param {Object} params - The parameters for creating or retrieving a Bridge instance.
|
|
1246
|
-
* @param {string} [params.id] - Optional. The ID of the Bridge instance to create or retrieve.
|
|
1247
|
-
*
|
|
1248
|
-
* @returns {BridgeInstance} A BridgeInstance object, either newly created or retrieved from existing instances.
|
|
1249
|
-
*
|
|
1250
|
-
* @throws {Error} If there's an error in creating or retrieving the Bridge instance.
|
|
1251
|
-
*/
|
|
1252
|
-
Bridge({ id }) {
|
|
1253
|
-
try {
|
|
1254
|
-
if (!id) {
|
|
1255
|
-
const instance = new BridgeInstance(this.client, this.baseClient);
|
|
1256
|
-
this.bridgeInstances.set(instance.id, instance);
|
|
1257
|
-
console.log(`New bridge instance created with ID: ${instance.id}`);
|
|
1258
|
-
return instance;
|
|
1259
|
-
}
|
|
1260
|
-
if (!this.bridgeInstances.has(id)) {
|
|
1261
|
-
const instance = new BridgeInstance(this.client, this.baseClient, id);
|
|
1262
|
-
this.bridgeInstances.set(id, instance);
|
|
1263
|
-
console.log(`New bridge instance created with provided ID: ${id}`);
|
|
1264
|
-
return instance;
|
|
1265
|
-
}
|
|
1266
|
-
console.log(`Returning existing bridge instance: ${id}`);
|
|
1267
|
-
return this.bridgeInstances.get(id);
|
|
1268
|
-
} catch (error) {
|
|
1269
|
-
const message = getErrorMessage(error);
|
|
1270
|
-
console.error(`Error creating/retrieving bridge instance:`, message);
|
|
1271
|
-
throw new Error(`Failed to manage bridge instance: ${message}`);
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
919
|
/**
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1277
|
-
* This function removes the specified bridge instance, cleans up its event listeners,
|
|
1278
|
-
* and logs the removal. If the bridge instance doesn't exist, it logs a warning.
|
|
1279
|
-
*
|
|
1280
|
-
* @param {string} bridgeId - The unique identifier of the bridge instance to be removed.
|
|
1281
|
-
* @throws {Error} Throws an error if the bridgeId is not provided.
|
|
1282
|
-
* @returns {void}
|
|
1283
|
-
*/
|
|
1284
|
-
removeBridgeInstance(bridgeId) {
|
|
1285
|
-
if (!bridgeId) {
|
|
1286
|
-
throw new Error("ID da bridge \xE9 obrigat\xF3rio");
|
|
1287
|
-
}
|
|
1288
|
-
if (this.bridgeInstances.has(bridgeId)) {
|
|
1289
|
-
const instance = this.bridgeInstances.get(bridgeId);
|
|
1290
|
-
instance?.removeAllListeners();
|
|
1291
|
-
this.bridgeInstances.delete(bridgeId);
|
|
1292
|
-
console.log(`Inst\xE2ncia de bridge removida: ${bridgeId}`);
|
|
1293
|
-
} else {
|
|
1294
|
-
console.warn(`Tentativa de remover inst\xE2ncia inexistente: ${bridgeId}`);
|
|
1295
|
-
}
|
|
1296
|
-
}
|
|
1297
|
-
/**
|
|
1298
|
-
* Propagates a WebSocket event to a specific bridge instance.
|
|
1299
|
-
*
|
|
1300
|
-
* This function checks if the received event is valid and related to a bridge,
|
|
1301
|
-
* then emits the event to the corresponding bridge instance if it exists.
|
|
1302
|
-
*
|
|
1303
|
-
* @param {WebSocketEvent} event - The WebSocket event to be propagated.
|
|
1304
|
-
* This should be an object containing information about the event,
|
|
1305
|
-
* including the bridge ID and event type.
|
|
1306
|
-
*
|
|
1307
|
-
* @returns {void}
|
|
1308
|
-
*
|
|
1309
|
-
* @remarks
|
|
1310
|
-
* - If the event is invalid (null or undefined), a warning is logged and the function returns early.
|
|
1311
|
-
* - The function checks if the event is bridge-related and if the event type is included in the predefined bridge events.
|
|
1312
|
-
* - If a matching bridge instance is found, the event is emitted to that instance.
|
|
1313
|
-
* - If no matching bridge instance is found, a warning is logged.
|
|
1314
|
-
*/
|
|
1315
|
-
propagateEventToBridge(event) {
|
|
1316
|
-
if (!event) {
|
|
1317
|
-
console.warn("Evento WebSocket inv\xE1lido recebido");
|
|
1318
|
-
return;
|
|
1319
|
-
}
|
|
1320
|
-
if ("bridge" in event && event.bridge?.id && bridgeEvents.includes(event.type)) {
|
|
1321
|
-
const instance = this.bridgeInstances.get(event.bridge.id);
|
|
1322
|
-
if (instance) {
|
|
1323
|
-
instance.emitEvent(event);
|
|
1324
|
-
console.log(
|
|
1325
|
-
`Evento propagado para bridge ${event.bridge.id}: ${event.type}`
|
|
1326
|
-
);
|
|
1327
|
-
} else {
|
|
1328
|
-
console.warn(
|
|
1329
|
-
`Nenhuma inst\xE2ncia encontrada para bridge ${event.bridge.id}`
|
|
1330
|
-
);
|
|
1331
|
-
}
|
|
1332
|
-
}
|
|
1333
|
-
}
|
|
1334
|
-
/**
|
|
1335
|
-
* Lists all active bridges in the system.
|
|
1336
|
-
*
|
|
1337
|
-
* This asynchronous function retrieves a list of all currently active bridges
|
|
1338
|
-
* by making a GET request to the "/bridges" endpoint using the base client.
|
|
1339
|
-
*
|
|
1340
|
-
* @returns {Promise<Bridge[]>} A promise that resolves to an array of Bridge objects.
|
|
1341
|
-
* Each Bridge object represents an active bridge in the system.
|
|
1342
|
-
*
|
|
1343
|
-
* @throws {Error} If there's an error in fetching the bridges or if the request fails.
|
|
1344
|
-
*
|
|
1345
|
-
* @example
|
|
1346
|
-
* try {
|
|
1347
|
-
* const bridges = await bridgesInstance.list();
|
|
1348
|
-
* console.log('Active bridges:', bridges);
|
|
1349
|
-
* } catch (error) {
|
|
1350
|
-
* console.error('Failed to fetch bridges:', error);
|
|
1351
|
-
* }
|
|
920
|
+
* Lists all active bridges.
|
|
1352
921
|
*/
|
|
1353
922
|
async list() {
|
|
1354
|
-
return this.
|
|
923
|
+
return this.client.get("/bridges");
|
|
1355
924
|
}
|
|
1356
925
|
/**
|
|
1357
|
-
* Creates a new bridge
|
|
1358
|
-
*
|
|
1359
|
-
* This asynchronous function sends a POST request to create a new bridge
|
|
1360
|
-
* using the provided configuration details.
|
|
1361
|
-
*
|
|
1362
|
-
* @param request - The configuration details for creating the new bridge.
|
|
1363
|
-
* @param request.type - The type of bridge to create (e.g., 'mixing', 'holding').
|
|
1364
|
-
* @param request.name - Optional. A custom name for the bridge.
|
|
1365
|
-
* @param request.bridgeId - Optional. A specific ID for the bridge. If not provided, one will be generated.
|
|
1366
|
-
*
|
|
1367
|
-
* @returns A Promise that resolves to a Bridge object representing the newly created bridge.
|
|
1368
|
-
* The Bridge object contains details such as id, technology, bridge_type, bridge_class, channels, etc.
|
|
1369
|
-
*
|
|
1370
|
-
* @throws Will throw an error if the bridge creation fails or if there's a network issue.
|
|
926
|
+
* Creates a new bridge.
|
|
1371
927
|
*/
|
|
1372
928
|
async createBridge(request) {
|
|
1373
|
-
return this.
|
|
929
|
+
return this.client.post("/bridges", request);
|
|
1374
930
|
}
|
|
1375
931
|
/**
|
|
1376
|
-
* Retrieves
|
|
1377
|
-
*
|
|
1378
|
-
* This asynchronous function fetches the complete details of a bridge
|
|
1379
|
-
* identified by its unique ID. It makes a GET request to the ARI endpoint
|
|
1380
|
-
* for the specified bridge.
|
|
1381
|
-
*
|
|
1382
|
-
* @param bridgeId - The unique identifier of the bridge to retrieve details for.
|
|
1383
|
-
* This should be a string that uniquely identifies the bridge in the system.
|
|
1384
|
-
*
|
|
1385
|
-
* @returns A Promise that resolves to a Bridge object containing all the details
|
|
1386
|
-
* of the specified bridge. This includes information such as the bridge's
|
|
1387
|
-
* ID, type, channels, and other relevant properties.
|
|
1388
|
-
*
|
|
1389
|
-
* @throws Will throw an error if the bridge cannot be found, if there's a network issue,
|
|
1390
|
-
* or if the server responds with an error.
|
|
932
|
+
* Retrieves details of a specific bridge.
|
|
1391
933
|
*/
|
|
1392
|
-
async
|
|
1393
|
-
return this.
|
|
934
|
+
async getDetails(bridgeId) {
|
|
935
|
+
return this.client.get(`/bridges/${bridgeId}`);
|
|
1394
936
|
}
|
|
1395
937
|
/**
|
|
1396
|
-
* Destroys (deletes) a specific bridge
|
|
1397
|
-
*
|
|
1398
|
-
* This asynchronous function sends a DELETE request to remove a bridge
|
|
1399
|
-
* identified by its unique ID. Once destroyed, the bridge and all its
|
|
1400
|
-
* associated resources are permanently removed from the system.
|
|
1401
|
-
*
|
|
1402
|
-
* @param bridgeId - The unique identifier of the bridge to be destroyed.
|
|
1403
|
-
* This should be a string that uniquely identifies the bridge in the system.
|
|
1404
|
-
*
|
|
1405
|
-
* @returns A Promise that resolves to void when the bridge is successfully destroyed.
|
|
1406
|
-
* If the operation is successful, the bridge no longer exists in the system.
|
|
1407
|
-
*
|
|
1408
|
-
* @throws Will throw an error if the bridge cannot be found, if there's a network issue,
|
|
1409
|
-
* or if the server responds with an error during the deletion process.
|
|
938
|
+
* Destroys (deletes) a specific bridge.
|
|
1410
939
|
*/
|
|
1411
940
|
async destroy(bridgeId) {
|
|
1412
|
-
return this.
|
|
941
|
+
return this.client.delete(`/bridges/${bridgeId}`);
|
|
1413
942
|
}
|
|
1414
943
|
/**
|
|
1415
|
-
* Adds
|
|
1416
|
-
*
|
|
1417
|
-
* This asynchronous function sends a POST request to add channels to an existing bridge.
|
|
1418
|
-
* It can handle adding a single channel or multiple channels in one operation.
|
|
1419
|
-
*
|
|
1420
|
-
* @param bridgeId - The unique identifier of the bridge to which channels will be added.
|
|
1421
|
-
* @param request - An object containing the details of the channel(s) to be added.
|
|
1422
|
-
* @param request.channel - A single channel ID or an array of channel IDs to add to the bridge.
|
|
1423
|
-
* @param request.role - Optional. Specifies the role of the channel(s) in the bridge.
|
|
1424
|
-
*
|
|
1425
|
-
* @returns A Promise that resolves to void when the operation is successful.
|
|
1426
|
-
*
|
|
1427
|
-
* @throws Will throw an error if the request fails, such as if the bridge doesn't exist
|
|
1428
|
-
* or if there's a network issue.
|
|
944
|
+
* Adds a channel or multiple channels to a bridge.
|
|
1429
945
|
*/
|
|
1430
946
|
async addChannels(bridgeId, request) {
|
|
1431
|
-
const queryParams =
|
|
947
|
+
const queryParams = new URLSearchParams({
|
|
1432
948
|
channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel,
|
|
1433
949
|
...request.role && { role: request.role }
|
|
1434
|
-
});
|
|
1435
|
-
await this.
|
|
950
|
+
}).toString();
|
|
951
|
+
await this.client.post(
|
|
1436
952
|
`/bridges/${bridgeId}/addChannel?${queryParams}`
|
|
1437
953
|
);
|
|
1438
954
|
}
|
|
1439
955
|
/**
|
|
1440
|
-
* Removes
|
|
1441
|
-
*
|
|
1442
|
-
* This asynchronous function sends a POST request to remove channels from an existing bridge.
|
|
1443
|
-
* It can handle removing a single channel or multiple channels in one operation.
|
|
1444
|
-
*
|
|
1445
|
-
* @param bridgeId - The unique identifier of the bridge from which channels will be removed.
|
|
1446
|
-
* @param request - An object containing the details of the channel(s) to be removed.
|
|
1447
|
-
* @param request.channel - A single channel ID or an array of channel IDs to remove from the bridge.
|
|
1448
|
-
*
|
|
1449
|
-
* @returns A Promise that resolves to void when the operation is successful.
|
|
1450
|
-
*
|
|
1451
|
-
* @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
|
|
1452
|
-
* if the channels are not in the bridge, or if there's a network issue.
|
|
956
|
+
* Removes a channel or multiple channels from a bridge.
|
|
1453
957
|
*/
|
|
1454
958
|
async removeChannels(bridgeId, request) {
|
|
1455
|
-
const queryParams =
|
|
959
|
+
const queryParams = new URLSearchParams({
|
|
1456
960
|
channel: Array.isArray(request.channel) ? request.channel.join(",") : request.channel
|
|
1457
|
-
});
|
|
1458
|
-
await this.
|
|
961
|
+
}).toString();
|
|
962
|
+
await this.client.post(
|
|
1459
963
|
`/bridges/${bridgeId}/removeChannel?${queryParams}`
|
|
1460
964
|
);
|
|
1461
965
|
}
|
|
1462
966
|
/**
|
|
1463
|
-
* Plays media
|
|
1464
|
-
*
|
|
1465
|
-
* This asynchronous function initiates media playback on a bridge identified by its ID.
|
|
1466
|
-
* It allows for customization of the playback through various options in the request.
|
|
1467
|
-
*
|
|
1468
|
-
* @param bridgeId - The unique identifier of the bridge on which to play the media.
|
|
1469
|
-
* @param request - An object containing the media playback request details.
|
|
1470
|
-
* @param request.media - The media to be played (e.g., sound file, URL).
|
|
1471
|
-
* @param request.lang - Optional. The language of the media content.
|
|
1472
|
-
* @param request.offsetms - Optional. The offset in milliseconds to start playing from.
|
|
1473
|
-
* @param request.skipms - Optional. The number of milliseconds to skip before playing.
|
|
1474
|
-
* @param request.playbackId - Optional. A custom ID for the playback session.
|
|
1475
|
-
*
|
|
1476
|
-
* @returns A Promise that resolves to a BridgePlayback object, containing details about the initiated playback.
|
|
1477
|
-
*
|
|
1478
|
-
* @throws Will throw an error if the playback request fails or if there's a network issue.
|
|
967
|
+
* Plays media to a bridge.
|
|
1479
968
|
*/
|
|
1480
969
|
async playMedia(bridgeId, request) {
|
|
1481
|
-
const queryParams =
|
|
970
|
+
const queryParams = new URLSearchParams({
|
|
1482
971
|
...request.lang && { lang: request.lang },
|
|
1483
972
|
...request.offsetms && { offsetms: request.offsetms.toString() },
|
|
1484
973
|
...request.skipms && { skipms: request.skipms.toString() },
|
|
1485
974
|
...request.playbackId && { playbackId: request.playbackId }
|
|
1486
|
-
});
|
|
1487
|
-
return this.
|
|
975
|
+
}).toString();
|
|
976
|
+
return this.client.post(
|
|
1488
977
|
`/bridges/${bridgeId}/play?${queryParams}`,
|
|
1489
978
|
{ media: request.media }
|
|
1490
979
|
);
|
|
1491
980
|
}
|
|
1492
981
|
/**
|
|
1493
|
-
* Stops media playback on a
|
|
1494
|
-
*
|
|
1495
|
-
* This asynchronous function sends a DELETE request to stop the playback of media
|
|
1496
|
-
* on a bridge identified by its ID and a specific playback session.
|
|
1497
|
-
*
|
|
1498
|
-
* @param bridgeId - The unique identifier of the bridge where the playback is to be stopped.
|
|
1499
|
-
* @param playbackId - The unique identifier of the playback session to be stopped.
|
|
1500
|
-
*
|
|
1501
|
-
* @returns A Promise that resolves to void when the playback is successfully stopped.
|
|
1502
|
-
*
|
|
1503
|
-
* @throws Will throw an error if the request fails, such as if the bridge or playback session
|
|
1504
|
-
* doesn't exist, or if there's a network issue.
|
|
982
|
+
* Stops media playback on a bridge.
|
|
1505
983
|
*/
|
|
1506
984
|
async stopPlayback(bridgeId, playbackId) {
|
|
1507
|
-
await this.
|
|
1508
|
-
`/bridges/${bridgeId}/play/${playbackId}`
|
|
1509
|
-
);
|
|
985
|
+
await this.client.delete(`/bridges/${bridgeId}/play/${playbackId}`);
|
|
1510
986
|
}
|
|
1511
987
|
/**
|
|
1512
|
-
* Sets the video source for a
|
|
1513
|
-
*
|
|
1514
|
-
* This asynchronous function configures a channel as the video source for a given bridge.
|
|
1515
|
-
* It sends a POST request to the ARI endpoint to update the bridge's video source.
|
|
1516
|
-
*
|
|
1517
|
-
* @param bridgeId - The unique identifier of the bridge for which to set the video source.
|
|
1518
|
-
* @param channelId - The unique identifier of the channel to be set as the video source.
|
|
1519
|
-
*
|
|
1520
|
-
* @returns A Promise that resolves to void when the video source is successfully set.
|
|
1521
|
-
*
|
|
1522
|
-
* @throws Will throw an error if the request fails, such as if the bridge or channel
|
|
1523
|
-
* doesn't exist, or if there's a network issue.
|
|
988
|
+
* Sets the video source for a bridge.
|
|
1524
989
|
*/
|
|
1525
990
|
async setVideoSource(bridgeId, channelId) {
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
`/bridges/${bridgeId}/videoSource?${queryParams}`
|
|
991
|
+
await this.client.post(
|
|
992
|
+
`/bridges/${bridgeId}/videoSource?channelId=${encodeURIComponent(channelId)}`
|
|
1529
993
|
);
|
|
1530
994
|
}
|
|
1531
995
|
/**
|
|
1532
|
-
* Clears the video source for a
|
|
1533
|
-
*
|
|
1534
|
-
* This asynchronous function removes the currently set video source from a bridge.
|
|
1535
|
-
* It sends a DELETE request to the ARI endpoint to clear the video source configuration.
|
|
1536
|
-
*
|
|
1537
|
-
* @param bridgeId - The unique identifier of the bridge from which to clear the video source.
|
|
1538
|
-
* This should be a string that uniquely identifies the bridge in the system.
|
|
1539
|
-
*
|
|
1540
|
-
* @returns A Promise that resolves to void when the video source is successfully cleared.
|
|
1541
|
-
* If the operation is successful, the bridge will no longer have a designated video source.
|
|
1542
|
-
*
|
|
1543
|
-
* @throws Will throw an error if the request fails, such as if the bridge doesn't exist,
|
|
1544
|
-
* if there's no video source set, or if there's a network issue.
|
|
996
|
+
* Clears the video source for a bridge.
|
|
1545
997
|
*/
|
|
1546
998
|
async clearVideoSource(bridgeId) {
|
|
1547
|
-
await this.
|
|
1548
|
-
}
|
|
1549
|
-
/**
|
|
1550
|
-
* Retrieves the count of active bridge instances.
|
|
1551
|
-
*
|
|
1552
|
-
* This function returns the total number of bridge instances currently
|
|
1553
|
-
* managed by the Bridges class. It provides a quick way to check how many
|
|
1554
|
-
* active bridges are present in the system.
|
|
1555
|
-
*
|
|
1556
|
-
* @returns {number} The count of active bridge instances.
|
|
1557
|
-
*/
|
|
1558
|
-
getInstanceCount() {
|
|
1559
|
-
return this.bridgeInstances.size;
|
|
1560
|
-
}
|
|
1561
|
-
/**
|
|
1562
|
-
* Checks if a bridge instance exists in the collection of managed bridges.
|
|
1563
|
-
*
|
|
1564
|
-
* This function verifies whether a bridge instance with the specified ID
|
|
1565
|
-
* is currently being managed by the Bridges class.
|
|
1566
|
-
*
|
|
1567
|
-
* @param bridgeId - The unique identifier of the bridge instance to check.
|
|
1568
|
-
* This should be a string that uniquely identifies the bridge in the system.
|
|
1569
|
-
*
|
|
1570
|
-
* @returns A boolean value indicating whether the bridge instance exists.
|
|
1571
|
-
* Returns true if the bridge instance is found, false otherwise.
|
|
1572
|
-
*/
|
|
1573
|
-
hasInstance(bridgeId) {
|
|
1574
|
-
return this.bridgeInstances.has(bridgeId);
|
|
1575
|
-
}
|
|
1576
|
-
/**
|
|
1577
|
-
* Retrieves all active bridge instances currently managed by the Bridges class.
|
|
1578
|
-
*
|
|
1579
|
-
* This method provides a way to access all the BridgeInstance objects that are
|
|
1580
|
-
* currently active and being managed. It returns a new Map to prevent direct
|
|
1581
|
-
* modification of the internal bridgeInstances collection.
|
|
1582
|
-
*
|
|
1583
|
-
* @returns A new Map object containing all active bridge instances, where the keys
|
|
1584
|
-
* are the bridge IDs (strings) and the values are the corresponding
|
|
1585
|
-
* BridgeInstance objects. If no bridges are active, an empty Map is returned.
|
|
1586
|
-
*/
|
|
1587
|
-
getAllInstances() {
|
|
1588
|
-
return new Map(this.bridgeInstances);
|
|
999
|
+
await this.client.delete(`/bridges/${bridgeId}/videoSource`);
|
|
1589
1000
|
}
|
|
1590
1001
|
};
|
|
1591
1002
|
|
|
1592
1003
|
// src/ari-client/resources/channels.ts
|
|
1593
|
-
import { EventEmitter
|
|
1594
|
-
import { isAxiosError as
|
|
1004
|
+
import { EventEmitter } from "events";
|
|
1005
|
+
import { isAxiosError as isAxiosError2 } from "axios";
|
|
1595
1006
|
|
|
1596
1007
|
// node_modules/uuid/dist/esm/stringify.js
|
|
1597
1008
|
var byteToHex = [];
|
|
@@ -1638,9 +1049,16 @@ function v4(options, buf, offset) {
|
|
|
1638
1049
|
}
|
|
1639
1050
|
var v4_default = v4;
|
|
1640
1051
|
|
|
1052
|
+
// src/ari-client/utils.ts
|
|
1053
|
+
function toQueryParams2(options) {
|
|
1054
|
+
return new URLSearchParams(
|
|
1055
|
+
Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
|
|
1056
|
+
).toString();
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1641
1059
|
// src/ari-client/resources/channels.ts
|
|
1642
|
-
var
|
|
1643
|
-
if (
|
|
1060
|
+
var getErrorMessage = (error) => {
|
|
1061
|
+
if (isAxiosError2(error)) {
|
|
1644
1062
|
return error.response?.data?.message || error.message || "An axios error occurred";
|
|
1645
1063
|
}
|
|
1646
1064
|
if (error instanceof Error) {
|
|
@@ -1653,9 +1071,8 @@ var ChannelInstance = class {
|
|
|
1653
1071
|
this.client = client;
|
|
1654
1072
|
this.baseClient = baseClient;
|
|
1655
1073
|
this.id = channelId || `channel-${Date.now()}`;
|
|
1656
|
-
console.log(`Channel instance initialized with ID: ${this.id}`);
|
|
1657
1074
|
}
|
|
1658
|
-
eventEmitter = new
|
|
1075
|
+
eventEmitter = new EventEmitter();
|
|
1659
1076
|
channelData = null;
|
|
1660
1077
|
id;
|
|
1661
1078
|
/**
|
|
@@ -1671,7 +1088,6 @@ var ChannelInstance = class {
|
|
|
1671
1088
|
}
|
|
1672
1089
|
};
|
|
1673
1090
|
this.eventEmitter.on(event, wrappedListener);
|
|
1674
|
-
console.log(`Event listener registered for ${event} on channel ${this.id}`);
|
|
1675
1091
|
}
|
|
1676
1092
|
/**
|
|
1677
1093
|
* Registers a one-time event listener
|
|
@@ -1686,9 +1102,6 @@ var ChannelInstance = class {
|
|
|
1686
1102
|
}
|
|
1687
1103
|
};
|
|
1688
1104
|
this.eventEmitter.once(event, wrappedListener);
|
|
1689
|
-
console.log(
|
|
1690
|
-
`One-time event listener registered for ${event} on channel ${this.id}`
|
|
1691
|
-
);
|
|
1692
1105
|
}
|
|
1693
1106
|
/**
|
|
1694
1107
|
* Removes event listener(s) for a specific WebSocket event type.
|
|
@@ -1705,12 +1118,8 @@ var ChannelInstance = class {
|
|
|
1705
1118
|
}
|
|
1706
1119
|
if (listener) {
|
|
1707
1120
|
this.eventEmitter.off(event, listener);
|
|
1708
|
-
console.log(
|
|
1709
|
-
`Specific listener removed for ${event} on channel ${this.id}`
|
|
1710
|
-
);
|
|
1711
1121
|
} else {
|
|
1712
1122
|
this.eventEmitter.removeAllListeners(event);
|
|
1713
|
-
console.log(`All listeners removed for ${event} on channel ${this.id}`);
|
|
1714
1123
|
}
|
|
1715
1124
|
}
|
|
1716
1125
|
/**
|
|
@@ -1723,7 +1132,6 @@ var ChannelInstance = class {
|
|
|
1723
1132
|
}
|
|
1724
1133
|
if ("channel" in event && event.channel?.id === this.id) {
|
|
1725
1134
|
this.eventEmitter.emit(event.type, event);
|
|
1726
|
-
console.log(`Event ${event.type} emitted for channel ${this.id}`);
|
|
1727
1135
|
}
|
|
1728
1136
|
}
|
|
1729
1137
|
/**
|
|
@@ -1733,7 +1141,6 @@ var ChannelInstance = class {
|
|
|
1733
1141
|
* @return {void} This method does not return a value.
|
|
1734
1142
|
*/
|
|
1735
1143
|
removeAllListeners() {
|
|
1736
|
-
console.log(`Removendo todos os listeners para o canal ${this.id}`);
|
|
1737
1144
|
this.eventEmitter.removeAllListeners();
|
|
1738
1145
|
}
|
|
1739
1146
|
/**
|
|
@@ -1742,9 +1149,8 @@ var ChannelInstance = class {
|
|
|
1742
1149
|
async answer() {
|
|
1743
1150
|
try {
|
|
1744
1151
|
await this.baseClient.post(`/channels/${this.id}/answer`);
|
|
1745
|
-
console.log(`Channel ${this.id} answered`);
|
|
1746
1152
|
} catch (error) {
|
|
1747
|
-
const message =
|
|
1153
|
+
const message = getErrorMessage(error);
|
|
1748
1154
|
console.error(`Error answering channel ${this.id}:`, message);
|
|
1749
1155
|
throw new Error(`Failed to answer channel: ${message}`);
|
|
1750
1156
|
}
|
|
@@ -1765,12 +1171,9 @@ var ChannelInstance = class {
|
|
|
1765
1171
|
"/channels",
|
|
1766
1172
|
data
|
|
1767
1173
|
);
|
|
1768
|
-
console.log(
|
|
1769
|
-
`Channel originated successfully with ID: ${this.channelData.id}`
|
|
1770
|
-
);
|
|
1771
1174
|
return this.channelData;
|
|
1772
1175
|
} catch (error) {
|
|
1773
|
-
const message =
|
|
1176
|
+
const message = getErrorMessage(error);
|
|
1774
1177
|
console.error(`Error originating channel:`, message);
|
|
1775
1178
|
throw new Error(`Failed to originate channel: ${message}`);
|
|
1776
1179
|
}
|
|
@@ -1784,7 +1187,6 @@ var ChannelInstance = class {
|
|
|
1784
1187
|
}
|
|
1785
1188
|
try {
|
|
1786
1189
|
if (!this.channelData) {
|
|
1787
|
-
console.log("Initializing channel details...");
|
|
1788
1190
|
this.channelData = await this.getDetails();
|
|
1789
1191
|
}
|
|
1790
1192
|
const playback = this.client.Playback(playbackId || v4_default());
|
|
@@ -1792,10 +1194,9 @@ var ChannelInstance = class {
|
|
|
1792
1194
|
`/channels/${this.id}/play/${playback.id}`,
|
|
1793
1195
|
options
|
|
1794
1196
|
);
|
|
1795
|
-
console.log(`Media playback started on channel ${this.id}`);
|
|
1796
1197
|
return playback;
|
|
1797
1198
|
} catch (error) {
|
|
1798
|
-
const message =
|
|
1199
|
+
const message = getErrorMessage(error);
|
|
1799
1200
|
console.error(`Error playing media on channel ${this.id}:`, message);
|
|
1800
1201
|
throw new Error(`Failed to play media: ${message}`);
|
|
1801
1202
|
}
|
|
@@ -1815,10 +1216,9 @@ var ChannelInstance = class {
|
|
|
1815
1216
|
`/channels/${this.id}`
|
|
1816
1217
|
);
|
|
1817
1218
|
this.channelData = details;
|
|
1818
|
-
console.log(`Retrieved channel details for ${this.id}`);
|
|
1819
1219
|
return details;
|
|
1820
1220
|
} catch (error) {
|
|
1821
|
-
const message =
|
|
1221
|
+
const message = getErrorMessage(error);
|
|
1822
1222
|
console.error(
|
|
1823
1223
|
`Error retrieving channel details for ${this.id}:`,
|
|
1824
1224
|
message
|
|
@@ -1862,7 +1262,6 @@ var ChannelInstance = class {
|
|
|
1862
1262
|
*/
|
|
1863
1263
|
async hangup() {
|
|
1864
1264
|
if (!this.channelData) {
|
|
1865
|
-
console.log("Canal n\xE3o inicializado, buscando detalhes...");
|
|
1866
1265
|
this.channelData = await this.getDetails();
|
|
1867
1266
|
}
|
|
1868
1267
|
if (!this.channelData?.id) {
|
|
@@ -2051,19 +1450,16 @@ var Channels = class {
|
|
|
2051
1450
|
if (!id) {
|
|
2052
1451
|
const instance = new ChannelInstance(this.client, this.baseClient);
|
|
2053
1452
|
this.channelInstances.set(instance.id, instance);
|
|
2054
|
-
console.log(`New channel instance created with ID: ${instance.id}`);
|
|
2055
1453
|
return instance;
|
|
2056
1454
|
}
|
|
2057
1455
|
if (!this.channelInstances.has(id)) {
|
|
2058
1456
|
const instance = new ChannelInstance(this.client, this.baseClient, id);
|
|
2059
1457
|
this.channelInstances.set(id, instance);
|
|
2060
|
-
console.log(`New channel instance created with provided ID: ${id}`);
|
|
2061
1458
|
return instance;
|
|
2062
1459
|
}
|
|
2063
|
-
console.log(`Returning existing channel instance: ${id}`);
|
|
2064
1460
|
return this.channelInstances.get(id);
|
|
2065
1461
|
} catch (error) {
|
|
2066
|
-
const message =
|
|
1462
|
+
const message = getErrorMessage(error);
|
|
2067
1463
|
console.error(`Error creating/retrieving channel instance:`, message);
|
|
2068
1464
|
throw new Error(`Failed to manage channel instance: ${message}`);
|
|
2069
1465
|
}
|
|
@@ -2080,11 +1476,9 @@ var Channels = class {
|
|
|
2080
1476
|
if (!id) {
|
|
2081
1477
|
throw new Error("No channel ID associated with this instance");
|
|
2082
1478
|
}
|
|
2083
|
-
|
|
2084
|
-
console.log(`Retrieved channel details for ${id}`);
|
|
2085
|
-
return details;
|
|
1479
|
+
return await this.baseClient.get(`/channels/${id}`);
|
|
2086
1480
|
} catch (error) {
|
|
2087
|
-
const message =
|
|
1481
|
+
const message = getErrorMessage(error);
|
|
2088
1482
|
console.error(`Error retrieving channel details for ${id}:`, message);
|
|
2089
1483
|
throw new Error(`Failed to get channel details: ${message}`);
|
|
2090
1484
|
}
|
|
@@ -2100,7 +1494,6 @@ var Channels = class {
|
|
|
2100
1494
|
const instance = this.channelInstances.get(channelId);
|
|
2101
1495
|
instance?.removeAllListeners();
|
|
2102
1496
|
this.channelInstances.delete(channelId);
|
|
2103
|
-
console.log(`Channel instance removed: ${channelId}`);
|
|
2104
1497
|
} else {
|
|
2105
1498
|
console.warn(`Attempt to remove non-existent instance: ${channelId}`);
|
|
2106
1499
|
}
|
|
@@ -2117,9 +1510,6 @@ var Channels = class {
|
|
|
2117
1510
|
const instance = this.channelInstances.get(event.channel.id);
|
|
2118
1511
|
if (instance) {
|
|
2119
1512
|
instance.emitEvent(event);
|
|
2120
|
-
console.log(
|
|
2121
|
-
`Event propagated to channel ${event.channel.id}: ${event.type}`
|
|
2122
|
-
);
|
|
2123
1513
|
} else {
|
|
2124
1514
|
console.warn(`No instance found for channel ${event.channel.id}`);
|
|
2125
1515
|
}
|
|
@@ -2133,11 +1523,9 @@ var Channels = class {
|
|
|
2133
1523
|
throw new Error("Endpoint is required for channel origination");
|
|
2134
1524
|
}
|
|
2135
1525
|
try {
|
|
2136
|
-
|
|
2137
|
-
console.log(`Channel originated successfully with ID: ${channel.id}`);
|
|
2138
|
-
return channel;
|
|
1526
|
+
return await this.baseClient.post("/channels", data);
|
|
2139
1527
|
} catch (error) {
|
|
2140
|
-
const message =
|
|
1528
|
+
const message = getErrorMessage(error);
|
|
2141
1529
|
console.error(`Error originating channel:`, message);
|
|
2142
1530
|
throw new Error(`Failed to originate channel: ${message}`);
|
|
2143
1531
|
}
|
|
@@ -2151,10 +1539,9 @@ var Channels = class {
|
|
|
2151
1539
|
if (!Array.isArray(channels)) {
|
|
2152
1540
|
throw new Error("API response for /channels is not an array");
|
|
2153
1541
|
}
|
|
2154
|
-
console.log(`Retrieved ${channels.length} active channels`);
|
|
2155
1542
|
return channels;
|
|
2156
1543
|
} catch (error) {
|
|
2157
|
-
const message =
|
|
1544
|
+
const message = getErrorMessage(error);
|
|
2158
1545
|
console.error(`Error listing channels:`, message);
|
|
2159
1546
|
throw new Error(`Failed to list channels: ${message}`);
|
|
2160
1547
|
}
|
|
@@ -2581,10 +1968,10 @@ var Endpoints = class {
|
|
|
2581
1968
|
};
|
|
2582
1969
|
|
|
2583
1970
|
// src/ari-client/resources/playbacks.ts
|
|
2584
|
-
import { EventEmitter as
|
|
2585
|
-
import { isAxiosError as
|
|
2586
|
-
var
|
|
2587
|
-
if (
|
|
1971
|
+
import { EventEmitter as EventEmitter2 } from "events";
|
|
1972
|
+
import { isAxiosError as isAxiosError3 } from "axios";
|
|
1973
|
+
var getErrorMessage2 = (error) => {
|
|
1974
|
+
if (isAxiosError3(error)) {
|
|
2588
1975
|
return error.response?.data?.message || error.message || "An axios error occurred";
|
|
2589
1976
|
}
|
|
2590
1977
|
if (error instanceof Error) {
|
|
@@ -2605,9 +1992,8 @@ var PlaybackInstance = class {
|
|
|
2605
1992
|
this.baseClient = baseClient;
|
|
2606
1993
|
this.playbackId = playbackId;
|
|
2607
1994
|
this.id = playbackId;
|
|
2608
|
-
console.log(`PlaybackInstance initialized with ID: ${this.id}`);
|
|
2609
1995
|
}
|
|
2610
|
-
eventEmitter = new
|
|
1996
|
+
eventEmitter = new EventEmitter2();
|
|
2611
1997
|
playbackData = null;
|
|
2612
1998
|
id;
|
|
2613
1999
|
/**
|
|
@@ -2626,9 +2012,6 @@ var PlaybackInstance = class {
|
|
|
2626
2012
|
}
|
|
2627
2013
|
};
|
|
2628
2014
|
this.eventEmitter.on(event, wrappedListener);
|
|
2629
|
-
console.log(
|
|
2630
|
-
`Event listener registered for ${event} on playback ${this.id}`
|
|
2631
|
-
);
|
|
2632
2015
|
}
|
|
2633
2016
|
/**
|
|
2634
2017
|
* Registers a one-time event listener for a specific WebSocket event type.
|
|
@@ -2646,9 +2029,6 @@ var PlaybackInstance = class {
|
|
|
2646
2029
|
}
|
|
2647
2030
|
};
|
|
2648
2031
|
this.eventEmitter.once(event, wrappedListener);
|
|
2649
|
-
console.log(
|
|
2650
|
-
`One-time event listener registered for ${event} on playback ${this.id}`
|
|
2651
|
-
);
|
|
2652
2032
|
}
|
|
2653
2033
|
/**
|
|
2654
2034
|
* Removes event listener(s) for a specific WebSocket event type.
|
|
@@ -2662,12 +2042,8 @@ var PlaybackInstance = class {
|
|
|
2662
2042
|
}
|
|
2663
2043
|
if (listener) {
|
|
2664
2044
|
this.eventEmitter.off(event, listener);
|
|
2665
|
-
console.log(
|
|
2666
|
-
`Specific listener removed for ${event} on playback ${this.id}`
|
|
2667
|
-
);
|
|
2668
2045
|
} else {
|
|
2669
2046
|
this.eventEmitter.removeAllListeners(event);
|
|
2670
|
-
console.log(`All listeners removed for ${event} on playback ${this.id}`);
|
|
2671
2047
|
}
|
|
2672
2048
|
}
|
|
2673
2049
|
/**
|
|
@@ -2682,7 +2058,6 @@ var PlaybackInstance = class {
|
|
|
2682
2058
|
}
|
|
2683
2059
|
if ("playback" in event && event.playback?.id === this.id) {
|
|
2684
2060
|
this.eventEmitter.emit(event.type, event);
|
|
2685
|
-
console.log(`Event ${event.type} emitted for playback ${this.id}`);
|
|
2686
2061
|
}
|
|
2687
2062
|
}
|
|
2688
2063
|
/**
|
|
@@ -2699,11 +2074,10 @@ var PlaybackInstance = class {
|
|
|
2699
2074
|
this.playbackData = await this.baseClient.get(
|
|
2700
2075
|
`/playbacks/${this.id}`
|
|
2701
2076
|
);
|
|
2702
|
-
console.log(`Retrieved playback data for ${this.id}`);
|
|
2703
2077
|
return this.playbackData;
|
|
2704
2078
|
} catch (error) {
|
|
2705
|
-
const message =
|
|
2706
|
-
console.
|
|
2079
|
+
const message = getErrorMessage2(error);
|
|
2080
|
+
console.warn(`Error retrieving playback data for ${this.id}:`, message);
|
|
2707
2081
|
throw new Error(`Failed to get playback data: ${message}`);
|
|
2708
2082
|
}
|
|
2709
2083
|
}
|
|
@@ -2721,12 +2095,9 @@ var PlaybackInstance = class {
|
|
|
2721
2095
|
await this.baseClient.post(
|
|
2722
2096
|
`/playbacks/${this.id}/control?operation=${operation}`
|
|
2723
2097
|
);
|
|
2724
|
-
console.log(
|
|
2725
|
-
`Operation ${operation} executed successfully on playback ${this.id}`
|
|
2726
|
-
);
|
|
2727
2098
|
} catch (error) {
|
|
2728
|
-
const message =
|
|
2729
|
-
console.
|
|
2099
|
+
const message = getErrorMessage2(error);
|
|
2100
|
+
console.warn(`Error controlling playback ${this.id}:`, message);
|
|
2730
2101
|
throw new Error(`Failed to control playback: ${message}`);
|
|
2731
2102
|
}
|
|
2732
2103
|
}
|
|
@@ -2741,10 +2112,9 @@ var PlaybackInstance = class {
|
|
|
2741
2112
|
}
|
|
2742
2113
|
try {
|
|
2743
2114
|
await this.baseClient.delete(`/playbacks/${this.id}`);
|
|
2744
|
-
console.log(`Playback ${this.id} stopped successfully`);
|
|
2745
2115
|
} catch (error) {
|
|
2746
|
-
const message =
|
|
2747
|
-
console.
|
|
2116
|
+
const message = getErrorMessage2(error);
|
|
2117
|
+
console.warn(`Error stopping playback ${this.id}:`, message);
|
|
2748
2118
|
throw new Error(`Failed to stop playback: ${message}`);
|
|
2749
2119
|
}
|
|
2750
2120
|
}
|
|
@@ -2753,7 +2123,6 @@ var PlaybackInstance = class {
|
|
|
2753
2123
|
*/
|
|
2754
2124
|
removeAllListeners() {
|
|
2755
2125
|
this.eventEmitter.removeAllListeners();
|
|
2756
|
-
console.log(`All listeners removed from playback ${this.id}`);
|
|
2757
2126
|
}
|
|
2758
2127
|
/**
|
|
2759
2128
|
* Checks if the playback instance has any listeners for a specific event.
|
|
@@ -2791,20 +2160,17 @@ var Playbacks = class {
|
|
|
2791
2160
|
if (!id) {
|
|
2792
2161
|
const instance = new PlaybackInstance(this.client, this.baseClient);
|
|
2793
2162
|
this.playbackInstances.set(instance.id, instance);
|
|
2794
|
-
console.log(`New playback instance created with ID: ${instance.id}`);
|
|
2795
2163
|
return instance;
|
|
2796
2164
|
}
|
|
2797
2165
|
if (!this.playbackInstances.has(id)) {
|
|
2798
2166
|
const instance = new PlaybackInstance(this.client, this.baseClient, id);
|
|
2799
2167
|
this.playbackInstances.set(id, instance);
|
|
2800
|
-
console.log(`New playback instance created with provided ID: ${id}`);
|
|
2801
2168
|
return instance;
|
|
2802
2169
|
}
|
|
2803
|
-
console.log(`Returning existing playback instance: ${id}`);
|
|
2804
2170
|
return this.playbackInstances.get(id);
|
|
2805
2171
|
} catch (error) {
|
|
2806
|
-
const message =
|
|
2807
|
-
console.
|
|
2172
|
+
const message = getErrorMessage2(error);
|
|
2173
|
+
console.warn(`Error creating/retrieving playback instance:`, message);
|
|
2808
2174
|
throw new Error(`Failed to manage playback instance: ${message}`);
|
|
2809
2175
|
}
|
|
2810
2176
|
}
|
|
@@ -2821,7 +2187,6 @@ var Playbacks = class {
|
|
|
2821
2187
|
const instance = this.playbackInstances.get(playbackId);
|
|
2822
2188
|
instance?.removeAllListeners();
|
|
2823
2189
|
this.playbackInstances.delete(playbackId);
|
|
2824
|
-
console.log(`Playback instance removed: ${playbackId}`);
|
|
2825
2190
|
} else {
|
|
2826
2191
|
console.warn(`Attempt to remove non-existent instance: ${playbackId}`);
|
|
2827
2192
|
}
|
|
@@ -2832,16 +2197,12 @@ var Playbacks = class {
|
|
|
2832
2197
|
*/
|
|
2833
2198
|
propagateEventToPlayback(event) {
|
|
2834
2199
|
if (!event) {
|
|
2835
|
-
console.warn("Invalid WebSocket event received");
|
|
2836
2200
|
return;
|
|
2837
2201
|
}
|
|
2838
2202
|
if ("playback" in event && event.playback?.id) {
|
|
2839
2203
|
const instance = this.playbackInstances.get(event.playback.id);
|
|
2840
2204
|
if (instance) {
|
|
2841
2205
|
instance.emitEvent(event);
|
|
2842
|
-
console.log(
|
|
2843
|
-
`Event propagated to playback ${event.playback.id}: ${event.type}`
|
|
2844
|
-
);
|
|
2845
2206
|
} else {
|
|
2846
2207
|
console.warn(`No instance found for playback ${event.playback.id}`);
|
|
2847
2208
|
}
|
|
@@ -2853,15 +2214,15 @@ var Playbacks = class {
|
|
|
2853
2214
|
* @returns {Promise<Playback>} Promise resolving to playback details
|
|
2854
2215
|
* @throws {Error} If the playback ID is invalid or the request fails
|
|
2855
2216
|
*/
|
|
2856
|
-
async
|
|
2217
|
+
async get(playbackId) {
|
|
2857
2218
|
if (!playbackId) {
|
|
2858
2219
|
throw new Error("Playback ID is required");
|
|
2859
2220
|
}
|
|
2860
2221
|
try {
|
|
2861
2222
|
return await this.baseClient.get(`/playbacks/${playbackId}`);
|
|
2862
2223
|
} catch (error) {
|
|
2863
|
-
const message =
|
|
2864
|
-
console.
|
|
2224
|
+
const message = getErrorMessage2(error);
|
|
2225
|
+
console.warn(`Error getting playback details ${playbackId}:`, message);
|
|
2865
2226
|
throw new Error(`Failed to get playback details: ${message}`);
|
|
2866
2227
|
}
|
|
2867
2228
|
}
|
|
@@ -2878,10 +2239,9 @@ var Playbacks = class {
|
|
|
2878
2239
|
try {
|
|
2879
2240
|
const playback = this.Playback({ id: playbackId });
|
|
2880
2241
|
await playback.control(operation);
|
|
2881
|
-
console.log(`Operation ${operation} executed on playback ${playbackId}`);
|
|
2882
2242
|
} catch (error) {
|
|
2883
|
-
const message =
|
|
2884
|
-
console.
|
|
2243
|
+
const message = getErrorMessage2(error);
|
|
2244
|
+
console.warn(`Error controlling playback ${playbackId}:`, message);
|
|
2885
2245
|
throw new Error(`Failed to control playback: ${message}`);
|
|
2886
2246
|
}
|
|
2887
2247
|
}
|
|
@@ -2897,10 +2257,9 @@ var Playbacks = class {
|
|
|
2897
2257
|
try {
|
|
2898
2258
|
const playback = this.Playback({ id: playbackId });
|
|
2899
2259
|
await playback.stop();
|
|
2900
|
-
console.log(`Playback ${playbackId} stopped`);
|
|
2901
2260
|
} catch (error) {
|
|
2902
|
-
const message =
|
|
2903
|
-
console.
|
|
2261
|
+
const message = getErrorMessage2(error);
|
|
2262
|
+
console.warn(`Error stopping playback ${playbackId}:`, message);
|
|
2904
2263
|
throw new Error(`Failed to stop playback: ${message}`);
|
|
2905
2264
|
}
|
|
2906
2265
|
}
|
|
@@ -2954,19 +2313,24 @@ var Sounds = class {
|
|
|
2954
2313
|
|
|
2955
2314
|
// src/ari-client/websocketClient.ts
|
|
2956
2315
|
var import_exponential_backoff = __toESM(require_backoff(), 1);
|
|
2957
|
-
import { EventEmitter as
|
|
2316
|
+
import { EventEmitter as EventEmitter3 } from "events";
|
|
2958
2317
|
import WebSocket from "ws";
|
|
2959
2318
|
var DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
|
|
2960
2319
|
var DEFAULT_STARTING_DELAY = 500;
|
|
2961
2320
|
var DEFAULT_MAX_DELAY = 1e4;
|
|
2962
|
-
var WebSocketClient = class extends
|
|
2321
|
+
var WebSocketClient = class extends EventEmitter3 {
|
|
2963
2322
|
/**
|
|
2964
|
-
* Creates a new
|
|
2323
|
+
* Creates a new WebSocketClient instance.
|
|
2324
|
+
*
|
|
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.
|
|
2965
2332
|
*
|
|
2966
|
-
* @
|
|
2967
|
-
* @param {string[]} apps - List of applications to connect to
|
|
2968
|
-
* @param {WebSocketEventType[]} [subscribedEvents] - Optional list of events to subscribe to
|
|
2969
|
-
* @param {AriClient} [ariClient] - Optional ARI client for handling channel and playback events
|
|
2333
|
+
* @throws {Error} Throws an error if the apps array is empty.
|
|
2970
2334
|
*/
|
|
2971
2335
|
constructor(baseClient, apps, subscribedEvents, ariClient) {
|
|
2972
2336
|
super();
|
|
@@ -2981,6 +2345,8 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
2981
2345
|
ws;
|
|
2982
2346
|
isReconnecting = false;
|
|
2983
2347
|
maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS;
|
|
2348
|
+
reconnectionAttempts = 0;
|
|
2349
|
+
lastWsUrl = "";
|
|
2984
2350
|
backOffOptions = {
|
|
2985
2351
|
numOfAttempts: DEFAULT_MAX_RECONNECT_ATTEMPTS,
|
|
2986
2352
|
startingDelay: DEFAULT_STARTING_DELAY,
|
|
@@ -2997,10 +2363,14 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
2997
2363
|
}
|
|
2998
2364
|
};
|
|
2999
2365
|
/**
|
|
3000
|
-
* Establishes a WebSocket connection.
|
|
2366
|
+
* Establishes a WebSocket connection to the Asterisk server.
|
|
3001
2367
|
*
|
|
3002
|
-
*
|
|
3003
|
-
*
|
|
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.
|
|
3004
2374
|
*/
|
|
3005
2375
|
async connect() {
|
|
3006
2376
|
const { baseUrl, username, password } = this.baseClient.getCredentials();
|
|
@@ -3015,15 +2385,24 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3015
2385
|
} else {
|
|
3016
2386
|
queryParams.append("subscribeAll", "true");
|
|
3017
2387
|
}
|
|
3018
|
-
|
|
2388
|
+
this.lastWsUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${normalizedHost}/ari/events?${queryParams.toString()}`;
|
|
3019
2389
|
console.log("Connecting to WebSocket...");
|
|
3020
|
-
return this.initializeWebSocket(
|
|
2390
|
+
return this.initializeWebSocket(this.lastWsUrl);
|
|
3021
2391
|
}
|
|
3022
2392
|
/**
|
|
3023
|
-
* Initializes WebSocket connection with
|
|
2393
|
+
* Initializes a WebSocket connection with exponential backoff retry mechanism.
|
|
3024
2394
|
*
|
|
3025
|
-
*
|
|
3026
|
-
*
|
|
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.
|
|
3027
2406
|
*/
|
|
3028
2407
|
async initializeWebSocket(wsUrl) {
|
|
3029
2408
|
return (0, import_exponential_backoff.backOff)(async () => {
|
|
@@ -3032,7 +2411,14 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3032
2411
|
this.ws = new WebSocket(wsUrl);
|
|
3033
2412
|
this.ws.on("open", () => {
|
|
3034
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
|
+
}
|
|
3035
2420
|
this.isReconnecting = false;
|
|
2421
|
+
this.reconnectionAttempts = 0;
|
|
3036
2422
|
this.emit("connected");
|
|
3037
2423
|
resolve();
|
|
3038
2424
|
});
|
|
@@ -3042,13 +2428,13 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3042
2428
|
`WebSocket disconnected with code ${code}. Attempting to reconnect...`
|
|
3043
2429
|
);
|
|
3044
2430
|
if (!this.isReconnecting) {
|
|
3045
|
-
this.reconnect(
|
|
2431
|
+
this.reconnect(this.lastWsUrl);
|
|
3046
2432
|
}
|
|
3047
2433
|
});
|
|
3048
2434
|
this.ws.on("error", (err) => {
|
|
3049
2435
|
console.error("WebSocket error:", err.message);
|
|
3050
2436
|
if (!this.isReconnecting) {
|
|
3051
|
-
this.reconnect(
|
|
2437
|
+
this.reconnect(this.lastWsUrl);
|
|
3052
2438
|
}
|
|
3053
2439
|
reject(err);
|
|
3054
2440
|
});
|
|
@@ -3059,9 +2445,16 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3059
2445
|
}, this.backOffOptions);
|
|
3060
2446
|
}
|
|
3061
2447
|
/**
|
|
3062
|
-
*
|
|
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.
|
|
3063
2453
|
*
|
|
3064
|
-
* @param
|
|
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.
|
|
2456
|
+
*
|
|
2457
|
+
* @throws Will emit an 'error' event if the message cannot be parsed or processed.
|
|
3065
2458
|
*/
|
|
3066
2459
|
handleMessage(rawMessage) {
|
|
3067
2460
|
try {
|
|
@@ -3079,13 +2472,7 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3079
2472
|
instancePlayback.emitEvent(event);
|
|
3080
2473
|
event.instancePlayback = instancePlayback;
|
|
3081
2474
|
}
|
|
3082
|
-
if ("bridge" in event && event.bridge?.id && this.ariClient) {
|
|
3083
|
-
const instanceBridge = this.ariClient.Bridge(event.bridge.id);
|
|
3084
|
-
instanceBridge.emitEvent(event);
|
|
3085
|
-
event.instanceBridge = instanceBridge;
|
|
3086
|
-
}
|
|
3087
2475
|
this.emit(event.type, event);
|
|
3088
|
-
console.log(`Event processed: ${event.type}`);
|
|
3089
2476
|
} catch (error) {
|
|
3090
2477
|
console.error(
|
|
3091
2478
|
"Error processing WebSocket message:",
|
|
@@ -3095,18 +2482,27 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3095
2482
|
}
|
|
3096
2483
|
}
|
|
3097
2484
|
/**
|
|
3098
|
-
* Attempts to reconnect to the WebSocket.
|
|
2485
|
+
* Attempts to reconnect to the WebSocket server using an exponential backoff strategy.
|
|
2486
|
+
*
|
|
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.
|
|
3099
2490
|
*
|
|
3100
|
-
* @param
|
|
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.
|
|
3101
2495
|
*/
|
|
3102
2496
|
reconnect(wsUrl) {
|
|
3103
2497
|
this.isReconnecting = true;
|
|
3104
|
-
|
|
3105
|
-
|
|
2498
|
+
this.reconnectionAttempts++;
|
|
2499
|
+
console.log(
|
|
2500
|
+
`Initiating reconnection attempt #${this.reconnectionAttempts}...`
|
|
2501
|
+
);
|
|
3106
2502
|
(0, import_exponential_backoff.backOff)(() => this.initializeWebSocket(wsUrl), this.backOffOptions).catch(
|
|
3107
2503
|
(error) => {
|
|
3108
2504
|
console.error(
|
|
3109
|
-
|
|
2505
|
+
`Failed to reconnect after ${this.reconnectionAttempts} attempts:`,
|
|
3110
2506
|
error instanceof Error ? error.message : "Unknown error"
|
|
3111
2507
|
);
|
|
3112
2508
|
this.emit("reconnectFailed", error);
|
|
@@ -3114,7 +2510,13 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3114
2510
|
);
|
|
3115
2511
|
}
|
|
3116
2512
|
/**
|
|
3117
|
-
*
|
|
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.
|
|
3118
2520
|
*/
|
|
3119
2521
|
close() {
|
|
3120
2522
|
try {
|
|
@@ -3131,17 +2533,30 @@ var WebSocketClient = class extends EventEmitter4 {
|
|
|
3131
2533
|
}
|
|
3132
2534
|
}
|
|
3133
2535
|
/**
|
|
3134
|
-
* Checks if the WebSocket is currently
|
|
2536
|
+
* Checks if the WebSocket connection is currently open and active.
|
|
2537
|
+
*
|
|
2538
|
+
* This method examines the readyState of the WebSocket instance to determine
|
|
2539
|
+
* if the connection is established and ready for communication.
|
|
3135
2540
|
*
|
|
3136
|
-
* @returns {boolean} True if
|
|
2541
|
+
* @returns {boolean} True if the WebSocket connection is open and ready,
|
|
2542
|
+
* false otherwise.
|
|
3137
2543
|
*/
|
|
3138
2544
|
isConnected() {
|
|
3139
2545
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
3140
2546
|
}
|
|
3141
2547
|
/**
|
|
3142
|
-
*
|
|
2548
|
+
* Retrieves the current state of the WebSocket connection.
|
|
3143
2549
|
*
|
|
3144
|
-
*
|
|
2550
|
+
* This method returns the readyState of the WebSocket instance, which
|
|
2551
|
+
* indicates the current state of the connection. If no WebSocket instance
|
|
2552
|
+
* exists, it returns the CLOSED state.
|
|
2553
|
+
*
|
|
2554
|
+
* @returns {number} The current state of the WebSocket connection.
|
|
2555
|
+
* Possible values are:
|
|
2556
|
+
* - WebSocket.CONNECTING (0): The connection is not yet open.
|
|
2557
|
+
* - WebSocket.OPEN (1): The connection is open and ready to communicate.
|
|
2558
|
+
* - WebSocket.CLOSING (2): The connection is in the process of closing.
|
|
2559
|
+
* - WebSocket.CLOSED (3): The connection is closed or couldn't be opened.
|
|
3145
2560
|
*/
|
|
3146
2561
|
getState() {
|
|
3147
2562
|
return this.ws?.readyState ?? WebSocket.CLOSED;
|
|
@@ -3167,11 +2582,11 @@ var AriClient = class {
|
|
|
3167
2582
|
this.baseClient = new BaseClient(baseUrl, config.username, config.password);
|
|
3168
2583
|
this.channels = new Channels(this.baseClient, this);
|
|
3169
2584
|
this.playbacks = new Playbacks(this.baseClient, this);
|
|
3170
|
-
this.bridges = new Bridges(this.baseClient, this);
|
|
3171
2585
|
this.endpoints = new Endpoints(this.baseClient);
|
|
3172
2586
|
this.applications = new Applications(this.baseClient);
|
|
3173
2587
|
this.sounds = new Sounds(this.baseClient);
|
|
3174
2588
|
this.asterisk = new Asterisk(this.baseClient);
|
|
2589
|
+
this.bridges = new Bridges(this.baseClient);
|
|
3175
2590
|
console.log(`ARI Client initialized with base URL: ${baseUrl}`);
|
|
3176
2591
|
}
|
|
3177
2592
|
baseClient;
|
|
@@ -3287,20 +2702,6 @@ var AriClient = class {
|
|
|
3287
2702
|
Playback(playbackId, _app) {
|
|
3288
2703
|
return this.playbacks.Playback({ id: playbackId });
|
|
3289
2704
|
}
|
|
3290
|
-
/**
|
|
3291
|
-
* Creates or retrieves a Bridge instance.
|
|
3292
|
-
*
|
|
3293
|
-
* This function allows you to create a new Bridge instance or retrieve an existing one
|
|
3294
|
-
* based on the provided bridge ID.
|
|
3295
|
-
*
|
|
3296
|
-
* @param {string} [bridgeId] - Optional ID of an existing bridge. If provided, retrieves the
|
|
3297
|
-
* existing bridge with this ID. If omitted, creates a new bridge.
|
|
3298
|
-
* @returns {BridgeInstance} A new or existing Bridge instance that can be used to interact
|
|
3299
|
-
* with the Asterisk bridge.
|
|
3300
|
-
*/
|
|
3301
|
-
Bridge(bridgeId) {
|
|
3302
|
-
return this.bridges.Bridge({ id: bridgeId });
|
|
3303
|
-
}
|
|
3304
2705
|
/**
|
|
3305
2706
|
* Gets the current WebSocket connection status.
|
|
3306
2707
|
*
|
|
@@ -3314,7 +2715,6 @@ export {
|
|
|
3314
2715
|
Applications,
|
|
3315
2716
|
AriClient,
|
|
3316
2717
|
Asterisk,
|
|
3317
|
-
BridgeInstance,
|
|
3318
2718
|
Bridges,
|
|
3319
2719
|
ChannelInstance,
|
|
3320
2720
|
Channels,
|