@calimero-network/mero-js 7.0.5 → 7.1.1

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/index.cjs CHANGED
@@ -1584,6 +1584,7 @@ var SseClient = class {
1584
1584
  this.abortController = null;
1585
1585
  this.reconnectTimer = null;
1586
1586
  this.subscribedContextIds = /* @__PURE__ */ new Set();
1587
+ this.subscribedGroupIds = /* @__PURE__ */ new Set();
1587
1588
  this.closed = false;
1588
1589
  this.listeners = { connect: [], event: [], error: [] };
1589
1590
  this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
@@ -1706,21 +1707,25 @@ var SseClient = class {
1706
1707
  if (msg.type === "connect" && msg.session_id) {
1707
1708
  this.sessionId = msg.session_id;
1708
1709
  this.emit("connect", msg.session_id);
1709
- if (this.subscribedContextIds.size > 0) {
1710
- this.sendSubscription("subscribe", [...this.subscribedContextIds]);
1710
+ if (this.subscribedContextIds.size > 0 || this.subscribedGroupIds.size > 0) {
1711
+ this.sendSubscription("subscribe", {
1712
+ contextIds: [...this.subscribedContextIds],
1713
+ groupIds: [...this.subscribedGroupIds]
1714
+ });
1711
1715
  }
1712
1716
  return;
1713
1717
  }
1714
- if (msg.result && msg.result.contextId) {
1715
- let eventData = msg.result.data;
1716
- if (Array.isArray(eventData)) {
1717
- try {
1718
- const bytes = new Uint8Array(eventData);
1719
- const text = new TextDecoder().decode(bytes);
1720
- eventData = JSON.parse(text);
1721
- } catch {
1722
- }
1718
+ if (!msg.result) return;
1719
+ let eventData = msg.result.data;
1720
+ if (Array.isArray(eventData)) {
1721
+ try {
1722
+ const bytes = new Uint8Array(eventData);
1723
+ const text = new TextDecoder().decode(bytes);
1724
+ eventData = JSON.parse(text);
1725
+ } catch {
1723
1726
  }
1727
+ }
1728
+ if (msg.result.contextId) {
1724
1729
  this.emit("event", {
1725
1730
  contextId: msg.result.contextId,
1726
1731
  // Forward the event tag (sibling of `data` in core's flattened
@@ -1728,31 +1733,52 @@ var SseClient = class {
1728
1733
  type: msg.result.type,
1729
1734
  data: eventData
1730
1735
  });
1736
+ return;
1737
+ }
1738
+ if (msg.result.groupId) {
1739
+ this.emit("event", {
1740
+ groupId: msg.result.groupId,
1741
+ type: msg.result.type,
1742
+ data: eventData
1743
+ });
1731
1744
  }
1732
1745
  } catch {
1733
1746
  }
1734
1747
  }
1735
- async subscribe(contextIds) {
1736
- const newIds = contextIds.filter((id) => !this.subscribedContextIds.has(id));
1737
- for (const id of contextIds) {
1748
+ async subscribe(idsOrOpts) {
1749
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1750
+ const newContextIds = (opts.contextIds ?? []).filter((id) => !this.subscribedContextIds.has(id));
1751
+ const newGroupIds = (opts.groupIds ?? []).filter((id) => !this.subscribedGroupIds.has(id));
1752
+ for (const id of opts.contextIds ?? []) {
1738
1753
  this.subscribedContextIds.add(id);
1739
1754
  }
1740
- if (newIds.length > 0 && this.sessionId) {
1741
- await this.sendSubscription("subscribe", newIds);
1755
+ for (const id of opts.groupIds ?? []) {
1756
+ this.subscribedGroupIds.add(id);
1757
+ }
1758
+ if ((newContextIds.length > 0 || newGroupIds.length > 0) && this.sessionId) {
1759
+ await this.sendSubscription("subscribe", { contextIds: newContextIds, groupIds: newGroupIds });
1742
1760
  }
1743
1761
  }
1744
- async unsubscribe(contextIds) {
1745
- const hadIds = contextIds.filter((id) => this.subscribedContextIds.has(id));
1746
- for (const id of contextIds) {
1762
+ async unsubscribe(idsOrOpts) {
1763
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1764
+ const hadContextIds = (opts.contextIds ?? []).filter((id) => this.subscribedContextIds.has(id));
1765
+ const hadGroupIds = (opts.groupIds ?? []).filter((id) => this.subscribedGroupIds.has(id));
1766
+ for (const id of opts.contextIds ?? []) {
1747
1767
  this.subscribedContextIds.delete(id);
1748
1768
  }
1749
- if (hadIds.length > 0 && this.sessionId) {
1750
- await this.sendSubscription("unsubscribe", hadIds);
1769
+ for (const id of opts.groupIds ?? []) {
1770
+ this.subscribedGroupIds.delete(id);
1771
+ }
1772
+ if ((hadContextIds.length > 0 || hadGroupIds.length > 0) && this.sessionId) {
1773
+ await this.sendSubscription("unsubscribe", { contextIds: hadContextIds, groupIds: hadGroupIds });
1751
1774
  }
1752
1775
  }
1753
- async sendSubscription(method, contextIds) {
1776
+ async sendSubscription(method, ids) {
1754
1777
  try {
1755
1778
  const token = await this.getAuthToken();
1779
+ const params = {};
1780
+ if (ids.contextIds && ids.contextIds.length > 0) params.contextIds = ids.contextIds;
1781
+ if (ids.groupIds && ids.groupIds.length > 0) params.groupIds = ids.groupIds;
1756
1782
  const response = await fetch(`${this.baseUrl}/sse/subscription`, {
1757
1783
  method: "POST",
1758
1784
  headers: {
@@ -1762,7 +1788,7 @@ var SseClient = class {
1762
1788
  body: JSON.stringify({
1763
1789
  id: this.sessionId,
1764
1790
  method,
1765
- params: { contextIds }
1791
+ params
1766
1792
  })
1767
1793
  });
1768
1794
  if (!response.ok) {
@@ -1802,6 +1828,7 @@ var SseClient = class {
1802
1828
  }
1803
1829
  this.sessionId = null;
1804
1830
  this.subscribedContextIds.clear();
1831
+ this.subscribedGroupIds.clear();
1805
1832
  }
1806
1833
  };
1807
1834
 
@@ -1813,6 +1840,7 @@ var _WsClient = class _WsClient {
1813
1840
  this.reconnectAttempt = 0;
1814
1841
  this.reconnectTimer = null;
1815
1842
  this.subscribedContextIds = /* @__PURE__ */ new Set();
1843
+ this.subscribedGroupIds = /* @__PURE__ */ new Set();
1816
1844
  this.listeners = { connect: [], event: [], error: [] };
1817
1845
  this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
1818
1846
  this.getAuthToken = opts.getAuthToken;
@@ -1844,7 +1872,7 @@ var _WsClient = class _WsClient {
1844
1872
  }
1845
1873
  }
1846
1874
  async connect() {
1847
- if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {
1875
+ if (this.ws && (this.ws.readyState === 0 || this.ws.readyState === 1)) {
1848
1876
  return;
1849
1877
  }
1850
1878
  this.closed = false;
@@ -1858,13 +1886,7 @@ var _WsClient = class _WsClient {
1858
1886
  this.ws.onopen = () => {
1859
1887
  this.reconnectAttempt = 0;
1860
1888
  this.emit("connect");
1861
- if (this.subscribedContextIds.size > 0) {
1862
- this.sendMessage({
1863
- id: null,
1864
- method: "subscribe",
1865
- params: { contextIds: [...this.subscribedContextIds] }
1866
- });
1867
- }
1889
+ this.resubscribeAfterReconnect();
1868
1890
  };
1869
1891
  this.ws.onmessage = (event) => {
1870
1892
  this.handleMessage(event.data);
@@ -1888,16 +1910,17 @@ var _WsClient = class _WsClient {
1888
1910
  if (typeof raw !== "string") return;
1889
1911
  try {
1890
1912
  const msg = JSON.parse(raw);
1891
- if (msg.result && msg.result.contextId) {
1892
- let eventData = msg.result.data;
1893
- if (Array.isArray(eventData)) {
1894
- try {
1895
- const bytes = new Uint8Array(eventData);
1896
- const text = new TextDecoder().decode(bytes);
1897
- eventData = JSON.parse(text);
1898
- } catch {
1899
- }
1913
+ if (!msg.result) return;
1914
+ let eventData = msg.result.data;
1915
+ if (Array.isArray(eventData)) {
1916
+ try {
1917
+ const bytes = new Uint8Array(eventData);
1918
+ const text = new TextDecoder().decode(bytes);
1919
+ eventData = JSON.parse(text);
1920
+ } catch {
1900
1921
  }
1922
+ }
1923
+ if (msg.result.contextId) {
1901
1924
  this.emit("event", {
1902
1925
  contextId: msg.result.contextId,
1903
1926
  // Forward the event tag (sibling of `data` in core's flattened
@@ -1905,36 +1928,66 @@ var _WsClient = class _WsClient {
1905
1928
  type: msg.result.type,
1906
1929
  data: eventData
1907
1930
  });
1931
+ return;
1932
+ }
1933
+ if (msg.result.groupId) {
1934
+ this.emit("event", {
1935
+ groupId: msg.result.groupId,
1936
+ type: msg.result.type,
1937
+ data: eventData
1938
+ });
1908
1939
  }
1909
1940
  } catch {
1910
1941
  }
1911
1942
  }
1912
- subscribe(contextIds) {
1913
- for (const id of contextIds) {
1943
+ subscribe(idsOrOpts) {
1944
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1945
+ for (const id of opts.contextIds ?? []) {
1914
1946
  this.subscribedContextIds.add(id);
1915
1947
  }
1916
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1948
+ for (const id of opts.groupIds ?? []) {
1949
+ this.subscribedGroupIds.add(id);
1950
+ }
1951
+ if (this.ws && this.ws.readyState === 1) {
1917
1952
  this.sendMessage({
1918
1953
  id: null,
1919
1954
  method: "subscribe",
1920
- params: { contextIds }
1955
+ params: this.buildSubscriptionParams(opts.contextIds ?? [], opts.groupIds ?? [])
1921
1956
  });
1922
1957
  }
1923
1958
  }
1924
- unsubscribe(contextIds) {
1925
- for (const id of contextIds) {
1959
+ unsubscribe(idsOrOpts) {
1960
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1961
+ for (const id of opts.contextIds ?? []) {
1926
1962
  this.subscribedContextIds.delete(id);
1927
1963
  }
1928
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1964
+ for (const id of opts.groupIds ?? []) {
1965
+ this.subscribedGroupIds.delete(id);
1966
+ }
1967
+ if (this.ws && this.ws.readyState === 1) {
1929
1968
  this.sendMessage({
1930
1969
  id: null,
1931
1970
  method: "unsubscribe",
1932
- params: { contextIds }
1971
+ params: this.buildSubscriptionParams(opts.contextIds ?? [], opts.groupIds ?? [])
1933
1972
  });
1934
1973
  }
1935
1974
  }
1975
+ resubscribeAfterReconnect() {
1976
+ if (this.subscribedContextIds.size === 0 && this.subscribedGroupIds.size === 0) return;
1977
+ this.sendMessage({
1978
+ id: null,
1979
+ method: "subscribe",
1980
+ params: this.buildSubscriptionParams([...this.subscribedContextIds], [...this.subscribedGroupIds])
1981
+ });
1982
+ }
1983
+ buildSubscriptionParams(contextIds, groupIds) {
1984
+ const params = {};
1985
+ if (contextIds.length > 0) params.contextIds = contextIds;
1986
+ if (groupIds.length > 0) params.groupIds = groupIds;
1987
+ return params;
1988
+ }
1936
1989
  sendMessage(msg) {
1937
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1990
+ if (this.ws && this.ws.readyState === 1) {
1938
1991
  this.ws.send(JSON.stringify(msg));
1939
1992
  }
1940
1993
  }
@@ -1965,6 +2018,7 @@ var _WsClient = class _WsClient {
1965
2018
  this.ws = null;
1966
2019
  }
1967
2020
  this.subscribedContextIds.clear();
2021
+ this.subscribedGroupIds.clear();
1968
2022
  }
1969
2023
  };
1970
2024
  _WsClient.MAX_BACKOFF_MS = 3e4;