@waku/core 0.0.23 → 0.0.25-a42b7be.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/bundle/{base_protocol-84d9b670.js → base_protocol-4bcf7514.js} +194 -134
  3. package/bundle/{browser-bde977a3.js → browser-90197c87.js} +26 -1
  4. package/bundle/index-27b91e3b.js +31 -0
  5. package/bundle/index.js +19384 -2421
  6. package/bundle/lib/base_protocol.js +3 -2
  7. package/bundle/lib/message/version_0.js +3 -2
  8. package/bundle/lib/predefined_bootstrap_nodes.js +2 -0
  9. package/bundle/{version_0-74b4b9db.js → version_0-2f1176e3.js} +36 -24
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/lib/connection_manager.d.ts +17 -4
  12. package/dist/lib/connection_manager.js +111 -45
  13. package/dist/lib/connection_manager.js.map +1 -1
  14. package/dist/lib/filter/index.js +53 -37
  15. package/dist/lib/filter/index.js.map +1 -1
  16. package/dist/lib/keep_alive_manager.d.ts +1 -0
  17. package/dist/lib/keep_alive_manager.js +42 -18
  18. package/dist/lib/keep_alive_manager.js.map +1 -1
  19. package/dist/lib/light_push/index.js +60 -38
  20. package/dist/lib/light_push/index.js.map +1 -1
  21. package/dist/lib/light_push/push_rpc.d.ts +1 -1
  22. package/dist/lib/light_push/push_rpc.js +2 -2
  23. package/dist/lib/message/version_0.d.ts +13 -13
  24. package/dist/lib/message/version_0.js +22 -20
  25. package/dist/lib/message/version_0.js.map +1 -1
  26. package/dist/lib/store/history_rpc.d.ts +1 -1
  27. package/dist/lib/store/history_rpc.js +1 -1
  28. package/dist/lib/store/index.d.ts +1 -1
  29. package/dist/lib/store/index.js +43 -17
  30. package/dist/lib/store/index.js.map +1 -1
  31. package/dist/lib/stream_manager.d.ts +1 -1
  32. package/dist/lib/stream_manager.js +8 -5
  33. package/dist/lib/stream_manager.js.map +1 -1
  34. package/dist/lib/wait_for_remote_peer.d.ts +2 -2
  35. package/dist/lib/wait_for_remote_peer.js +13 -11
  36. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  37. package/dist/lib/waku.d.ts +4 -3
  38. package/dist/lib/waku.js +13 -11
  39. package/dist/lib/waku.js.map +1 -1
  40. package/package.json +1 -137
  41. package/src/lib/connection_manager.ts +156 -51
  42. package/src/lib/filter/index.ts +76 -40
  43. package/src/lib/keep_alive_manager.ts +53 -20
  44. package/src/lib/light_push/index.ts +74 -38
  45. package/src/lib/light_push/push_rpc.ts +2 -2
  46. package/src/lib/message/version_0.ts +25 -17
  47. package/src/lib/store/history_rpc.ts +2 -2
  48. package/src/lib/store/index.ts +60 -23
  49. package/src/lib/stream_manager.ts +12 -7
  50. package/src/lib/wait_for_remote_peer.ts +13 -11
  51. package/src/lib/waku.ts +12 -9
  52. package/dist/lib/push_or_init_map.d.ts +0 -1
  53. package/dist/lib/push_or_init_map.js +0 -9
  54. package/dist/lib/push_or_init_map.js.map +0 -1
  55. package/src/lib/push_or_init_map.ts +0 -13
@@ -2,19 +2,19 @@ import type { PeerUpdate } from "@libp2p/interface";
2
2
  import type { Stream } from "@libp2p/interface/connection";
3
3
  import { Peer } from "@libp2p/interface/peer-store";
4
4
  import { Libp2p } from "@waku/interfaces";
5
+ import { Logger } from "@waku/utils";
5
6
  import { selectConnection } from "@waku/utils/libp2p";
6
- import debug from "debug";
7
7
 
8
8
  export class StreamManager {
9
- private streamPool: Map<string, Promise<Stream>>;
10
- private log: debug.Debugger;
9
+ private streamPool: Map<string, Promise<Stream | void>>;
10
+ private readonly log: Logger;
11
11
 
12
12
  constructor(
13
13
  public multicodec: string,
14
14
  public getConnections: Libp2p["getConnections"],
15
15
  public addEventListener: Libp2p["addEventListener"]
16
16
  ) {
17
- this.log = debug(`waku:stream-manager:${multicodec}`);
17
+ this.log = new Logger(`stream-manager:${multicodec}`);
18
18
  this.addEventListener(
19
19
  "peer:update",
20
20
  this.handlePeerUpdateStreamPool.bind(this)
@@ -38,7 +38,7 @@ export class StreamManager {
38
38
 
39
39
  const stream = await streamPromise;
40
40
 
41
- if (stream.status === "closed") {
41
+ if (!stream || stream.status === "closed") {
42
42
  return this.newStream(peer); // fallback by creating a new stream on the spot
43
43
  }
44
44
 
@@ -55,14 +55,19 @@ export class StreamManager {
55
55
  }
56
56
 
57
57
  private prepareNewStream(peer: Peer): void {
58
- const streamPromise = this.newStream(peer);
58
+ const streamPromise = this.newStream(peer).catch(() => {
59
+ // No error thrown as this call is not triggered by the user
60
+ this.log.error(
61
+ `Failed to prepare a new stream for ${peer.id.toString()}`
62
+ );
63
+ });
59
64
  this.streamPool.set(peer.id.toString(), streamPromise);
60
65
  }
61
66
 
62
67
  private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
63
68
  const peer = evt.detail.peer;
64
69
  if (peer.protocols.includes(this.multicodec)) {
65
- this.log(`Preemptively opening a stream to ${peer.id.toString()}`);
70
+ this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
66
71
  this.prepareNewStream(peer);
67
72
  }
68
73
  };
@@ -1,16 +1,16 @@
1
1
  import type { IdentifyResult } from "@libp2p/interface";
2
2
  import type { IBaseProtocol, IRelay, Waku } from "@waku/interfaces";
3
3
  import { Protocols } from "@waku/interfaces";
4
- import debug from "debug";
4
+ import { Logger } from "@waku/utils";
5
5
  import { pEvent } from "p-event";
6
6
 
7
- const log = debug("waku:wait-for-remote-peer");
7
+ const log = new Logger("wait-for-remote-peer");
8
8
 
9
9
  /**
10
10
  * Wait for a remote peer to be ready given the passed protocols.
11
11
  * Must be used after attempting to connect to nodes, using
12
- * {@link @waku/core.WakuNode.dial} or a bootstrap method with
13
- * {@link @waku/sdk.createLightNode}.
12
+ * {@link @waku/core!WakuNode.dial} or a bootstrap method with
13
+ * {@link @waku/sdk!createLightNode}.
14
14
  *
15
15
  * If the passed protocols is a GossipSub protocol, then it resolves only once
16
16
  * a peer is in a mesh, to help ensure that other peers will send and receive
@@ -79,14 +79,13 @@ async function waitForConnectedPeer(protocol: IBaseProtocol): Promise<void> {
79
79
  const peers = await protocol.peers();
80
80
 
81
81
  if (peers.length) {
82
- log(`${codec} peer found: `, peers[0].id.toString());
82
+ log.info(`${codec} peer found: `, peers[0].id.toString());
83
83
  return;
84
84
  }
85
85
 
86
86
  await new Promise<void>((resolve) => {
87
87
  const cb = (evt: CustomEvent<IdentifyResult>): void => {
88
88
  if (evt.detail?.protocols?.includes(codec)) {
89
- log("Resolving for", codec, evt.detail.protocols);
90
89
  protocol.removeLibp2pEventListener("peer:identify", cb);
91
90
  resolve();
92
91
  }
@@ -96,15 +95,18 @@ async function waitForConnectedPeer(protocol: IBaseProtocol): Promise<void> {
96
95
  }
97
96
 
98
97
  /**
99
- * Wait for a peer with the given protocol to be connected and in the gossipsub
100
- * mesh.
98
+ * Wait for at least one peer with the given protocol to be connected and in the gossipsub
99
+ * mesh for all pubsubTopics.
101
100
  */
102
101
  async function waitForGossipSubPeerInMesh(waku: IRelay): Promise<void> {
103
102
  let peers = waku.getMeshPeers();
103
+ const pubsubTopics = waku.pubsubTopics;
104
104
 
105
- while (peers.length == 0) {
106
- await pEvent(waku.gossipSub, "gossipsub:heartbeat");
107
- peers = waku.getMeshPeers();
105
+ for (const topic of pubsubTopics) {
106
+ while (peers.length == 0) {
107
+ await pEvent(waku.gossipSub, "gossipsub:heartbeat");
108
+ peers = waku.getMeshPeers(topic);
109
+ }
108
110
  }
109
111
  }
110
112
 
package/src/lib/waku.ts CHANGED
@@ -7,18 +7,19 @@ import type {
7
7
  IRelay,
8
8
  IStore,
9
9
  Libp2p,
10
+ PubSubTopic,
10
11
  Waku
11
12
  } from "@waku/interfaces";
12
13
  import { Protocols } from "@waku/interfaces";
13
- import debug from "debug";
14
+ import { Logger } from "@waku/utils";
14
15
 
15
16
  import { ConnectionManager } from "./connection_manager.js";
16
17
 
17
- export const DefaultPingKeepAliveValueSecs = 0;
18
+ export const DefaultPingKeepAliveValueSecs = 5 * 60;
18
19
  export const DefaultRelayKeepAliveValueSecs = 5 * 60;
19
20
  export const DefaultUserAgent = "js-waku";
20
21
 
21
- const log = debug("waku:waku");
22
+ const log = new Logger("waku");
22
23
 
23
24
  export interface WakuOptions {
24
25
  /**
@@ -52,6 +53,7 @@ export class WakuNode implements Waku {
52
53
 
53
54
  constructor(
54
55
  options: WakuOptions,
56
+ public readonly pubsubTopics: PubSubTopic[],
55
57
  libp2p: Libp2p,
56
58
  store?: (libp2p: Libp2p) => IStore,
57
59
  lightPush?: (libp2p: Libp2p) => ILightPush,
@@ -86,10 +88,11 @@ export class WakuNode implements Waku {
86
88
  peerId,
87
89
  libp2p,
88
90
  { pingKeepAlive, relayKeepAlive },
91
+ pubsubTopics,
89
92
  this.relay
90
93
  );
91
94
 
92
- log(
95
+ log.info(
93
96
  "Waku node created",
94
97
  peerId,
95
98
  `relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
@@ -124,7 +127,7 @@ export class WakuNode implements Waku {
124
127
  codecs.push(codec)
125
128
  );
126
129
  } else {
127
- log(
130
+ log.error(
128
131
  "Relay codec not included in dial codec: protocol not mounted locally"
129
132
  );
130
133
  }
@@ -133,7 +136,7 @@ export class WakuNode implements Waku {
133
136
  if (this.store) {
134
137
  codecs.push(this.store.multicodec);
135
138
  } else {
136
- log(
139
+ log.error(
137
140
  "Store codec not included in dial codec: protocol not mounted locally"
138
141
  );
139
142
  }
@@ -142,7 +145,7 @@ export class WakuNode implements Waku {
142
145
  if (this.lightPush) {
143
146
  codecs.push(this.lightPush.multicodec);
144
147
  } else {
145
- log(
148
+ log.error(
146
149
  "Light Push codec not included in dial codec: protocol not mounted locally"
147
150
  );
148
151
  }
@@ -151,13 +154,13 @@ export class WakuNode implements Waku {
151
154
  if (this.filter) {
152
155
  codecs.push(this.filter.multicodec);
153
156
  } else {
154
- log(
157
+ log.error(
155
158
  "Filter codec not included in dial codec: protocol not mounted locally"
156
159
  );
157
160
  }
158
161
  }
159
162
 
160
- log(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
163
+ log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
161
164
 
162
165
  return this.libp2p.dialProtocol(peerId, codecs);
163
166
  }
@@ -1 +0,0 @@
1
- export declare function pushOrInitMapSet<K, V>(map: Map<K, Set<V>>, key: K, newValue: V): void;
@@ -1,9 +0,0 @@
1
- export function pushOrInitMapSet(map, key, newValue) {
2
- let arr = map.get(key);
3
- if (typeof arr === "undefined") {
4
- map.set(key, new Set());
5
- arr = map.get(key);
6
- }
7
- arr.add(newValue);
8
- }
9
- //# sourceMappingURL=push_or_init_map.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"push_or_init_map.js","sourceRoot":"","sources":["../../src/lib/push_or_init_map.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,gBAAgB,CAC9B,GAAmB,EACnB,GAAM,EACN,QAAW;IAEX,IAAI,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvB,IAAI,OAAO,GAAG,KAAK,WAAW,EAAE;QAC9B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;QACxB,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAW,CAAC;KAC9B;IAED,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AACpB,CAAC"}
@@ -1,13 +0,0 @@
1
- export function pushOrInitMapSet<K, V>(
2
- map: Map<K, Set<V>>,
3
- key: K,
4
- newValue: V
5
- ): void {
6
- let arr = map.get(key);
7
- if (typeof arr === "undefined") {
8
- map.set(key, new Set());
9
- arr = map.get(key) as Set<V>;
10
- }
11
-
12
- arr.add(newValue);
13
- }