@waku/core 0.0.33-00c3261.0 → 0.0.33-252ec59.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.
@@ -1,58 +1,4 @@
1
- import { h as bytesToUtf8, T as Tags, L as Logger, e as pubsubTopicsToShardInfo } from './index-tdQNdKHx.js';
2
-
3
- const decodeRelayShard = (bytes) => {
4
- // explicitly converting to Uint8Array to avoid Buffer
5
- // https://github.com/libp2p/js-libp2p/issues/2146
6
- bytes = new Uint8Array(bytes);
7
- if (bytes.length < 3)
8
- throw new Error("Insufficient data");
9
- const view = new DataView(bytes.buffer);
10
- const clusterId = view.getUint16(0);
11
- const shards = [];
12
- if (bytes.length === 130) {
13
- // rsv format (Bit Vector)
14
- for (let i = 0; i < 1024; i++) {
15
- const byteIndex = Math.floor(i / 8) + 2; // Adjusted for the 2-byte cluster field
16
- const bitIndex = 7 - (i % 8);
17
- if (view.getUint8(byteIndex) & (1 << bitIndex)) {
18
- shards.push(i);
19
- }
20
- }
21
- }
22
- else {
23
- // rs format (Index List)
24
- const numIndices = view.getUint8(2);
25
- for (let i = 0, offset = 3; i < numIndices; i++, offset += 2) {
26
- if (offset + 1 >= bytes.length)
27
- throw new Error("Unexpected end of data");
28
- shards.push(view.getUint16(offset));
29
- }
30
- }
31
- return { clusterId, shards };
32
- };
33
- const encodeRelayShard = (shardInfo) => {
34
- const { clusterId, shards } = shardInfo;
35
- const totalLength = shards.length >= 64 ? 130 : 3 + 2 * shards.length;
36
- const buffer = new ArrayBuffer(totalLength);
37
- const view = new DataView(buffer);
38
- view.setUint16(0, clusterId);
39
- if (shards.length >= 64) {
40
- // rsv format (Bit Vector)
41
- for (const index of shards) {
42
- const byteIndex = Math.floor(index / 8) + 2; // Adjusted for the 2-byte cluster field
43
- const bitIndex = 7 - (index % 8);
44
- view.setUint8(byteIndex, view.getUint8(byteIndex) | (1 << bitIndex));
45
- }
46
- }
47
- else {
48
- // rs format (Index List)
49
- view.setUint8(2, shards.length);
50
- for (let i = 0, offset = 3; i < shards.length; i++, offset += 2) {
51
- view.setUint16(offset, shards[i]);
52
- }
53
- }
54
- return new Uint8Array(buffer);
55
- };
1
+ import { g as bytesToUtf8, T as Tags, L as Logger } from './index-BVysxsMu.js';
56
2
 
57
3
  /**
58
4
  * Function to sort peers by latency from lowest to highest
@@ -96,28 +42,6 @@ async function getPeersForProtocol(peerStore, protocols) {
96
42
  });
97
43
  return peers;
98
44
  }
99
- async function getConnectedPeersForProtocolAndShard(connections, peerStore, protocols, shardInfo) {
100
- const openConnections = connections.filter((connection) => connection.status === "open");
101
- const peerPromises = openConnections.map(async (connection) => {
102
- const peer = await peerStore.get(connection.remotePeer);
103
- const supportsProtocol = protocols.some((protocol) => peer.protocols.includes(protocol));
104
- if (supportsProtocol) {
105
- if (shardInfo) {
106
- const encodedPeerShardInfo = peer.metadata.get("shardInfo");
107
- const peerShardInfo = encodedPeerShardInfo && decodeRelayShard(encodedPeerShardInfo);
108
- if (peerShardInfo && shardInfo.clusterId === peerShardInfo.clusterId) {
109
- return peer;
110
- }
111
- }
112
- else {
113
- return peer;
114
- }
115
- }
116
- return null;
117
- });
118
- const peersWithNulls = await Promise.all(peerPromises);
119
- return peersWithNulls.filter((peer) => peer !== null);
120
- }
121
45
 
122
46
  /**
123
47
  * Retrieves a list of peers based on the specified criteria:
@@ -156,26 +80,13 @@ function filterPeersByDiscovery(peers, numPeers, maxBootstrapPeers) {
156
80
  return selectedPeers;
157
81
  }
158
82
 
159
- function selectConnection(connections) {
160
- if (!connections.length)
161
- return;
162
- if (connections.length === 1)
163
- return connections[0];
164
- let latestConnection;
165
- connections.forEach((connection) => {
166
- if (connection.status === "open") {
167
- if (!latestConnection) {
168
- latestConnection = connection;
169
- }
170
- else if (connection.timeline.open > latestConnection.timeline.open) {
171
- latestConnection = connection;
172
- }
173
- }
174
- });
175
- return latestConnection;
83
+ function selectOpenConnection(connections) {
84
+ return connections
85
+ .filter((c) => c.status === "open")
86
+ .sort((left, right) => right.timeline.open - left.timeline.open)
87
+ .at(0);
176
88
  }
177
89
 
178
- const CONNECTION_TIMEOUT = 5_000;
179
90
  class StreamManager {
180
91
  multicodec;
181
92
  getConnections;
@@ -193,9 +104,11 @@ class StreamManager {
193
104
  async getStream(peer) {
194
105
  const peerId = peer.id.toString();
195
106
  const scheduledStream = this.streamPool.get(peerId);
196
- this.streamPool.delete(peerId);
197
- await scheduledStream;
198
- const stream = this.getStreamForCodec(peer.id);
107
+ if (scheduledStream) {
108
+ this.streamPool.delete(peerId);
109
+ await scheduledStream;
110
+ }
111
+ const stream = this.getOpenStreamForCodec(peer.id);
199
112
  if (stream) {
200
113
  this.log.info(`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
201
114
  return stream;
@@ -204,7 +117,7 @@ class StreamManager {
204
117
  }
205
118
  async createStream(peer, retries = 0) {
206
119
  const connections = this.getConnections(peer.id);
207
- const connection = selectConnection(connections);
120
+ const connection = selectOpenConnection(connections);
208
121
  if (!connection) {
209
122
  throw new Error(`Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
210
123
  }
@@ -215,6 +128,7 @@ class StreamManager {
215
128
  this.log.info(`Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
216
129
  stream = await connection.newStream(this.multicodec);
217
130
  this.log.info(`Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
131
+ break;
218
132
  }
219
133
  catch (error) {
220
134
  lastError = error;
@@ -236,16 +150,20 @@ class StreamManager {
236
150
  this.ongoingCreation.add(peerId);
237
151
  await this.createStream(peer);
238
152
  }
153
+ catch (error) {
154
+ this.log.error(`Failed to createStreamWithLock:`, error);
155
+ }
239
156
  finally {
240
157
  this.ongoingCreation.delete(peerId);
241
158
  }
159
+ return;
242
160
  }
243
161
  handlePeerUpdateStreamPool = (evt) => {
244
162
  const { peer } = evt.detail;
245
163
  if (!peer.protocols.includes(this.multicodec)) {
246
164
  return;
247
165
  }
248
- const stream = this.getStreamForCodec(peer.id);
166
+ const stream = this.getOpenStreamForCodec(peer.id);
249
167
  if (stream) {
250
168
  return;
251
169
  }
@@ -253,19 +171,23 @@ class StreamManager {
253
171
  };
254
172
  scheduleNewStream(peer) {
255
173
  this.log.info(`Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
256
- const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(undefined), CONNECTION_TIMEOUT));
257
- const streamPromise = Promise.race([
258
- this.createStreamWithLock(peer),
259
- timeoutPromise
260
- ]);
261
- this.streamPool.set(peer.id.toString(), streamPromise);
174
+ // abandon previous attempt
175
+ if (this.streamPool.has(peer.id.toString())) {
176
+ this.streamPool.delete(peer.id.toString());
177
+ }
178
+ this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
262
179
  }
263
- getStreamForCodec(peerId) {
264
- const connection = this.getConnections(peerId).find((c) => c.status === "open");
180
+ getOpenStreamForCodec(peerId) {
181
+ const connections = this.getConnections(peerId);
182
+ const connection = selectOpenConnection(connections);
265
183
  if (!connection) {
266
184
  return;
267
185
  }
268
186
  const stream = connection.streams.find((s) => s.protocol === this.multicodec);
187
+ const isStreamUnusable = ["done", "closed", "closing"].includes(stream?.writeStatus || "");
188
+ if (isStreamUnusable) {
189
+ return;
190
+ }
269
191
  return stream;
270
192
  }
271
193
  }
@@ -294,21 +216,19 @@ class BaseProtocol {
294
216
  async getStream(peer) {
295
217
  return this.streamManager.getStream(peer);
296
218
  }
297
- get peerStore() {
298
- return this.components.peerStore;
299
- }
300
219
  /**
301
220
  * Returns known peers from the address book (`libp2p.peerStore`) that support
302
221
  * the class protocol. Waku may or may not be currently connected to these
303
222
  * peers.
304
223
  */
305
224
  async allPeers() {
306
- return getPeersForProtocol(this.peerStore, [this.multicodec]);
225
+ return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
307
226
  }
308
227
  async connectedPeers() {
309
228
  const peers = await this.allPeers();
310
229
  return peers.filter((peer) => {
311
- return (this.components.connectionManager.getConnections(peer.id).length > 0);
230
+ const connections = this.components.connectionManager.getConnections(peer.id);
231
+ return connections.length > 0;
312
232
  });
313
233
  }
314
234
  /**
@@ -316,19 +236,18 @@ class BaseProtocol {
316
236
  *
317
237
  * @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
318
238
  * @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
319
-
320
- * @returns A list of peers that support the protocol sorted by latency.
321
- */
239
+ * @returns A list of peers that support the protocol sorted by latency. By default, returns all peers available, including bootstrap.
240
+ */
322
241
  async getPeers({ numPeers, maxBootstrapPeers } = {
323
- maxBootstrapPeers: 1,
242
+ maxBootstrapPeers: 0,
324
243
  numPeers: 0
325
244
  }) {
326
245
  // Retrieve all connected peers that support the protocol & shard (if configured)
327
- const connectedPeersForProtocolAndShard = await getConnectedPeersForProtocolAndShard(this.components.connectionManager.getConnections(), this.peerStore, [this.multicodec], pubsubTopicsToShardInfo(this.pubsubTopics));
246
+ const allAvailableConnectedPeers = await this.connectedPeers();
328
247
  // Filter the peers based on discovery & number of peers requested
329
- const filteredPeers = filterPeersByDiscovery(connectedPeersForProtocolAndShard, numPeers, maxBootstrapPeers);
248
+ const filteredPeers = filterPeersByDiscovery(allAvailableConnectedPeers, numPeers, maxBootstrapPeers);
330
249
  // Sort the peers by latency
331
- const sortedFilteredPeers = await sortPeersByLatency(this.peerStore, filteredPeers);
250
+ const sortedFilteredPeers = await sortPeersByLatency(this.components.peerStore, filteredPeers);
332
251
  if (sortedFilteredPeers.length === 0) {
333
252
  this.log.warn("No peers found. Ensure you have a connection to the network.");
334
253
  }
@@ -339,4 +258,4 @@ class BaseProtocol {
339
258
  }
340
259
  }
341
260
 
342
- export { BaseProtocol as B, StreamManager as S, decodeRelayShard as d, encodeRelayShard as e };
261
+ export { BaseProtocol as B, StreamManager as S };