@waku/core 0.0.24 → 0.0.25

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 (38) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/bundle/{base_protocol-2a0c882e.js → base_protocol-4bcf7514.js} +19 -11
  3. package/bundle/index-27b91e3b.js +31 -0
  4. package/bundle/index.js +199 -167
  5. package/bundle/lib/base_protocol.js +2 -1
  6. package/bundle/lib/message/version_0.js +2 -1
  7. package/bundle/lib/predefined_bootstrap_nodes.js +2 -0
  8. package/bundle/{version_0-f4afd324.js → version_0-2f1176e3.js} +4 -4
  9. package/dist/.tsbuildinfo +1 -1
  10. package/dist/lib/connection_manager.d.ts +2 -1
  11. package/dist/lib/connection_manager.js +35 -27
  12. package/dist/lib/connection_manager.js.map +1 -1
  13. package/dist/lib/filter/index.js +28 -17
  14. package/dist/lib/filter/index.js.map +1 -1
  15. package/dist/lib/keep_alive_manager.js +8 -8
  16. package/dist/lib/keep_alive_manager.js.map +1 -1
  17. package/dist/lib/light_push/index.js +16 -12
  18. package/dist/lib/light_push/index.js.map +1 -1
  19. package/dist/lib/message/version_0.js +3 -4
  20. package/dist/lib/message/version_0.js.map +1 -1
  21. package/dist/lib/store/index.js +7 -8
  22. package/dist/lib/store/index.js.map +1 -1
  23. package/dist/lib/stream_manager.js +4 -4
  24. package/dist/lib/stream_manager.js.map +1 -1
  25. package/dist/lib/wait_for_remote_peer.js +3 -4
  26. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  27. package/dist/lib/waku.js +8 -8
  28. package/dist/lib/waku.js.map +1 -1
  29. package/package.json +6 -6
  30. package/src/lib/connection_manager.ts +46 -28
  31. package/src/lib/filter/index.ts +40 -17
  32. package/src/lib/keep_alive_manager.ts +8 -8
  33. package/src/lib/light_push/index.ts +23 -12
  34. package/src/lib/message/version_0.ts +3 -4
  35. package/src/lib/store/index.ts +7 -9
  36. package/src/lib/stream_manager.ts +7 -5
  37. package/src/lib/wait_for_remote_peer.ts +3 -4
  38. package/src/lib/waku.ts +8 -8
@@ -11,8 +11,11 @@ import {
11
11
  SendResult
12
12
  } from "@waku/interfaces";
13
13
  import { PushResponse } from "@waku/proto";
14
- import { ensurePubsubTopicIsConfigured, isSizeValid } from "@waku/utils";
15
- import debug from "debug";
14
+ import {
15
+ ensurePubsubTopicIsConfigured,
16
+ isMessageSizeUnderCap
17
+ } from "@waku/utils";
18
+ import { Logger } from "@waku/utils";
16
19
  import all from "it-all";
17
20
  import * as lp from "it-length-prefixed";
18
21
  import { pipe } from "it-pipe";
@@ -23,7 +26,7 @@ import { DefaultPubSubTopic } from "../constants.js";
23
26
 
24
27
  import { PushRpc } from "./push_rpc.js";
25
28
 
26
- const log = debug("waku:light-push");
29
+ const log = new Logger("light-push");
27
30
 
28
31
  export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
29
32
  export { PushResponse };
@@ -56,14 +59,19 @@ class LightPush extends BaseProtocol implements ILightPush {
56
59
  pubsubTopic: string
57
60
  ): Promise<PreparePushMessageResult> {
58
61
  try {
59
- if (!isSizeValid(message.payload)) {
60
- log("Failed to send waku light push: message is bigger than 1MB");
62
+ if (!message.payload || message.payload.length === 0) {
63
+ log.error("Failed to send waku light push: payload is empty");
64
+ return { query: null, error: SendError.EMPTY_PAYLOAD };
65
+ }
66
+
67
+ if (!(await isMessageSizeUnderCap(encoder, message))) {
68
+ log.error("Failed to send waku light push: message is bigger than 1MB");
61
69
  return { query: null, error: SendError.SIZE_TOO_BIG };
62
70
  }
63
71
 
64
72
  const protoMessage = await encoder.toProtoObj(message);
65
73
  if (!protoMessage) {
66
- log("Failed to encode to protoMessage, aborting push");
74
+ log.error("Failed to encode to protoMessage, aborting push");
67
75
  return {
68
76
  query: null,
69
77
  error: SendError.ENCODE_FAILED
@@ -73,7 +81,7 @@ class LightPush extends BaseProtocol implements ILightPush {
73
81
  const query = PushRpc.createRequest(protoMessage, pubsubTopic);
74
82
  return { query, error: null };
75
83
  } catch (error) {
76
- log("Failed to prepare push message", error);
84
+ log.error("Failed to prepare push message", error);
77
85
 
78
86
  return {
79
87
  query: null,
@@ -119,7 +127,10 @@ class LightPush extends BaseProtocol implements ILightPush {
119
127
  try {
120
128
  stream = await this.getStream(peer);
121
129
  } catch (err) {
122
- log(`Failed to get a stream for remote peer${peer.id.toString()}`, err);
130
+ log.error(
131
+ `Failed to get a stream for remote peer${peer.id.toString()}`,
132
+ err
133
+ );
123
134
  return { recipients, error: SendError.REMOTE_PEER_FAULT };
124
135
  }
125
136
 
@@ -133,7 +144,7 @@ class LightPush extends BaseProtocol implements ILightPush {
133
144
  async (source) => await all(source)
134
145
  );
135
146
  } catch (err) {
136
- log("Failed to send waku light push request", err);
147
+ log.error("Failed to send waku light push request", err);
137
148
  return { recipients, error: SendError.GENERIC_FAIL };
138
149
  }
139
150
 
@@ -146,17 +157,17 @@ class LightPush extends BaseProtocol implements ILightPush {
146
157
  try {
147
158
  response = PushRpc.decode(bytes).response;
148
159
  } catch (err) {
149
- log("Failed to decode push reply", err);
160
+ log.error("Failed to decode push reply", err);
150
161
  return { recipients, error: SendError.DECODE_FAILED };
151
162
  }
152
163
 
153
164
  if (!response) {
154
- log("Remote peer fault: No response in PushRPC");
165
+ log.error("Remote peer fault: No response in PushRPC");
155
166
  return { recipients, error: SendError.REMOTE_PEER_FAULT };
156
167
  }
157
168
 
158
169
  if (!response.isSuccess) {
159
- log("Remote peer rejected the message: ", response.info);
170
+ log.error("Remote peer rejected the message: ", response.info);
160
171
  return { recipients, error: SendError.REMOTE_PEER_REJECTED };
161
172
  }
162
173
 
@@ -10,11 +10,11 @@ import type {
10
10
  PubSubTopic
11
11
  } from "@waku/interfaces";
12
12
  import { proto_message as proto } from "@waku/proto";
13
- import debug from "debug";
13
+ import { Logger } from "@waku/utils";
14
14
 
15
15
  import { DefaultPubSubTopic } from "../constants.js";
16
16
 
17
- const log = debug("waku:message:version-0");
17
+ const log = new Logger("message:version-0");
18
18
  const OneMillion = BigInt(1_000_000);
19
19
 
20
20
  export const Version = 0;
@@ -139,7 +139,6 @@ export class Decoder implements IDecoder<DecodedMessage> {
139
139
 
140
140
  fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
141
141
  const protoMessage = proto.WakuMessage.decode(bytes);
142
- log("Message decoded", protoMessage);
143
142
  return Promise.resolve({
144
143
  payload: protoMessage.payload,
145
144
  contentTopic: protoMessage.contentTopic,
@@ -158,7 +157,7 @@ export class Decoder implements IDecoder<DecodedMessage> {
158
157
  // https://rfc.vac.dev/spec/14/
159
158
  // > If omitted, the value SHOULD be interpreted as version 0.
160
159
  if (proto.version ?? 0 !== Version) {
161
- log(
160
+ log.error(
162
161
  "Failed to decode due to incorrect version, expected:",
163
162
  Version,
164
163
  ", actual:",
@@ -11,8 +11,8 @@ import {
11
11
  } from "@waku/interfaces";
12
12
  import { proto_store as proto } from "@waku/proto";
13
13
  import { ensurePubsubTopicIsConfigured, isDefined } from "@waku/utils";
14
+ import { Logger } from "@waku/utils";
14
15
  import { concat, utf8ToBytes } from "@waku/utils/bytes";
15
- import debug from "debug";
16
16
  import all from "it-all";
17
17
  import * as lp from "it-length-prefixed";
18
18
  import { pipe } from "it-pipe";
@@ -26,7 +26,7 @@ import { HistoryRpc, PageDirection, Params } from "./history_rpc.js";
26
26
 
27
27
  import HistoryError = proto.HistoryResponse.HistoryError;
28
28
 
29
- const log = debug("waku:store");
29
+ const log = new Logger("store");
30
30
 
31
31
  export const StoreCodec = "/vac/waku/store/2.0.0-beta4";
32
32
 
@@ -284,8 +284,6 @@ class Store extends BaseProtocol implements IStore {
284
284
  { contentTopics, startTime, endTime }
285
285
  );
286
286
 
287
- log("Querying history with the following options", options);
288
-
289
287
  const peer = (
290
288
  await this.getPeers({
291
289
  numPeers: this.NUM_PEERS_PROTOCOL,
@@ -325,7 +323,7 @@ async function* paginate<T extends IDecodedMessage>(
325
323
 
326
324
  const historyRpcQuery = HistoryRpc.createQuery(queryOpts);
327
325
 
328
- log(
326
+ log.info(
329
327
  "Querying store peer",
330
328
  `for (${queryOpts.pubsubTopic})`,
331
329
  queryOpts.contentTopics
@@ -349,7 +347,7 @@ async function* paginate<T extends IDecodedMessage>(
349
347
  const reply = historyRpcQuery.decode(bytes);
350
348
 
351
349
  if (!reply.response) {
352
- log("Stopping pagination due to store `response` field missing");
350
+ log.warn("Stopping pagination due to store `response` field missing");
353
351
  break;
354
352
  }
355
353
 
@@ -360,13 +358,13 @@ async function* paginate<T extends IDecodedMessage>(
360
358
  }
361
359
 
362
360
  if (!response.messages || !response.messages.length) {
363
- log(
361
+ log.warn(
364
362
  "Stopping pagination due to store `response.messages` field missing or empty"
365
363
  );
366
364
  break;
367
365
  }
368
366
 
369
- log(`${response.messages.length} messages retrieved from store`);
367
+ log.error(`${response.messages.length} messages retrieved from store`);
370
368
 
371
369
  yield response.messages.map((protoMsg) => {
372
370
  const contentTopic = protoMsg.contentTopic;
@@ -386,7 +384,7 @@ async function* paginate<T extends IDecodedMessage>(
386
384
  if (typeof nextCursor === "undefined") {
387
385
  // If the server does not return cursor then there is an issue,
388
386
  // Need to abort, or we end up in an infinite loop
389
- log(
387
+ log.warn(
390
388
  "Stopping pagination due to `response.pagingInfo.cursor` missing from store response"
391
389
  );
392
390
  break;
@@ -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
9
  private streamPool: Map<string, Promise<Stream | void>>;
10
- private readonly log: debug.Debugger;
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)
@@ -57,7 +57,9 @@ export class StreamManager {
57
57
  private prepareNewStream(peer: Peer): void {
58
58
  const streamPromise = this.newStream(peer).catch(() => {
59
59
  // No error thrown as this call is not triggered by the user
60
- this.log(`Failed to prepare a new stream for ${peer.id.toString()}`);
60
+ this.log.error(
61
+ `Failed to prepare a new stream for ${peer.id.toString()}`
62
+ );
61
63
  });
62
64
  this.streamPool.set(peer.id.toString(), streamPromise);
63
65
  }
@@ -65,7 +67,7 @@ export class StreamManager {
65
67
  private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
66
68
  const peer = evt.detail.peer;
67
69
  if (peer.protocols.includes(this.multicodec)) {
68
- this.log(`Preemptively opening a stream to ${peer.id.toString()}`);
70
+ this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
69
71
  this.prepareNewStream(peer);
70
72
  }
71
73
  };
@@ -1,10 +1,10 @@
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.
@@ -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
  }
package/src/lib/waku.ts CHANGED
@@ -11,7 +11,7 @@ import type {
11
11
  Waku
12
12
  } from "@waku/interfaces";
13
13
  import { Protocols } from "@waku/interfaces";
14
- import debug from "debug";
14
+ import { Logger } from "@waku/utils";
15
15
 
16
16
  import { ConnectionManager } from "./connection_manager.js";
17
17
 
@@ -19,7 +19,7 @@ export const DefaultPingKeepAliveValueSecs = 5 * 60;
19
19
  export const DefaultRelayKeepAliveValueSecs = 5 * 60;
20
20
  export const DefaultUserAgent = "js-waku";
21
21
 
22
- const log = debug("waku:waku");
22
+ const log = new Logger("waku");
23
23
 
24
24
  export interface WakuOptions {
25
25
  /**
@@ -92,7 +92,7 @@ export class WakuNode implements Waku {
92
92
  this.relay
93
93
  );
94
94
 
95
- log(
95
+ log.info(
96
96
  "Waku node created",
97
97
  peerId,
98
98
  `relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
@@ -127,7 +127,7 @@ export class WakuNode implements Waku {
127
127
  codecs.push(codec)
128
128
  );
129
129
  } else {
130
- log(
130
+ log.error(
131
131
  "Relay codec not included in dial codec: protocol not mounted locally"
132
132
  );
133
133
  }
@@ -136,7 +136,7 @@ export class WakuNode implements Waku {
136
136
  if (this.store) {
137
137
  codecs.push(this.store.multicodec);
138
138
  } else {
139
- log(
139
+ log.error(
140
140
  "Store codec not included in dial codec: protocol not mounted locally"
141
141
  );
142
142
  }
@@ -145,7 +145,7 @@ export class WakuNode implements Waku {
145
145
  if (this.lightPush) {
146
146
  codecs.push(this.lightPush.multicodec);
147
147
  } else {
148
- log(
148
+ log.error(
149
149
  "Light Push codec not included in dial codec: protocol not mounted locally"
150
150
  );
151
151
  }
@@ -154,13 +154,13 @@ export class WakuNode implements Waku {
154
154
  if (this.filter) {
155
155
  codecs.push(this.filter.multicodec);
156
156
  } else {
157
- log(
157
+ log.error(
158
158
  "Filter codec not included in dial codec: protocol not mounted locally"
159
159
  );
160
160
  }
161
161
  }
162
162
 
163
- log(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
163
+ log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
164
164
 
165
165
  return this.libp2p.dialProtocol(peerId, codecs);
166
166
  }