@waku/core 0.0.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 (235) hide show
  1. package/CHANGELOG.md +614 -0
  2. package/README.md +56 -0
  3. package/bundle/browser-1e1a2f27.js +722 -0
  4. package/bundle/crypto-8551d579.js +2585 -0
  5. package/bundle/crypto-b00764b7.js +1772 -0
  6. package/bundle/enr-564d4a51.js +20785 -0
  7. package/bundle/enr-9fc5eed8.js +20786 -0
  8. package/bundle/enr-f6e82a53.js +20785 -0
  9. package/bundle/events-158407bb.js +1929 -0
  10. package/bundle/events-fcbda4dc.js +76 -0
  11. package/bundle/index-02d21809.js +20 -0
  12. package/bundle/index-0a4bdddc.js +2976 -0
  13. package/bundle/index-2ae915be.js +1854 -0
  14. package/bundle/index-64ce43f0.js +69 -0
  15. package/bundle/index-691c0be6.js +4059 -0
  16. package/bundle/index-a013a259.js +20 -0
  17. package/bundle/index-ba42b4fc.js +862 -0
  18. package/bundle/index.js +13428 -0
  19. package/bundle/lib/enr.js +8 -0
  20. package/bundle/lib/peer_discovery_dns.js +5018 -0
  21. package/bundle/lib/peer_discovery_static_list.js +75 -0
  22. package/bundle/lib/predefined_bootstrap_nodes.js +59 -0
  23. package/bundle/lib/utils.js +1 -0
  24. package/bundle/lib/wait_for_remote_peer.js +327 -0
  25. package/bundle/lib/waku_message/topic_only_message.js +4 -0
  26. package/bundle/lib/waku_message/version_0.js +4 -0
  27. package/bundle/lib/waku_message/version_1.js +463 -0
  28. package/bundle/message-e2db79d7.js +8393 -0
  29. package/bundle/multiaddr_to_peer_info-c406b1e1.js +19 -0
  30. package/bundle/multiaddr_to_peer_info-fd1de516.js +19 -0
  31. package/bundle/random_subset-75d1c511.js +26 -0
  32. package/bundle/topic_only_message-34f36fa6.js +82 -0
  33. package/bundle/utils-9a3221f2.js +815 -0
  34. package/bundle/version_0-e6fe440c.js +317 -0
  35. package/dist/index.d.ts +16 -0
  36. package/dist/index.js +17 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/lib/constants.d.ts +4 -0
  39. package/dist/lib/constants.js +5 -0
  40. package/dist/lib/constants.js.map +1 -0
  41. package/dist/lib/crypto.d.ts +34 -0
  42. package/dist/lib/crypto.js +79 -0
  43. package/dist/lib/crypto.js.map +1 -0
  44. package/dist/lib/enr/constants.d.ts +4 -0
  45. package/dist/lib/enr/constants.js +8 -0
  46. package/dist/lib/enr/constants.js.map +1 -0
  47. package/dist/lib/enr/enr.d.ts +90 -0
  48. package/dist/lib/enr/enr.js +432 -0
  49. package/dist/lib/enr/enr.js.map +1 -0
  50. package/dist/lib/enr/index.d.ts +5 -0
  51. package/dist/lib/enr/index.js +6 -0
  52. package/dist/lib/enr/index.js.map +1 -0
  53. package/dist/lib/enr/keypair/index.d.ts +8 -0
  54. package/dist/lib/enr/keypair/index.js +53 -0
  55. package/dist/lib/enr/keypair/index.js.map +1 -0
  56. package/dist/lib/enr/keypair/secp256k1.d.ts +13 -0
  57. package/dist/lib/enr/keypair/secp256k1.js +57 -0
  58. package/dist/lib/enr/keypair/secp256k1.js.map +1 -0
  59. package/dist/lib/enr/keypair/types.d.ts +13 -0
  60. package/dist/lib/enr/keypair/types.js +7 -0
  61. package/dist/lib/enr/keypair/types.js.map +1 -0
  62. package/dist/lib/enr/multiaddr_from_fields.d.ts +2 -0
  63. package/dist/lib/enr/multiaddr_from_fields.js +8 -0
  64. package/dist/lib/enr/multiaddr_from_fields.js.map +1 -0
  65. package/dist/lib/enr/multiaddrs_codec.d.ts +3 -0
  66. package/dist/lib/enr/multiaddrs_codec.js +32 -0
  67. package/dist/lib/enr/multiaddrs_codec.js.map +1 -0
  68. package/dist/lib/enr/types.d.ts +8 -0
  69. package/dist/lib/enr/types.js +3 -0
  70. package/dist/lib/enr/types.js.map +1 -0
  71. package/dist/lib/enr/v4.d.ts +3 -0
  72. package/dist/lib/enr/v4.js +14 -0
  73. package/dist/lib/enr/v4.js.map +1 -0
  74. package/dist/lib/enr/waku2_codec.d.ts +8 -0
  75. package/dist/lib/enr/waku2_codec.js +36 -0
  76. package/dist/lib/enr/waku2_codec.js.map +1 -0
  77. package/dist/lib/group_by.d.ts +3 -0
  78. package/dist/lib/group_by.js +13 -0
  79. package/dist/lib/group_by.js.map +1 -0
  80. package/dist/lib/multiaddr_to_peer_info.d.ts +3 -0
  81. package/dist/lib/multiaddr_to_peer_info.js +15 -0
  82. package/dist/lib/multiaddr_to_peer_info.js.map +1 -0
  83. package/dist/lib/peer_discovery_dns/dns.d.ts +48 -0
  84. package/dist/lib/peer_discovery_dns/dns.js +158 -0
  85. package/dist/lib/peer_discovery_dns/dns.js.map +1 -0
  86. package/dist/lib/peer_discovery_dns/dns_over_https.d.ts +32 -0
  87. package/dist/lib/peer_discovery_dns/dns_over_https.js +87 -0
  88. package/dist/lib/peer_discovery_dns/dns_over_https.js.map +1 -0
  89. package/dist/lib/peer_discovery_dns/enrtree.d.ts +33 -0
  90. package/dist/lib/peer_discovery_dns/enrtree.js +76 -0
  91. package/dist/lib/peer_discovery_dns/enrtree.js.map +1 -0
  92. package/dist/lib/peer_discovery_dns/fetch_nodes.d.ts +14 -0
  93. package/dist/lib/peer_discovery_dns/fetch_nodes.js +133 -0
  94. package/dist/lib/peer_discovery_dns/fetch_nodes.js.map +1 -0
  95. package/dist/lib/peer_discovery_dns/index.d.ts +30 -0
  96. package/dist/lib/peer_discovery_dns/index.js +54 -0
  97. package/dist/lib/peer_discovery_dns/index.js.map +1 -0
  98. package/dist/lib/peer_discovery_static_list.d.ts +44 -0
  99. package/dist/lib/peer_discovery_static_list.js +72 -0
  100. package/dist/lib/peer_discovery_static_list.js.map +1 -0
  101. package/dist/lib/predefined_bootstrap_nodes.d.ts +35 -0
  102. package/dist/lib/predefined_bootstrap_nodes.js +56 -0
  103. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -0
  104. package/dist/lib/push_or_init_map.d.ts +1 -0
  105. package/dist/lib/push_or_init_map.js +9 -0
  106. package/dist/lib/push_or_init_map.js.map +1 -0
  107. package/dist/lib/random_subset.d.ts +4 -0
  108. package/dist/lib/random_subset.js +25 -0
  109. package/dist/lib/random_subset.js.map +1 -0
  110. package/dist/lib/select_connection.d.ts +2 -0
  111. package/dist/lib/select_connection.js +19 -0
  112. package/dist/lib/select_connection.js.map +1 -0
  113. package/dist/lib/select_peer.d.ts +15 -0
  114. package/dist/lib/select_peer.js +59 -0
  115. package/dist/lib/select_peer.js.map +1 -0
  116. package/dist/lib/to_proto_message.d.ts +3 -0
  117. package/dist/lib/to_proto_message.js +11 -0
  118. package/dist/lib/to_proto_message.js.map +1 -0
  119. package/dist/lib/utils.d.ts +22 -0
  120. package/dist/lib/utils.js +40 -0
  121. package/dist/lib/utils.js.map +1 -0
  122. package/dist/lib/wait_for_remote_peer.d.ts +22 -0
  123. package/dist/lib/wait_for_remote_peer.js +113 -0
  124. package/dist/lib/wait_for_remote_peer.js.map +1 -0
  125. package/dist/lib/waku.d.ts +61 -0
  126. package/dist/lib/waku.js +174 -0
  127. package/dist/lib/waku.js.map +1 -0
  128. package/dist/lib/waku_filter/filter_rpc.d.ts +25 -0
  129. package/dist/lib/waku_filter/filter_rpc.js +44 -0
  130. package/dist/lib/waku_filter/filter_rpc.js.map +1 -0
  131. package/dist/lib/waku_filter/index.d.ts +50 -0
  132. package/dist/lib/waku_filter/index.js +181 -0
  133. package/dist/lib/waku_filter/index.js.map +1 -0
  134. package/dist/lib/waku_light_push/index.d.ts +38 -0
  135. package/dist/lib/waku_light_push/index.js +83 -0
  136. package/dist/lib/waku_light_push/index.js.map +1 -0
  137. package/dist/lib/waku_light_push/push_rpc.d.ts +11 -0
  138. package/dist/lib/waku_light_push/push_rpc.js +31 -0
  139. package/dist/lib/waku_light_push/push_rpc.js.map +1 -0
  140. package/dist/lib/waku_message/constants.d.ts +12 -0
  141. package/dist/lib/waku_message/constants.js +10 -0
  142. package/dist/lib/waku_message/constants.js.map +1 -0
  143. package/dist/lib/waku_message/ecies.d.ts +17 -0
  144. package/dist/lib/waku_message/ecies.js +126 -0
  145. package/dist/lib/waku_message/ecies.js.map +1 -0
  146. package/dist/lib/waku_message/symmetric.d.ts +3 -0
  147. package/dist/lib/waku_message/symmetric.js +18 -0
  148. package/dist/lib/waku_message/symmetric.js.map +1 -0
  149. package/dist/lib/waku_message/topic_only_message.d.ts +15 -0
  150. package/dist/lib/waku_message/topic_only_message.js +31 -0
  151. package/dist/lib/waku_message/topic_only_message.js.map +1 -0
  152. package/dist/lib/waku_message/version_0.d.ts +26 -0
  153. package/dist/lib/waku_message/version_0.js +96 -0
  154. package/dist/lib/waku_message/version_0.js.map +1 -0
  155. package/dist/lib/waku_message/version_1.d.ts +93 -0
  156. package/dist/lib/waku_message/version_1.js +325 -0
  157. package/dist/lib/waku_message/version_1.js.map +1 -0
  158. package/dist/lib/waku_relay/constants.d.ts +63 -0
  159. package/dist/lib/waku_relay/constants.js +67 -0
  160. package/dist/lib/waku_relay/constants.js.map +1 -0
  161. package/dist/lib/waku_relay/index.d.ts +65 -0
  162. package/dist/lib/waku_relay/index.js +111 -0
  163. package/dist/lib/waku_relay/index.js.map +1 -0
  164. package/dist/lib/waku_store/history_rpc.d.ts +27 -0
  165. package/dist/lib/waku_store/history_rpc.js +71 -0
  166. package/dist/lib/waku_store/history_rpc.js.map +1 -0
  167. package/dist/lib/waku_store/index.d.ts +126 -0
  168. package/dist/lib/waku_store/index.js +218 -0
  169. package/dist/lib/waku_store/index.js.map +1 -0
  170. package/dist/proto/filter.d.ts +65 -0
  171. package/dist/proto/filter.js +425 -0
  172. package/dist/proto/filter.js.map +1 -0
  173. package/dist/proto/light_push.d.ts +57 -0
  174. package/dist/proto/light_push.js +369 -0
  175. package/dist/proto/light_push.js.map +1 -0
  176. package/dist/proto/message.d.ts +29 -0
  177. package/dist/proto/message.js +215 -0
  178. package/dist/proto/message.js.map +1 -0
  179. package/dist/proto/store.d.ts +104 -0
  180. package/dist/proto/store.js +602 -0
  181. package/dist/proto/store.js.map +1 -0
  182. package/dist/proto/topic_only_message.d.ts +10 -0
  183. package/dist/proto/topic_only_message.js +46 -0
  184. package/dist/proto/topic_only_message.js.map +1 -0
  185. package/package.json +292 -0
  186. package/src/index.ts +33 -0
  187. package/src/lib/constants.ts +4 -0
  188. package/src/lib/crypto.ts +100 -0
  189. package/src/lib/enr/constants.ts +10 -0
  190. package/src/lib/enr/enr.ts +516 -0
  191. package/src/lib/enr/index.ts +5 -0
  192. package/src/lib/enr/keypair/index.ts +76 -0
  193. package/src/lib/enr/keypair/secp256k1.ts +69 -0
  194. package/src/lib/enr/keypair/types.ts +14 -0
  195. package/src/lib/enr/multiaddr_from_fields.ts +18 -0
  196. package/src/lib/enr/multiaddrs_codec.ts +50 -0
  197. package/src/lib/enr/types.ts +11 -0
  198. package/src/lib/enr/v4.ts +22 -0
  199. package/src/lib/enr/waku2_codec.ts +39 -0
  200. package/src/lib/group_by.ts +14 -0
  201. package/src/lib/multiaddr_to_peer_info.ts +17 -0
  202. package/src/lib/peer_discovery_dns/dns.ts +223 -0
  203. package/src/lib/peer_discovery_dns/dns_over_https.ts +98 -0
  204. package/src/lib/peer_discovery_dns/enrtree.ts +123 -0
  205. package/src/lib/peer_discovery_dns/fetch_nodes.ts +180 -0
  206. package/src/lib/peer_discovery_dns/index.ts +84 -0
  207. package/src/lib/peer_discovery_static_list.ts +118 -0
  208. package/src/lib/predefined_bootstrap_nodes.ts +72 -0
  209. package/src/lib/push_or_init_map.ts +13 -0
  210. package/src/lib/random_subset.ts +30 -0
  211. package/src/lib/select_connection.ts +24 -0
  212. package/src/lib/select_peer.ts +77 -0
  213. package/src/lib/to_proto_message.ts +15 -0
  214. package/src/lib/utils.ts +50 -0
  215. package/src/lib/wait_for_remote_peer.ts +151 -0
  216. package/src/lib/waku.ts +258 -0
  217. package/src/lib/waku_filter/filter_rpc.ts +57 -0
  218. package/src/lib/waku_filter/index.ts +291 -0
  219. package/src/lib/waku_light_push/index.ts +137 -0
  220. package/src/lib/waku_light_push/push_rpc.ts +39 -0
  221. package/src/lib/waku_message/constants.ts +10 -0
  222. package/src/lib/waku_message/ecies.ts +194 -0
  223. package/src/lib/waku_message/symmetric.ts +33 -0
  224. package/src/lib/waku_message/topic_only_message.ts +40 -0
  225. package/src/lib/waku_message/version_0.ts +121 -0
  226. package/src/lib/waku_message/version_1.ts +457 -0
  227. package/src/lib/waku_relay/constants.ts +77 -0
  228. package/src/lib/waku_relay/index.ts +189 -0
  229. package/src/lib/waku_store/history_rpc.ts +94 -0
  230. package/src/lib/waku_store/index.ts +372 -0
  231. package/src/proto/filter.ts +602 -0
  232. package/src/proto/light_push.ts +526 -0
  233. package/src/proto/message.ts +304 -0
  234. package/src/proto/store.ts +844 -0
  235. package/src/proto/topic_only_message.ts +67 -0
@@ -0,0 +1,75 @@
1
+ import { m as multiaddrsToPeerInfo, s as symbol } from '../multiaddr_to_peer_info-fd1de516.js';
2
+ import { E as EventEmitter, C as CustomEvent } from '../events-fcbda4dc.js';
3
+ import { m as multiaddr } from '../index-0a4bdddc.js';
4
+ import { d as debug } from '../browser-1e1a2f27.js';
5
+ import { g as getPseudoRandomSubset } from '../random_subset-75d1c511.js';
6
+ import '../index-2ae915be.js';
7
+ import '../index-64ce43f0.js';
8
+
9
+ const log = debug("waku:peer-discovery-static-list");
10
+ /**
11
+ * Pass a list of multiaddr that will be used to bootstrap a node.
12
+ */
13
+ class PeerDiscoveryStaticPeers extends EventEmitter {
14
+ /**
15
+ * @param peers Multiaddrs of peers to connect to.
16
+ * @param opts
17
+ */
18
+ constructor(peers, opts) {
19
+ super();
20
+ this.interval = opts?.interval ?? PeerDiscoveryStaticPeers.DefaultInterval;
21
+ const maxPeers = opts?.maxPeers ?? peers?.length;
22
+ const peerMas = peers.map((peer) => {
23
+ if (typeof peer === "string") {
24
+ return multiaddr(peer);
25
+ }
26
+ else {
27
+ return peer;
28
+ }
29
+ });
30
+ this.peers = multiaddrsToPeerInfo(getPseudoRandomSubset(peerMas, maxPeers));
31
+ log("Use provided list of peers (reduced to maxPeers)", this.peers.map((ma) => ma.toString()));
32
+ }
33
+ /**
34
+ * Start emitting static peers.
35
+ */
36
+ start() {
37
+ this._startTimer();
38
+ }
39
+ _startTimer() {
40
+ if (this.peers) {
41
+ log("Starting to emit static peers.");
42
+ if (this.timer != null) {
43
+ return;
44
+ }
45
+ this.timer = setInterval(() => this._returnPeers(), this.interval);
46
+ this._returnPeers();
47
+ }
48
+ }
49
+ _returnPeers() {
50
+ if (this.timer == null) {
51
+ return;
52
+ }
53
+ this.peers.forEach((peerData) => {
54
+ this.dispatchEvent(new CustomEvent("peer", { detail: peerData }));
55
+ });
56
+ }
57
+ /**
58
+ * Stop emitting peers.
59
+ */
60
+ stop() {
61
+ if (this.timer != null) {
62
+ clearInterval(this.timer);
63
+ }
64
+ this.timer = undefined;
65
+ }
66
+ get [symbol]() {
67
+ return true;
68
+ }
69
+ get [Symbol.toStringTag]() {
70
+ return "@waku/peer-discovery-static-list";
71
+ }
72
+ }
73
+ PeerDiscoveryStaticPeers.DefaultInterval = 200;
74
+
75
+ export { PeerDiscoveryStaticPeers };
@@ -0,0 +1,59 @@
1
+ import { m as multiaddr } from '../index-0a4bdddc.js';
2
+ import { g as getPseudoRandomSubset } from '../random_subset-75d1c511.js';
3
+ import '../index-64ce43f0.js';
4
+
5
+ const DefaultWantedNumber = 1;
6
+ var Fleet;
7
+ (function (Fleet) {
8
+ Fleet["Prod"] = "prod";
9
+ Fleet["Test"] = "test";
10
+ })(Fleet || (Fleet = {}));
11
+ /**
12
+ * Return list of pre-defined (hardcoded) bootstrap nodes.
13
+ *
14
+ * Default behavior is to return nodes of the nwaku Status Prod fleet.
15
+ *
16
+ * @param fleet The fleet to be returned. Defaults to production fleet.
17
+ * @param wantedNumber The number of connections desired. Defaults to {@link DefaultWantedNumber}.
18
+ *
19
+ * @returns An array of multiaddresses.
20
+ */
21
+ function getPredefinedBootstrapNodes(fleet = Fleet.Prod, wantedNumber = DefaultWantedNumber) {
22
+ if (wantedNumber <= 0) {
23
+ return [];
24
+ }
25
+ let nodes;
26
+ switch (fleet) {
27
+ case Fleet.Prod:
28
+ nodes = fleets.fleets["wakuv2.prod"]["waku-websocket"];
29
+ break;
30
+ case Fleet.Test:
31
+ nodes = fleets.fleets["wakuv2.test"]["waku-websocket"];
32
+ break;
33
+ default:
34
+ nodes = fleets.fleets["wakuv2.prod"]["waku-websocket"];
35
+ }
36
+ nodes = Object.values(nodes);
37
+ nodes = nodes.map((node) => multiaddr(node));
38
+ return getPseudoRandomSubset(nodes, wantedNumber);
39
+ }
40
+ const fleets = {
41
+ fleets: {
42
+ "wakuv2.prod": {
43
+ "waku-websocket": {
44
+ "node-01.ac-cn-hongkong-c.wakuv2.prod": "/dns4/node-01.ac-cn-hongkong-c.wakuv2.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD",
45
+ "node-01.do-ams3.wakuv2.prod": "/dns4/node-01.do-ams3.wakuv2.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAmL5okWopX7NqZWBUKVqW8iUxCEmd5GMHLVPwCgzYzQv3e",
46
+ "node-01.gc-us-central1-a.wakuv2.prod": "/dns4/node-01.gc-us-central1-a.wakuv2.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAmVkKntsECaYfefR1V2yCR79CegLATuTPE6B9TxgxBiiiA",
47
+ },
48
+ },
49
+ "wakuv2.test": {
50
+ "waku-websocket": {
51
+ "node-01.ac-cn-hongkong-c.wakuv2.test": "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm",
52
+ "node-01.do-ams3.wakuv2.test": "/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ",
53
+ "node-01.gc-us-central1-a.wakuv2.test": "/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS",
54
+ },
55
+ },
56
+ },
57
+ };
58
+
59
+ export { DefaultWantedNumber, Fleet, fleets, getPredefinedBootstrapNodes };
@@ -0,0 +1 @@
1
+ export { d as bytesToHex, g as bytesToUtf8, c as concat, h as hexToBytes, e as utf8ToBytes } from '../utils-9a3221f2.js';
@@ -0,0 +1,327 @@
1
+ import { P as Protocols, S as StoreCodec, L as LightPushCodec, F as FilterCodec } from '../index-691c0be6.js';
2
+ import { d as debug } from '../browser-1e1a2f27.js';
3
+ import '../message-e2db79d7.js';
4
+ import '../index-ba42b4fc.js';
5
+ import '../index-64ce43f0.js';
6
+
7
+ class TimeoutError extends Error {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = 'TimeoutError';
11
+ }
12
+ }
13
+
14
+ /**
15
+ An error to be thrown when the request is aborted by AbortController.
16
+ DOMException is thrown instead of this Error when DOMException is available.
17
+ */
18
+ class AbortError extends Error {
19
+ constructor(message) {
20
+ super();
21
+ this.name = 'AbortError';
22
+ this.message = message;
23
+ }
24
+ }
25
+
26
+ /**
27
+ TODO: Remove AbortError and just throw DOMException when targeting Node 18.
28
+ */
29
+ const getDOMException = errorMessage => globalThis.DOMException === undefined ?
30
+ new AbortError(errorMessage) :
31
+ new DOMException(errorMessage);
32
+
33
+ /**
34
+ TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18.
35
+ */
36
+ const getAbortedReason = signal => {
37
+ const reason = signal.reason === undefined ?
38
+ getDOMException('This operation was aborted.') :
39
+ signal.reason;
40
+
41
+ return reason instanceof Error ? reason : getDOMException(reason);
42
+ };
43
+
44
+ function pTimeout(promise, milliseconds, fallback, options) {
45
+ let timer;
46
+
47
+ const cancelablePromise = new Promise((resolve, reject) => {
48
+ if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
49
+ throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
50
+ }
51
+
52
+ if (milliseconds === Number.POSITIVE_INFINITY) {
53
+ resolve(promise);
54
+ return;
55
+ }
56
+
57
+ options = {
58
+ customTimers: {setTimeout, clearTimeout},
59
+ ...options
60
+ };
61
+
62
+ if (options.signal) {
63
+ const {signal} = options;
64
+ if (signal.aborted) {
65
+ reject(getAbortedReason(signal));
66
+ }
67
+
68
+ signal.addEventListener('abort', () => {
69
+ reject(getAbortedReason(signal));
70
+ });
71
+ }
72
+
73
+ timer = options.customTimers.setTimeout.call(undefined, () => {
74
+ if (typeof fallback === 'function') {
75
+ try {
76
+ resolve(fallback());
77
+ } catch (error) {
78
+ reject(error);
79
+ }
80
+
81
+ return;
82
+ }
83
+
84
+ const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`;
85
+ const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message);
86
+
87
+ if (typeof promise.cancel === 'function') {
88
+ promise.cancel();
89
+ }
90
+
91
+ reject(timeoutError);
92
+ }, milliseconds);
93
+
94
+ (async () => {
95
+ try {
96
+ resolve(await promise);
97
+ } catch (error) {
98
+ reject(error);
99
+ } finally {
100
+ options.customTimers.clearTimeout.call(undefined, timer);
101
+ }
102
+ })();
103
+ });
104
+
105
+ cancelablePromise.clear = () => {
106
+ clearTimeout(timer);
107
+ timer = undefined;
108
+ };
109
+
110
+ return cancelablePromise;
111
+ }
112
+
113
+ const normalizeEmitter = emitter => {
114
+ const addListener = emitter.on || emitter.addListener || emitter.addEventListener;
115
+ const removeListener = emitter.off || emitter.removeListener || emitter.removeEventListener;
116
+
117
+ if (!addListener || !removeListener) {
118
+ throw new TypeError('Emitter is not compatible');
119
+ }
120
+
121
+ return {
122
+ addListener: addListener.bind(emitter),
123
+ removeListener: removeListener.bind(emitter),
124
+ };
125
+ };
126
+
127
+ function pEventMultiple(emitter, event, options) {
128
+ let cancel;
129
+ const returnValue = new Promise((resolve, reject) => {
130
+ options = {
131
+ rejectionEvents: ['error'],
132
+ multiArgs: false,
133
+ resolveImmediately: false,
134
+ ...options,
135
+ };
136
+
137
+ if (!(options.count >= 0 && (options.count === Number.POSITIVE_INFINITY || Number.isInteger(options.count)))) {
138
+ throw new TypeError('The `count` option should be at least 0 or more');
139
+ }
140
+
141
+ // Allow multiple events
142
+ const events = [event].flat();
143
+
144
+ const items = [];
145
+ const {addListener, removeListener} = normalizeEmitter(emitter);
146
+
147
+ const onItem = (...arguments_) => {
148
+ const value = options.multiArgs ? arguments_ : arguments_[0];
149
+
150
+ // eslint-disable-next-line unicorn/no-array-callback-reference
151
+ if (options.filter && !options.filter(value)) {
152
+ return;
153
+ }
154
+
155
+ items.push(value);
156
+
157
+ if (options.count === items.length) {
158
+ cancel();
159
+ resolve(items);
160
+ }
161
+ };
162
+
163
+ const rejectHandler = error => {
164
+ cancel();
165
+ reject(error);
166
+ };
167
+
168
+ cancel = () => {
169
+ for (const event of events) {
170
+ removeListener(event, onItem);
171
+ }
172
+
173
+ for (const rejectionEvent of options.rejectionEvents) {
174
+ removeListener(rejectionEvent, rejectHandler);
175
+ }
176
+ };
177
+
178
+ for (const event of events) {
179
+ addListener(event, onItem);
180
+ }
181
+
182
+ for (const rejectionEvent of options.rejectionEvents) {
183
+ addListener(rejectionEvent, rejectHandler);
184
+ }
185
+
186
+ if (options.resolveImmediately) {
187
+ resolve(items);
188
+ }
189
+ });
190
+
191
+ returnValue.cancel = cancel;
192
+
193
+ if (typeof options.timeout === 'number') {
194
+ const timeout = pTimeout(returnValue, options.timeout);
195
+ timeout.cancel = cancel;
196
+ return timeout;
197
+ }
198
+
199
+ return returnValue;
200
+ }
201
+
202
+ function pEvent(emitter, event, options) {
203
+ if (typeof options === 'function') {
204
+ options = {filter: options};
205
+ }
206
+
207
+ options = {
208
+ ...options,
209
+ count: 1,
210
+ resolveImmediately: false,
211
+ };
212
+
213
+ const arrayPromise = pEventMultiple(emitter, event, options);
214
+ const promise = arrayPromise.then(array => array[0]); // eslint-disable-line promise/prefer-await-to-then
215
+ promise.cancel = arrayPromise.cancel;
216
+
217
+ return promise;
218
+ }
219
+
220
+ const log = debug("waku:wait-for-remote-peer");
221
+ /**
222
+ * Wait for a remote peer to be ready given the passed protocols.
223
+ * Must be used after attempting to connect to nodes, using
224
+ * {@link index.waku.WakuNode.dial} or a bootstrap method with
225
+ * {@link lib/create_waku.createLightNode}.
226
+ *
227
+ * If the passed protocols is a GossipSub protocol, then it resolves only once
228
+ * a peer is in a mesh, to help ensure that other peers will send and receive
229
+ * message to us.
230
+ *
231
+ * @param waku The Waku Node
232
+ * @param protocols The protocols that need to be enabled by remote peers.
233
+ * @param timeoutMs A timeout value in milliseconds..
234
+ *
235
+ * @returns A promise that **resolves** if all desired protocols are fulfilled by
236
+ * remote nodes, **rejects** if the timeoutMs is reached.
237
+ * @throws If passing a protocol that is not mounted
238
+ * @default Wait for remote peers with protocols enabled locally and no time out is applied.
239
+ */
240
+ async function waitForRemotePeer(waku, protocols, timeoutMs) {
241
+ protocols = protocols ?? getEnabledProtocols(waku);
242
+ if (!waku.isStarted())
243
+ return Promise.reject("Waku node is not started");
244
+ const promises = [];
245
+ if (protocols.includes(Protocols.Relay)) {
246
+ if (!waku.relay)
247
+ throw new Error("Cannot wait for Relay peer: protocol not mounted");
248
+ promises.push(waitForGossipSubPeerInMesh(waku.relay));
249
+ }
250
+ if (protocols.includes(Protocols.Store)) {
251
+ if (!waku.store)
252
+ throw new Error("Cannot wait for Store peer: protocol not mounted");
253
+ promises.push(waitForConnectedPeer(waku.store, [StoreCodec]));
254
+ }
255
+ if (protocols.includes(Protocols.LightPush)) {
256
+ if (!waku.lightPush)
257
+ throw new Error("Cannot wait for LightPush peer: protocol not mounted");
258
+ promises.push(waitForConnectedPeer(waku.lightPush, [LightPushCodec]));
259
+ }
260
+ if (protocols.includes(Protocols.Filter)) {
261
+ if (!waku.filter)
262
+ throw new Error("Cannot wait for Filter peer: protocol not mounted");
263
+ promises.push(waitForConnectedPeer(waku.filter, [FilterCodec]));
264
+ }
265
+ if (timeoutMs) {
266
+ await rejectOnTimeout(Promise.all(promises), timeoutMs, "Timed out waiting for a remote peer.");
267
+ }
268
+ else {
269
+ await Promise.all(promises);
270
+ }
271
+ }
272
+ /**
273
+ * Wait for a peer with the given protocol to be connected.
274
+ */
275
+ async function waitForConnectedPeer(waku, codecs) {
276
+ const peers = await waku.peers();
277
+ if (peers.length) {
278
+ log(`${codecs} peer found: `, peers[0].id.toString());
279
+ return;
280
+ }
281
+ await new Promise((resolve) => {
282
+ const cb = (evt) => {
283
+ for (const codec of codecs) {
284
+ if (evt.detail.protocols.includes(codec)) {
285
+ log("Resolving for", codec, evt.detail.protocols);
286
+ waku.libp2p.peerStore.removeEventListener("change:protocols", cb);
287
+ resolve();
288
+ break;
289
+ }
290
+ }
291
+ };
292
+ waku.libp2p.peerStore.addEventListener("change:protocols", cb);
293
+ });
294
+ }
295
+ /**
296
+ * Wait for a peer with the given protocol to be connected and in the gossipsub
297
+ * mesh.
298
+ */
299
+ async function waitForGossipSubPeerInMesh(waku) {
300
+ let peers = waku.getMeshPeers();
301
+ while (peers.length == 0) {
302
+ await pEvent(waku, "gossipsub:heartbeat");
303
+ peers = waku.getMeshPeers();
304
+ }
305
+ }
306
+ const awaitTimeout = (ms, rejectReason) => new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
307
+ async function rejectOnTimeout(promise, timeoutMs, rejectReason) {
308
+ await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
309
+ }
310
+ function getEnabledProtocols(waku) {
311
+ const protocols = [];
312
+ if (waku.relay) {
313
+ protocols.push(Protocols.Relay);
314
+ }
315
+ if (waku.filter) {
316
+ protocols.push(Protocols.Filter);
317
+ }
318
+ if (waku.store) {
319
+ protocols.push(Protocols.Store);
320
+ }
321
+ if (waku.lightPush) {
322
+ protocols.push(Protocols.LightPush);
323
+ }
324
+ return protocols;
325
+ }
326
+
327
+ export { waitForRemotePeer };
@@ -0,0 +1,4 @@
1
+ import '../../browser-1e1a2f27.js';
2
+ export { T as TopicOnlyDecoder, a as TopicOnlyMessage } from '../../topic_only_message-34f36fa6.js';
3
+ import '../../message-e2db79d7.js';
4
+ import '../../index-ba42b4fc.js';
@@ -0,0 +1,4 @@
1
+ import '../../browser-1e1a2f27.js';
2
+ export { D as DecoderV0, E as EncoderV0, M as MessageV0, V as Version } from '../../version_0-e6fe440c.js';
3
+ import '../../message-e2db79d7.js';
4
+ import '../../index-ba42b4fc.js';