@ipcom/asterisk-ari 0.0.39 → 0.0.41
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 +137 -19
- package/dist/cjs/index.cjs.map +2 -2
- package/dist/esm/index.js +137 -19
- package/dist/esm/index.js.map +2 -2
- package/dist/types/ari-client/ariClient.d.ts +27 -2
- package/dist/types/ari-client/ariClient.d.ts.map +1 -1
- package/dist/types/ari-client/interfaces/events.types.d.ts +6 -0
- 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/channels.d.ts +27 -0
- package/dist/types/ari-client/resources/channels.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 +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -864,6 +864,37 @@ function toQueryParams2(options) {
|
|
|
864
864
|
Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
|
|
865
865
|
).toString();
|
|
866
866
|
}
|
|
867
|
+
var ChannelEventEmitter = class {
|
|
868
|
+
eventEmitter = new import_events.EventEmitter();
|
|
869
|
+
channel;
|
|
870
|
+
constructor(channel) {
|
|
871
|
+
this.channel = channel;
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Registra um listener para um evento no canal.
|
|
875
|
+
*/
|
|
876
|
+
on(eventType, callback) {
|
|
877
|
+
this.eventEmitter.on(eventType, callback);
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* Emite um evento no canal.
|
|
881
|
+
*/
|
|
882
|
+
emit(eventType, data) {
|
|
883
|
+
this.eventEmitter.emit(eventType, data);
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Remove um listener de evento do canal.
|
|
887
|
+
*/
|
|
888
|
+
off(eventType, callback) {
|
|
889
|
+
this.eventEmitter.off(eventType, callback);
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Remove todos os listeners de um evento no canal.
|
|
893
|
+
*/
|
|
894
|
+
removeAllListeners(eventType) {
|
|
895
|
+
this.eventEmitter.removeAllListeners(eventType);
|
|
896
|
+
}
|
|
897
|
+
};
|
|
867
898
|
var ChannelInstance = class extends import_events.EventEmitter {
|
|
868
899
|
constructor(client, baseClient, channelId = `channel-${Date.now()}`) {
|
|
869
900
|
super();
|
|
@@ -1695,6 +1726,7 @@ var AriClient = class {
|
|
|
1695
1726
|
this.bridges = new Bridges(this.baseClient);
|
|
1696
1727
|
}
|
|
1697
1728
|
wsClient = null;
|
|
1729
|
+
channelEmitters = /* @__PURE__ */ new Map();
|
|
1698
1730
|
baseClient;
|
|
1699
1731
|
isReconnecting = false;
|
|
1700
1732
|
eventEmitter = new import_events4.EventEmitter();
|
|
@@ -1811,34 +1843,43 @@ var AriClient = class {
|
|
|
1811
1843
|
`WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
|
|
1812
1844
|
);
|
|
1813
1845
|
}
|
|
1814
|
-
|
|
1846
|
+
wsClient.on("ChannelDestroyed", (data) => {
|
|
1847
|
+
if (data.type === "ChannelDestroyed" && "channel" in data) {
|
|
1848
|
+
console.log(`[${app}] Canal destru\xEDdo: ${data.channel.id}`);
|
|
1849
|
+
this.removeChannel(data.channel.id);
|
|
1850
|
+
}
|
|
1851
|
+
});
|
|
1852
|
+
wsClient.on("StasisEnd", (data) => {
|
|
1853
|
+
if (data.type === "StasisEnd" && "channel" in data) {
|
|
1854
|
+
console.log(`[${app}] StasisEnd para o canal: ${data.channel.id}`);
|
|
1855
|
+
this.removeChannel(data.channel.id);
|
|
1856
|
+
}
|
|
1857
|
+
});
|
|
1815
1858
|
const eventHandlers = {
|
|
1816
|
-
|
|
1859
|
+
PlaybackStarted: (data) => {
|
|
1817
1860
|
if ("playbackId" in data) {
|
|
1818
|
-
|
|
1861
|
+
console.log(`[${app}] PlaybackStarted:`, data);
|
|
1819
1862
|
}
|
|
1820
|
-
console.log(`[${app}] PlaybackFinished:`, data);
|
|
1821
|
-
this.emitGlobalEvent(data);
|
|
1822
1863
|
},
|
|
1823
|
-
|
|
1824
|
-
if ("
|
|
1825
|
-
console.log(`[${app}]
|
|
1826
|
-
this.channels.emitChannelEvent("ChannelStateChange", data);
|
|
1864
|
+
PlaybackFinished: (data) => {
|
|
1865
|
+
if ("playbackId" in data) {
|
|
1866
|
+
console.log(`[${app}] PlaybackFinished:`, data);
|
|
1827
1867
|
}
|
|
1828
|
-
this.emitGlobalEvent(data);
|
|
1829
1868
|
},
|
|
1830
|
-
|
|
1831
|
-
if ("
|
|
1832
|
-
console.log(`[${app}]
|
|
1869
|
+
ChannelDtmfReceived: (data) => {
|
|
1870
|
+
if (data.type === "ChannelDtmfReceived" && "digit" in data) {
|
|
1871
|
+
console.log(`[${app}] DTMF recebido: ${data.digit}`);
|
|
1872
|
+
} else {
|
|
1873
|
+
console.warn(
|
|
1874
|
+
`[${app}] Evento inesperado em ChannelDtmfReceived`,
|
|
1875
|
+
data
|
|
1876
|
+
);
|
|
1833
1877
|
}
|
|
1834
|
-
this.emitGlobalEvent(data);
|
|
1835
1878
|
},
|
|
1836
|
-
|
|
1879
|
+
ChannelStateChange: (data) => {
|
|
1837
1880
|
if ("channel" in data) {
|
|
1838
|
-
console.log(`[${app}]
|
|
1839
|
-
this.channels.emitChannelEvent("ChannelDtmfReceived", data);
|
|
1881
|
+
console.log(`[${app}] Estado do canal alterado:`, data.channel);
|
|
1840
1882
|
}
|
|
1841
|
-
this.emitGlobalEvent(data);
|
|
1842
1883
|
}
|
|
1843
1884
|
};
|
|
1844
1885
|
for (const [eventType, handler] of Object.entries(eventHandlers)) {
|
|
@@ -1882,6 +1923,17 @@ var AriClient = class {
|
|
|
1882
1923
|
unregisterPlaybackListener(eventType, playbackId, callback) {
|
|
1883
1924
|
this.playbacks.unregisterListener(eventType, playbackId, callback);
|
|
1884
1925
|
}
|
|
1926
|
+
registerListener(eventType, condition, callback) {
|
|
1927
|
+
if (!this.isWebSocketConnected()) {
|
|
1928
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1929
|
+
}
|
|
1930
|
+
const handler = (eventData) => {
|
|
1931
|
+
if (eventData.type === eventType && condition(eventData)) {
|
|
1932
|
+
callback(eventData);
|
|
1933
|
+
}
|
|
1934
|
+
};
|
|
1935
|
+
this.wsClient?.on(eventType, handler);
|
|
1936
|
+
}
|
|
1885
1937
|
/**
|
|
1886
1938
|
* Registers a listener for playback events.
|
|
1887
1939
|
* The listener is triggered for events such as "PlaybackFinished".
|
|
@@ -1891,7 +1943,34 @@ var AriClient = class {
|
|
|
1891
1943
|
* @param callback - The callback function to execute when the event occurs.
|
|
1892
1944
|
*/
|
|
1893
1945
|
registerPlaybackListener(eventType, playbackId, callback) {
|
|
1894
|
-
this.
|
|
1946
|
+
if (!this.isWebSocketConnected()) {
|
|
1947
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1948
|
+
}
|
|
1949
|
+
console.log(
|
|
1950
|
+
`Registrando listener para evento ${eventType} com playbackId ${playbackId}`
|
|
1951
|
+
);
|
|
1952
|
+
const handler = (eventData) => {
|
|
1953
|
+
if ("playbackId" in eventData && eventData.playbackId === playbackId) {
|
|
1954
|
+
callback(eventData);
|
|
1955
|
+
}
|
|
1956
|
+
};
|
|
1957
|
+
this.wsClient?.on(eventType, handler);
|
|
1958
|
+
}
|
|
1959
|
+
registerChannelListener(eventType, channelId, callback) {
|
|
1960
|
+
if (!this.isWebSocketConnected()) {
|
|
1961
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1962
|
+
}
|
|
1963
|
+
console.log(
|
|
1964
|
+
`Registrando listener para evento ${eventType} com channelId ${channelId}`
|
|
1965
|
+
);
|
|
1966
|
+
const handler = (eventData) => {
|
|
1967
|
+
if (eventData.type === eventType && "channel" in eventData && eventData.channel?.id === channelId) {
|
|
1968
|
+
callback(
|
|
1969
|
+
eventData
|
|
1970
|
+
);
|
|
1971
|
+
}
|
|
1972
|
+
};
|
|
1973
|
+
this.wsClient?.on(eventType, handler);
|
|
1895
1974
|
}
|
|
1896
1975
|
/**
|
|
1897
1976
|
* Checks if a listener is already registered for a specific event and playback.
|
|
@@ -2012,6 +2091,45 @@ var AriClient = class {
|
|
|
2012
2091
|
this.wsClient = null;
|
|
2013
2092
|
}
|
|
2014
2093
|
}
|
|
2094
|
+
/**
|
|
2095
|
+
* Obtém ou cria um emissor de eventos para o canal especificado.
|
|
2096
|
+
*/
|
|
2097
|
+
getOrCreateChannelEmitter(channel) {
|
|
2098
|
+
if (!this.channelEmitters.has(channel.id)) {
|
|
2099
|
+
this.channelEmitters.set(channel.id, new ChannelEventEmitter(channel));
|
|
2100
|
+
}
|
|
2101
|
+
return this.channelEmitters.get(channel.id);
|
|
2102
|
+
}
|
|
2103
|
+
/**
|
|
2104
|
+
* Adiciona um listener para um evento específico de canal.
|
|
2105
|
+
*/
|
|
2106
|
+
onChannelEvent(channelId, eventType, callback) {
|
|
2107
|
+
const channelEmitter = this.channelEmitters.get(channelId);
|
|
2108
|
+
if (!channelEmitter) {
|
|
2109
|
+
throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
|
|
2110
|
+
}
|
|
2111
|
+
channelEmitter.on(eventType, callback);
|
|
2112
|
+
}
|
|
2113
|
+
removeChannel(channelId) {
|
|
2114
|
+
const channelEmitter = this.channelEmitters.get(channelId);
|
|
2115
|
+
if (channelEmitter) {
|
|
2116
|
+
channelEmitter.removeAllListeners();
|
|
2117
|
+
this.channelEmitters.delete(channelId);
|
|
2118
|
+
console.log(`Listeners para o canal ${channelId} foram removidos.`);
|
|
2119
|
+
} else {
|
|
2120
|
+
console.warn(`Nenhum listener encontrado para o canal ${channelId}.`);
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
/**
|
|
2124
|
+
* Remove um listener para um evento específico de canal.
|
|
2125
|
+
*/
|
|
2126
|
+
offChannelEvent(channelId, eventType, callback) {
|
|
2127
|
+
const channelEmitter = this.channelEmitters.get(channelId);
|
|
2128
|
+
if (!channelEmitter) {
|
|
2129
|
+
throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
|
|
2130
|
+
}
|
|
2131
|
+
channelEmitter.off(eventType, callback);
|
|
2132
|
+
}
|
|
2015
2133
|
/**
|
|
2016
2134
|
* Retrieves a list of active channels from the Asterisk ARI.
|
|
2017
2135
|
*
|