@webex/plugin-meetings 3.10.0-next.8 → 3.10.0-webex-services-ready.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.
Files changed (265) hide show
  1. package/dist/annotation/annotation.types.js.map +1 -1
  2. package/dist/annotation/constants.js.map +1 -1
  3. package/dist/annotation/index.js +19 -22
  4. package/dist/annotation/index.js.map +1 -1
  5. package/dist/breakouts/breakout.js +6 -6
  6. package/dist/breakouts/breakout.js.map +1 -1
  7. package/dist/breakouts/collection.js.map +1 -1
  8. package/dist/breakouts/edit-lock-error.js +9 -11
  9. package/dist/breakouts/edit-lock-error.js.map +1 -1
  10. package/dist/breakouts/events.js.map +1 -1
  11. package/dist/breakouts/index.js +126 -127
  12. package/dist/breakouts/index.js.map +1 -1
  13. package/dist/breakouts/request.js +6 -8
  14. package/dist/breakouts/request.js.map +1 -1
  15. package/dist/breakouts/utils.js.map +1 -1
  16. package/dist/common/browser-detection.js.map +1 -1
  17. package/dist/common/collection.js +1 -2
  18. package/dist/common/collection.js.map +1 -1
  19. package/dist/common/config.js.map +1 -1
  20. package/dist/common/errors/captcha-error.js +9 -11
  21. package/dist/common/errors/captcha-error.js.map +1 -1
  22. package/dist/common/errors/intent-to-join.js +10 -12
  23. package/dist/common/errors/intent-to-join.js.map +1 -1
  24. package/dist/common/errors/join-forbidden-error.js +10 -12
  25. package/dist/common/errors/join-forbidden-error.js.map +1 -1
  26. package/dist/common/errors/join-meeting.js +10 -12
  27. package/dist/common/errors/join-meeting.js.map +1 -1
  28. package/dist/common/errors/join-webinar-error.js +9 -11
  29. package/dist/common/errors/join-webinar-error.js.map +1 -1
  30. package/dist/common/errors/media.js +9 -11
  31. package/dist/common/errors/media.js.map +1 -1
  32. package/dist/common/errors/multistream-not-supported-error.js +9 -11
  33. package/dist/common/errors/multistream-not-supported-error.js.map +1 -1
  34. package/dist/common/errors/no-meeting-info.js +9 -11
  35. package/dist/common/errors/no-meeting-info.js.map +1 -1
  36. package/dist/common/errors/parameter.js +11 -14
  37. package/dist/common/errors/parameter.js.map +1 -1
  38. package/dist/common/errors/password-error.js +9 -11
  39. package/dist/common/errors/password-error.js.map +1 -1
  40. package/dist/common/errors/permission.js +9 -11
  41. package/dist/common/errors/permission.js.map +1 -1
  42. package/dist/common/errors/reclaim-host-role-errors.js +32 -38
  43. package/dist/common/errors/reclaim-host-role-errors.js.map +1 -1
  44. package/dist/common/errors/reconnection-not-started.js +5 -6
  45. package/dist/common/errors/reconnection-not-started.js.map +1 -1
  46. package/dist/common/errors/reconnection.js +9 -11
  47. package/dist/common/errors/reconnection.js.map +1 -1
  48. package/dist/common/errors/stats.js +9 -11
  49. package/dist/common/errors/stats.js.map +1 -1
  50. package/dist/common/errors/webex-errors.js +20 -29
  51. package/dist/common/errors/webex-errors.js.map +1 -1
  52. package/dist/common/errors/webex-meetings-error.js +9 -12
  53. package/dist/common/errors/webex-meetings-error.js.map +1 -1
  54. package/dist/common/events/events-scope.js +9 -10
  55. package/dist/common/events/events-scope.js.map +1 -1
  56. package/dist/common/events/events.js +9 -10
  57. package/dist/common/events/events.js.map +1 -1
  58. package/dist/common/events/trigger-proxy.js.map +1 -1
  59. package/dist/common/events/util.js.map +1 -1
  60. package/dist/common/logs/logger-config.js.map +1 -1
  61. package/dist/common/logs/logger-proxy.js.map +1 -1
  62. package/dist/common/logs/request.js +17 -17
  63. package/dist/common/logs/request.js.map +1 -1
  64. package/dist/common/queue.js +1 -2
  65. package/dist/common/queue.js.map +1 -1
  66. package/dist/config.js +0 -1
  67. package/dist/config.js.map +1 -1
  68. package/dist/constants.js +11 -8
  69. package/dist/constants.js.map +1 -1
  70. package/dist/controls-options-manager/constants.js.map +1 -1
  71. package/dist/controls-options-manager/enums.js.map +1 -1
  72. package/dist/controls-options-manager/index.js +1 -2
  73. package/dist/controls-options-manager/index.js.map +1 -1
  74. package/dist/controls-options-manager/types.js.map +1 -1
  75. package/dist/controls-options-manager/util.js +1 -2
  76. package/dist/controls-options-manager/util.js.map +1 -1
  77. package/dist/hashTree/constants.js +20 -0
  78. package/dist/hashTree/constants.js.map +1 -0
  79. package/dist/hashTree/hashTree.js +515 -0
  80. package/dist/hashTree/hashTree.js.map +1 -0
  81. package/dist/hashTree/hashTreeParser.js +1266 -0
  82. package/dist/hashTree/hashTreeParser.js.map +1 -0
  83. package/dist/hashTree/types.js +21 -0
  84. package/dist/hashTree/types.js.map +1 -0
  85. package/dist/hashTree/utils.js +48 -0
  86. package/dist/hashTree/utils.js.map +1 -0
  87. package/dist/index.js +1 -2
  88. package/dist/index.js.map +1 -1
  89. package/dist/interceptors/index.js.map +1 -1
  90. package/dist/interceptors/locusRetry.js +6 -8
  91. package/dist/interceptors/locusRetry.js.map +1 -1
  92. package/dist/interceptors/locusRouteToken.js +6 -8
  93. package/dist/interceptors/locusRouteToken.js.map +1 -1
  94. package/dist/interpretation/collection.js.map +1 -1
  95. package/dist/interpretation/index.js +1 -2
  96. package/dist/interpretation/index.js.map +1 -1
  97. package/dist/interpretation/siLanguage.js +1 -1
  98. package/dist/interpretation/siLanguage.js.map +1 -1
  99. package/dist/locus-info/controlsUtils.js.map +1 -1
  100. package/dist/locus-info/embeddedAppsUtils.js.map +1 -1
  101. package/dist/locus-info/fullState.js.map +1 -1
  102. package/dist/locus-info/hostUtils.js.map +1 -1
  103. package/dist/locus-info/index.js +551 -94
  104. package/dist/locus-info/index.js.map +1 -1
  105. package/dist/locus-info/infoUtils.js.map +1 -1
  106. package/dist/locus-info/mediaSharesUtils.js.map +1 -1
  107. package/dist/locus-info/parser.js +3 -4
  108. package/dist/locus-info/parser.js.map +1 -1
  109. package/dist/locus-info/selfUtils.js.map +1 -1
  110. package/dist/locus-info/types.js +7 -0
  111. package/dist/locus-info/types.js.map +1 -0
  112. package/dist/media/MediaConnectionAwaiter.js +1 -2
  113. package/dist/media/MediaConnectionAwaiter.js.map +1 -1
  114. package/dist/media/index.js +0 -2
  115. package/dist/media/index.js.map +1 -1
  116. package/dist/media/properties.js +15 -17
  117. package/dist/media/properties.js.map +1 -1
  118. package/dist/media/util.js.map +1 -1
  119. package/dist/meeting/brbState.js +8 -9
  120. package/dist/meeting/brbState.js.map +1 -1
  121. package/dist/meeting/connectionStateHandler.js +10 -13
  122. package/dist/meeting/connectionStateHandler.js.map +1 -1
  123. package/dist/meeting/in-meeting-actions.js.map +1 -1
  124. package/dist/meeting/index.js +1555 -1527
  125. package/dist/meeting/index.js.map +1 -1
  126. package/dist/meeting/locusMediaRequest.js +13 -17
  127. package/dist/meeting/locusMediaRequest.js.map +1 -1
  128. package/dist/meeting/muteState.js +11 -12
  129. package/dist/meeting/muteState.js.map +1 -1
  130. package/dist/meeting/request.js +101 -104
  131. package/dist/meeting/request.js.map +1 -1
  132. package/dist/meeting/request.type.js.map +1 -1
  133. package/dist/meeting/state.js.map +1 -1
  134. package/dist/meeting/type.js.map +1 -1
  135. package/dist/meeting/util.js +24 -23
  136. package/dist/meeting/util.js.map +1 -1
  137. package/dist/meeting/voicea-meeting.js +3 -3
  138. package/dist/meeting/voicea-meeting.js.map +1 -1
  139. package/dist/meeting-info/collection.js +7 -10
  140. package/dist/meeting-info/collection.js.map +1 -1
  141. package/dist/meeting-info/index.js +1 -2
  142. package/dist/meeting-info/index.js.map +1 -1
  143. package/dist/meeting-info/meeting-info-v2.js +135 -146
  144. package/dist/meeting-info/meeting-info-v2.js.map +1 -1
  145. package/dist/meeting-info/request.js +1 -2
  146. package/dist/meeting-info/request.js.map +1 -1
  147. package/dist/meeting-info/util.js +36 -37
  148. package/dist/meeting-info/util.js.map +1 -1
  149. package/dist/meeting-info/utilv2.js +30 -31
  150. package/dist/meeting-info/utilv2.js.map +1 -1
  151. package/dist/meetings/collection.js +6 -8
  152. package/dist/meetings/collection.js.map +1 -1
  153. package/dist/meetings/index.js +179 -141
  154. package/dist/meetings/index.js.map +1 -1
  155. package/dist/meetings/meetings.types.js.map +1 -1
  156. package/dist/meetings/request.js +6 -8
  157. package/dist/meetings/request.js.map +1 -1
  158. package/dist/meetings/util.js +25 -23
  159. package/dist/meetings/util.js.map +1 -1
  160. package/dist/member/index.js +1 -2
  161. package/dist/member/index.js.map +1 -1
  162. package/dist/member/types.js +6 -3
  163. package/dist/member/types.js.map +1 -1
  164. package/dist/member/util.js.map +1 -1
  165. package/dist/members/collection.js +1 -2
  166. package/dist/members/collection.js.map +1 -1
  167. package/dist/members/index.js +18 -21
  168. package/dist/members/index.js.map +1 -1
  169. package/dist/members/request.js +8 -11
  170. package/dist/members/request.js.map +1 -1
  171. package/dist/members/types.js.map +1 -1
  172. package/dist/members/util.js.map +1 -1
  173. package/dist/metrics/constants.js +3 -1
  174. package/dist/metrics/constants.js.map +1 -1
  175. package/dist/metrics/index.js +3 -4
  176. package/dist/metrics/index.js.map +1 -1
  177. package/dist/multistream/mediaRequestManager.js +1 -2
  178. package/dist/multistream/mediaRequestManager.js.map +1 -1
  179. package/dist/multistream/receiveSlot.js +34 -45
  180. package/dist/multistream/receiveSlot.js.map +1 -1
  181. package/dist/multistream/receiveSlotManager.js +8 -9
  182. package/dist/multistream/receiveSlotManager.js.map +1 -1
  183. package/dist/multistream/remoteMedia.js +12 -15
  184. package/dist/multistream/remoteMedia.js.map +1 -1
  185. package/dist/multistream/remoteMediaGroup.js +1 -2
  186. package/dist/multistream/remoteMediaGroup.js.map +1 -1
  187. package/dist/multistream/remoteMediaManager.js +122 -123
  188. package/dist/multistream/remoteMediaManager.js.map +1 -1
  189. package/dist/multistream/sendSlotManager.js +29 -30
  190. package/dist/multistream/sendSlotManager.js.map +1 -1
  191. package/dist/personal-meeting-room/index.js +16 -19
  192. package/dist/personal-meeting-room/index.js.map +1 -1
  193. package/dist/personal-meeting-room/request.js +7 -10
  194. package/dist/personal-meeting-room/request.js.map +1 -1
  195. package/dist/personal-meeting-room/util.js.map +1 -1
  196. package/dist/reachability/clusterReachability.js +56 -373
  197. package/dist/reachability/clusterReachability.js.map +1 -1
  198. package/dist/reachability/index.js +203 -205
  199. package/dist/reachability/index.js.map +1 -1
  200. package/dist/reachability/reachability.types.js +14 -1
  201. package/dist/reachability/reachability.types.js.map +1 -1
  202. package/dist/reachability/reachabilityPeerConnection.js +445 -0
  203. package/dist/reachability/reachabilityPeerConnection.js.map +1 -0
  204. package/dist/reachability/request.js.map +1 -1
  205. package/dist/reachability/util.js.map +1 -1
  206. package/dist/reactions/constants.js.map +1 -1
  207. package/dist/reactions/reactions.js.map +1 -1
  208. package/dist/reactions/reactions.type.js.map +1 -1
  209. package/dist/reconnection-manager/index.js +178 -176
  210. package/dist/reconnection-manager/index.js.map +1 -1
  211. package/dist/recording-controller/enums.js.map +1 -1
  212. package/dist/recording-controller/index.js +1 -2
  213. package/dist/recording-controller/index.js.map +1 -1
  214. package/dist/recording-controller/util.js.map +1 -1
  215. package/dist/roap/index.js +12 -15
  216. package/dist/roap/index.js.map +1 -1
  217. package/dist/roap/request.js +24 -26
  218. package/dist/roap/request.js.map +1 -1
  219. package/dist/roap/turnDiscovery.js +75 -76
  220. package/dist/roap/turnDiscovery.js.map +1 -1
  221. package/dist/roap/types.js.map +1 -1
  222. package/dist/transcription/index.js +4 -5
  223. package/dist/transcription/index.js.map +1 -1
  224. package/dist/types/constants.d.ts +26 -21
  225. package/dist/types/hashTree/constants.d.ts +8 -0
  226. package/dist/types/hashTree/hashTree.d.ts +129 -0
  227. package/dist/types/hashTree/hashTreeParser.d.ts +260 -0
  228. package/dist/types/hashTree/types.d.ts +25 -0
  229. package/dist/types/hashTree/utils.d.ts +9 -0
  230. package/dist/types/locus-info/index.d.ts +91 -42
  231. package/dist/types/locus-info/types.d.ts +46 -0
  232. package/dist/types/meeting/index.d.ts +22 -9
  233. package/dist/types/meetings/index.d.ts +9 -2
  234. package/dist/types/metrics/constants.d.ts +2 -0
  235. package/dist/types/reachability/clusterReachability.d.ts +10 -88
  236. package/dist/types/reachability/reachability.types.d.ts +12 -1
  237. package/dist/types/reachability/reachabilityPeerConnection.d.ts +111 -0
  238. package/dist/webinar/collection.js +1 -2
  239. package/dist/webinar/collection.js.map +1 -1
  240. package/dist/webinar/index.js +148 -158
  241. package/dist/webinar/index.js.map +1 -1
  242. package/package.json +22 -21
  243. package/src/constants.ts +13 -1
  244. package/src/hashTree/constants.ts +9 -0
  245. package/src/hashTree/hashTree.ts +463 -0
  246. package/src/hashTree/hashTreeParser.ts +1161 -0
  247. package/src/hashTree/types.ts +30 -0
  248. package/src/hashTree/utils.ts +42 -0
  249. package/src/locus-info/index.ts +556 -85
  250. package/src/locus-info/types.ts +48 -0
  251. package/src/meeting/index.ts +58 -26
  252. package/src/meeting/util.ts +1 -0
  253. package/src/meetings/index.ts +104 -51
  254. package/src/metrics/constants.ts +2 -0
  255. package/src/reachability/clusterReachability.ts +50 -347
  256. package/src/reachability/reachability.types.ts +15 -1
  257. package/src/reachability/reachabilityPeerConnection.ts +416 -0
  258. package/test/unit/spec/hashTree/hashTree.ts +655 -0
  259. package/test/unit/spec/hashTree/hashTreeParser.ts +1532 -0
  260. package/test/unit/spec/hashTree/utils.ts +103 -0
  261. package/test/unit/spec/locus-info/index.js +667 -1
  262. package/test/unit/spec/meeting/index.js +91 -20
  263. package/test/unit/spec/meeting/utils.js +77 -0
  264. package/test/unit/spec/meetings/index.js +71 -26
  265. package/test/unit/spec/reachability/clusterReachability.ts +281 -138
package/src/constants.ts CHANGED
@@ -793,7 +793,19 @@ export const LOCUSEVENT = {
793
793
  RECORDING_STOPPED: 'locus.recording_stopped',
794
794
 
795
795
  SELF_CHANGED: 'locus.self_changed',
796
- };
796
+
797
+ HASH_TREE_DATA_UPDATED: 'locus.state_message',
798
+
799
+ // events generated internally by SDK
800
+ SDK_LOCUS_FROM_SYNC_MEETINGS: 'jsSdk.locus_from_sync_meetings', // generated for each meeting from response to GET /loci Locus API call
801
+ SDK_NO_EVENT: 'jsSdk.no_event', // used in cases where eventType is irrelevant
802
+ } as const;
803
+
804
+ export type LOCUSEVENT = Enum<typeof LOCUSEVENT>;
805
+
806
+ // HASH_TREE_DATA_UPDATED event can come over Mercury (so it's listed above with other Mercury events),
807
+ // but also over LLM as an event like this:
808
+ export const LOCUS_LLM_EVENT = `event:${LOCUSEVENT.HASH_TREE_DATA_UPDATED}`;
797
809
 
798
810
  export const MEDIA_TRACK_CONSTRAINT = {
799
811
  CURSOR: {
@@ -0,0 +1,9 @@
1
+ export const EMPTY_HASH = '99aa06d3014798d86001c324468d497f';
2
+
3
+ export const DataSetNames = {
4
+ MAIN: 'main', // sent to web client, contains also panelists, over LLM
5
+ ATTENDEES: 'attendees', // NOT SENT to web client, all the attendees in the locus
6
+ ATD_ACTIVE: 'atd-active', // only sent to panelists, over LLM; the attendees that have their hands raised or are allowed to unmute themselves
7
+ ATD_UNMUTED: 'atd-unmuted', // sent to web client, over LLM, not sent to panelists; the attendees that are unmuted
8
+ SELF: 'self', // sent to web client, over Mercury
9
+ };
@@ -0,0 +1,463 @@
1
+ import {XXH3_128} from 'xxh3-ts';
2
+ import {EMPTY_HASH} from './constants';
3
+ import {ObjectType} from './types';
4
+
5
+ export type LeafDataItem = {
6
+ type: ObjectType;
7
+ id: number;
8
+ version: number;
9
+ };
10
+
11
+ /**
12
+ * HashTree is a data structure that organizes items into leaves based on their IDs,
13
+ */
14
+ class HashTree {
15
+ leaves: Array<Record<string, Record<number, LeafDataItem>>>;
16
+
17
+ leafHashes: Array<string>;
18
+ readonly numLeaves: number;
19
+
20
+ /**
21
+ * Constructs a new HashTree.
22
+ * @param {LeafDataItem[]} leafData Initial data to populate the tree.
23
+ * @param {number} numLeaves The number of leaf nodes in the tree. Must be 0 or a power of 2.
24
+ * @throws {Error} If numLeaves is not 0 or a power of 2.
25
+ */
26
+ constructor(leafData: LeafDataItem[], numLeaves: number) {
27
+ // eslint-disable-next-line no-bitwise
28
+ if ((numLeaves & (numLeaves - 1)) !== 0) {
29
+ throw new Error(`Number of leaves must be a power of 2, saw ${numLeaves}`);
30
+ }
31
+
32
+ this.numLeaves = numLeaves;
33
+ this.leafHashes = new Array(numLeaves).fill(EMPTY_HASH);
34
+
35
+ this.leaves = new Array(numLeaves).fill(null).map(() => {
36
+ return {};
37
+ });
38
+
39
+ if (leafData) {
40
+ this.putItems(leafData);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Internal logic for adding or updating an item, without computing the leaf hash.
46
+ * @param {LeafDataItem} item The item to add or update.
47
+ * @returns {{put: boolean, index: (number|null)}} Object indicating if put and the leaf index.
48
+ * @private
49
+ */
50
+ private _putItemInternal(item: LeafDataItem): {put: boolean; index: number | null} {
51
+ if (this.numLeaves === 0) {
52
+ return {put: false, index: null}; // Cannot add to a tree with 0 leaves
53
+ }
54
+
55
+ const index = item.id % this.numLeaves;
56
+
57
+ if (!this.leaves[index][item.type]) {
58
+ this.leaves[index][item.type] = {};
59
+ }
60
+
61
+ const existingItem = this.leaves[index][item.type][item.id];
62
+
63
+ if (!existingItem || existingItem.version < item.version) {
64
+ this.leaves[index][item.type][item.id] = item;
65
+
66
+ return {put: true, index};
67
+ }
68
+
69
+ return {put: false, index: null};
70
+ }
71
+
72
+ /**
73
+ * Adds or updates a single item in the hash tree.
74
+ * @param {LeafDataItem} item The item to add or update.
75
+ * @returns {boolean} True if the item was added or updated, false otherwise (e.g., older version or tree has 0 leaves).
76
+ */
77
+ putItem(item: LeafDataItem): boolean {
78
+ const {put, index} = this._putItemInternal(item);
79
+
80
+ if (put && index !== null) {
81
+ this.computeLeafHash(index);
82
+ }
83
+
84
+ return put;
85
+ }
86
+
87
+ /**
88
+ * Adds or updates multiple items in the hash tree.
89
+ * @param {LeafDataItem[]} items The array of items to add or update.
90
+ * @returns {boolean[]} An array of booleans indicating success for each item.
91
+ */
92
+ putItems(items: LeafDataItem[]): boolean[] {
93
+ if (this.numLeaves === 0 && items.length > 0) {
94
+ // Cannot add items to a tree with 0 leaves.
95
+ return items.map(() => false);
96
+ }
97
+ const results: boolean[] = [];
98
+ const changedLeafIndexes = new Set<number>();
99
+
100
+ items.forEach((item) => {
101
+ const {put, index} = this._putItemInternal(item);
102
+ results.push(put);
103
+ if (put && index !== null) {
104
+ changedLeafIndexes.add(index);
105
+ }
106
+ });
107
+
108
+ changedLeafIndexes.forEach((index) => {
109
+ this.computeLeafHash(index);
110
+ });
111
+
112
+ return results;
113
+ }
114
+
115
+ /**
116
+ * Internal logic for removing an item, without computing the leaf hash.
117
+ * @param {LeafDataItem} item The item to remove.
118
+ * @returns {{removed: boolean, index: (number|null)}} Object indicating if removed and the leaf index.
119
+ * @private
120
+ */
121
+ private _removeItemInternal(item: LeafDataItem): {removed: boolean; index: number | null} {
122
+ if (this.numLeaves === 0) {
123
+ return {removed: false, index: null};
124
+ }
125
+
126
+ const index = item.id % this.numLeaves;
127
+
128
+ if (
129
+ this.leaves[index] &&
130
+ this.leaves[index][item.type] &&
131
+ this.leaves[index][item.type][item.id]
132
+ ) {
133
+ const existingItem = this.leaves[index][item.type][item.id];
134
+ if (
135
+ existingItem.id === item.id &&
136
+ existingItem.type === item.type &&
137
+ existingItem.version <= item.version
138
+ ) {
139
+ delete this.leaves[index][item.type][item.id];
140
+ if (Object.keys(this.leaves[index][item.type]).length === 0) {
141
+ delete this.leaves[index][item.type];
142
+ }
143
+
144
+ return {removed: true, index};
145
+ }
146
+ }
147
+
148
+ return {removed: false, index: null};
149
+ }
150
+
151
+ /**
152
+ * Removes a single item from the hash tree.
153
+ * The removal is based on matching type, id, and the provided item's version
154
+ * being greater than or equal to the existing item's version.
155
+ * @param {LeafDataItem} item The item to remove.
156
+ * @returns {boolean} True if the item was removed, false otherwise.
157
+ */
158
+ removeItem(item: LeafDataItem): boolean {
159
+ const {removed, index} = this._removeItemInternal(item);
160
+
161
+ if (removed && index !== null) {
162
+ this.computeLeafHash(index);
163
+ }
164
+
165
+ return removed;
166
+ }
167
+
168
+ /**
169
+ * Removes multiple items from the hash tree.
170
+ * @param {LeafDataItem[]} items The array of items to remove.
171
+ * @returns {boolean[]} An array of booleans indicating success for each item.
172
+ */
173
+ removeItems(items: LeafDataItem[]): boolean[] {
174
+ if (this.numLeaves === 0 && items.length > 0) {
175
+ return items.map(() => false);
176
+ }
177
+
178
+ const results: boolean[] = [];
179
+ const changedLeafIndexes = new Set<number>();
180
+
181
+ items.forEach((item) => {
182
+ const {removed, index} = this._removeItemInternal(item);
183
+ results.push(removed);
184
+ if (removed && index !== null) {
185
+ changedLeafIndexes.add(index);
186
+ }
187
+ });
188
+
189
+ changedLeafIndexes.forEach((index) => {
190
+ this.computeLeafHash(index);
191
+ });
192
+
193
+ return results;
194
+ }
195
+
196
+ /**
197
+ * Updates multiple items in the hash tree.
198
+ * This method can handle both updating and removing items based on the `operation` flag.
199
+ *
200
+ * @param {object[]} itemUpdates An array of objects containing `operation` flag and the `item` to update.
201
+ * @returns {boolean[]} An array of booleans indicating success for each operation.
202
+ */
203
+ updateItems(itemUpdates: {operation: 'update' | 'remove'; item: LeafDataItem}[]): boolean[] {
204
+ if (this.numLeaves === 0 && itemUpdates.length > 0) {
205
+ return itemUpdates.map(() => false);
206
+ }
207
+
208
+ const results: boolean[] = [];
209
+ const changedLeafIndexes = new Set<number>();
210
+
211
+ itemUpdates.forEach(({operation, item}) => {
212
+ if (operation === 'remove') {
213
+ const {removed, index} = this._removeItemInternal(item);
214
+ results.push(removed);
215
+ if (removed && index !== null) {
216
+ changedLeafIndexes.add(index);
217
+ }
218
+ } else {
219
+ const {put, index} = this._putItemInternal(item);
220
+ results.push(put);
221
+ if (put && index !== null) {
222
+ changedLeafIndexes.add(index);
223
+ }
224
+ }
225
+ });
226
+
227
+ changedLeafIndexes.forEach((index) => {
228
+ this.computeLeafHash(index);
229
+ });
230
+
231
+ return results;
232
+ }
233
+
234
+ /**
235
+ * Computes the hash for a specific leaf.
236
+ * The hash is based on the sorted items within the leaf.
237
+ * @param {number} index The index of the leaf to compute the hash for.
238
+ * @returns {void}
239
+ */
240
+ computeLeafHash(index: number) {
241
+ if (index < 0 || index >= this.numLeaves) {
242
+ // nothing to do
243
+ return;
244
+ }
245
+ const leafContent = this.leaves[index];
246
+
247
+ const totalItemsCount = Object.keys(leafContent).reduce((count, type) => {
248
+ return count + Object.keys(leafContent[type]).length;
249
+ }, 0);
250
+ const buffer = Buffer.alloc(totalItemsCount * 16);
251
+
252
+ let offset = 0;
253
+
254
+ // iterate through the item types lexicographically
255
+ const itemTypes = Object.keys(leafContent).sort();
256
+ itemTypes.forEach((type) => {
257
+ // iterate through the items of this type in ascending order of ID
258
+ const items = Object.values(leafContent[type]).sort(
259
+ (a: LeafDataItem, b: LeafDataItem) => a.id - b.id
260
+ );
261
+
262
+ // add all the items id and version to the hasher
263
+ items.forEach((item: LeafDataItem) => {
264
+ buffer.writeBigInt64LE(BigInt(item.id), offset);
265
+ buffer.writeBigInt64LE(BigInt(item.version), offset + 8);
266
+
267
+ offset += 16;
268
+ });
269
+ });
270
+
271
+ this.leafHashes[index] = XXH3_128(buffer, BigInt(0)).toString(16).padStart(32, '0');
272
+ }
273
+
274
+ /**
275
+ * Computes all internal and leaf node hashes of the tree.
276
+ * Internal node hashes are computed bottom-up from the leaf hashes.
277
+ * @returns {string[]} An array of hash strings, with internal node hashes first, followed by leaf hashes.
278
+ * Returns `[EMPTY_HASH]` if the tree has 0 leaves.
279
+ */
280
+ computeTreeHashes(): string[] {
281
+ if (this.numLeaves === 0) {
282
+ return [EMPTY_HASH];
283
+ }
284
+
285
+ let currentLevelHashes = [...this.leafHashes];
286
+ const allHashes = [];
287
+
288
+ while (currentLevelHashes.length > 1) {
289
+ const nextLevelHashes: string[] = [];
290
+ for (let i = 0; i < currentLevelHashes.length; i += 2) {
291
+ const leftHash = currentLevelHashes[i];
292
+ const rightHash = i + 1 < currentLevelHashes.length ? currentLevelHashes[i + 1] : leftHash;
293
+
294
+ const input = Buffer.concat([
295
+ Buffer.from(leftHash, 'hex').subarray(0, 8).reverse(),
296
+ Buffer.from(leftHash, 'hex').subarray(8, 16).reverse(),
297
+ Buffer.from(rightHash, 'hex').subarray(0, 8).reverse(),
298
+ Buffer.from(rightHash, 'hex').subarray(8, 16).reverse(),
299
+ ]);
300
+
301
+ nextLevelHashes.push(XXH3_128(input, BigInt(0)).toString(16).padStart(32, '0'));
302
+ }
303
+ currentLevelHashes = nextLevelHashes;
304
+ allHashes.unshift(...currentLevelHashes);
305
+ }
306
+
307
+ return [...allHashes, ...this.leafHashes];
308
+ }
309
+
310
+ /**
311
+ * Returns all hashes in the tree (internal nodes then leaf nodes).
312
+ * @returns {string[]} An array of hash strings.
313
+ */
314
+ getHashes(): string[] {
315
+ return this.computeTreeHashes();
316
+ }
317
+
318
+ /**
319
+ * Computes and returns the hash value of the root node.
320
+ * @returns {string} The root hash of the entire tree. Returns `EMPTY_HASH` if the tree has 0 leaves.
321
+ */
322
+ getRootHash(): string {
323
+ if (this.numLeaves === 0) {
324
+ return EMPTY_HASH;
325
+ }
326
+
327
+ return this.computeTreeHashes()[0];
328
+ }
329
+
330
+ /**
331
+ * Gets the number of leaves in the tree.
332
+ * @returns {number} The number of leaves.
333
+ */
334
+ getLeafCount(): number {
335
+ return this.numLeaves;
336
+ }
337
+
338
+ /**
339
+ * Calculates the total number of items stored in the tree.
340
+ * @returns {number} The total number of items.
341
+ */
342
+ getTotalItemCount(): number {
343
+ let count = 0;
344
+ for (const leaf of this.leaves) {
345
+ for (const type of Object.keys(leaf)) {
346
+ count += Object.keys(leaf[type]).length;
347
+ }
348
+ }
349
+
350
+ return count;
351
+ }
352
+
353
+ /**
354
+ * Retrieves all data items from a specific leaf.
355
+ * @param {number} leafIndex The index of the leaf.
356
+ * @returns {LeafDataItem[]} An array of LeafDataItem in the specified leaf, sorted by ID,
357
+ * or an empty array if the index is invalid or leaf is empty.
358
+ */
359
+ getLeafData(leafIndex: number): LeafDataItem[] {
360
+ if (leafIndex < 0 || leafIndex >= this.numLeaves) {
361
+ return [];
362
+ }
363
+ const leafContent = this.leaves[leafIndex];
364
+ const items: LeafDataItem[] = [];
365
+ for (const type of Object.keys(leafContent)) {
366
+ items.push(...Object.values(leafContent[type]));
367
+ }
368
+ // Optionally sort them if a specific order is required, e.g., by ID
369
+ items.sort((a, b) => a.id - b.id);
370
+
371
+ return items;
372
+ }
373
+
374
+ /**
375
+ * Resizes the HashTree to have a new number of leaf nodes, redistributing all existing items.
376
+ * @param {number} newNumLeaves The new number of leaf nodes (must be 0 or a power of 2).
377
+ * @returns {boolean} true if the tree was resized, false if the size didn't change.
378
+ * @throws {Error} if newNumLeaves is not 0 or a power of 2.
379
+ */
380
+ resize(newNumLeaves: number): boolean {
381
+ // eslint-disable-next-line no-bitwise
382
+ if (newNumLeaves < 0 || (newNumLeaves !== 0 && (newNumLeaves & (newNumLeaves - 1)) !== 0)) {
383
+ throw new Error('New number of leaves must be 0 or a power of 2');
384
+ }
385
+
386
+ if (newNumLeaves === this.numLeaves) {
387
+ return false;
388
+ }
389
+
390
+ const allItems: LeafDataItem[] = [];
391
+ for (const leaf of this.leaves) {
392
+ for (const type of Object.keys(leaf)) {
393
+ allItems.push(...Object.values(leaf[type]));
394
+ }
395
+ }
396
+
397
+ // Re-initialize
398
+ // eslint-disable-next-line no-extra-semi
399
+ (this as any).numLeaves = newNumLeaves; // Type assertion to update readonly property
400
+ this.leafHashes = new Array(newNumLeaves).fill(EMPTY_HASH);
401
+ this.leaves = new Array(newNumLeaves).fill(null).map(() => ({}));
402
+
403
+ if (newNumLeaves > 0) {
404
+ this.putItems(allItems); // Re-add items which will be re-assigned to leaves and re-hashed
405
+ }
406
+
407
+ return true;
408
+ }
409
+
410
+ /**
411
+ * Compares the tree's leaf hashes with an external set of hashes and returns the indices of differing leaf nodes.
412
+ * The externalHashes array is expected to contain all node hashes (internal followed by leaves),
413
+ * similar to the output of getHashes().
414
+ * @param {string[]} externalHashes An array of hash strings (internal node hashes then leaf hashes).
415
+ * @returns {number[]} An array of indices of the leaf nodes that have different hashes.
416
+ */
417
+ diffHashes(externalHashes: string[]): number[] {
418
+ if (this.numLeaves === 0) {
419
+ // If this tree is empty, its hash is EMPTY_HASH.
420
+ // It differs if externalHashes is not a single EMPTY_HASH.
421
+ if (externalHashes && externalHashes.length === 1 && externalHashes[0] === EMPTY_HASH) {
422
+ return []; // No differences
423
+ }
424
+ // An empty tree has no leaves to differ, so return an empty array
425
+ // as no specific leaf indexes are different.
426
+
427
+ return [];
428
+ }
429
+
430
+ // We are interested in comparing the leaf hashes part.
431
+ // The externalHashes array should also have its leaf hashes at the end.
432
+ const differingLeafIndexes: number[] = [];
433
+ // Calculate where the leaf hashes would start in the externalHashes array,
434
+ // assuming it has the same number of leaves as this tree.
435
+ const externalLeafHashesStart = externalHashes.length - this.numLeaves;
436
+
437
+ if (externalLeafHashesStart < 0) {
438
+ // externalHashes is too short to contain a complete set of leaf hashes
439
+ // corresponding to this tree's numLeaves.
440
+ // In this case, consider all of this tree's leaves as "different".
441
+ for (let i = 0; i < this.numLeaves; i += 1) {
442
+ differingLeafIndexes.push(i);
443
+ }
444
+
445
+ return differingLeafIndexes;
446
+ }
447
+
448
+ // Compare each leaf hash
449
+ for (let i = 0; i < this.numLeaves; i += 1) {
450
+ const ownLeafHash = this.leafHashes[i];
451
+ // externalLeafHash might be undefined if externalHashes is shorter than expected
452
+ // but externalLeafHashesStart was non-negative, implying a structural mismatch.
453
+ const externalLeafHash = externalHashes[externalLeafHashesStart + i];
454
+ if (ownLeafHash !== externalLeafHash) {
455
+ differingLeafIndexes.push(i);
456
+ }
457
+ }
458
+
459
+ return differingLeafIndexes;
460
+ }
461
+ }
462
+
463
+ export default HashTree;