@waku/core 0.0.26 → 0.0.28-434be7b.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 (56) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/bundle/{base_protocol-pDODy0G6.js → base_protocol-BCwLeb-A.js} +134 -89
  3. package/bundle/{browser-mTOOnVZp.js → browser-DoQRY-an.js} +518 -712
  4. package/bundle/{index-cmONXM-V.js → index-vlQahmUj.js} +98 -41
  5. package/bundle/index.js +3081 -21642
  6. package/bundle/lib/base_protocol.js +3 -3
  7. package/bundle/lib/message/version_0.js +3 -3
  8. package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
  9. package/bundle/{version_0-LQTFNC7k.js → version_0-DiakMc1A.js} +1246 -2444
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/index.d.ts +1 -5
  12. package/dist/index.js +1 -5
  13. package/dist/index.js.map +1 -1
  14. package/dist/lib/base_protocol.d.ts +15 -13
  15. package/dist/lib/base_protocol.js +35 -22
  16. package/dist/lib/base_protocol.js.map +1 -1
  17. package/dist/lib/connection_manager.d.ts +2 -2
  18. package/dist/lib/connection_manager.js +16 -6
  19. package/dist/lib/connection_manager.js.map +1 -1
  20. package/dist/lib/filter/index.d.ts +1 -1
  21. package/dist/lib/filter/index.js +144 -82
  22. package/dist/lib/filter/index.js.map +1 -1
  23. package/dist/lib/filterPeers.d.ts +8 -5
  24. package/dist/lib/filterPeers.js +12 -5
  25. package/dist/lib/filterPeers.js.map +1 -1
  26. package/dist/lib/keep_alive_manager.d.ts +2 -3
  27. package/dist/lib/keep_alive_manager.js.map +1 -1
  28. package/dist/lib/light_push/index.d.ts +12 -2
  29. package/dist/lib/light_push/index.js +80 -80
  30. package/dist/lib/light_push/index.js.map +1 -1
  31. package/dist/lib/metadata/index.d.ts +2 -2
  32. package/dist/lib/metadata/index.js +58 -16
  33. package/dist/lib/metadata/index.js.map +1 -1
  34. package/dist/lib/store/index.js +1 -3
  35. package/dist/lib/store/index.js.map +1 -1
  36. package/dist/lib/stream_manager.d.ts +2 -2
  37. package/dist/lib/stream_manager.js.map +1 -1
  38. package/dist/lib/wait_for_remote_peer.d.ts +1 -1
  39. package/dist/lib/wait_for_remote_peer.js +42 -10
  40. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  41. package/package.json +1 -127
  42. package/src/index.ts +1 -6
  43. package/src/lib/base_protocol.ts +57 -37
  44. package/src/lib/connection_manager.ts +17 -10
  45. package/src/lib/filter/index.ts +234 -136
  46. package/src/lib/filterPeers.ts +15 -7
  47. package/src/lib/keep_alive_manager.ts +2 -3
  48. package/src/lib/light_push/index.ts +104 -124
  49. package/src/lib/metadata/index.ts +92 -30
  50. package/src/lib/store/index.ts +3 -6
  51. package/src/lib/stream_manager.ts +2 -3
  52. package/src/lib/wait_for_remote_peer.ts +68 -12
  53. package/dist/lib/waku.d.ts +0 -57
  54. package/dist/lib/waku.js +0 -130
  55. package/dist/lib/waku.js.map +0 -1
  56. package/src/lib/waku.ts +0 -214
package/package.json CHANGED
@@ -1,127 +1 @@
1
- {
2
- "name": "@waku/core",
3
- "version": "0.0.26",
4
- "description": "TypeScript implementation of the Waku v2 protocol",
5
- "types": "./dist/index.d.ts",
6
- "module": "./dist/index.js",
7
- "exports": {
8
- ".": {
9
- "types": "./dist/index.d.ts",
10
- "import": "./dist/index.js"
11
- },
12
- "./lib/predefined_bootstrap_nodes": {
13
- "types": "./dist/lib/predefined_bootstrap_nodes.d.ts",
14
- "import": "./dist/lib/predefined_bootstrap_nodes.js"
15
- },
16
- "./lib/message/version_0": {
17
- "types": "./dist/lib/message/version_0.d.ts",
18
- "import": "./dist/lib/message/version_0.js"
19
- },
20
- "./lib/base_protocol": {
21
- "types": "./dist/lib/base_protocol.d.ts",
22
- "import": "./dist/lib/base_protocol.js"
23
- }
24
- },
25
- "typesVersions": {
26
- "*": {
27
- "lib/*": [
28
- "dist/lib/*"
29
- ],
30
- "constants/*": [
31
- "dist/constants/*"
32
- ]
33
- }
34
- },
35
- "type": "module",
36
- "homepage": "https://github.com/waku-org/js-waku/tree/master/packages/core#readme",
37
- "repository": {
38
- "type": "git",
39
- "url": "https://github.com/waku-org/js-waku.git"
40
- },
41
- "bugs": {
42
- "url": "https://github.com/waku-org/js-waku/issues"
43
- },
44
- "license": "MIT OR Apache-2.0",
45
- "keywords": [
46
- "waku",
47
- "decentralised",
48
- "communication",
49
- "web3",
50
- "ethereum",
51
- "dapps"
52
- ],
53
- "scripts": {
54
- "build": "run-s build:**",
55
- "build:esm": "tsc",
56
- "build:bundle": "rollup --config rollup.config.js",
57
- "fix": "run-s fix:*",
58
- "fix:lint": "eslint src *.js --fix",
59
- "check": "run-s check:*",
60
- "check:tsc": "tsc -p tsconfig.dev.json",
61
- "check:lint": "eslint src *.js",
62
- "check:spelling": "cspell \"{README.md,src/**/*.ts}\"",
63
- "test": "NODE_ENV=test run-s test:*",
64
- "test:node": "NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha",
65
- "test:browser": "NODE_ENV=test karma start karma.conf.cjs",
66
- "watch:build": "tsc -p tsconfig.json -w",
67
- "watch:test": "mocha --watch",
68
- "prepublish": "npm run build",
69
- "reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build"
70
- },
71
- "engines": {
72
- "node": ">=18"
73
- },
74
- "dependencies": {
75
- "@noble/hashes": "^1.3.2",
76
- "@waku/enr": "^0.0.20",
77
- "@waku/interfaces": "0.0.21",
78
- "@waku/proto": "0.0.6",
79
- "@waku/utils": "0.0.14",
80
- "debug": "^4.3.4",
81
- "it-all": "^3.0.4",
82
- "it-length-prefixed": "^9.0.1",
83
- "it-pipe": "^3.0.1",
84
- "p-event": "^6.0.0",
85
- "uint8arraylist": "^2.4.3",
86
- "uuid": "^9.0.0"
87
- },
88
- "devDependencies": {
89
- "@multiformats/multiaddr": "^12.0.0",
90
- "@rollup/plugin-commonjs": "^25.0.7",
91
- "@rollup/plugin-json": "^6.0.0",
92
- "@rollup/plugin-node-resolve": "^15.2.3",
93
- "@types/chai": "^4.3.11",
94
- "@types/debug": "^4.1.12",
95
- "@types/mocha": "^10.0.1",
96
- "@types/uuid": "^9.0.7",
97
- "@waku/build-utils": "*",
98
- "chai": "^4.3.10",
99
- "cspell": "^7.3.2",
100
- "fast-check": "^3.14.0",
101
- "ignore-loader": "^0.1.2",
102
- "isomorphic-fetch": "^3.0.0",
103
- "mocha": "^10.2.0",
104
- "npm-run-all": "^4.1.5",
105
- "process": "^0.11.10",
106
- "rollup": "^4.6.0"
107
- },
108
- "peerDependencies": {
109
- "@multiformats/multiaddr": "^12.0.0",
110
- "libp2p": "^0.46.3"
111
- },
112
- "peerDependenciesMeta": {
113
- "@multiformats/multiaddr": {
114
- "optional": true
115
- }
116
- },
117
- "files": [
118
- "dist",
119
- "bundle",
120
- "src/**/*.ts",
121
- "!**/*.spec.*",
122
- "!**/*.json",
123
- "CHANGELOG.md",
124
- "LICENSE",
125
- "README.md"
126
- ]
127
- }
1
+ {"name":"@waku/core","version":"0.0.28-434be7b.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","react-native":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js","react-native":"./dist/index.js"},"./lib/predefined_bootstrap_nodes":{"types":"./dist/lib/predefined_bootstrap_nodes.d.ts","import":"./dist/lib/predefined_bootstrap_nodes.js","react-native":"./dist/lib/predefined_bootstrap_nodes.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js","react-native":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js","react-native":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=18"},"dependencies":{"@libp2p/ping":"^1.0.12","@noble/hashes":"^1.3.2","@waku/enr":"0.0.22-434be7b.0","@waku/interfaces":"0.0.23-434be7b.0","@waku/message-hash":"0.1.12-434be7b.0","@waku/proto":"0.0.7-434be7b.0","@waku/utils":"0.0.16-434be7b.0","debug":"^4.3.4","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","p-event":"^6.0.0","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","cspell":"^8.3.2","fast-check":"^3.15.1","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"^1.1.2","@waku/enr":"0.0.22-434be7b.0","@waku/interfaces":"0.0.23-434be7b.0","@waku/message-hash":"0.1.12-434be7b.0","@waku/proto":"0.0.7-434be7b.0","@waku/utils":"0.0.16-434be7b.0"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"@waku/interfaces":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
- export { DefaultUserAgent } from "./lib/waku.js";
2
1
  export { createEncoder, createDecoder } from "./lib/message/version_0.js";
3
2
  export type {
4
3
  Encoder,
@@ -7,15 +6,11 @@ export type {
7
6
  } from "./lib/message/version_0.js";
8
7
  export * as message from "./lib/message/index.js";
9
8
 
10
- export * as waku from "./lib/waku.js";
11
- export { WakuNode, WakuOptions } from "./lib/waku.js";
12
-
13
9
  export * as waku_filter from "./lib/filter/index.js";
14
10
  export { wakuFilter, FilterCodecs } from "./lib/filter/index.js";
15
11
 
16
12
  export * as waku_light_push from "./lib/light_push/index.js";
17
- export { LightPushCodec } from "./lib/light_push/index.js";
18
- export { wakuLightPush } from "./lib/light_push/index.js";
13
+ export { LightPushCodec, LightPushCore } from "./lib/light_push/index.js";
19
14
 
20
15
  export * as waku_store from "./lib/store/index.js";
21
16
 
@@ -1,36 +1,36 @@
1
1
  import type { Libp2p } from "@libp2p/interface";
2
- import type { Stream } from "@libp2p/interface/connection";
3
- import type { PeerId } from "@libp2p/interface/peer-id";
4
- import { Peer, PeerStore } from "@libp2p/interface/peer-store";
2
+ import type { Peer, PeerStore, Stream } from "@libp2p/interface";
5
3
  import type {
6
- IBaseProtocol,
4
+ IBaseProtocolCore,
7
5
  Libp2pComponents,
8
6
  ProtocolCreateOptions,
9
7
  PubsubTopic
10
8
  } from "@waku/interfaces";
11
- import { DefaultPubsubTopic } from "@waku/interfaces";
12
- import { shardInfoToPubsubTopics } from "@waku/utils";
9
+ import { ensureShardingConfigured, Logger } from "@waku/utils";
13
10
  import {
14
- getConnectedPeersForProtocol,
11
+ getConnectedPeersForProtocolAndShard,
15
12
  getPeersForProtocol,
16
- selectPeerForProtocol
13
+ sortPeersByLatency
17
14
  } from "@waku/utils/libp2p";
18
15
 
19
- import { filterPeers } from "./filterPeers.js";
16
+ import { filterPeersByDiscovery } from "./filterPeers.js";
20
17
  import { StreamManager } from "./stream_manager.js";
21
18
 
22
19
  /**
23
20
  * A class with predefined helpers, to be used as a base to implement Waku
24
21
  * Protocols.
25
22
  */
26
- export class BaseProtocol implements IBaseProtocol {
23
+ export class BaseProtocol implements IBaseProtocolCore {
27
24
  public readonly addLibp2pEventListener: Libp2p["addEventListener"];
28
25
  public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
29
26
  protected streamManager: StreamManager;
30
27
 
31
28
  constructor(
32
29
  public multicodec: string,
33
- private components: Libp2pComponents
30
+ private components: Libp2pComponents,
31
+ private log: Logger,
32
+ public readonly pubsubTopics: PubsubTopic[],
33
+ private options?: ProtocolCreateOptions
34
34
  ) {
35
35
  this.addLibp2pEventListener = components.events.addEventListener.bind(
36
36
  components.events
@@ -60,27 +60,28 @@ export class BaseProtocol implements IBaseProtocol {
60
60
  * the class protocol. Waku may or may not be currently connected to these
61
61
  * peers.
62
62
  */
63
- public async peers(): Promise<Peer[]> {
63
+ public async allPeers(): Promise<Peer[]> {
64
64
  return getPeersForProtocol(this.peerStore, [this.multicodec]);
65
65
  }
66
66
 
67
- protected async getPeer(peerId?: PeerId): Promise<Peer> {
68
- const { peer } = await selectPeerForProtocol(
69
- this.peerStore,
70
- [this.multicodec],
71
- peerId
72
- );
73
- return peer;
67
+ public async connectedPeers(): Promise<Peer[]> {
68
+ const peers = await this.allPeers();
69
+ return peers.filter((peer) => {
70
+ return (
71
+ this.components.connectionManager.getConnections(peer.id).length > 0
72
+ );
73
+ });
74
74
  }
75
75
 
76
76
  /**
77
- * Retrieves a list of connected peers based on the specified criteria.
77
+ * Retrieves a list of connected peers that support the protocol. The list is sorted by latency.
78
78
  *
79
79
  * @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
80
80
  * @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
81
- * @returns A Promise that resolves to an array of peers based on the specified criteria.
82
- */
83
- protected async getPeers(
81
+
82
+ * @returns A list of peers that support the protocol sorted by latency.
83
+ */
84
+ async getPeers(
84
85
  {
85
86
  numPeers,
86
87
  maxBootstrapPeers
@@ -92,23 +93,42 @@ export class BaseProtocol implements IBaseProtocol {
92
93
  numPeers: 0
93
94
  }
94
95
  ): Promise<Peer[]> {
95
- // Retrieve all connected peers that support the protocol
96
- const allPeersForProtocol = await getConnectedPeersForProtocol(
97
- this.components.connectionManager.getConnections(),
96
+ // Retrieve all connected peers that support the protocol & shard (if configured)
97
+ const connectedPeersForProtocolAndShard =
98
+ await getConnectedPeersForProtocolAndShard(
99
+ this.components.connectionManager.getConnections(),
100
+ this.peerStore,
101
+ [this.multicodec],
102
+ this.options?.shardInfo
103
+ ? ensureShardingConfigured(this.options.shardInfo).shardInfo
104
+ : undefined
105
+ );
106
+
107
+ // Filter the peers based on discovery & number of peers requested
108
+ const filteredPeers = filterPeersByDiscovery(
109
+ connectedPeersForProtocolAndShard,
110
+ numPeers,
111
+ maxBootstrapPeers
112
+ );
113
+
114
+ // Sort the peers by latency
115
+ const sortedFilteredPeers = await sortPeersByLatency(
98
116
  this.peerStore,
99
- [this.multicodec]
117
+ filteredPeers
100
118
  );
101
119
 
102
- // Filter the peers based on the specified criteria
103
- return filterPeers(allPeersForProtocol, numPeers, maxBootstrapPeers);
104
- }
120
+ if (sortedFilteredPeers.length === 0) {
121
+ this.log.warn(
122
+ "No peers found. Ensure you have a connection to the network."
123
+ );
124
+ }
105
125
 
106
- initializePubsubTopic(options?: ProtocolCreateOptions): PubsubTopic[] {
107
- return (
108
- options?.pubsubTopics ??
109
- (options?.shardInfo
110
- ? shardInfoToPubsubTopics(options.shardInfo)
111
- : [DefaultPubsubTopic])
112
- );
126
+ if (sortedFilteredPeers.length < numPeers) {
127
+ this.log.warn(
128
+ `Only ${sortedFilteredPeers.length} peers found. Requested ${numPeers}.`
129
+ );
130
+ }
131
+
132
+ return sortedFilteredPeers;
113
133
  }
114
134
  }
@@ -1,9 +1,5 @@
1
- import type { PeerId } from "@libp2p/interface/peer-id";
2
- import type { PeerInfo } from "@libp2p/interface/peer-info";
3
- import type { Peer } from "@libp2p/interface/peer-store";
4
- import type { PeerStore } from "@libp2p/interface/peer-store";
5
- import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
6
- import { decodeRelayShard } from "@waku/enr";
1
+ import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface";
2
+ import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
7
3
  import {
8
4
  ConnectionManagerOptions,
9
5
  EConnectionStateEvents,
@@ -18,7 +14,7 @@ import {
18
14
  ShardInfo
19
15
  } from "@waku/interfaces";
20
16
  import { Libp2p, Tags } from "@waku/interfaces";
21
- import { shardInfoToPubsubTopics } from "@waku/utils";
17
+ import { decodeRelayShard, shardInfoToPubsubTopics } from "@waku/utils";
22
18
  import { Logger } from "@waku/utils";
23
19
 
24
20
  import { KeepAliveManager } from "./keep_alive_manager.js";
@@ -30,7 +26,7 @@ export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
30
26
  export const DEFAULT_MAX_PARALLEL_DIALS = 3;
31
27
 
32
28
  export class ConnectionManager
33
- extends EventEmitter<IPeersByDiscoveryEvents & IConnectionStateEvents>
29
+ extends TypedEventEmitter<IPeersByDiscoveryEvents & IConnectionStateEvents>
34
30
  implements IConnectionManager
35
31
  {
36
32
  private static instances = new Map<string, ConnectionManager>();
@@ -101,8 +97,11 @@ export class ConnectionManager
101
97
 
102
98
  const peersDiscoveredByBootstrap: Peer[] = [];
103
99
  const peersDiscoveredByPeerExchange: Peer[] = [];
100
+ const peersDiscoveredByLocal: Peer[] = [];
101
+
104
102
  const peersConnectedByBootstrap: Peer[] = [];
105
103
  const peersConnectedByPeerExchange: Peer[] = [];
104
+ const peersConnectedByLocal: Peer[] = [];
106
105
 
107
106
  for (const peer of peersDiscovered) {
108
107
  const tags = await this.getTagNamesForPeer(peer.id);
@@ -111,6 +110,8 @@ export class ConnectionManager
111
110
  peersDiscoveredByBootstrap.push(peer);
112
111
  } else if (tags.includes(Tags.PEER_EXCHANGE)) {
113
112
  peersDiscoveredByPeerExchange.push(peer);
113
+ } else if (tags.includes(Tags.LOCAL)) {
114
+ peersDiscoveredByLocal.push(peer);
114
115
  }
115
116
  }
116
117
 
@@ -122,17 +123,21 @@ export class ConnectionManager
122
123
  peersConnectedByBootstrap.push(peer);
123
124
  } else if (tags.includes(Tags.PEER_EXCHANGE)) {
124
125
  peersConnectedByPeerExchange.push(peer);
126
+ } else if (tags.includes(Tags.LOCAL)) {
127
+ peersConnectedByLocal.push(peer);
125
128
  }
126
129
  }
127
130
 
128
131
  return {
129
132
  DISCOVERED: {
130
133
  [Tags.BOOTSTRAP]: peersDiscoveredByBootstrap,
131
- [Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange
134
+ [Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange,
135
+ [Tags.LOCAL]: peersDiscoveredByLocal
132
136
  },
133
137
  CONNECTED: {
134
138
  [Tags.BOOTSTRAP]: peersConnectedByBootstrap,
135
- [Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange
139
+ [Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange,
140
+ [Tags.LOCAL]: peersConnectedByLocal
136
141
  }
137
142
  };
138
143
  }
@@ -380,6 +385,8 @@ export class ConnectionManager
380
385
  },
381
386
  "peer:connect": (evt: CustomEvent<PeerId>): void => {
382
387
  void (async () => {
388
+ log.info(`Connected to peer ${evt.detail.toString()}`);
389
+
383
390
  const peerId = evt.detail;
384
391
 
385
392
  this.keepAliveManager.start(