@waku/core 0.0.21 → 0.0.23
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 +48 -0
- package/bundle/base_protocol-84d9b670.js +1198 -0
- package/bundle/index.js +1163 -1281
- package/bundle/lib/base_protocol.js +2 -116
- package/bundle/lib/message/version_0.js +1 -1
- package/bundle/lib/predefined_bootstrap_nodes.js +6 -6
- package/bundle/{version_0-86411fdf.js → version_0-74b4b9db.js} +875 -794
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +18 -5
- package/dist/lib/base_protocol.js +25 -8
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +3 -5
- package/dist/lib/connection_manager.js +53 -45
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/filter_rpc.js +4 -4
- package/dist/lib/filter/index.d.ts +4 -0
- package/dist/lib/filter/index.js +24 -27
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/filterPeers.d.ts +10 -0
- package/dist/lib/filterPeers.js +31 -0
- package/dist/lib/filterPeers.js.map +1 -0
- package/dist/lib/keep_alive_manager.d.ts +4 -6
- package/dist/lib/keep_alive_manager.js +27 -8
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.js +62 -33
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/push_rpc.js +2 -2
- package/dist/lib/message/version_0.d.ts +1 -1
- package/dist/lib/message/version_0.js +3 -3
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/predefined_bootstrap_nodes.js +6 -6
- package/dist/lib/store/history_rpc.js +3 -3
- package/dist/lib/store/index.d.ts +0 -5
- package/dist/lib/store/index.js +54 -37
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +15 -0
- package/dist/lib/stream_manager.js +53 -0
- package/dist/lib/stream_manager.js.map +1 -0
- package/dist/lib/to_proto_message.js +1 -1
- package/dist/lib/waku.d.ts +2 -2
- package/dist/lib/waku.js +1 -1
- package/package.json +16 -22
- package/src/index.ts +7 -13
- package/src/lib/base_protocol.ts +49 -18
- package/src/lib/connection_manager.ts +82 -66
- package/src/lib/filter/filter_rpc.ts +4 -4
- package/src/lib/filter/index.ts +32 -39
- package/src/lib/filterPeers.ts +43 -0
- package/src/lib/keep_alive_manager.ts +34 -14
- package/src/lib/light_push/index.ts +103 -47
- package/src/lib/light_push/push_rpc.ts +2 -2
- package/src/lib/message/version_0.ts +8 -5
- package/src/lib/predefined_bootstrap_nodes.ts +7 -7
- package/src/lib/store/history_rpc.ts +4 -4
- package/src/lib/store/index.ts +70 -51
- package/src/lib/stream_manager.ts +69 -0
- package/src/lib/to_proto_message.ts +1 -1
- package/src/lib/wait_for_remote_peer.ts +1 -1
- package/src/lib/waku.ts +3 -3
package/dist/lib/store/index.js
CHANGED
@@ -23,10 +23,45 @@ export { PageDirection };
|
|
23
23
|
*/
|
24
24
|
class Store extends BaseProtocol {
|
25
25
|
options;
|
26
|
+
NUM_PEERS_PROTOCOL = 1;
|
26
27
|
constructor(libp2p, options) {
|
27
28
|
super(StoreCodec, libp2p.components);
|
28
29
|
this.options = options ?? {};
|
29
30
|
}
|
31
|
+
/**
|
32
|
+
* Processes messages based on the provided callback and options.
|
33
|
+
* @private
|
34
|
+
*/
|
35
|
+
async processMessages(messages, callback, options) {
|
36
|
+
let abort = false;
|
37
|
+
const messagesOrUndef = await Promise.all(messages);
|
38
|
+
let processedMessages = messagesOrUndef.filter(isDefined);
|
39
|
+
if (this.shouldReverseOrder(options)) {
|
40
|
+
processedMessages = processedMessages.reverse();
|
41
|
+
}
|
42
|
+
await Promise.all(processedMessages.map(async (msg) => {
|
43
|
+
if (msg && !abort) {
|
44
|
+
abort = Boolean(await callback(msg));
|
45
|
+
}
|
46
|
+
}));
|
47
|
+
return abort;
|
48
|
+
}
|
49
|
+
/**
|
50
|
+
* Determines whether to reverse the order of messages based on the provided options.
|
51
|
+
*
|
52
|
+
* Messages in pages are ordered from oldest (first) to most recent (last).
|
53
|
+
* https://github.com/vacp2p/rfc/issues/533
|
54
|
+
*
|
55
|
+
* @private
|
56
|
+
*/
|
57
|
+
shouldReverseOrder(options) {
|
58
|
+
return (typeof options?.pageDirection === "undefined" ||
|
59
|
+
options?.pageDirection === PageDirection.BACKWARD);
|
60
|
+
}
|
61
|
+
/**
|
62
|
+
* @deprecated Use `queryWithOrderedCallback` instead
|
63
|
+
**/
|
64
|
+
queryOrderedCallback = this.queryWithOrderedCallback;
|
30
65
|
/**
|
31
66
|
* Do a query to a Waku Store to retrieve historical/missed messages.
|
32
67
|
*
|
@@ -44,31 +79,16 @@ class Store extends BaseProtocol {
|
|
44
79
|
* or if an error is encountered when processing the reply,
|
45
80
|
* or if two decoders with the same content topic are passed.
|
46
81
|
*/
|
47
|
-
async
|
48
|
-
let abort = false;
|
82
|
+
async queryWithOrderedCallback(decoders, callback, options) {
|
49
83
|
for await (const promises of this.queryGenerator(decoders, options)) {
|
50
|
-
if (
|
84
|
+
if (await this.processMessages(promises, callback, options))
|
51
85
|
break;
|
52
|
-
const messagesOrUndef = await Promise.all(promises);
|
53
|
-
let messages = messagesOrUndef.filter(isDefined);
|
54
|
-
// Messages in pages are ordered from oldest (first) to most recent (last).
|
55
|
-
// https://github.com/vacp2p/rfc/issues/533
|
56
|
-
if (typeof options?.pageDirection === "undefined" ||
|
57
|
-
options?.pageDirection === PageDirection.BACKWARD) {
|
58
|
-
messages = messages.reverse();
|
59
|
-
}
|
60
|
-
await Promise.all(messages.map(async (msg) => {
|
61
|
-
if (msg && !abort) {
|
62
|
-
abort = Boolean(await callback(msg));
|
63
|
-
}
|
64
|
-
}));
|
65
86
|
}
|
66
87
|
}
|
67
88
|
/**
|
68
89
|
* Do a query to a Waku Store to retrieve historical/missed messages.
|
69
|
-
*
|
70
90
|
* The callback function takes a `Promise<WakuMessage>` in input,
|
71
|
-
* useful if messages
|
91
|
+
* useful if messages need to be decrypted and performance matters.
|
72
92
|
*
|
73
93
|
* The order of the messages passed to the callback is as follows:
|
74
94
|
* - within a page, messages are expected to be ordered from oldest to most recent
|
@@ -82,18 +102,18 @@ class Store extends BaseProtocol {
|
|
82
102
|
* or if an error is encountered when processing the reply,
|
83
103
|
* or if two decoders with the same content topic are passed.
|
84
104
|
*/
|
85
|
-
async
|
105
|
+
async queryWithPromiseCallback(decoders, callback, options) {
|
86
106
|
let abort = false;
|
87
|
-
let promises = [];
|
88
107
|
for await (const page of this.queryGenerator(decoders, options)) {
|
89
|
-
const _promises = page.map(async (
|
90
|
-
if (
|
91
|
-
|
92
|
-
|
108
|
+
const _promises = page.map(async (msgPromise) => {
|
109
|
+
if (abort)
|
110
|
+
return;
|
111
|
+
abort = Boolean(await callback(msgPromise));
|
93
112
|
});
|
94
|
-
|
113
|
+
await Promise.all(_promises);
|
114
|
+
if (abort)
|
115
|
+
break;
|
95
116
|
}
|
96
|
-
await Promise.all(promises);
|
97
117
|
}
|
98
118
|
/**
|
99
119
|
* Do a query to a Waku Store to retrieve historical/missed messages.
|
@@ -105,9 +125,6 @@ class Store extends BaseProtocol {
|
|
105
125
|
* as follows:
|
106
126
|
* - within a page, messages SHOULD be ordered from oldest to most recent
|
107
127
|
* - pages direction depends on { @link QueryOptions.pageDirection }
|
108
|
-
*
|
109
|
-
* However, there is no way to guarantee the behavior of the remote node.
|
110
|
-
*
|
111
128
|
* @throws If not able to reach a Waku Store peer to query,
|
112
129
|
* or if an error is encountered when processing the reply,
|
113
130
|
* or if two decoders with the same content topic are passed.
|
@@ -130,14 +147,14 @@ class Store extends BaseProtocol {
|
|
130
147
|
const queryOpts = Object.assign({
|
131
148
|
pubSubTopic: pubSubTopic,
|
132
149
|
pageDirection: PageDirection.BACKWARD,
|
133
|
-
pageSize: DefaultPageSize
|
150
|
+
pageSize: DefaultPageSize
|
134
151
|
}, options, { contentTopics, startTime, endTime });
|
135
|
-
log("Querying history with the following options",
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
for await (const messages of paginate(this.
|
152
|
+
log("Querying history with the following options", options);
|
153
|
+
const peer = (await this.getPeers({
|
154
|
+
numPeers: this.NUM_PEERS_PROTOCOL,
|
155
|
+
maxBootstrapPeers: 1
|
156
|
+
}))[0];
|
157
|
+
for await (const messages of paginate(this.getStream.bind(this, peer), queryOpts, decodersAsMap, options?.cursor)) {
|
141
158
|
yield messages;
|
142
159
|
}
|
143
160
|
}
|
@@ -215,7 +232,7 @@ export async function createCursor(message, pubsubTopic = DefaultPubSubTopic) {
|
|
215
232
|
digest,
|
216
233
|
pubsubTopic,
|
217
234
|
senderTime: messageTime,
|
218
|
-
receiverTime: messageTime
|
235
|
+
receiverTime: messageTime
|
219
236
|
};
|
220
237
|
}
|
221
238
|
export function wakuStore(init = {}) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/store/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/store/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAS9C,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD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AAG,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC;QAClE;QACE,uEAAuE;QACvE,gBAAgB;YAChB,aAAa;YACb,gBAAgB,GAAG,aAAa,EAChC;YACA,MAAM;SACP;KACF;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAAwB,EACxB,cAAsB,kBAAkB;IAExC,IACE,CAAC,OAAO;QACR,CAAC,OAAO,CAAC,SAAS;QAClB,CAAC,OAAO,CAAC,OAAO;QAChB,CAAC,OAAO,CAAC,YAAY,EACrB;QACA,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;KACvD;IAED,MAAM,iBAAiB,GAAG,WAAW,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAE5D,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,iBAAiB,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEpE,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAE1E,OAAO;QACL,MAAM;QACN,WAAW;QACX,UAAU,EAAE,WAAW;QACvB,YAAY,EAAE,WAAW;KAC1B,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CACvB,OAAuC,EAAE;IAEzC,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AACrD,CAAC"}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import type { Stream } from "@libp2p/interface/connection";
|
2
|
+
import { Peer } from "@libp2p/interface/peer-store";
|
3
|
+
import { Libp2p } from "@waku/interfaces";
|
4
|
+
export declare class StreamManager {
|
5
|
+
multicodec: string;
|
6
|
+
getConnections: Libp2p["getConnections"];
|
7
|
+
addEventListener: Libp2p["addEventListener"];
|
8
|
+
private streamPool;
|
9
|
+
private log;
|
10
|
+
constructor(multicodec: string, getConnections: Libp2p["getConnections"], addEventListener: Libp2p["addEventListener"]);
|
11
|
+
getStream(peer: Peer): Promise<Stream>;
|
12
|
+
private newStream;
|
13
|
+
private prepareNewStream;
|
14
|
+
private handlePeerUpdateStreamPool;
|
15
|
+
}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
import { selectConnection } from "@waku/utils/libp2p";
|
2
|
+
import debug from "debug";
|
3
|
+
export class StreamManager {
|
4
|
+
multicodec;
|
5
|
+
getConnections;
|
6
|
+
addEventListener;
|
7
|
+
streamPool;
|
8
|
+
log;
|
9
|
+
constructor(multicodec, getConnections, addEventListener) {
|
10
|
+
this.multicodec = multicodec;
|
11
|
+
this.getConnections = getConnections;
|
12
|
+
this.addEventListener = addEventListener;
|
13
|
+
this.log = debug(`waku:stream-manager:${multicodec}`);
|
14
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool.bind(this));
|
15
|
+
this.getStream = this.getStream.bind(this);
|
16
|
+
this.streamPool = new Map();
|
17
|
+
}
|
18
|
+
async getStream(peer) {
|
19
|
+
const peerIdStr = peer.id.toString();
|
20
|
+
const streamPromise = this.streamPool.get(peerIdStr);
|
21
|
+
if (!streamPromise) {
|
22
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
23
|
+
}
|
24
|
+
// We have the stream, let's remove it from the map
|
25
|
+
this.streamPool.delete(peerIdStr);
|
26
|
+
this.prepareNewStream(peer);
|
27
|
+
const stream = await streamPromise;
|
28
|
+
if (stream.status === "closed") {
|
29
|
+
return this.newStream(peer); // fallback by creating a new stream on the spot
|
30
|
+
}
|
31
|
+
return stream;
|
32
|
+
}
|
33
|
+
async newStream(peer) {
|
34
|
+
const connections = this.getConnections(peer.id);
|
35
|
+
const connection = selectConnection(connections);
|
36
|
+
if (!connection) {
|
37
|
+
throw new Error("Failed to get a connection to the peer");
|
38
|
+
}
|
39
|
+
return connection.newStream(this.multicodec);
|
40
|
+
}
|
41
|
+
prepareNewStream(peer) {
|
42
|
+
const streamPromise = this.newStream(peer);
|
43
|
+
this.streamPool.set(peer.id.toString(), streamPromise);
|
44
|
+
}
|
45
|
+
handlePeerUpdateStreamPool = (evt) => {
|
46
|
+
const peer = evt.detail.peer;
|
47
|
+
if (peer.protocols.includes(this.multicodec)) {
|
48
|
+
this.log(`Preemptively opening a stream to ${peer.id.toString()}`);
|
49
|
+
this.prepareNewStream(peer);
|
50
|
+
}
|
51
|
+
};
|
52
|
+
}
|
53
|
+
//# sourceMappingURL=stream_manager.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"stream_manager.js","sourceRoot":"","sources":["../../src/lib/stream_manager.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,OAAO,aAAa;IAKf;IACA;IACA;IAND,UAAU,CAA+B;IACzC,GAAG,CAAiB;IAE5B,YACS,UAAkB,EAClB,cAAwC,EACxC,gBAA4C;QAF5C,eAAU,GAAV,UAAU,CAAQ;QAClB,mBAAc,GAAd,cAAc,CAA0B;QACxC,qBAAgB,GAAhB,gBAAgB,CAA4B;QAEnD,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,uBAAuB,UAAU,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,gBAAgB,CACnB,aAAa,EACb,IAAI,CAAC,0BAA0B,CAAC,IAAI,CAAC,IAAI,CAAC,CAC3C,CAAC;QACF,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;IAC9B,CAAC;IAEM,KAAK,CAAC,SAAS,CAAC,IAAU;QAC/B,MAAM,SAAS,GAAG,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;QACrC,MAAM,aAAa,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG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C,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;YACnE,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;SAC7B;IACH,CAAC,CAAC;CACH"}
|
package/dist/lib/waku.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import type { Stream } from "@libp2p/interface
|
2
|
-
import { PeerId } from "@libp2p/interface
|
1
|
+
import type { Stream } from "@libp2p/interface/connection";
|
2
|
+
import { PeerId } from "@libp2p/interface/peer-id";
|
3
3
|
import { MultiaddrInput } from "@multiformats/multiaddr";
|
4
4
|
import type { IFilter, ILightPush, IRelay, IStore, Libp2p, Waku } from "@waku/interfaces";
|
5
5
|
import { Protocols } from "@waku/interfaces";
|
package/dist/lib/waku.js
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@waku/core",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.23",
|
4
4
|
"description": "TypeScript implementation of the Waku v2 protocol",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -72,12 +72,12 @@
|
|
72
72
|
"node": ">=16"
|
73
73
|
},
|
74
74
|
"dependencies": {
|
75
|
-
"@noble/hashes": "^1.3.
|
76
|
-
"@waku/interfaces": "0.0.
|
75
|
+
"@noble/hashes": "^1.3.2",
|
76
|
+
"@waku/interfaces": "0.0.18",
|
77
77
|
"@waku/proto": "0.0.5",
|
78
|
-
"@waku/utils": "0.0.
|
78
|
+
"@waku/utils": "0.0.11",
|
79
79
|
"debug": "^4.3.4",
|
80
|
-
"it-all": "^3.0.
|
80
|
+
"it-all": "^3.0.3",
|
81
81
|
"it-length-prefixed": "^9.0.1",
|
82
82
|
"it-pipe": "^3.0.1",
|
83
83
|
"p-event": "^5.0.1",
|
@@ -85,42 +85,36 @@
|
|
85
85
|
"uuid": "^9.0.0"
|
86
86
|
},
|
87
87
|
"devDependencies": {
|
88
|
-
"@libp2p/interface-connection": "^5.1.1",
|
89
|
-
"@libp2p/interface-libp2p": "^3.2.0",
|
90
|
-
"@libp2p/interface-peer-id": "^2.0.2",
|
91
|
-
"@libp2p/interface-peer-store": "^2.0.4",
|
92
|
-
"@libp2p/interface-registrar": "^2.0.12",
|
93
88
|
"@multiformats/multiaddr": "^12.0.0",
|
94
|
-
"@rollup/plugin-commonjs": "^
|
89
|
+
"@rollup/plugin-commonjs": "^25.0.4",
|
95
90
|
"@rollup/plugin-json": "^6.0.0",
|
96
|
-
"@rollup/plugin-node-resolve": "^15.0
|
97
|
-
"@types/chai": "^4.3.
|
98
|
-
"@types/debug": "^4.1.
|
91
|
+
"@rollup/plugin-node-resolve": "^15.1.0",
|
92
|
+
"@types/chai": "^4.3.5",
|
93
|
+
"@types/debug": "^4.1.8",
|
99
94
|
"@types/mocha": "^10.0.1",
|
100
|
-
"@types/uuid": "^9.0.
|
95
|
+
"@types/uuid": "^9.0.3",
|
101
96
|
"@waku/build-utils": "*",
|
102
97
|
"chai": "^4.3.7",
|
103
|
-
"cspell": "^
|
104
|
-
"fast-check": "^3.
|
98
|
+
"cspell": "^7.3.2",
|
99
|
+
"fast-check": "^3.12.0",
|
105
100
|
"ignore-loader": "^0.1.2",
|
106
101
|
"isomorphic-fetch": "^3.0.0",
|
107
102
|
"karma": "^6.4.1",
|
108
|
-
"karma-chrome-launcher": "^3.
|
103
|
+
"karma-chrome-launcher": "^3.2.0",
|
109
104
|
"karma-mocha": "^2.0.1",
|
110
105
|
"karma-webpack": "^5.0.0",
|
111
106
|
"mocha": "^10.2.0",
|
112
107
|
"npm-run-all": "^4.1.5",
|
113
|
-
"prettier": "^2.8.8",
|
114
108
|
"process": "^0.11.10",
|
115
|
-
"puppeteer": "^
|
116
|
-
"rollup": "^3.
|
109
|
+
"puppeteer": "^21.1.1",
|
110
|
+
"rollup": "^3.29.0",
|
117
111
|
"ts-loader": "^9.4.2",
|
118
112
|
"ts-node": "^10.9.1",
|
119
113
|
"typescript": "^5.0.4"
|
120
114
|
},
|
121
115
|
"peerDependencies": {
|
122
116
|
"@multiformats/multiaddr": "^12.0.0",
|
123
|
-
"libp2p": "^0.
|
117
|
+
"libp2p": "^0.46.3"
|
124
118
|
},
|
125
119
|
"peerDependenciesMeta": {
|
126
120
|
"@multiformats/multiaddr": {
|
package/src/index.ts
CHANGED
@@ -4,7 +4,7 @@ export { createEncoder, createDecoder } from "./lib/message/version_0.js";
|
|
4
4
|
export type {
|
5
5
|
Encoder,
|
6
6
|
Decoder,
|
7
|
-
DecodedMessage
|
7
|
+
DecodedMessage
|
8
8
|
} from "./lib/message/version_0.js";
|
9
9
|
export * as message from "./lib/message/index.js";
|
10
10
|
|
@@ -12,24 +12,18 @@ export * as waku from "./lib/waku.js";
|
|
12
12
|
export { WakuNode, WakuOptions } from "./lib/waku.js";
|
13
13
|
|
14
14
|
export * as waku_filter from "./lib/filter/index.js";
|
15
|
-
export { wakuFilter } from "./lib/filter/index.js";
|
15
|
+
export { wakuFilter, FilterCodecs } from "./lib/filter/index.js";
|
16
16
|
|
17
17
|
export * as waku_light_push from "./lib/light_push/index.js";
|
18
|
-
export { wakuLightPush
|
18
|
+
export { wakuLightPush } from "./lib/light_push/index.js";
|
19
19
|
|
20
20
|
export * as waku_store from "./lib/store/index.js";
|
21
|
-
|
22
|
-
|
23
|
-
wakuStore,
|
24
|
-
StoreCodec,
|
25
|
-
createCursor,
|
26
|
-
} from "./lib/store/index.js";
|
21
|
+
|
22
|
+
export { PageDirection, wakuStore, createCursor } from "./lib/store/index.js";
|
27
23
|
|
28
24
|
export { waitForRemotePeer } from "./lib/wait_for_remote_peer.js";
|
29
25
|
|
30
26
|
export { ConnectionManager } from "./lib/connection_manager.js";
|
31
27
|
|
32
|
-
export {
|
33
|
-
|
34
|
-
KeepAliveOptions,
|
35
|
-
} from "./lib/keep_alive_manager.js";
|
28
|
+
export { KeepAliveManager } from "./lib/keep_alive_manager.js";
|
29
|
+
export { StreamManager } from "./lib/stream_manager.js";
|
package/src/lib/base_protocol.ts
CHANGED
@@ -1,13 +1,12 @@
|
|
1
|
-
import type {
|
2
|
-
import type {
|
3
|
-
import type { PeerId } from "@libp2p/interface
|
4
|
-
import { Peer, PeerStore } from "@libp2p/interface
|
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";
|
5
5
|
import type { IBaseProtocol, Libp2pComponents } from "@waku/interfaces";
|
6
|
-
import {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
} from "@waku/utils/libp2p";
|
6
|
+
import { getPeersForProtocol, selectPeerForProtocol } from "@waku/utils/libp2p";
|
7
|
+
|
8
|
+
import { filterPeers } from "./filterPeers.js";
|
9
|
+
import { StreamManager } from "./stream_manager.js";
|
11
10
|
|
12
11
|
/**
|
13
12
|
* A class with predefined helpers, to be used as a base to implement Waku
|
@@ -16,14 +15,29 @@ import {
|
|
16
15
|
export class BaseProtocol implements IBaseProtocol {
|
17
16
|
public readonly addLibp2pEventListener: Libp2p["addEventListener"];
|
18
17
|
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
|
18
|
+
protected streamManager: StreamManager;
|
19
19
|
|
20
|
-
constructor(
|
20
|
+
constructor(
|
21
|
+
public multicodec: string,
|
22
|
+
private components: Libp2pComponents
|
23
|
+
) {
|
21
24
|
this.addLibp2pEventListener = components.events.addEventListener.bind(
|
22
25
|
components.events
|
23
26
|
);
|
24
27
|
this.removeLibp2pEventListener = components.events.removeEventListener.bind(
|
25
28
|
components.events
|
26
29
|
);
|
30
|
+
|
31
|
+
this.streamManager = new StreamManager(
|
32
|
+
multicodec,
|
33
|
+
components.connectionManager.getConnections.bind(
|
34
|
+
components.connectionManager
|
35
|
+
),
|
36
|
+
this.addLibp2pEventListener
|
37
|
+
);
|
38
|
+
}
|
39
|
+
protected async getStream(peer: Peer): Promise<Stream> {
|
40
|
+
return this.streamManager.getStream(peer);
|
27
41
|
}
|
28
42
|
|
29
43
|
public get peerStore(): PeerStore {
|
@@ -47,15 +61,32 @@ export class BaseProtocol implements IBaseProtocol {
|
|
47
61
|
);
|
48
62
|
return peer;
|
49
63
|
}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Retrieves a list of peers based on the specified criteria.
|
67
|
+
*
|
68
|
+
* @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
|
69
|
+
* @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
|
70
|
+
* @returns A Promise that resolves to an array of peers based on the specified criteria.
|
71
|
+
*/
|
72
|
+
protected async getPeers(
|
73
|
+
{
|
74
|
+
numPeers,
|
75
|
+
maxBootstrapPeers
|
76
|
+
}: {
|
77
|
+
numPeers: number;
|
78
|
+
maxBootstrapPeers: number;
|
79
|
+
} = {
|
80
|
+
maxBootstrapPeers: 1,
|
81
|
+
numPeers: 0
|
57
82
|
}
|
83
|
+
): Promise<Peer[]> {
|
84
|
+
// Retrieve all peers that support the protocol
|
85
|
+
const allPeersForProtocol = await getPeersForProtocol(this.peerStore, [
|
86
|
+
this.multicodec
|
87
|
+
]);
|
58
88
|
|
59
|
-
|
89
|
+
// Filter the peers based on the specified criteria
|
90
|
+
return filterPeers(allPeersForProtocol, numPeers, maxBootstrapPeers);
|
60
91
|
}
|
61
92
|
}
|