@calimero-network/mero-js 7.0.5 → 7.1.0

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.d.ts CHANGED
@@ -11,7 +11,7 @@ export { RpcClient, RpcError } from './rpc';
11
11
  export type { MigrateMyEntriesSummary } from './rpc';
12
12
  export type { ExecuteParams } from './rpc';
13
13
  export { SseClient, WsClient } from './events';
14
- export type { SseEventData, WsEventData, AppVersionChangedEvent } from './events';
14
+ export type { SseEventData, WsEventData, AppVersionChangedEvent, GroupMembershipEventData } from './events';
15
15
  export * from './cloud';
16
16
  export * from './capabilities';
17
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACjD,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGzD,cAAc,eAAe,CAAC;AAG9B,cAAc,YAAY,CAAC;AAG3B,cAAc,aAAa,CAAC;AAG5B,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAC9D,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAGnE,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACzE,YAAY,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,YAAY,EAAE,uBAAuB,EAAE,MAAM,OAAO,CAAC;AACrD,YAAY,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAG3C,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAC/C,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAGlF,cAAc,SAAS,CAAC;AAGxB,cAAc,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACjD,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGzD,cAAc,eAAe,CAAC;AAG9B,cAAc,YAAY,CAAC;AAG3B,cAAc,aAAa,CAAC;AAG5B,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAC9D,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAGnE,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACzE,YAAY,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,YAAY,EAAE,uBAAuB,EAAE,MAAM,OAAO,CAAC;AACrD,YAAY,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAG3C,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAC/C,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAC;AAG5G,cAAc,SAAS,CAAC;AAGxB,cAAc,gBAAgB,CAAC"}
package/dist/index.mjs CHANGED
@@ -1523,6 +1523,7 @@ var SseClient = class {
1523
1523
  this.abortController = null;
1524
1524
  this.reconnectTimer = null;
1525
1525
  this.subscribedContextIds = /* @__PURE__ */ new Set();
1526
+ this.subscribedGroupIds = /* @__PURE__ */ new Set();
1526
1527
  this.closed = false;
1527
1528
  this.listeners = { connect: [], event: [], error: [] };
1528
1529
  this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
@@ -1645,21 +1646,25 @@ var SseClient = class {
1645
1646
  if (msg.type === "connect" && msg.session_id) {
1646
1647
  this.sessionId = msg.session_id;
1647
1648
  this.emit("connect", msg.session_id);
1648
- if (this.subscribedContextIds.size > 0) {
1649
- this.sendSubscription("subscribe", [...this.subscribedContextIds]);
1649
+ if (this.subscribedContextIds.size > 0 || this.subscribedGroupIds.size > 0) {
1650
+ this.sendSubscription("subscribe", {
1651
+ contextIds: [...this.subscribedContextIds],
1652
+ groupIds: [...this.subscribedGroupIds]
1653
+ });
1650
1654
  }
1651
1655
  return;
1652
1656
  }
1653
- if (msg.result && msg.result.contextId) {
1654
- let eventData = msg.result.data;
1655
- if (Array.isArray(eventData)) {
1656
- try {
1657
- const bytes = new Uint8Array(eventData);
1658
- const text = new TextDecoder().decode(bytes);
1659
- eventData = JSON.parse(text);
1660
- } catch {
1661
- }
1657
+ if (!msg.result) return;
1658
+ let eventData = msg.result.data;
1659
+ if (Array.isArray(eventData)) {
1660
+ try {
1661
+ const bytes = new Uint8Array(eventData);
1662
+ const text = new TextDecoder().decode(bytes);
1663
+ eventData = JSON.parse(text);
1664
+ } catch {
1662
1665
  }
1666
+ }
1667
+ if (msg.result.contextId) {
1663
1668
  this.emit("event", {
1664
1669
  contextId: msg.result.contextId,
1665
1670
  // Forward the event tag (sibling of `data` in core's flattened
@@ -1667,31 +1672,52 @@ var SseClient = class {
1667
1672
  type: msg.result.type,
1668
1673
  data: eventData
1669
1674
  });
1675
+ return;
1676
+ }
1677
+ if (msg.result.groupId) {
1678
+ this.emit("event", {
1679
+ groupId: msg.result.groupId,
1680
+ type: msg.result.type,
1681
+ data: eventData
1682
+ });
1670
1683
  }
1671
1684
  } catch {
1672
1685
  }
1673
1686
  }
1674
- async subscribe(contextIds) {
1675
- const newIds = contextIds.filter((id) => !this.subscribedContextIds.has(id));
1676
- for (const id of contextIds) {
1687
+ async subscribe(idsOrOpts) {
1688
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1689
+ const newContextIds = (opts.contextIds ?? []).filter((id) => !this.subscribedContextIds.has(id));
1690
+ const newGroupIds = (opts.groupIds ?? []).filter((id) => !this.subscribedGroupIds.has(id));
1691
+ for (const id of opts.contextIds ?? []) {
1677
1692
  this.subscribedContextIds.add(id);
1678
1693
  }
1679
- if (newIds.length > 0 && this.sessionId) {
1680
- await this.sendSubscription("subscribe", newIds);
1694
+ for (const id of opts.groupIds ?? []) {
1695
+ this.subscribedGroupIds.add(id);
1696
+ }
1697
+ if ((newContextIds.length > 0 || newGroupIds.length > 0) && this.sessionId) {
1698
+ await this.sendSubscription("subscribe", { contextIds: newContextIds, groupIds: newGroupIds });
1681
1699
  }
1682
1700
  }
1683
- async unsubscribe(contextIds) {
1684
- const hadIds = contextIds.filter((id) => this.subscribedContextIds.has(id));
1685
- for (const id of contextIds) {
1701
+ async unsubscribe(idsOrOpts) {
1702
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1703
+ const hadContextIds = (opts.contextIds ?? []).filter((id) => this.subscribedContextIds.has(id));
1704
+ const hadGroupIds = (opts.groupIds ?? []).filter((id) => this.subscribedGroupIds.has(id));
1705
+ for (const id of opts.contextIds ?? []) {
1686
1706
  this.subscribedContextIds.delete(id);
1687
1707
  }
1688
- if (hadIds.length > 0 && this.sessionId) {
1689
- await this.sendSubscription("unsubscribe", hadIds);
1708
+ for (const id of opts.groupIds ?? []) {
1709
+ this.subscribedGroupIds.delete(id);
1710
+ }
1711
+ if ((hadContextIds.length > 0 || hadGroupIds.length > 0) && this.sessionId) {
1712
+ await this.sendSubscription("unsubscribe", { contextIds: hadContextIds, groupIds: hadGroupIds });
1690
1713
  }
1691
1714
  }
1692
- async sendSubscription(method, contextIds) {
1715
+ async sendSubscription(method, ids) {
1693
1716
  try {
1694
1717
  const token = await this.getAuthToken();
1718
+ const params = {};
1719
+ if (ids.contextIds && ids.contextIds.length > 0) params.contextIds = ids.contextIds;
1720
+ if (ids.groupIds && ids.groupIds.length > 0) params.groupIds = ids.groupIds;
1695
1721
  const response = await fetch(`${this.baseUrl}/sse/subscription`, {
1696
1722
  method: "POST",
1697
1723
  headers: {
@@ -1701,7 +1727,7 @@ var SseClient = class {
1701
1727
  body: JSON.stringify({
1702
1728
  id: this.sessionId,
1703
1729
  method,
1704
- params: { contextIds }
1730
+ params
1705
1731
  })
1706
1732
  });
1707
1733
  if (!response.ok) {
@@ -1741,6 +1767,7 @@ var SseClient = class {
1741
1767
  }
1742
1768
  this.sessionId = null;
1743
1769
  this.subscribedContextIds.clear();
1770
+ this.subscribedGroupIds.clear();
1744
1771
  }
1745
1772
  };
1746
1773
 
@@ -1752,6 +1779,7 @@ var _WsClient = class _WsClient {
1752
1779
  this.reconnectAttempt = 0;
1753
1780
  this.reconnectTimer = null;
1754
1781
  this.subscribedContextIds = /* @__PURE__ */ new Set();
1782
+ this.subscribedGroupIds = /* @__PURE__ */ new Set();
1755
1783
  this.listeners = { connect: [], event: [], error: [] };
1756
1784
  this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
1757
1785
  this.getAuthToken = opts.getAuthToken;
@@ -1783,7 +1811,7 @@ var _WsClient = class _WsClient {
1783
1811
  }
1784
1812
  }
1785
1813
  async connect() {
1786
- if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {
1814
+ if (this.ws && (this.ws.readyState === 0 || this.ws.readyState === 1)) {
1787
1815
  return;
1788
1816
  }
1789
1817
  this.closed = false;
@@ -1797,13 +1825,7 @@ var _WsClient = class _WsClient {
1797
1825
  this.ws.onopen = () => {
1798
1826
  this.reconnectAttempt = 0;
1799
1827
  this.emit("connect");
1800
- if (this.subscribedContextIds.size > 0) {
1801
- this.sendMessage({
1802
- id: null,
1803
- method: "subscribe",
1804
- params: { contextIds: [...this.subscribedContextIds] }
1805
- });
1806
- }
1828
+ this.resubscribeAfterReconnect();
1807
1829
  };
1808
1830
  this.ws.onmessage = (event) => {
1809
1831
  this.handleMessage(event.data);
@@ -1827,16 +1849,17 @@ var _WsClient = class _WsClient {
1827
1849
  if (typeof raw !== "string") return;
1828
1850
  try {
1829
1851
  const msg = JSON.parse(raw);
1830
- if (msg.result && msg.result.contextId) {
1831
- let eventData = msg.result.data;
1832
- if (Array.isArray(eventData)) {
1833
- try {
1834
- const bytes = new Uint8Array(eventData);
1835
- const text = new TextDecoder().decode(bytes);
1836
- eventData = JSON.parse(text);
1837
- } catch {
1838
- }
1852
+ if (!msg.result) return;
1853
+ let eventData = msg.result.data;
1854
+ if (Array.isArray(eventData)) {
1855
+ try {
1856
+ const bytes = new Uint8Array(eventData);
1857
+ const text = new TextDecoder().decode(bytes);
1858
+ eventData = JSON.parse(text);
1859
+ } catch {
1839
1860
  }
1861
+ }
1862
+ if (msg.result.contextId) {
1840
1863
  this.emit("event", {
1841
1864
  contextId: msg.result.contextId,
1842
1865
  // Forward the event tag (sibling of `data` in core's flattened
@@ -1844,36 +1867,66 @@ var _WsClient = class _WsClient {
1844
1867
  type: msg.result.type,
1845
1868
  data: eventData
1846
1869
  });
1870
+ return;
1871
+ }
1872
+ if (msg.result.groupId) {
1873
+ this.emit("event", {
1874
+ groupId: msg.result.groupId,
1875
+ type: msg.result.type,
1876
+ data: eventData
1877
+ });
1847
1878
  }
1848
1879
  } catch {
1849
1880
  }
1850
1881
  }
1851
- subscribe(contextIds) {
1852
- for (const id of contextIds) {
1882
+ subscribe(idsOrOpts) {
1883
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1884
+ for (const id of opts.contextIds ?? []) {
1853
1885
  this.subscribedContextIds.add(id);
1854
1886
  }
1855
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1887
+ for (const id of opts.groupIds ?? []) {
1888
+ this.subscribedGroupIds.add(id);
1889
+ }
1890
+ if (this.ws && this.ws.readyState === 1) {
1856
1891
  this.sendMessage({
1857
1892
  id: null,
1858
1893
  method: "subscribe",
1859
- params: { contextIds }
1894
+ params: this.buildSubscriptionParams(opts.contextIds ?? [], opts.groupIds ?? [])
1860
1895
  });
1861
1896
  }
1862
1897
  }
1863
- unsubscribe(contextIds) {
1864
- for (const id of contextIds) {
1898
+ unsubscribe(idsOrOpts) {
1899
+ const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
1900
+ for (const id of opts.contextIds ?? []) {
1865
1901
  this.subscribedContextIds.delete(id);
1866
1902
  }
1867
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1903
+ for (const id of opts.groupIds ?? []) {
1904
+ this.subscribedGroupIds.delete(id);
1905
+ }
1906
+ if (this.ws && this.ws.readyState === 1) {
1868
1907
  this.sendMessage({
1869
1908
  id: null,
1870
1909
  method: "unsubscribe",
1871
- params: { contextIds }
1910
+ params: this.buildSubscriptionParams(opts.contextIds ?? [], opts.groupIds ?? [])
1872
1911
  });
1873
1912
  }
1874
1913
  }
1914
+ resubscribeAfterReconnect() {
1915
+ if (this.subscribedContextIds.size === 0 && this.subscribedGroupIds.size === 0) return;
1916
+ this.sendMessage({
1917
+ id: null,
1918
+ method: "subscribe",
1919
+ params: this.buildSubscriptionParams([...this.subscribedContextIds], [...this.subscribedGroupIds])
1920
+ });
1921
+ }
1922
+ buildSubscriptionParams(contextIds, groupIds) {
1923
+ const params = {};
1924
+ if (contextIds.length > 0) params.contextIds = contextIds;
1925
+ if (groupIds.length > 0) params.groupIds = groupIds;
1926
+ return params;
1927
+ }
1875
1928
  sendMessage(msg) {
1876
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
1929
+ if (this.ws && this.ws.readyState === 1) {
1877
1930
  this.ws.send(JSON.stringify(msg));
1878
1931
  }
1879
1932
  }
@@ -1904,6 +1957,7 @@ var _WsClient = class _WsClient {
1904
1957
  this.ws = null;
1905
1958
  }
1906
1959
  this.subscribedContextIds.clear();
1960
+ this.subscribedGroupIds.clear();
1907
1961
  }
1908
1962
  };
1909
1963
  _WsClient.MAX_BACKOFF_MS = 3e4;