@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/esm/index.js
CHANGED
|
@@ -842,6 +842,37 @@ function toQueryParams2(options) {
|
|
|
842
842
|
Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
|
|
843
843
|
).toString();
|
|
844
844
|
}
|
|
845
|
+
var ChannelEventEmitter = class {
|
|
846
|
+
eventEmitter = new EventEmitter();
|
|
847
|
+
channel;
|
|
848
|
+
constructor(channel) {
|
|
849
|
+
this.channel = channel;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Registra um listener para um evento no canal.
|
|
853
|
+
*/
|
|
854
|
+
on(eventType, callback) {
|
|
855
|
+
this.eventEmitter.on(eventType, callback);
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Emite um evento no canal.
|
|
859
|
+
*/
|
|
860
|
+
emit(eventType, data) {
|
|
861
|
+
this.eventEmitter.emit(eventType, data);
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Remove um listener de evento do canal.
|
|
865
|
+
*/
|
|
866
|
+
off(eventType, callback) {
|
|
867
|
+
this.eventEmitter.off(eventType, callback);
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Remove todos os listeners de um evento no canal.
|
|
871
|
+
*/
|
|
872
|
+
removeAllListeners(eventType) {
|
|
873
|
+
this.eventEmitter.removeAllListeners(eventType);
|
|
874
|
+
}
|
|
875
|
+
};
|
|
845
876
|
var ChannelInstance = class extends EventEmitter {
|
|
846
877
|
constructor(client, baseClient, channelId = `channel-${Date.now()}`) {
|
|
847
878
|
super();
|
|
@@ -1673,6 +1704,7 @@ var AriClient = class {
|
|
|
1673
1704
|
this.bridges = new Bridges(this.baseClient);
|
|
1674
1705
|
}
|
|
1675
1706
|
wsClient = null;
|
|
1707
|
+
channelEmitters = /* @__PURE__ */ new Map();
|
|
1676
1708
|
baseClient;
|
|
1677
1709
|
isReconnecting = false;
|
|
1678
1710
|
eventEmitter = new EventEmitter4();
|
|
@@ -1789,34 +1821,43 @@ var AriClient = class {
|
|
|
1789
1821
|
`WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
|
|
1790
1822
|
);
|
|
1791
1823
|
}
|
|
1792
|
-
|
|
1824
|
+
wsClient.on("ChannelDestroyed", (data) => {
|
|
1825
|
+
if (data.type === "ChannelDestroyed" && "channel" in data) {
|
|
1826
|
+
console.log(`[${app}] Canal destru\xEDdo: ${data.channel.id}`);
|
|
1827
|
+
this.removeChannel(data.channel.id);
|
|
1828
|
+
}
|
|
1829
|
+
});
|
|
1830
|
+
wsClient.on("StasisEnd", (data) => {
|
|
1831
|
+
if (data.type === "StasisEnd" && "channel" in data) {
|
|
1832
|
+
console.log(`[${app}] StasisEnd para o canal: ${data.channel.id}`);
|
|
1833
|
+
this.removeChannel(data.channel.id);
|
|
1834
|
+
}
|
|
1835
|
+
});
|
|
1793
1836
|
const eventHandlers = {
|
|
1794
|
-
|
|
1837
|
+
PlaybackStarted: (data) => {
|
|
1795
1838
|
if ("playbackId" in data) {
|
|
1796
|
-
|
|
1839
|
+
console.log(`[${app}] PlaybackStarted:`, data);
|
|
1797
1840
|
}
|
|
1798
|
-
console.log(`[${app}] PlaybackFinished:`, data);
|
|
1799
|
-
this.emitGlobalEvent(data);
|
|
1800
1841
|
},
|
|
1801
|
-
|
|
1802
|
-
if ("
|
|
1803
|
-
console.log(`[${app}]
|
|
1804
|
-
this.channels.emitChannelEvent("ChannelStateChange", data);
|
|
1842
|
+
PlaybackFinished: (data) => {
|
|
1843
|
+
if ("playbackId" in data) {
|
|
1844
|
+
console.log(`[${app}] PlaybackFinished:`, data);
|
|
1805
1845
|
}
|
|
1806
|
-
this.emitGlobalEvent(data);
|
|
1807
1846
|
},
|
|
1808
|
-
|
|
1809
|
-
if ("
|
|
1810
|
-
console.log(`[${app}]
|
|
1847
|
+
ChannelDtmfReceived: (data) => {
|
|
1848
|
+
if (data.type === "ChannelDtmfReceived" && "digit" in data) {
|
|
1849
|
+
console.log(`[${app}] DTMF recebido: ${data.digit}`);
|
|
1850
|
+
} else {
|
|
1851
|
+
console.warn(
|
|
1852
|
+
`[${app}] Evento inesperado em ChannelDtmfReceived`,
|
|
1853
|
+
data
|
|
1854
|
+
);
|
|
1811
1855
|
}
|
|
1812
|
-
this.emitGlobalEvent(data);
|
|
1813
1856
|
},
|
|
1814
|
-
|
|
1857
|
+
ChannelStateChange: (data) => {
|
|
1815
1858
|
if ("channel" in data) {
|
|
1816
|
-
console.log(`[${app}]
|
|
1817
|
-
this.channels.emitChannelEvent("ChannelDtmfReceived", data);
|
|
1859
|
+
console.log(`[${app}] Estado do canal alterado:`, data.channel);
|
|
1818
1860
|
}
|
|
1819
|
-
this.emitGlobalEvent(data);
|
|
1820
1861
|
}
|
|
1821
1862
|
};
|
|
1822
1863
|
for (const [eventType, handler] of Object.entries(eventHandlers)) {
|
|
@@ -1860,6 +1901,17 @@ var AriClient = class {
|
|
|
1860
1901
|
unregisterPlaybackListener(eventType, playbackId, callback) {
|
|
1861
1902
|
this.playbacks.unregisterListener(eventType, playbackId, callback);
|
|
1862
1903
|
}
|
|
1904
|
+
registerListener(eventType, condition, callback) {
|
|
1905
|
+
if (!this.isWebSocketConnected()) {
|
|
1906
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1907
|
+
}
|
|
1908
|
+
const handler = (eventData) => {
|
|
1909
|
+
if (eventData.type === eventType && condition(eventData)) {
|
|
1910
|
+
callback(eventData);
|
|
1911
|
+
}
|
|
1912
|
+
};
|
|
1913
|
+
this.wsClient?.on(eventType, handler);
|
|
1914
|
+
}
|
|
1863
1915
|
/**
|
|
1864
1916
|
* Registers a listener for playback events.
|
|
1865
1917
|
* The listener is triggered for events such as "PlaybackFinished".
|
|
@@ -1869,7 +1921,34 @@ var AriClient = class {
|
|
|
1869
1921
|
* @param callback - The callback function to execute when the event occurs.
|
|
1870
1922
|
*/
|
|
1871
1923
|
registerPlaybackListener(eventType, playbackId, callback) {
|
|
1872
|
-
this.
|
|
1924
|
+
if (!this.isWebSocketConnected()) {
|
|
1925
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1926
|
+
}
|
|
1927
|
+
console.log(
|
|
1928
|
+
`Registrando listener para evento ${eventType} com playbackId ${playbackId}`
|
|
1929
|
+
);
|
|
1930
|
+
const handler = (eventData) => {
|
|
1931
|
+
if ("playbackId" in eventData && eventData.playbackId === playbackId) {
|
|
1932
|
+
callback(eventData);
|
|
1933
|
+
}
|
|
1934
|
+
};
|
|
1935
|
+
this.wsClient?.on(eventType, handler);
|
|
1936
|
+
}
|
|
1937
|
+
registerChannelListener(eventType, channelId, callback) {
|
|
1938
|
+
if (!this.isWebSocketConnected()) {
|
|
1939
|
+
throw new Error("WebSocket n\xE3o est\xE1 conectado.");
|
|
1940
|
+
}
|
|
1941
|
+
console.log(
|
|
1942
|
+
`Registrando listener para evento ${eventType} com channelId ${channelId}`
|
|
1943
|
+
);
|
|
1944
|
+
const handler = (eventData) => {
|
|
1945
|
+
if (eventData.type === eventType && "channel" in eventData && eventData.channel?.id === channelId) {
|
|
1946
|
+
callback(
|
|
1947
|
+
eventData
|
|
1948
|
+
);
|
|
1949
|
+
}
|
|
1950
|
+
};
|
|
1951
|
+
this.wsClient?.on(eventType, handler);
|
|
1873
1952
|
}
|
|
1874
1953
|
/**
|
|
1875
1954
|
* Checks if a listener is already registered for a specific event and playback.
|
|
@@ -1990,6 +2069,45 @@ var AriClient = class {
|
|
|
1990
2069
|
this.wsClient = null;
|
|
1991
2070
|
}
|
|
1992
2071
|
}
|
|
2072
|
+
/**
|
|
2073
|
+
* Obtém ou cria um emissor de eventos para o canal especificado.
|
|
2074
|
+
*/
|
|
2075
|
+
getOrCreateChannelEmitter(channel) {
|
|
2076
|
+
if (!this.channelEmitters.has(channel.id)) {
|
|
2077
|
+
this.channelEmitters.set(channel.id, new ChannelEventEmitter(channel));
|
|
2078
|
+
}
|
|
2079
|
+
return this.channelEmitters.get(channel.id);
|
|
2080
|
+
}
|
|
2081
|
+
/**
|
|
2082
|
+
* Adiciona um listener para um evento específico de canal.
|
|
2083
|
+
*/
|
|
2084
|
+
onChannelEvent(channelId, eventType, callback) {
|
|
2085
|
+
const channelEmitter = this.channelEmitters.get(channelId);
|
|
2086
|
+
if (!channelEmitter) {
|
|
2087
|
+
throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
|
|
2088
|
+
}
|
|
2089
|
+
channelEmitter.on(eventType, callback);
|
|
2090
|
+
}
|
|
2091
|
+
removeChannel(channelId) {
|
|
2092
|
+
const channelEmitter = this.channelEmitters.get(channelId);
|
|
2093
|
+
if (channelEmitter) {
|
|
2094
|
+
channelEmitter.removeAllListeners();
|
|
2095
|
+
this.channelEmitters.delete(channelId);
|
|
2096
|
+
console.log(`Listeners para o canal ${channelId} foram removidos.`);
|
|
2097
|
+
} else {
|
|
2098
|
+
console.warn(`Nenhum listener encontrado para o canal ${channelId}.`);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
/**
|
|
2102
|
+
* Remove um listener para um evento específico de canal.
|
|
2103
|
+
*/
|
|
2104
|
+
offChannelEvent(channelId, eventType, callback) {
|
|
2105
|
+
const channelEmitter = this.channelEmitters.get(channelId);
|
|
2106
|
+
if (!channelEmitter) {
|
|
2107
|
+
throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
|
|
2108
|
+
}
|
|
2109
|
+
channelEmitter.off(eventType, callback);
|
|
2110
|
+
}
|
|
1993
2111
|
/**
|
|
1994
2112
|
* Retrieves a list of active channels from the Asterisk ARI.
|
|
1995
2113
|
*
|