@waku/core 0.0.26-678635e.0 → 0.0.26
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.
- package/CHANGELOG.md +30 -0
- package/bundle/base_protocol-pDODy0G6.js +260 -0
- package/bundle/{base_protocol-4bcf7514.js → browser-mTOOnVZp.js} +751 -381
- package/bundle/index-cmONXM-V.js +595 -0
- package/bundle/index.js +637 -1879
- package/bundle/lib/base_protocol.js +3 -3
- package/bundle/lib/message/version_0.js +3 -3
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/{version_0-2f1176e3.js → version_0-LQTFNC7k.js} +118 -11
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +3 -2
- package/dist/lib/base_protocol.js +12 -6
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +8 -4
- package/dist/lib/connection_manager.js +34 -12
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.js +8 -5
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/filterPeers.js.map +1 -1
- package/dist/lib/keep_alive_manager.d.ts +1 -0
- package/dist/lib/keep_alive_manager.js +6 -3
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.js +1 -2
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/message/version_0.d.ts +7 -7
- package/dist/lib/message/version_0.js +5 -6
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +3 -0
- package/dist/lib/metadata/index.js +67 -0
- package/dist/lib/metadata/index.js.map +1 -0
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
- package/dist/lib/store/history_rpc.js.map +1 -1
- package/dist/lib/store/index.js +10 -11
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/dist/lib/waku.d.ts +4 -3
- package/dist/lib/waku.js +15 -6
- package/dist/lib/waku.js.map +1 -1
- package/package.json +127 -1
- package/src/index.ts +3 -1
- package/src/lib/base_protocol.ts +29 -7
- package/src/lib/connection_manager.ts +44 -13
- package/src/lib/filter/index.ts +18 -11
- package/src/lib/keep_alive_manager.ts +9 -3
- package/src/lib/light_push/index.ts +3 -4
- package/src/lib/message/version_0.ts +18 -10
- package/src/lib/metadata/index.ts +110 -0
- package/src/lib/store/index.ts +12 -13
- package/src/lib/waku.ts +19 -5
- package/bundle/browser-90197c87.js +0 -754
- package/bundle/index-27b91e3b.js +0 -31
- package/dist/lib/constants.d.ts +0 -4
- package/dist/lib/constants.js +0 -5
- package/dist/lib/constants.js.map +0 -1
- package/src/lib/constants.ts +0 -4
package/CHANGELOG.md
CHANGED
@@ -5,6 +5,36 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The file is maintained by [Release Please](https://github.com/googleapis/release-please) based on [Conventional Commits](https://www.conventionalcommits.org) specification,
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.0.26](https://github.com/waku-org/js-waku/compare/core-v0.0.25...core-v0.0.26) (2024-01-10)
|
9
|
+
|
10
|
+
|
11
|
+
### ⚠ BREAKING CHANGES
|
12
|
+
|
13
|
+
* add support for sharded pubsub topics & remove support for named pubsub topics ([#1697](https://github.com/waku-org/js-waku/issues/1697))
|
14
|
+
* change all instances of `PubSubTopic` to `PubsubTopic` ([#1703](https://github.com/waku-org/js-waku/issues/1703))
|
15
|
+
|
16
|
+
### Features
|
17
|
+
|
18
|
+
* Add support for autosharded pubsub topics ([2bc3735](https://github.com/waku-org/js-waku/commit/2bc3735e4dcf85f06b3dee542024d7f20a40fac2))
|
19
|
+
* Add support for sharded pubsub topics & remove support for named pubsub topics ([#1697](https://github.com/waku-org/js-waku/issues/1697)) ([4cf2ffe](https://github.com/waku-org/js-waku/commit/4cf2ffefa75e0571805036b71644d2cdd4fe3192))
|
20
|
+
* Metadata protocol ([#1732](https://github.com/waku-org/js-waku/issues/1732)) ([9ac2a3f](https://github.com/waku-org/js-waku/commit/9ac2a3f36352523b79fcd8f8a94bd6e0e109fc30))
|
21
|
+
* Track node connection state ([#1719](https://github.com/waku-org/js-waku/issues/1719)) ([1d0e2ac](https://github.com/waku-org/js-waku/commit/1d0e2ace7fa5b44ab192505c7ebce01a7ce343e0))
|
22
|
+
|
23
|
+
|
24
|
+
### Miscellaneous Chores
|
25
|
+
|
26
|
+
* Change all instances of `PubSubTopic` to `PubsubTopic` ([#1703](https://github.com/waku-org/js-waku/issues/1703)) ([3166a51](https://github.com/waku-org/js-waku/commit/3166a5135e77583da4fa722ee2aa47c785854a38))
|
27
|
+
|
28
|
+
|
29
|
+
### Dependencies
|
30
|
+
|
31
|
+
* The following workspace dependencies were updated
|
32
|
+
* dependencies
|
33
|
+
* @waku/enr bumped from ^0.0.19 to ^0.0.20
|
34
|
+
* @waku/interfaces bumped from 0.0.20 to 0.0.21
|
35
|
+
* @waku/proto bumped from 0.0.5 to 0.0.6
|
36
|
+
* @waku/utils bumped from 0.0.13 to 0.0.14
|
37
|
+
|
8
38
|
## [0.0.25](https://github.com/waku-org/js-waku/compare/core-v0.0.24...core-v0.0.25) (2023-11-01)
|
9
39
|
|
10
40
|
|
@@ -0,0 +1,260 @@
|
|
1
|
+
import { T as Tags } from './browser-mTOOnVZp.js';
|
2
|
+
import { k as bytesToUtf8, L as Logger, s as shardInfoToPubsubTopics, D as DefaultPubsubTopic } from './index-cmONXM-V.js';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Returns a pseudo-random peer that supports the given protocol.
|
6
|
+
* Useful for protocols such as store and light push
|
7
|
+
*/
|
8
|
+
function selectRandomPeer(peers) {
|
9
|
+
if (peers.length === 0)
|
10
|
+
return;
|
11
|
+
const index = Math.round(Math.random() * (peers.length - 1));
|
12
|
+
return peers[index];
|
13
|
+
}
|
14
|
+
/**
|
15
|
+
* Returns the peer with the lowest latency.
|
16
|
+
* @param peerStore - The Libp2p PeerStore
|
17
|
+
* @param peers - The list of peers to choose from
|
18
|
+
* @returns The peer with the lowest latency, or undefined if no peer could be reached
|
19
|
+
*/
|
20
|
+
async function selectLowestLatencyPeer(peerStore, peers) {
|
21
|
+
if (peers.length === 0)
|
22
|
+
return;
|
23
|
+
const results = await Promise.all(peers.map(async (peer) => {
|
24
|
+
const pingBytes = (await peerStore.get(peer.id)).metadata.get("ping");
|
25
|
+
if (!pingBytes)
|
26
|
+
return { peer, ping: Infinity };
|
27
|
+
const ping = Number(bytesToUtf8(pingBytes)) ?? Infinity;
|
28
|
+
return { peer, ping };
|
29
|
+
}));
|
30
|
+
const lowestLatencyResult = results.sort((a, b) => a.ping - b.ping)[0];
|
31
|
+
if (!lowestLatencyResult) {
|
32
|
+
return undefined;
|
33
|
+
}
|
34
|
+
return lowestLatencyResult.ping !== Infinity
|
35
|
+
? lowestLatencyResult.peer
|
36
|
+
: undefined;
|
37
|
+
}
|
38
|
+
/**
|
39
|
+
* Returns the list of peers that supports the given protocol.
|
40
|
+
*/
|
41
|
+
async function getPeersForProtocol(peerStore, protocols) {
|
42
|
+
const peers = [];
|
43
|
+
await peerStore.forEach((peer) => {
|
44
|
+
for (let i = 0; i < protocols.length; i++) {
|
45
|
+
if (peer.protocols.includes(protocols[i])) {
|
46
|
+
peers.push(peer);
|
47
|
+
break;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
});
|
51
|
+
return peers;
|
52
|
+
}
|
53
|
+
async function getConnectedPeersForProtocol(connections, peerStore, protocols) {
|
54
|
+
const openConnections = connections.filter((connection) => connection.status === "open");
|
55
|
+
const peerPromises = openConnections.map(async (connection) => {
|
56
|
+
const peer = await peerStore.get(connection.remotePeer);
|
57
|
+
const supportsProtocol = peer.protocols.some((protocol) => protocols.includes(protocol));
|
58
|
+
return supportsProtocol ? peer : null;
|
59
|
+
});
|
60
|
+
const peersWithNulls = await Promise.all(peerPromises);
|
61
|
+
return peersWithNulls.filter((peer) => peer !== null);
|
62
|
+
}
|
63
|
+
/**
|
64
|
+
* Returns a peer that supports the given protocol.
|
65
|
+
* If peerId is provided, the peer with that id is returned.
|
66
|
+
* Otherwise, the peer with the lowest latency is returned.
|
67
|
+
* If no peer is found from the above criteria, a random peer is returned.
|
68
|
+
*/
|
69
|
+
async function selectPeerForProtocol(peerStore, protocols, peerId) {
|
70
|
+
let peer;
|
71
|
+
if (peerId) {
|
72
|
+
peer = await peerStore.get(peerId);
|
73
|
+
if (!peer) {
|
74
|
+
throw new Error(`Failed to retrieve connection details for provided peer in peer store: ${peerId.toString()}`);
|
75
|
+
}
|
76
|
+
}
|
77
|
+
else {
|
78
|
+
const peers = await getPeersForProtocol(peerStore, protocols);
|
79
|
+
peer = await selectLowestLatencyPeer(peerStore, peers);
|
80
|
+
if (!peer) {
|
81
|
+
peer = selectRandomPeer(peers);
|
82
|
+
if (!peer)
|
83
|
+
throw new Error(`Failed to find known peer that registers protocols: ${protocols}`);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
let protocol;
|
87
|
+
for (const codec of protocols) {
|
88
|
+
if (peer.protocols.includes(codec)) {
|
89
|
+
protocol = codec;
|
90
|
+
// Do not break as we want to keep the last value
|
91
|
+
}
|
92
|
+
}
|
93
|
+
if (!protocol) {
|
94
|
+
throw new Error(`Peer does not register required protocols (${peer.id.toString()}): ${protocols}`);
|
95
|
+
}
|
96
|
+
return { peer, protocol };
|
97
|
+
}
|
98
|
+
function selectConnection(connections) {
|
99
|
+
if (!connections.length)
|
100
|
+
return;
|
101
|
+
if (connections.length === 1)
|
102
|
+
return connections[0];
|
103
|
+
let latestConnection;
|
104
|
+
connections.forEach((connection) => {
|
105
|
+
if (connection.status === "open") {
|
106
|
+
if (!latestConnection) {
|
107
|
+
latestConnection = connection;
|
108
|
+
}
|
109
|
+
else if (connection.timeline.open > latestConnection.timeline.open) {
|
110
|
+
latestConnection = connection;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
});
|
114
|
+
return latestConnection;
|
115
|
+
}
|
116
|
+
|
117
|
+
/**
|
118
|
+
* Retrieves a list of peers based on the specified criteria.
|
119
|
+
*
|
120
|
+
* @param peers - The list of peers to filter from.
|
121
|
+
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
122
|
+
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
123
|
+
* @returns A Promise that resolves to an array of peers based on the specified criteria.
|
124
|
+
*/
|
125
|
+
async function filterPeers(peers, numPeers, maxBootstrapPeers) {
|
126
|
+
// Collect the bootstrap peers up to the specified maximum
|
127
|
+
const bootstrapPeers = peers
|
128
|
+
.filter((peer) => peer.tags.has(Tags.BOOTSTRAP))
|
129
|
+
.slice(0, maxBootstrapPeers);
|
130
|
+
// Collect non-bootstrap peers
|
131
|
+
const nonBootstrapPeers = peers.filter((peer) => !peer.tags.has(Tags.BOOTSTRAP));
|
132
|
+
// If numPeers is 0, return all peers
|
133
|
+
if (numPeers === 0) {
|
134
|
+
return [...bootstrapPeers, ...nonBootstrapPeers];
|
135
|
+
}
|
136
|
+
// Initialize the list of selected peers with the bootstrap peers
|
137
|
+
const selectedPeers = [...bootstrapPeers];
|
138
|
+
// Fill up to numPeers with remaining random peers if needed
|
139
|
+
while (selectedPeers.length < numPeers && nonBootstrapPeers.length > 0) {
|
140
|
+
const randomIndex = Math.floor(Math.random() * nonBootstrapPeers.length);
|
141
|
+
const randomPeer = nonBootstrapPeers.splice(randomIndex, 1)[0];
|
142
|
+
selectedPeers.push(randomPeer);
|
143
|
+
}
|
144
|
+
return selectedPeers;
|
145
|
+
}
|
146
|
+
|
147
|
+
class StreamManager {
|
148
|
+
multicodec;
|
149
|
+
getConnections;
|
150
|
+
addEventListener;
|
151
|
+
streamPool;
|
152
|
+
log;
|
153
|
+
constructor(multicodec, getConnections, addEventListener) {
|
154
|
+
this.multicodec = multicodec;
|
155
|
+
this.getConnections = getConnections;
|
156
|
+
this.addEventListener = addEventListener;
|
157
|
+
this.log = new Logger(`stream-manager:${multicodec}`);
|
158
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool.bind(this));
|
159
|
+
this.getStream = this.getStream.bind(this);
|
160
|
+
this.streamPool = new Map();
|
161
|
+
}
|
162
|
+
async getStream(peer) {
|
163
|
+
const peerIdStr = peer.id.toString();
|
164
|
+
const streamPromise = this.streamPool.get(peerIdStr);
|
165
|
+
if (!streamPromise) {
|
166
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
167
|
+
}
|
168
|
+
// We have the stream, let's remove it from the map
|
169
|
+
this.streamPool.delete(peerIdStr);
|
170
|
+
this.prepareNewStream(peer);
|
171
|
+
const stream = await streamPromise;
|
172
|
+
if (!stream || stream.status === "closed") {
|
173
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
174
|
+
}
|
175
|
+
return stream;
|
176
|
+
}
|
177
|
+
async newStream(peer) {
|
178
|
+
const connections = this.getConnections(peer.id);
|
179
|
+
const connection = selectConnection(connections);
|
180
|
+
if (!connection) {
|
181
|
+
throw new Error("Failed to get a connection to the peer");
|
182
|
+
}
|
183
|
+
return connection.newStream(this.multicodec);
|
184
|
+
}
|
185
|
+
prepareNewStream(peer) {
|
186
|
+
const streamPromise = this.newStream(peer).catch(() => {
|
187
|
+
// No error thrown as this call is not triggered by the user
|
188
|
+
this.log.error(`Failed to prepare a new stream for ${peer.id.toString()}`);
|
189
|
+
});
|
190
|
+
this.streamPool.set(peer.id.toString(), streamPromise);
|
191
|
+
}
|
192
|
+
handlePeerUpdateStreamPool = (evt) => {
|
193
|
+
const peer = evt.detail.peer;
|
194
|
+
if (peer.protocols.includes(this.multicodec)) {
|
195
|
+
this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
|
196
|
+
this.prepareNewStream(peer);
|
197
|
+
}
|
198
|
+
};
|
199
|
+
}
|
200
|
+
|
201
|
+
/**
|
202
|
+
* A class with predefined helpers, to be used as a base to implement Waku
|
203
|
+
* Protocols.
|
204
|
+
*/
|
205
|
+
class BaseProtocol {
|
206
|
+
multicodec;
|
207
|
+
components;
|
208
|
+
addLibp2pEventListener;
|
209
|
+
removeLibp2pEventListener;
|
210
|
+
streamManager;
|
211
|
+
constructor(multicodec, components) {
|
212
|
+
this.multicodec = multicodec;
|
213
|
+
this.components = components;
|
214
|
+
this.addLibp2pEventListener = components.events.addEventListener.bind(components.events);
|
215
|
+
this.removeLibp2pEventListener = components.events.removeEventListener.bind(components.events);
|
216
|
+
this.streamManager = new StreamManager(multicodec, components.connectionManager.getConnections.bind(components.connectionManager), this.addLibp2pEventListener);
|
217
|
+
}
|
218
|
+
async getStream(peer) {
|
219
|
+
return this.streamManager.getStream(peer);
|
220
|
+
}
|
221
|
+
get peerStore() {
|
222
|
+
return this.components.peerStore;
|
223
|
+
}
|
224
|
+
/**
|
225
|
+
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
226
|
+
* the class protocol. Waku may or may not be currently connected to these
|
227
|
+
* peers.
|
228
|
+
*/
|
229
|
+
async peers() {
|
230
|
+
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
231
|
+
}
|
232
|
+
async getPeer(peerId) {
|
233
|
+
const { peer } = await selectPeerForProtocol(this.peerStore, [this.multicodec], peerId);
|
234
|
+
return peer;
|
235
|
+
}
|
236
|
+
/**
|
237
|
+
* Retrieves a list of connected peers based on the specified criteria.
|
238
|
+
*
|
239
|
+
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
240
|
+
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
241
|
+
* @returns A Promise that resolves to an array of peers based on the specified criteria.
|
242
|
+
*/
|
243
|
+
async getPeers({ numPeers, maxBootstrapPeers } = {
|
244
|
+
maxBootstrapPeers: 1,
|
245
|
+
numPeers: 0
|
246
|
+
}) {
|
247
|
+
// Retrieve all connected peers that support the protocol
|
248
|
+
const allPeersForProtocol = await getConnectedPeersForProtocol(this.components.connectionManager.getConnections(), this.peerStore, [this.multicodec]);
|
249
|
+
// Filter the peers based on the specified criteria
|
250
|
+
return filterPeers(allPeersForProtocol, numPeers, maxBootstrapPeers);
|
251
|
+
}
|
252
|
+
initializePubsubTopic(options) {
|
253
|
+
return (options?.pubsubTopics ??
|
254
|
+
(options?.shardInfo
|
255
|
+
? shardInfoToPubsubTopics(options.shardInfo)
|
256
|
+
: [DefaultPubsubTopic]));
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
export { BaseProtocol as B, StreamManager as S };
|