@waku/core 0.0.33-b93134a.0 → 0.0.33-c9fdfb3.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.
- package/bundle/{base_protocol-1U9jKpZH.js → base_protocol-BDjsZTsQ.js} +29 -94
- package/bundle/{index-tdQNdKHx.js → index-CeEH6b9b.js} +24 -450
- package/bundle/index.js +100 -417
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-BrbNEwD-.js → version_0-BYg0O3M-.js} +451 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +4 -6
- package/dist/lib/base_protocol.js +10 -14
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/filter/index.js +2 -2
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/metadata/index.js +1 -1
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/stream_manager/stream_manager.d.ts +2 -0
- package/dist/lib/stream_manager/stream_manager.js +18 -4
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -3
- package/src/lib/base_protocol.ts +14 -28
- package/src/lib/filter/index.ts +11 -2
- package/src/lib/metadata/index.ts +1 -1
- package/src/lib/stream_manager/stream_manager.ts +23 -4
- package/dist/lib/wait_for_remote_peer.d.ts +0 -22
- package/dist/lib/wait_for_remote_peer.js +0 -142
- package/dist/lib/wait_for_remote_peer.js.map +0 -1
- package/src/lib/wait_for_remote_peer.ts +0 -200
@@ -1,58 +1,4 @@
|
|
1
|
-
import {
|
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-CeEH6b9b.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:
|
@@ -163,6 +87,7 @@ function selectOpenConnection(connections) {
|
|
163
87
|
.at(0);
|
164
88
|
}
|
165
89
|
|
90
|
+
const STREAM_LOCK_KEY = "consumed";
|
166
91
|
class StreamManager {
|
167
92
|
multicodec;
|
168
93
|
getConnections;
|
@@ -184,12 +109,15 @@ class StreamManager {
|
|
184
109
|
this.streamPool.delete(peerId);
|
185
110
|
await scheduledStream;
|
186
111
|
}
|
187
|
-
|
112
|
+
let stream = this.getOpenStreamForCodec(peer.id);
|
188
113
|
if (stream) {
|
189
114
|
this.log.info(`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
115
|
+
this.lockStream(peer.id.toString(), stream);
|
190
116
|
return stream;
|
191
117
|
}
|
192
|
-
|
118
|
+
stream = await this.createStream(peer);
|
119
|
+
this.lockStream(peer.id.toString(), stream);
|
120
|
+
return stream;
|
193
121
|
}
|
194
122
|
async createStream(peer, retries = 0) {
|
195
123
|
const connections = this.getConnections(peer.id);
|
@@ -260,12 +188,22 @@ class StreamManager {
|
|
260
188
|
return;
|
261
189
|
}
|
262
190
|
const stream = connection.streams.find((s) => s.protocol === this.multicodec);
|
263
|
-
|
264
|
-
|
191
|
+
if (!stream) {
|
192
|
+
return;
|
193
|
+
}
|
194
|
+
const isStreamUnusable = ["done", "closed", "closing"].includes(stream.writeStatus || "");
|
195
|
+
if (isStreamUnusable || this.isStreamLocked(stream)) {
|
265
196
|
return;
|
266
197
|
}
|
267
198
|
return stream;
|
268
199
|
}
|
200
|
+
lockStream(peerId, stream) {
|
201
|
+
this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`);
|
202
|
+
stream.metadata[STREAM_LOCK_KEY] = true;
|
203
|
+
}
|
204
|
+
isStreamLocked(stream) {
|
205
|
+
return !!stream.metadata[STREAM_LOCK_KEY];
|
206
|
+
}
|
269
207
|
}
|
270
208
|
|
271
209
|
/**
|
@@ -292,21 +230,19 @@ class BaseProtocol {
|
|
292
230
|
async getStream(peer) {
|
293
231
|
return this.streamManager.getStream(peer);
|
294
232
|
}
|
295
|
-
get peerStore() {
|
296
|
-
return this.components.peerStore;
|
297
|
-
}
|
298
233
|
/**
|
299
234
|
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
300
235
|
* the class protocol. Waku may or may not be currently connected to these
|
301
236
|
* peers.
|
302
237
|
*/
|
303
238
|
async allPeers() {
|
304
|
-
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
239
|
+
return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
|
305
240
|
}
|
306
241
|
async connectedPeers() {
|
307
242
|
const peers = await this.allPeers();
|
308
243
|
return peers.filter((peer) => {
|
309
|
-
|
244
|
+
const connections = this.components.connectionManager.getConnections(peer.id);
|
245
|
+
return connections.length > 0;
|
310
246
|
});
|
311
247
|
}
|
312
248
|
/**
|
@@ -314,19 +250,18 @@ class BaseProtocol {
|
|
314
250
|
*
|
315
251
|
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
316
252
|
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
317
|
-
|
318
|
-
|
319
|
-
*/
|
253
|
+
* @returns A list of peers that support the protocol sorted by latency. By default, returns all peers available, including bootstrap.
|
254
|
+
*/
|
320
255
|
async getPeers({ numPeers, maxBootstrapPeers } = {
|
321
|
-
maxBootstrapPeers:
|
256
|
+
maxBootstrapPeers: 0,
|
322
257
|
numPeers: 0
|
323
258
|
}) {
|
324
259
|
// Retrieve all connected peers that support the protocol & shard (if configured)
|
325
|
-
const
|
260
|
+
const allAvailableConnectedPeers = await this.connectedPeers();
|
326
261
|
// Filter the peers based on discovery & number of peers requested
|
327
|
-
const filteredPeers = filterPeersByDiscovery(
|
262
|
+
const filteredPeers = filterPeersByDiscovery(allAvailableConnectedPeers, numPeers, maxBootstrapPeers);
|
328
263
|
// Sort the peers by latency
|
329
|
-
const sortedFilteredPeers = await sortPeersByLatency(this.peerStore, filteredPeers);
|
264
|
+
const sortedFilteredPeers = await sortPeersByLatency(this.components.peerStore, filteredPeers);
|
330
265
|
if (sortedFilteredPeers.length === 0) {
|
331
266
|
this.log.warn("No peers found. Ensure you have a connection to the network.");
|
332
267
|
}
|
@@ -337,4 +272,4 @@ class BaseProtocol {
|
|
337
272
|
}
|
338
273
|
}
|
339
274
|
|
340
|
-
export { BaseProtocol as B, StreamManager as S
|
275
|
+
export { BaseProtocol as B, StreamManager as S };
|