@waku/core 0.0.34-b6339f7.0 → 0.0.34-c43cec2.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-DxFKDXX2.js +153 -0
- package/bundle/{index-BIW3qNYx.js → index-yLOEQnIE.js} +5 -122
- package/bundle/index.js +74 -32
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-CdmZMfkQ.js → version_0-Che4t3mN.js} +122 -5
- 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 +2 -23
- package/dist/lib/base_protocol.js +1 -53
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/{connection_manager.d.ts → connection_manager/connection_manager.d.ts} +12 -9
- package/dist/lib/{connection_manager.js → connection_manager/connection_manager.js} +40 -23
- package/dist/lib/connection_manager/connection_manager.js.map +1 -0
- package/dist/lib/connection_manager/index.d.ts +1 -0
- package/dist/lib/connection_manager/index.js +2 -0
- package/dist/lib/connection_manager/index.js.map +1 -0
- package/dist/lib/{keep_alive_manager.d.ts → connection_manager/keep_alive_manager.d.ts} +4 -2
- package/dist/lib/{keep_alive_manager.js → connection_manager/keep_alive_manager.js} +2 -2
- package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -0
- package/dist/lib/connection_manager/utils.d.ts +7 -0
- package/dist/lib/connection_manager/utils.js +22 -0
- package/dist/lib/connection_manager/utils.js.map +1 -0
- package/dist/lib/filter/index.js +1 -1
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/light_push/index.js +2 -1
- package/dist/lib/light_push/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/store/index.js +1 -1
- package/dist/lib/store/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -2
- package/src/lib/base_protocol.ts +0 -89
- package/src/lib/{connection_manager.ts → connection_manager/connection_manager.ts} +54 -43
- package/src/lib/connection_manager/index.ts +1 -0
- package/src/lib/{keep_alive_manager.ts → connection_manager/keep_alive_manager.ts} +7 -3
- package/src/lib/connection_manager/utils.ts +25 -0
- package/src/lib/filter/index.ts +1 -1
- package/src/lib/light_push/index.ts +2 -1
- package/src/lib/metadata/index.ts +1 -1
- package/src/lib/store/index.ts +1 -1
- package/bundle/base_protocol-sjpC9_Fu.js +0 -283
- package/dist/lib/connection_manager.js.map +0 -1
- package/dist/lib/filterPeers.d.ts +0 -13
- package/dist/lib/filterPeers.js +0 -38
- package/dist/lib/filterPeers.js.map +0 -1
- package/dist/lib/keep_alive_manager.js.map +0 -1
- package/src/lib/filterPeers.ts +0 -51
@@ -0,0 +1,153 @@
|
|
1
|
+
import { L as Logger } from './index-yLOEQnIE.js';
|
2
|
+
|
3
|
+
function selectOpenConnection(connections) {
|
4
|
+
return connections
|
5
|
+
.filter((c) => c.status === "open")
|
6
|
+
.sort((left, right) => right.timeline.open - left.timeline.open)
|
7
|
+
.at(0);
|
8
|
+
}
|
9
|
+
|
10
|
+
const STREAM_LOCK_KEY = "consumed";
|
11
|
+
class StreamManager {
|
12
|
+
multicodec;
|
13
|
+
getConnections;
|
14
|
+
addEventListener;
|
15
|
+
log;
|
16
|
+
ongoingCreation = new Set();
|
17
|
+
streamPool = new Map();
|
18
|
+
constructor(multicodec, getConnections, addEventListener) {
|
19
|
+
this.multicodec = multicodec;
|
20
|
+
this.getConnections = getConnections;
|
21
|
+
this.addEventListener = addEventListener;
|
22
|
+
this.log = new Logger(`stream-manager:${multicodec}`);
|
23
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
24
|
+
}
|
25
|
+
async getStream(peer) {
|
26
|
+
const peerId = peer.id.toString();
|
27
|
+
const scheduledStream = this.streamPool.get(peerId);
|
28
|
+
if (scheduledStream) {
|
29
|
+
this.streamPool.delete(peerId);
|
30
|
+
await scheduledStream;
|
31
|
+
}
|
32
|
+
let stream = this.getOpenStreamForCodec(peer.id);
|
33
|
+
if (stream) {
|
34
|
+
this.log.info(`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
35
|
+
this.lockStream(peer.id.toString(), stream);
|
36
|
+
return stream;
|
37
|
+
}
|
38
|
+
stream = await this.createStream(peer);
|
39
|
+
this.lockStream(peer.id.toString(), stream);
|
40
|
+
return stream;
|
41
|
+
}
|
42
|
+
async createStream(peer, retries = 0) {
|
43
|
+
const connections = this.getConnections(peer.id);
|
44
|
+
const connection = selectOpenConnection(connections);
|
45
|
+
if (!connection) {
|
46
|
+
throw new Error(`Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
47
|
+
}
|
48
|
+
let lastError;
|
49
|
+
let stream;
|
50
|
+
for (let i = 0; i < retries + 1; i++) {
|
51
|
+
try {
|
52
|
+
this.log.info(`Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
53
|
+
stream = await connection.newStream(this.multicodec);
|
54
|
+
this.log.info(`Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
55
|
+
break;
|
56
|
+
}
|
57
|
+
catch (error) {
|
58
|
+
lastError = error;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
if (!stream) {
|
62
|
+
throw new Error(`Failed to create a new stream for ${peer.id.toString()} -- ` +
|
63
|
+
lastError);
|
64
|
+
}
|
65
|
+
return stream;
|
66
|
+
}
|
67
|
+
async createStreamWithLock(peer) {
|
68
|
+
const peerId = peer.id.toString();
|
69
|
+
if (this.ongoingCreation.has(peerId)) {
|
70
|
+
this.log.info(`Skipping creation of a stream due to lock for peerId=${peerId} multicodec=${this.multicodec}`);
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
try {
|
74
|
+
this.ongoingCreation.add(peerId);
|
75
|
+
await this.createStream(peer);
|
76
|
+
}
|
77
|
+
catch (error) {
|
78
|
+
this.log.error(`Failed to createStreamWithLock:`, error);
|
79
|
+
}
|
80
|
+
finally {
|
81
|
+
this.ongoingCreation.delete(peerId);
|
82
|
+
}
|
83
|
+
return;
|
84
|
+
}
|
85
|
+
handlePeerUpdateStreamPool = (evt) => {
|
86
|
+
const { peer } = evt.detail;
|
87
|
+
if (!peer.protocols.includes(this.multicodec)) {
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
const stream = this.getOpenStreamForCodec(peer.id);
|
91
|
+
if (stream) {
|
92
|
+
return;
|
93
|
+
}
|
94
|
+
this.scheduleNewStream(peer);
|
95
|
+
};
|
96
|
+
scheduleNewStream(peer) {
|
97
|
+
this.log.info(`Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
98
|
+
// abandon previous attempt
|
99
|
+
if (this.streamPool.has(peer.id.toString())) {
|
100
|
+
this.streamPool.delete(peer.id.toString());
|
101
|
+
}
|
102
|
+
this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
|
103
|
+
}
|
104
|
+
getOpenStreamForCodec(peerId) {
|
105
|
+
const connections = this.getConnections(peerId);
|
106
|
+
const connection = selectOpenConnection(connections);
|
107
|
+
if (!connection) {
|
108
|
+
return;
|
109
|
+
}
|
110
|
+
const stream = connection.streams.find((s) => s.protocol === this.multicodec);
|
111
|
+
if (!stream) {
|
112
|
+
return;
|
113
|
+
}
|
114
|
+
const isStreamUnusable = ["done", "closed", "closing"].includes(stream.writeStatus || "");
|
115
|
+
if (isStreamUnusable || this.isStreamLocked(stream)) {
|
116
|
+
return;
|
117
|
+
}
|
118
|
+
return stream;
|
119
|
+
}
|
120
|
+
lockStream(peerId, stream) {
|
121
|
+
this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`);
|
122
|
+
stream.metadata[STREAM_LOCK_KEY] = true;
|
123
|
+
}
|
124
|
+
isStreamLocked(stream) {
|
125
|
+
return !!stream.metadata[STREAM_LOCK_KEY];
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
/**
|
130
|
+
* A class with predefined helpers, to be used as a base to implement Waku
|
131
|
+
* Protocols.
|
132
|
+
*/
|
133
|
+
class BaseProtocol {
|
134
|
+
multicodec;
|
135
|
+
components;
|
136
|
+
pubsubTopics;
|
137
|
+
addLibp2pEventListener;
|
138
|
+
removeLibp2pEventListener;
|
139
|
+
streamManager;
|
140
|
+
constructor(multicodec, components, pubsubTopics) {
|
141
|
+
this.multicodec = multicodec;
|
142
|
+
this.components = components;
|
143
|
+
this.pubsubTopics = pubsubTopics;
|
144
|
+
this.addLibp2pEventListener = components.events.addEventListener.bind(components.events);
|
145
|
+
this.removeLibp2pEventListener = components.events.removeEventListener.bind(components.events);
|
146
|
+
this.streamManager = new StreamManager(multicodec, components.connectionManager.getConnections.bind(components.connectionManager), this.addLibp2pEventListener);
|
147
|
+
}
|
148
|
+
async getStream(peer) {
|
149
|
+
return this.streamManager.getStream(peer);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
export { BaseProtocol as B, StreamManager as S };
|
@@ -1,19 +1,3 @@
|
|
1
|
-
/**
|
2
|
-
* Returns a `Uint8Array` of the requested size. Referenced memory will
|
3
|
-
* be initialized to 0.
|
4
|
-
*/
|
5
|
-
function alloc(size = 0) {
|
6
|
-
return new Uint8Array(size);
|
7
|
-
}
|
8
|
-
/**
|
9
|
-
* Where possible returns a Uint8Array of the requested size that references
|
10
|
-
* uninitialized memory. Only use if you are certain you will immediately
|
11
|
-
* overwrite every value in the returned `Uint8Array`.
|
12
|
-
*/
|
13
|
-
function allocUnsafe(size = 0) {
|
14
|
-
return new Uint8Array(size);
|
15
|
-
}
|
16
|
-
|
17
1
|
function coerce(o) {
|
18
2
|
if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array')
|
19
3
|
return o;
|
@@ -24,10 +8,10 @@ function coerce(o) {
|
|
24
8
|
}
|
25
9
|
throw new Error('Unknown type, must be binary type');
|
26
10
|
}
|
27
|
-
function fromString
|
11
|
+
function fromString(str) {
|
28
12
|
return new TextEncoder().encode(str);
|
29
13
|
}
|
30
|
-
function toString
|
14
|
+
function toString(b) {
|
31
15
|
return new TextDecoder().decode(b);
|
32
16
|
}
|
33
17
|
|
@@ -636,8 +620,8 @@ var base8$1 = /*#__PURE__*/Object.freeze({
|
|
636
620
|
const identity = from({
|
637
621
|
prefix: '\x00',
|
638
622
|
name: 'identity',
|
639
|
-
encode: (buf) => toString
|
640
|
-
decode: (str) => fromString
|
623
|
+
encode: (buf) => toString(buf),
|
624
|
+
decode: (str) => fromString(str)
|
641
625
|
});
|
642
626
|
|
643
627
|
var identityBase = /*#__PURE__*/Object.freeze({
|
@@ -648,69 +632,6 @@ var identityBase = /*#__PURE__*/Object.freeze({
|
|
648
632
|
new TextEncoder();
|
649
633
|
new TextDecoder();
|
650
634
|
|
651
|
-
const bases = { ...identityBase, ...base2$1, ...base8$1, ...base10$1, ...base16$1, ...base32$1, ...base36$1, ...base58, ...base64$1, ...base256emoji$1 };
|
652
|
-
|
653
|
-
function createCodec(name, prefix, encode, decode) {
|
654
|
-
return {
|
655
|
-
name,
|
656
|
-
prefix,
|
657
|
-
encoder: {
|
658
|
-
name,
|
659
|
-
prefix,
|
660
|
-
encode
|
661
|
-
},
|
662
|
-
decoder: {
|
663
|
-
decode
|
664
|
-
}
|
665
|
-
};
|
666
|
-
}
|
667
|
-
const string = createCodec('utf8', 'u', (buf) => {
|
668
|
-
const decoder = new TextDecoder('utf8');
|
669
|
-
return 'u' + decoder.decode(buf);
|
670
|
-
}, (str) => {
|
671
|
-
const encoder = new TextEncoder();
|
672
|
-
return encoder.encode(str.substring(1));
|
673
|
-
});
|
674
|
-
const ascii = createCodec('ascii', 'a', (buf) => {
|
675
|
-
let string = 'a';
|
676
|
-
for (let i = 0; i < buf.length; i++) {
|
677
|
-
string += String.fromCharCode(buf[i]);
|
678
|
-
}
|
679
|
-
return string;
|
680
|
-
}, (str) => {
|
681
|
-
str = str.substring(1);
|
682
|
-
const buf = allocUnsafe(str.length);
|
683
|
-
for (let i = 0; i < str.length; i++) {
|
684
|
-
buf[i] = str.charCodeAt(i);
|
685
|
-
}
|
686
|
-
return buf;
|
687
|
-
});
|
688
|
-
const BASES = {
|
689
|
-
utf8: string,
|
690
|
-
'utf-8': string,
|
691
|
-
hex: bases.base16,
|
692
|
-
latin1: ascii,
|
693
|
-
ascii,
|
694
|
-
binary: ascii,
|
695
|
-
...bases
|
696
|
-
};
|
697
|
-
|
698
|
-
/**
|
699
|
-
* Create a `Uint8Array` from the passed string
|
700
|
-
*
|
701
|
-
* Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module.
|
702
|
-
*
|
703
|
-
* Also `ascii` which is similar to node's 'binary' encoding.
|
704
|
-
*/
|
705
|
-
function fromString(string, encoding = 'utf8') {
|
706
|
-
const base = BASES[encoding];
|
707
|
-
if (base == null) {
|
708
|
-
throw new Error(`Unsupported encoding "${encoding}"`);
|
709
|
-
}
|
710
|
-
// add multibase prefix
|
711
|
-
return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
|
712
|
-
}
|
713
|
-
|
714
635
|
var Protocols;
|
715
636
|
(function (Protocols) {
|
716
637
|
Protocols["Relay"] = "relay";
|
@@ -832,44 +753,6 @@ var HealthStatus;
|
|
832
753
|
HealthStatus["SufficientlyHealthy"] = "SufficientlyHealthy";
|
833
754
|
})(HealthStatus || (HealthStatus = {}));
|
834
755
|
|
835
|
-
/**
|
836
|
-
* Turns a `Uint8Array` into a string.
|
837
|
-
*
|
838
|
-
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
|
839
|
-
*
|
840
|
-
* Also `ascii` which is similar to node's 'binary' encoding.
|
841
|
-
*/
|
842
|
-
function toString(array, encoding = 'utf8') {
|
843
|
-
const base = BASES[encoding];
|
844
|
-
if (base == null) {
|
845
|
-
throw new Error(`Unsupported encoding "${encoding}"`);
|
846
|
-
}
|
847
|
-
// strip multibase prefix
|
848
|
-
return base.encoder.encode(array).substring(1);
|
849
|
-
}
|
850
|
-
|
851
|
-
/**
|
852
|
-
* Decode byte array to utf-8 string.
|
853
|
-
*/
|
854
|
-
const bytesToUtf8 = (b) => toString(b, "utf8");
|
855
|
-
/**
|
856
|
-
* Encode utf-8 string to byte array.
|
857
|
-
*/
|
858
|
-
const utf8ToBytes = (s) => fromString(s, "utf8");
|
859
|
-
/**
|
860
|
-
* Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
|
861
|
-
*/
|
862
|
-
function concat(byteArrays, totalLength) {
|
863
|
-
const len = byteArrays.reduce((acc, curr) => acc + curr.length, 0);
|
864
|
-
const res = new Uint8Array(len);
|
865
|
-
let offset = 0;
|
866
|
-
for (const bytes of byteArrays) {
|
867
|
-
res.set(bytes, offset);
|
868
|
-
offset += bytes.length;
|
869
|
-
}
|
870
|
-
return res;
|
871
|
-
}
|
872
|
-
|
873
756
|
function getDefaultExportFromCjs (x) {
|
874
757
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
875
758
|
}
|
@@ -1626,4 +1509,4 @@ class Logger {
|
|
1626
1509
|
}
|
1627
1510
|
}
|
1628
1511
|
|
1629
|
-
export { EPeersByDiscoveryEvents as E, HealthStatus as H, Logger as L, ProtocolError as P, Tags as T,
|
1512
|
+
export { EPeersByDiscoveryEvents as E, HealthStatus as H, Logger as L, ProtocolError as P, Tags as T, EConnectionStateEvents as a, Protocols as b, base2$1 as c, base8$1 as d, base10$1 as e, base16$1 as f, base32$1 as g, base36$1 as h, identityBase as i, base58 as j, base64$1 as k, base256emoji$1 as l };
|
package/bundle/index.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import { v as version_0, e as encodingLength,
|
2
|
-
export {
|
3
|
-
import {
|
4
|
-
import { B as BaseProtocol } from './base_protocol-
|
5
|
-
export { S as StreamManager } from './base_protocol-
|
1
|
+
import { v as version_0, a as allocUnsafe, b as alloc, e as encodingLength, c as encode$1, d as decode$1, M as MessagePush, F as FilterSubscribeRequest, f as FilterSubscribeResponse$1, P as PushRpc$1, g as PushResponse, S as StoreQueryRequest$1, h as StoreQueryResponse$1, u as utf8ToBytes, i as createEncoder, p as pubsubTopicToSingleShardInfo, j as bytesToUtf8, s as shardInfoToPubsubTopics, W as WakuMetadataRequest, k as pubsubTopicsToShardInfo, l as WakuMetadataResponse } from './version_0-Che4t3mN.js';
|
2
|
+
export { m as createDecoder } from './version_0-Che4t3mN.js';
|
3
|
+
import { L as Logger, P as ProtocolError, T as Tags, E as EPeersByDiscoveryEvents, a as EConnectionStateEvents, H as HealthStatus, b as Protocols } from './index-yLOEQnIE.js';
|
4
|
+
import { B as BaseProtocol } from './base_protocol-DxFKDXX2.js';
|
5
|
+
export { S as StreamManager } from './base_protocol-DxFKDXX2.js';
|
6
6
|
|
7
7
|
const MB = 1024 ** 2;
|
8
8
|
const SIZE_CAP_IN_MB = 1;
|
@@ -1620,7 +1620,7 @@ class FilterCore extends BaseProtocol {
|
|
1620
1620
|
handleIncomingMessage;
|
1621
1621
|
pubsubTopics;
|
1622
1622
|
constructor(handleIncomingMessage, pubsubTopics, libp2p) {
|
1623
|
-
super(FilterCodecs.SUBSCRIBE, libp2p.components,
|
1623
|
+
super(FilterCodecs.SUBSCRIBE, libp2p.components, pubsubTopics);
|
1624
1624
|
this.handleIncomingMessage = handleIncomingMessage;
|
1625
1625
|
this.pubsubTopics = pubsubTopics;
|
1626
1626
|
libp2p
|
@@ -1882,7 +1882,7 @@ const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
|
1882
1882
|
class LightPushCore extends BaseProtocol {
|
1883
1883
|
pubsubTopics;
|
1884
1884
|
constructor(pubsubTopics, libp2p) {
|
1885
|
-
super(LightPushCodec, libp2p.components,
|
1885
|
+
super(LightPushCodec, libp2p.components, pubsubTopics);
|
1886
1886
|
this.pubsubTopics = pubsubTopics;
|
1887
1887
|
}
|
1888
1888
|
async preparePushMessage(encoder, message) {
|
@@ -1914,6 +1914,7 @@ class LightPushCore extends BaseProtocol {
|
|
1914
1914
|
};
|
1915
1915
|
}
|
1916
1916
|
}
|
1917
|
+
// TODO(weboko): use peer.id as parameter instead
|
1917
1918
|
async send(encoder, message, peer) {
|
1918
1919
|
const { query, error: preparationError } = await this.preparePushMessage(encoder, message);
|
1919
1920
|
if (preparationError || !query) {
|
@@ -2103,7 +2104,7 @@ const StoreCodec = "/vac/waku/store-query/3.0.0";
|
|
2103
2104
|
class StoreCore extends BaseProtocol {
|
2104
2105
|
pubsubTopics;
|
2105
2106
|
constructor(pubsubTopics, libp2p) {
|
2106
|
-
super(StoreCodec, libp2p.components,
|
2107
|
+
super(StoreCodec, libp2p.components, pubsubTopics);
|
2107
2108
|
this.pubsubTopics = pubsubTopics;
|
2108
2109
|
}
|
2109
2110
|
async *queryPerPage(queryOpts, decoders, peer) {
|
@@ -2179,10 +2180,14 @@ var index = /*#__PURE__*/Object.freeze({
|
|
2179
2180
|
StoreCore: StoreCore
|
2180
2181
|
});
|
2181
2182
|
|
2182
|
-
/**
|
2183
|
+
/**
|
2184
|
+
* Noop for browser compatibility
|
2185
|
+
*/
|
2183
2186
|
function setMaxListeners$1() { }
|
2184
2187
|
|
2185
|
-
|
2188
|
+
/**
|
2189
|
+
* Create a setMaxListeners that doesn't break browser usage
|
2190
|
+
*/
|
2186
2191
|
const setMaxListeners = (n, ...eventTargets) => {
|
2187
2192
|
try {
|
2188
2193
|
setMaxListeners$1(n, ...eventTargets);
|
@@ -2356,13 +2361,36 @@ class KeepAliveManager {
|
|
2356
2361
|
}
|
2357
2362
|
}
|
2358
2363
|
|
2364
|
+
/**
|
2365
|
+
* Reads peer's metadata and retrieves ping value.
|
2366
|
+
* @param peer Peer or null
|
2367
|
+
* @returns -1 if no ping attached, otherwise returns ping value
|
2368
|
+
*/
|
2369
|
+
const getPeerPing = (peer) => {
|
2370
|
+
if (!peer) {
|
2371
|
+
return -1;
|
2372
|
+
}
|
2373
|
+
try {
|
2374
|
+
const bytes = peer.metadata.get("ping");
|
2375
|
+
if (!bytes) {
|
2376
|
+
return -1;
|
2377
|
+
}
|
2378
|
+
return Number(bytesToUtf8(bytes));
|
2379
|
+
}
|
2380
|
+
catch (e) {
|
2381
|
+
return -1;
|
2382
|
+
}
|
2383
|
+
};
|
2384
|
+
|
2359
2385
|
const log$1 = new Logger("connection-manager");
|
2360
2386
|
const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
|
2361
2387
|
const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
|
2362
2388
|
const DEFAULT_MAX_PARALLEL_DIALS = 3;
|
2389
|
+
const DEFAULT_PING_KEEP_ALIVE_SEC = 5 * 60;
|
2390
|
+
const DEFAULT_RELAY_KEEP_ALIVE_SEC = 5 * 60;
|
2363
2391
|
class ConnectionManager extends TypedEventEmitter {
|
2364
|
-
|
2365
|
-
|
2392
|
+
// TODO(weboko): make it private
|
2393
|
+
pubsubTopics;
|
2366
2394
|
keepAliveManager;
|
2367
2395
|
options;
|
2368
2396
|
libp2p;
|
@@ -2377,14 +2405,6 @@ class ConnectionManager extends TypedEventEmitter {
|
|
2377
2405
|
}
|
2378
2406
|
return this.isP2PNetworkConnected;
|
2379
2407
|
}
|
2380
|
-
static create(peerId, libp2p, keepAliveOptions, pubsubTopics, relay, options) {
|
2381
|
-
let instance = ConnectionManager.instances.get(peerId);
|
2382
|
-
if (!instance) {
|
2383
|
-
instance = new ConnectionManager(libp2p, keepAliveOptions, pubsubTopics, relay, options);
|
2384
|
-
ConnectionManager.instances.set(peerId, instance);
|
2385
|
-
}
|
2386
|
-
return instance;
|
2387
|
-
}
|
2388
2408
|
stop() {
|
2389
2409
|
this.keepAliveManager.stopAll();
|
2390
2410
|
this.libp2p.removeEventListener("peer:connect", this.onEventHandlers["peer:connect"]);
|
@@ -2451,21 +2471,25 @@ class ConnectionManager extends TypedEventEmitter {
|
|
2451
2471
|
}
|
2452
2472
|
};
|
2453
2473
|
}
|
2454
|
-
constructor(
|
2474
|
+
constructor(options) {
|
2455
2475
|
super();
|
2456
|
-
this.
|
2457
|
-
this.
|
2458
|
-
this.configuredPubsubTopics = configuredPubsubTopics;
|
2476
|
+
this.libp2p = options.libp2p;
|
2477
|
+
this.pubsubTopics = options.pubsubTopics;
|
2459
2478
|
this.options = {
|
2460
2479
|
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
|
2461
2480
|
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
|
2462
2481
|
maxParallelDials: DEFAULT_MAX_PARALLEL_DIALS,
|
2463
|
-
|
2482
|
+
pingKeepAlive: DEFAULT_PING_KEEP_ALIVE_SEC,
|
2483
|
+
relayKeepAlive: DEFAULT_RELAY_KEEP_ALIVE_SEC,
|
2484
|
+
...options.config
|
2464
2485
|
};
|
2465
2486
|
this.keepAliveManager = new KeepAliveManager({
|
2466
|
-
relay,
|
2467
|
-
libp2p,
|
2468
|
-
options:
|
2487
|
+
relay: options.relay,
|
2488
|
+
libp2p: options.libp2p,
|
2489
|
+
options: {
|
2490
|
+
pingKeepAlive: this.options.pingKeepAlive,
|
2491
|
+
relayKeepAlive: this.options.relayKeepAlive
|
2492
|
+
}
|
2469
2493
|
});
|
2470
2494
|
this.startEventListeners()
|
2471
2495
|
.then(() => log$1.info(`Connection Manager is now running`))
|
@@ -2475,6 +2499,24 @@ class ConnectionManager extends TypedEventEmitter {
|
|
2475
2499
|
// we will dial the peers in peerStore ONCE before we start to listen to the `peer:discovery` events within the ConnectionManager
|
2476
2500
|
this.dialPeerStorePeers().catch((error) => log$1.error(`Unexpected error while dialing peer store peers`, error));
|
2477
2501
|
}
|
2502
|
+
async getConnectedPeers(codec) {
|
2503
|
+
const peerIDs = this.libp2p.getPeers();
|
2504
|
+
if (peerIDs.length === 0) {
|
2505
|
+
return [];
|
2506
|
+
}
|
2507
|
+
const peers = await Promise.all(peerIDs.map(async (id) => {
|
2508
|
+
try {
|
2509
|
+
return await this.libp2p.peerStore.get(id);
|
2510
|
+
}
|
2511
|
+
catch (e) {
|
2512
|
+
return null;
|
2513
|
+
}
|
2514
|
+
}));
|
2515
|
+
return peers
|
2516
|
+
.filter((p) => !!p)
|
2517
|
+
.filter((p) => (codec ? p.protocols.includes(codec) : true))
|
2518
|
+
.sort((left, right) => getPeerPing(left) - getPeerPing(right));
|
2519
|
+
}
|
2478
2520
|
async dialPeerStorePeers() {
|
2479
2521
|
const peerInfos = await this.libp2p.peerStore.all();
|
2480
2522
|
const dialPromises = [];
|
@@ -2690,7 +2732,7 @@ class ConnectionManager extends TypedEventEmitter {
|
|
2690
2732
|
const isSameShard = await this.isPeerTopicConfigured(peerId);
|
2691
2733
|
if (!isSameShard) {
|
2692
2734
|
const shardInfo = await this.getPeerShardInfo(peerId, this.libp2p.peerStore);
|
2693
|
-
log$1.warn(`Discovered peer ${peerId.toString()} with ShardInfo ${shardInfo} is not part of any of the configured pubsub topics (${this.
|
2735
|
+
log$1.warn(`Discovered peer ${peerId.toString()} with ShardInfo ${shardInfo} is not part of any of the configured pubsub topics (${this.pubsubTopics}).
|
2694
2736
|
Not dialing.`);
|
2695
2737
|
return false;
|
2696
2738
|
}
|
@@ -2751,7 +2793,7 @@ class ConnectionManager extends TypedEventEmitter {
|
|
2751
2793
|
if (!shardInfo)
|
2752
2794
|
return true;
|
2753
2795
|
const pubsubTopics = shardInfoToPubsubTopics(shardInfo);
|
2754
|
-
const isTopicConfigured = pubsubTopics.some((topic) => this.
|
2796
|
+
const isTopicConfigured = pubsubTopics.some((topic) => this.pubsubTopics.includes(topic));
|
2755
2797
|
return isTopicConfigured;
|
2756
2798
|
}
|
2757
2799
|
async getPeerShardInfo(peerId, peerStore) {
|
@@ -2875,7 +2917,7 @@ class Metadata extends BaseProtocol {
|
|
2875
2917
|
libp2pComponents;
|
2876
2918
|
handshakesConfirmed = new Map();
|
2877
2919
|
constructor(pubsubTopics, libp2p) {
|
2878
|
-
super(MetadataCodec, libp2p.components,
|
2920
|
+
super(MetadataCodec, libp2p.components, pubsubTopics);
|
2879
2921
|
this.pubsubTopics = pubsubTopics;
|
2880
2922
|
this.libp2pComponents = libp2p;
|
2881
2923
|
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
@@ -2979,4 +3021,4 @@ function wakuMetadata(pubsubTopics) {
|
|
2979
3021
|
return (components) => new Metadata(pubsubTopics, components);
|
2980
3022
|
}
|
2981
3023
|
|
2982
|
-
export { ConnectionManager, FilterCodecs, FilterCore,
|
3024
|
+
export { ConnectionManager, FilterCodecs, FilterCore, LightPushCodec, LightPushCore, MetadataCodec, StoreCodec, StoreCore, createEncoder, getHealthManager, index$3 as message, wakuMetadata, index$2 as waku_filter, index$1 as waku_light_push, index as waku_store };
|
@@ -1,2 +1,2 @@
|
|
1
|
-
export { B as BaseProtocol } from '../base_protocol-
|
2
|
-
import '../index-
|
1
|
+
export { B as BaseProtocol } from '../base_protocol-DxFKDXX2.js';
|
2
|
+
import '../index-yLOEQnIE.js';
|
@@ -1,2 +1,2 @@
|
|
1
|
-
export { D as DecodedMessage,
|
2
|
-
import '../../index-
|
1
|
+
export { D as DecodedMessage, o as Decoder, E as Encoder, V as Version, m as createDecoder, i as createEncoder, n as proto } from '../../version_0-Che4t3mN.js';
|
2
|
+
import '../../index-yLOEQnIE.js';
|