@waku/core 0.0.9 → 0.0.11
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 +78 -2
- package/bundle/browser-2f1afe46.js +726 -0
- package/bundle/index.js +9166 -12074
- package/bundle/lib/base_protocol.js +108 -0
- package/bundle/lib/message/topic_only_message.js +3 -2
- package/bundle/lib/message/version_0.js +3 -2
- package/bundle/peer_exchange-1229c8b0.js +4302 -0
- package/bundle/{topic_only_message-ece0fef9.js → topic_only_message-e8406994.js} +12 -8
- package/bundle/{version_0-b1fc527d.js → version_0-e9a6cfb0.js} +35 -35
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +21 -0
- package/dist/lib/base_protocol.js +33 -0
- package/dist/lib/base_protocol.js.map +1 -0
- package/dist/lib/connection_manager.d.ts +31 -0
- package/dist/lib/connection_manager.js +146 -0
- package/dist/lib/connection_manager.js.map +1 -0
- package/dist/lib/filter/filter_rpc.d.ts +8 -8
- package/dist/lib/filter/filter_rpc.js +6 -6
- package/dist/lib/filter/index.d.ts +5 -22
- package/dist/lib/filter/index.js +31 -86
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/group_by.d.ts +1 -1
- package/dist/lib/group_by.js.map +1 -1
- package/dist/lib/keep_alive_manager.d.ts +17 -0
- package/dist/lib/keep_alive_manager.js +62 -0
- package/dist/lib/keep_alive_manager.js.map +1 -0
- package/dist/lib/light_push/index.d.ts +3 -19
- package/dist/lib/light_push/index.js +13 -39
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/push_rpc.d.ts +5 -5
- package/dist/lib/light_push/push_rpc.js +6 -6
- package/dist/lib/message/topic_only_message.d.ts +5 -3
- package/dist/lib/message/topic_only_message.js +8 -5
- package/dist/lib/message/topic_only_message.js.map +1 -1
- package/dist/lib/message/version_0.d.ts +12 -12
- package/dist/lib/message/version_0.js +29 -30
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/predefined_bootstrap_nodes.js +1 -1
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
- package/dist/lib/relay/index.d.ts +4 -18
- package/dist/lib/relay/index.js +47 -26
- package/dist/lib/relay/index.js.map +1 -1
- package/dist/lib/relay/message_validator.d.ts +4 -0
- package/dist/lib/relay/message_validator.js +25 -0
- package/dist/lib/relay/message_validator.js.map +1 -0
- package/dist/lib/store/history_rpc.d.ts +4 -4
- package/dist/lib/store/history_rpc.js +9 -9
- package/dist/lib/store/history_rpc.js.map +1 -1
- package/dist/lib/store/index.d.ts +4 -25
- package/dist/lib/store/index.js +20 -37
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/to_proto_message.js +3 -2
- package/dist/lib/to_proto_message.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js +12 -23
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/dist/lib/waku.d.ts +8 -15
- package/dist/lib/waku.js +34 -97
- package/dist/lib/waku.js.map +1 -1
- package/package.json +50 -61
- package/src/index.ts +11 -3
- package/src/lib/base_protocol.ts +47 -0
- package/src/lib/connection_manager.ts +220 -0
- package/src/lib/filter/filter_rpc.ts +10 -10
- package/src/lib/filter/index.ts +52 -147
- package/src/lib/group_by.ts +1 -1
- package/src/lib/keep_alive_manager.ts +89 -0
- package/src/lib/light_push/index.ts +17 -78
- package/src/lib/light_push/push_rpc.ts +9 -9
- package/src/lib/message/topic_only_message.ts +11 -5
- package/src/lib/message/version_0.ts +42 -37
- package/src/lib/predefined_bootstrap_nodes.ts +1 -1
- package/src/lib/relay/index.ts +77 -52
- package/src/lib/relay/message_validator.ts +35 -0
- package/src/lib/store/history_rpc.ts +12 -12
- package/src/lib/store/index.ts +30 -84
- package/src/lib/to_proto_message.ts +3 -2
- package/src/lib/wait_for_remote_peer.ts +13 -29
- package/src/lib/waku.ts +54 -136
- package/bundle/peer_exchange-53df2b11.js +0 -11824
- package/dist/lib/random_subset.d.ts +0 -4
- package/dist/lib/random_subset.js +0 -25
- package/dist/lib/random_subset.js.map +0 -1
- package/src/lib/random_subset.ts +0 -30
@@ -0,0 +1,146 @@
|
|
1
|
+
import { Tags } from "@waku/interfaces";
|
2
|
+
import debug from "debug";
|
3
|
+
import { KeepAliveManager } from "./keep_alive_manager.js";
|
4
|
+
const log = debug("waku:connection-manager");
|
5
|
+
export const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
|
6
|
+
export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
|
7
|
+
export class ConnectionManager {
|
8
|
+
static create(peerId, libp2p, keepAliveOptions, relay, options) {
|
9
|
+
let instance = ConnectionManager.instances.get(peerId);
|
10
|
+
if (!instance) {
|
11
|
+
instance = new ConnectionManager(libp2p, keepAliveOptions, relay, options);
|
12
|
+
ConnectionManager.instances.set(peerId, instance);
|
13
|
+
}
|
14
|
+
return instance;
|
15
|
+
}
|
16
|
+
constructor(libp2pComponents, keepAliveOptions, relay, options) {
|
17
|
+
this.dialAttemptsForPeer = new Map();
|
18
|
+
this.onEventHandlers = {
|
19
|
+
"peer:discovery": async (evt) => {
|
20
|
+
const { id: peerId } = evt.detail;
|
21
|
+
if (!(await this.shouldDialPeer(peerId)))
|
22
|
+
return;
|
23
|
+
this.dialPeer(peerId).catch((err) => log(`Error dialing peer ${peerId.toString()} : ${err}`));
|
24
|
+
},
|
25
|
+
"peer:connect": (evt) => {
|
26
|
+
{
|
27
|
+
this.keepAliveManager.start(evt.detail.remotePeer, this.libp2pComponents.ping.bind(this));
|
28
|
+
}
|
29
|
+
},
|
30
|
+
"peer:disconnect": () => {
|
31
|
+
return (evt) => {
|
32
|
+
this.keepAliveManager.stop(evt.detail.remotePeer);
|
33
|
+
};
|
34
|
+
},
|
35
|
+
};
|
36
|
+
this.libp2pComponents = libp2pComponents;
|
37
|
+
this.options = {
|
38
|
+
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
|
39
|
+
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
|
40
|
+
...options,
|
41
|
+
};
|
42
|
+
this.keepAliveManager = new KeepAliveManager(keepAliveOptions, relay);
|
43
|
+
this.run()
|
44
|
+
.then(() => log(`Connection Manager is now running`))
|
45
|
+
.catch((error) => log(`Unexpected error while running service`, error));
|
46
|
+
}
|
47
|
+
async run() {
|
48
|
+
// start event listeners
|
49
|
+
this.startPeerDiscoveryListener();
|
50
|
+
this.startPeerConnectionListener();
|
51
|
+
this.startPeerDisconnectionListener();
|
52
|
+
}
|
53
|
+
stop() {
|
54
|
+
this.keepAliveManager.stopAll();
|
55
|
+
this.libp2pComponents.removeEventListener("peer:connect", this.onEventHandlers["peer:connect"]);
|
56
|
+
this.libp2pComponents.removeEventListener("peer:disconnect", this.onEventHandlers["peer:disconnect"]);
|
57
|
+
this.libp2pComponents.removeEventListener("peer:discovery", this.onEventHandlers["peer:discovery"]);
|
58
|
+
}
|
59
|
+
async dialPeer(peerId) {
|
60
|
+
let dialAttempt = 0;
|
61
|
+
while (dialAttempt <= this.options.maxDialAttemptsForPeer) {
|
62
|
+
try {
|
63
|
+
log(`Dialing peer ${peerId.toString()}`);
|
64
|
+
await this.libp2pComponents.dial(peerId);
|
65
|
+
const tags = await this.getTagNamesForPeer(peerId);
|
66
|
+
// add tag to connection describing discovery mechanism
|
67
|
+
// don't add duplicate tags
|
68
|
+
this.libp2pComponents
|
69
|
+
.getConnections(peerId)
|
70
|
+
.forEach((conn) => (conn.tags = Array.from(new Set([...conn.tags, ...tags]))));
|
71
|
+
this.dialAttemptsForPeer.delete(peerId.toString());
|
72
|
+
return;
|
73
|
+
}
|
74
|
+
catch (error) {
|
75
|
+
log(`
|
76
|
+
Error dialing peer ${peerId.toString()}`);
|
77
|
+
dialAttempt = this.dialAttemptsForPeer.get(peerId.toString()) ?? 1;
|
78
|
+
this.dialAttemptsForPeer.set(peerId.toString(), dialAttempt + 1);
|
79
|
+
if (dialAttempt <= this.options.maxDialAttemptsForPeer) {
|
80
|
+
log(`Reattempting dial (${dialAttempt})`);
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
try {
|
85
|
+
log(`Deleting undialable peer ${peerId.toString()} from peer store`);
|
86
|
+
return await this.libp2pComponents.peerStore.delete(peerId);
|
87
|
+
}
|
88
|
+
catch (error) {
|
89
|
+
throw `Error deleting undialable peer ${peerId.toString()} from peer store - ${error}`;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
startPeerDiscoveryListener() {
|
93
|
+
this.libp2pComponents.peerStore.addEventListener("peer", this.onEventHandlers["peer:discovery"]);
|
94
|
+
}
|
95
|
+
startPeerConnectionListener() {
|
96
|
+
this.libp2pComponents.addEventListener("peer:connect", this.onEventHandlers["peer:connect"]);
|
97
|
+
}
|
98
|
+
startPeerDisconnectionListener() {
|
99
|
+
// TODO: ensure that these following issues are updated and confirmed
|
100
|
+
/**
|
101
|
+
* NOTE: Event is not being emitted on closing nor losing a connection.
|
102
|
+
* @see https://github.com/libp2p/js-libp2p/issues/939
|
103
|
+
* @see https://github.com/status-im/js-waku/issues/252
|
104
|
+
*
|
105
|
+
* >This event will be triggered anytime we are disconnected from another peer,
|
106
|
+
* >regardless of the circumstances of that disconnection.
|
107
|
+
* >If we happen to have multiple connections to a peer,
|
108
|
+
* >this event will **only** be triggered when the last connection is closed.
|
109
|
+
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
|
110
|
+
*/
|
111
|
+
this.libp2pComponents.addEventListener("peer:disconnect", this.onEventHandlers["peer:disconnect"]);
|
112
|
+
}
|
113
|
+
/**
|
114
|
+
* Checks if the peer is dialable based on the following conditions:
|
115
|
+
* 1. If the peer is a bootstrap peer, it is only dialable if the number of current bootstrap connections is less than the max allowed.
|
116
|
+
* 2. If the peer is not a bootstrap peer
|
117
|
+
*/
|
118
|
+
async shouldDialPeer(peerId) {
|
119
|
+
const isConnected = this.libp2pComponents.getConnections(peerId).length > 0;
|
120
|
+
if (isConnected)
|
121
|
+
return false;
|
122
|
+
const isBootstrap = (await this.getTagNamesForPeer(peerId)).some((tagName) => tagName === Tags.BOOTSTRAP);
|
123
|
+
if (isBootstrap) {
|
124
|
+
const currentBootstrapConnections = this.libp2pComponents
|
125
|
+
.getConnections()
|
126
|
+
.filter((conn) => {
|
127
|
+
conn.tags.find((name) => name === Tags.BOOTSTRAP);
|
128
|
+
}).length;
|
129
|
+
if (currentBootstrapConnections < this.options.maxBootstrapPeersAllowed)
|
130
|
+
return true;
|
131
|
+
}
|
132
|
+
else {
|
133
|
+
return true;
|
134
|
+
}
|
135
|
+
return false;
|
136
|
+
}
|
137
|
+
/**
|
138
|
+
* Fetches the tag names for a given peer
|
139
|
+
*/
|
140
|
+
async getTagNamesForPeer(peerId) {
|
141
|
+
const tags = (await this.libp2pComponents.peerStore.getTags(peerId)).map((tag) => tag.name);
|
142
|
+
return tags;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
ConnectionManager.instances = new Map();
|
146
|
+
//# sourceMappingURL=connection_manager.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"connection_manager.js","sourceRoot":"","sources":["../../src/lib/connection_manager.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,gBAAgB,EAAoB,MAAM,yBAAyB,CAAC;AAE7E,MAAM,GAAG,GAAG,KAAK,CAAC,yBAAyB,CAAC,CAAC;AAE7C,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAAC,CAAC;AACrD,MAAM,CAAC,MAAM,kCAAkC,GAAG,CAAC,CAAC;AAEpD,MAAM,OAAO,iBAAiB;IAOrB,MAAM,CAAC,MAAM,CAClB,MAAc,EACd,MAAc,EACd,gBAAkC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;YACZ,IAAI,2BAA2B,GAAG,IAAI,CAAC,OAAO,CAAC,wBAAwB;gBACrE,OAAO,IAAI,CAAC;SACf;aAAM;YACL,OAAO,IAAI,CAAC;SACb;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,kBAAkB,CAAC,MAAc;QAC7C,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CACtE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAClB,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;;AA1Mc,2BAAS,GAAG,IAAI,GAAG,EAA6B,CAAC"}
|
@@ -1,25 +1,25 @@
|
|
1
1
|
import { proto_filter as proto } from "@waku/proto";
|
2
|
-
export
|
2
|
+
export type ContentFilter = {
|
3
3
|
contentTopic: string;
|
4
4
|
};
|
5
5
|
/**
|
6
6
|
* FilterRPC represents a message conforming to the Waku Filter protocol
|
7
7
|
*/
|
8
|
-
export declare class
|
9
|
-
proto: proto.
|
10
|
-
constructor(proto: proto.
|
11
|
-
static createRequest(topic: string, contentFilters: ContentFilter[], requestId?: string, subscribe?: boolean):
|
8
|
+
export declare class FilterRpc {
|
9
|
+
proto: proto.FilterRpc;
|
10
|
+
constructor(proto: proto.FilterRpc);
|
11
|
+
static createRequest(topic: string, contentFilters: ContentFilter[], requestId?: string, subscribe?: boolean): FilterRpc;
|
12
12
|
/**
|
13
13
|
*
|
14
14
|
* @param bytes Uint8Array of bytes from a FilterRPC message
|
15
|
-
* @returns
|
15
|
+
* @returns FilterRpc
|
16
16
|
*/
|
17
|
-
static decode(bytes: Uint8Array):
|
17
|
+
static decode(bytes: Uint8Array): FilterRpc;
|
18
18
|
/**
|
19
19
|
* Encode the current FilterRPC request to bytes
|
20
20
|
* @returns Uint8Array
|
21
21
|
*/
|
22
22
|
encode(): Uint8Array;
|
23
23
|
get push(): proto.MessagePush | undefined;
|
24
|
-
get requestId(): string
|
24
|
+
get requestId(): string;
|
25
25
|
}
|
@@ -3,12 +3,12 @@ import { v4 as uuid } from "uuid";
|
|
3
3
|
/**
|
4
4
|
* FilterRPC represents a message conforming to the Waku Filter protocol
|
5
5
|
*/
|
6
|
-
export class
|
6
|
+
export class FilterRpc {
|
7
7
|
constructor(proto) {
|
8
8
|
this.proto = proto;
|
9
9
|
}
|
10
10
|
static createRequest(topic, contentFilters, requestId, subscribe = true) {
|
11
|
-
return new
|
11
|
+
return new FilterRpc({
|
12
12
|
requestId: requestId || uuid(),
|
13
13
|
request: {
|
14
14
|
subscribe,
|
@@ -21,18 +21,18 @@ export class FilterRPC {
|
|
21
21
|
/**
|
22
22
|
*
|
23
23
|
* @param bytes Uint8Array of bytes from a FilterRPC message
|
24
|
-
* @returns
|
24
|
+
* @returns FilterRpc
|
25
25
|
*/
|
26
26
|
static decode(bytes) {
|
27
|
-
const res = proto.
|
28
|
-
return new
|
27
|
+
const res = proto.FilterRpc.decode(bytes);
|
28
|
+
return new FilterRpc(res);
|
29
29
|
}
|
30
30
|
/**
|
31
31
|
* Encode the current FilterRPC request to bytes
|
32
32
|
* @returns Uint8Array
|
33
33
|
*/
|
34
34
|
encode() {
|
35
|
-
return proto.
|
35
|
+
return proto.FilterRpc.encode(this.proto);
|
36
36
|
}
|
37
37
|
get push() {
|
38
38
|
return this.proto.push;
|
@@ -1,25 +1,8 @@
|
|
1
|
-
import type {
|
2
|
-
import type {
|
3
|
-
import type { Registrar } from "@libp2p/interface-registrar";
|
4
|
-
import type { IFilter } from "@waku/interfaces";
|
1
|
+
import type { Libp2p } from "@libp2p/interface-libp2p";
|
2
|
+
import type { IFilter, ProtocolCreateOptions } from "@waku/interfaces";
|
5
3
|
import { ContentFilter } from "./filter_rpc.js";
|
6
4
|
export { ContentFilter };
|
7
5
|
export declare const FilterCodec = "/vac/waku/filter/2.0.0-beta1";
|
8
|
-
export
|
9
|
-
|
10
|
-
|
11
|
-
connectionManager: ConnectionManager;
|
12
|
-
}
|
13
|
-
export interface CreateOptions {
|
14
|
-
/**
|
15
|
-
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
|
16
|
-
*
|
17
|
-
* The usage of the default pubsub topic is recommended.
|
18
|
-
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
19
|
-
*
|
20
|
-
* @default {@link DefaultPubSubTopic}
|
21
|
-
*/
|
22
|
-
pubSubTopic?: string;
|
23
|
-
}
|
24
|
-
export declare type UnsubscribeFunction = () => Promise<void>;
|
25
|
-
export declare function wakuFilter(init?: Partial<CreateOptions>): (components: FilterComponents) => IFilter;
|
6
|
+
export type UnsubscribeFunction = () => Promise<void>;
|
7
|
+
export type RequestID = string;
|
8
|
+
export declare function wakuFilter(init?: Partial<ProtocolCreateOptions>): (libp2p: Libp2p) => IFilter;
|
package/dist/lib/filter/index.js
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
import { getPeersForProtocol, selectConnection, selectPeerForProtocol, selectRandomPeer, } from "@waku/libp2p-utils";
|
2
1
|
import debug from "debug";
|
3
2
|
import all from "it-all";
|
4
3
|
import * as lp from "it-length-prefixed";
|
5
4
|
import { pipe } from "it-pipe";
|
5
|
+
import { BaseProtocol } from "../base_protocol.js";
|
6
6
|
import { DefaultPubSubTopic } from "../constants.js";
|
7
7
|
import { groupByContentTopic } from "../group_by.js";
|
8
8
|
import { toProtoMessage } from "../to_proto_message.js";
|
9
|
-
import {
|
9
|
+
import { FilterRpc } from "./filter_rpc.js";
|
10
10
|
export const FilterCodec = "/vac/waku/filter/2.0.0-beta1";
|
11
11
|
const log = debug("waku:filter");
|
12
12
|
/**
|
@@ -16,14 +16,14 @@ const log = debug("waku:filter");
|
|
16
16
|
* - https://github.com/status-im/go-waku/issues/245
|
17
17
|
* - https://github.com/status-im/nwaku/issues/948
|
18
18
|
*/
|
19
|
-
class Filter {
|
20
|
-
constructor(
|
21
|
-
|
19
|
+
class Filter extends BaseProtocol {
|
20
|
+
constructor(libp2p, options) {
|
21
|
+
super(FilterCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
|
22
|
+
this.libp2p = libp2p;
|
23
|
+
this.options = options ?? {};
|
22
24
|
this.subscriptions = new Map();
|
23
|
-
this.
|
24
|
-
|
25
|
-
this.components.registrar
|
26
|
-
.handle(FilterCodec, this.onRequest.bind(this))
|
25
|
+
this.libp2p
|
26
|
+
.handle(this.multicodec, this.onRequest.bind(this))
|
27
27
|
.catch((e) => log("Failed to register filter protocol", e));
|
28
28
|
}
|
29
29
|
/**
|
@@ -33,16 +33,13 @@ class Filter {
|
|
33
33
|
* @returns Unsubscribe function that can be used to end the subscription.
|
34
34
|
*/
|
35
35
|
async subscribe(decoders, callback, opts) {
|
36
|
-
const
|
37
|
-
const
|
38
|
-
const contentTopics = Array.from(groupedDecoders.keys());
|
36
|
+
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
37
|
+
const contentTopics = Array.from(groupByContentTopic(decoders).keys());
|
39
38
|
const contentFilters = contentTopics.map((contentTopic) => ({
|
40
39
|
contentTopic,
|
41
40
|
}));
|
42
|
-
const request =
|
41
|
+
const request = FilterRpc.createRequest(pubSubTopic, contentFilters, undefined, true);
|
43
42
|
const requestId = request.requestId;
|
44
|
-
if (!requestId)
|
45
|
-
throw new Error("Internal error: createRequest expected to set `requestId`");
|
46
43
|
const peer = await this.getPeer(opts?.peerId);
|
47
44
|
const stream = await this.newStream(peer);
|
48
45
|
try {
|
@@ -53,23 +50,19 @@ class Filter {
|
|
53
50
|
log("Error subscribing to peer ", peer.id.toString(), "for content topics", contentTopics, ": ", e);
|
54
51
|
throw e;
|
55
52
|
}
|
56
|
-
|
57
|
-
this.
|
53
|
+
const subscription = { callback, decoders, pubSubTopic };
|
54
|
+
this.subscriptions.set(requestId, subscription);
|
58
55
|
return async () => {
|
59
|
-
await this.unsubscribe(
|
60
|
-
this.
|
61
|
-
this.deleteCallback(requestId);
|
56
|
+
await this.unsubscribe(pubSubTopic, contentFilters, requestId, peer);
|
57
|
+
this.subscriptions.delete(requestId);
|
62
58
|
};
|
63
59
|
}
|
64
|
-
get peerStore() {
|
65
|
-
return this.components.peerStore;
|
66
|
-
}
|
67
60
|
onRequest(streamData) {
|
68
61
|
log("Receiving message push");
|
69
62
|
try {
|
70
63
|
pipe(streamData.stream, lp.decode(), async (source) => {
|
71
64
|
for await (const bytes of source) {
|
72
|
-
const res =
|
65
|
+
const res = FilterRpc.decode(bytes.slice());
|
73
66
|
if (res.requestId && res.push?.messages?.length) {
|
74
67
|
await this.pushMessages(res.requestId, res.push.messages);
|
75
68
|
}
|
@@ -85,9 +78,14 @@ class Filter {
|
|
85
78
|
}
|
86
79
|
}
|
87
80
|
async pushMessages(requestId, messages) {
|
88
|
-
const
|
89
|
-
if (!
|
90
|
-
log(`No
|
81
|
+
const subscription = this.subscriptions.get(requestId);
|
82
|
+
if (!subscription) {
|
83
|
+
log(`No subscription locally registered for request ID ${requestId}`);
|
84
|
+
return;
|
85
|
+
}
|
86
|
+
const { decoders, callback, pubSubTopic } = subscription;
|
87
|
+
if (!decoders || !decoders.length) {
|
88
|
+
log(`No decoder registered for request ID ${requestId}`);
|
91
89
|
return;
|
92
90
|
}
|
93
91
|
for (const protoMessage of messages) {
|
@@ -96,59 +94,27 @@ class Filter {
|
|
96
94
|
log("Message has no content topic, skipping");
|
97
95
|
return;
|
98
96
|
}
|
99
|
-
|
100
|
-
if (!decoders) {
|
101
|
-
log("No decoder for", contentTopic);
|
102
|
-
return;
|
103
|
-
}
|
104
|
-
let msg;
|
97
|
+
let didDecodeMsg = false;
|
105
98
|
// We don't want to wait for decoding failure, just attempt to decode
|
106
99
|
// all messages and do the call back on the one that works
|
107
100
|
// noinspection ES6MissingAwait
|
108
101
|
decoders.forEach(async (dec) => {
|
109
|
-
if (
|
102
|
+
if (didDecodeMsg)
|
110
103
|
return;
|
111
|
-
const decoded = await dec.fromProtoObj(toProtoMessage(protoMessage));
|
104
|
+
const decoded = await dec.fromProtoObj(pubSubTopic, toProtoMessage(protoMessage));
|
112
105
|
if (!decoded) {
|
113
106
|
log("Not able to decode message");
|
114
107
|
return;
|
115
108
|
}
|
116
109
|
// This is just to prevent more decoding attempt
|
117
110
|
// TODO: Could be better if we were to abort promises
|
118
|
-
|
111
|
+
didDecodeMsg = Boolean(decoded);
|
119
112
|
await callback(decoded);
|
120
113
|
});
|
121
114
|
}
|
122
115
|
}
|
123
|
-
addCallback(requestId, callback) {
|
124
|
-
this.subscriptions.set(requestId, callback);
|
125
|
-
}
|
126
|
-
deleteCallback(requestId) {
|
127
|
-
this.subscriptions.delete(requestId);
|
128
|
-
}
|
129
|
-
addDecoders(decoders) {
|
130
|
-
decoders.forEach((decoders, contentTopic) => {
|
131
|
-
const currDecs = this.decoders.get(contentTopic);
|
132
|
-
if (!currDecs) {
|
133
|
-
this.decoders.set(contentTopic, new Set(decoders));
|
134
|
-
}
|
135
|
-
else {
|
136
|
-
this.decoders.set(contentTopic, new Set([...currDecs, ...decoders]));
|
137
|
-
}
|
138
|
-
});
|
139
|
-
}
|
140
|
-
deleteDecoders(decoders) {
|
141
|
-
decoders.forEach((decoders, contentTopic) => {
|
142
|
-
const currDecs = this.decoders.get(contentTopic);
|
143
|
-
if (currDecs) {
|
144
|
-
decoders.forEach((dec) => {
|
145
|
-
currDecs.delete(dec);
|
146
|
-
});
|
147
|
-
}
|
148
|
-
});
|
149
|
-
}
|
150
116
|
async unsubscribe(topic, contentFilters, requestId, peer) {
|
151
|
-
const unsubscribeRequest =
|
117
|
+
const unsubscribeRequest = FilterRpc.createRequest(topic, contentFilters, requestId, false);
|
152
118
|
const stream = await this.newStream(peer);
|
153
119
|
try {
|
154
120
|
await pipe([unsubscribeRequest.encode()], lp.encode(), stream.sink);
|
@@ -158,29 +124,8 @@ class Filter {
|
|
158
124
|
throw e;
|
159
125
|
}
|
160
126
|
}
|
161
|
-
async newStream(peer) {
|
162
|
-
const connections = this.components.connectionManager.getConnections(peer.id);
|
163
|
-
const connection = selectConnection(connections);
|
164
|
-
if (!connection) {
|
165
|
-
throw new Error("Failed to get a connection to the peer");
|
166
|
-
}
|
167
|
-
return connection.newStream(FilterCodec);
|
168
|
-
}
|
169
|
-
async getPeer(peerId) {
|
170
|
-
const res = await selectPeerForProtocol(this.components.peerStore, [FilterCodec], peerId);
|
171
|
-
if (!res) {
|
172
|
-
throw new Error(`Failed to select peer for ${FilterCodec}`);
|
173
|
-
}
|
174
|
-
return res.peer;
|
175
|
-
}
|
176
|
-
async peers() {
|
177
|
-
return getPeersForProtocol(this.components.peerStore, [FilterCodec]);
|
178
|
-
}
|
179
|
-
async randomPeer() {
|
180
|
-
return selectRandomPeer(await this.peers());
|
181
|
-
}
|
182
127
|
}
|
183
128
|
export function wakuFilter(init = {}) {
|
184
|
-
return (
|
129
|
+
return (libp2p) => new Filter(libp2p, init);
|
185
130
|
}
|
186
131
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/filter/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/filter/index.ts"],"names":[],"mappings":"AAYA,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;AAE/B,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAExD,OAAO,EAAiB,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI3D,MAAM,CAAC,MAAM,WAAW,GAA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AC,OAAO,CAAC,KAAK,EAAE,GAAgB,EAAE,EAAE;gBAC1C,IAAI,YAAY;oBAAE,OAAO;gBACzB,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,YAAY,CACpC,WAAW,EACX,cAAc,CAAC,YAAY,CAAC,CAC7B,CAAC;gBACF,IAAI,CAAC,OAAO,EAAE;oBACZ,GAAG,CAAC,4BAA4B,CAAC,CAAC;oBAClC,OAAO;iBACR;gBACD,gDAAgD;gBAChD,qDAAqD;gBACrD,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;gBAChC,MAAM,QAAQ,CAAC,OAAO,CAAC,CAAC;YAC1B,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;IAEO,KAAK,CAAC,WAAW,CACvB,KAAa,EACb,cAA+B,EAC/B,SAAiB,EACjB,IAAU;QAEV,MAAM,kBAAkB,GAAG,SAAS,CAAC,aAAa,CAChD,KAAK,EACL,cAAc,EACd,SAAS,EACT,KAAK,CACN,CAAC;QAEF,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAC1C,IAAI;YACF,MAAM,IAAI,CAAC,CAAC,kBAAkB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;SACrE;QAAC,OAAO,CAAC,EAAE;YACV,GAAG,CAAC,qBAAqB,EAAE,CAAC,CAAC,CAAC;YAC9B,MAAM,CAAC,CAAC;SACT;IACH,CAAC;CACF;AAED,MAAM,UAAU,UAAU,CACxB,OAAuC,EAAE;IAEzC,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AACtD,CAAC"}
|
package/dist/lib/group_by.d.ts
CHANGED
package/dist/lib/group_by.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"group_by.js","sourceRoot":"","sources":["../../src/lib/group_by.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,mBAAmB,CACjC,
|
1
|
+
{"version":3,"file":"group_by.js","sourceRoot":"","sources":["../../src/lib/group_by.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,mBAAmB,CACjC,MAAoB;IAEpB,MAAM,eAAe,GAAG,IAAI,GAAG,EAAE,CAAC;IAClC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACvB,IAAI,IAAI,GAAG,eAAe,CAAC,GAAG,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QACnD,IAAI,CAAC,IAAI,EAAE;YACT,eAAe,CAAC,GAAG,CAAC,KAAK,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;YAC5C,IAAI,GAAG,eAAe,CAAC,GAAG,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;SAChD;QACD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;IACH,OAAO,eAAe,CAAC;AACzB,CAAC"}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import type { PeerId } from "@libp2p/interface-peer-id";
|
2
|
+
import type { IRelay } from "@waku/interfaces";
|
3
|
+
import type { Libp2p } from "libp2p";
|
4
|
+
export interface KeepAliveOptions {
|
5
|
+
pingKeepAlive: number;
|
6
|
+
relayKeepAlive: number;
|
7
|
+
}
|
8
|
+
export declare class KeepAliveManager {
|
9
|
+
private pingKeepAliveTimers;
|
10
|
+
private relayKeepAliveTimers;
|
11
|
+
private options;
|
12
|
+
private relay?;
|
13
|
+
constructor(options: KeepAliveOptions, relay?: IRelay);
|
14
|
+
start(peerId: PeerId, libp2pPing: Libp2p["ping"]): void;
|
15
|
+
stop(peerId: PeerId): void;
|
16
|
+
stopAll(): void;
|
17
|
+
}
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import debug from "debug";
|
2
|
+
import { createEncoder } from "../index.js";
|
3
|
+
import { RelayPingContentTopic } from "./relay/constants.js";
|
4
|
+
const log = debug("waku:keep-alive");
|
5
|
+
export class KeepAliveManager {
|
6
|
+
constructor(options, relay) {
|
7
|
+
this.pingKeepAliveTimers = new Map();
|
8
|
+
this.relayKeepAliveTimers = new Map();
|
9
|
+
this.options = options;
|
10
|
+
this.relay = relay;
|
11
|
+
}
|
12
|
+
start(peerId, libp2pPing) {
|
13
|
+
// Just in case a timer already exist for this peer
|
14
|
+
this.stop(peerId);
|
15
|
+
const { pingKeepAlive: pingPeriodSecs, relayKeepAlive: relayPeriodSecs } = this.options;
|
16
|
+
const peerIdStr = peerId.toString();
|
17
|
+
if (pingPeriodSecs !== 0) {
|
18
|
+
const interval = setInterval(() => {
|
19
|
+
libp2pPing(peerId).catch((e) => {
|
20
|
+
log(`Ping failed (${peerIdStr})`, e);
|
21
|
+
});
|
22
|
+
}, pingPeriodSecs * 1000);
|
23
|
+
this.pingKeepAliveTimers.set(peerIdStr, interval);
|
24
|
+
}
|
25
|
+
const relay = this.relay;
|
26
|
+
if (relay && relayPeriodSecs !== 0) {
|
27
|
+
const encoder = createEncoder({
|
28
|
+
contentTopic: RelayPingContentTopic,
|
29
|
+
ephemeral: true,
|
30
|
+
});
|
31
|
+
const interval = setInterval(() => {
|
32
|
+
log("Sending Waku Relay ping message");
|
33
|
+
relay
|
34
|
+
.send(encoder, { payload: new Uint8Array([1]) })
|
35
|
+
.catch((e) => log("Failed to send relay ping", e));
|
36
|
+
}, relayPeriodSecs * 1000);
|
37
|
+
this.relayKeepAliveTimers.set(peerId, interval);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
stop(peerId) {
|
41
|
+
const peerIdStr = peerId.toString();
|
42
|
+
if (this.pingKeepAliveTimers.has(peerIdStr)) {
|
43
|
+
clearInterval(this.pingKeepAliveTimers.get(peerIdStr));
|
44
|
+
this.pingKeepAliveTimers.delete(peerIdStr);
|
45
|
+
}
|
46
|
+
if (this.relayKeepAliveTimers.has(peerId)) {
|
47
|
+
clearInterval(this.relayKeepAliveTimers.get(peerId));
|
48
|
+
this.relayKeepAliveTimers.delete(peerId);
|
49
|
+
}
|
50
|
+
}
|
51
|
+
stopAll() {
|
52
|
+
for (const timer of [
|
53
|
+
...Object.values(this.pingKeepAliveTimers),
|
54
|
+
...Object.values(this.relayKeepAliveTimers),
|
55
|
+
]) {
|
56
|
+
clearInterval(timer);
|
57
|
+
}
|
58
|
+
this.pingKeepAliveTimers.clear();
|
59
|
+
this.relayKeepAliveTimers.clear();
|
60
|
+
}
|
61
|
+
}
|
62
|
+
//# sourceMappingURL=keep_alive_manager.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"keep_alive_manager.js","sourceRoot":"","sources":["../../src/lib/keep_alive_manager.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAE7D,MAAM,GAAG,GAAG,KAAK,CAAC,iBAAiB,CAAC,CAAC;AAOrC,MAAM,OAAO,gBAAgB;IAM3B,YAAY,OAAyB,EAAE,KAAc;QACnD,IAAI,CAAC,mBAAmB,GAAG,IAAI,GAAG,EAAE,CAAC;QACrC,IAAI,CAAC,oBAAoB,GAAG,IAAI,GAAG,EAAE,CAAC;QACtC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAEM,KAAK,CAAC,MAAc,EAAE,UAA0B;QACrD,mDAAmD;QACnD,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAElB,MAAM,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,GACtE,IAAI,CAAC,OAAO,CAAC;QAEf,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;QAEpC,IAAI,cAAc,KAAK,CAAC,EAAE;YACxB,MAAM,QAAQ,GAAG,WAAW,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AAC,SAAS,CAAC,CAAC;SAC5C;QAED,IAAI,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;YACzC,aAAa,CAAC,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;YACrD,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;SAC1C;IACH,CAAC;IAEM,OAAO;QACZ,KAAK,MAAM,KAAK,IAAI;YAClB,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC;YAC1C,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC;SAC5C,EAAE;YACD,aAAa,CAAC,KAAK,CAAC,CAAC;SACtB;QAED,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE,CAAC;QACjC,IAAI,CAAC,oBAAoB,CAAC,KAAK,EAAE,CAAC;IACpC,CAAC;CACF"}
|
@@ -1,22 +1,6 @@
|
|
1
|
-
import {
|
2
|
-
import type {
|
3
|
-
import type { ILightPush } from "@waku/interfaces";
|
1
|
+
import type { Libp2p } from "@libp2p/interface-libp2p";
|
2
|
+
import type { ILightPush, ProtocolCreateOptions } from "@waku/interfaces";
|
4
3
|
import { PushResponse } from "@waku/proto";
|
5
4
|
export declare const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
6
5
|
export { PushResponse };
|
7
|
-
export
|
8
|
-
peerStore: PeerStore;
|
9
|
-
connectionManager: ConnectionManager;
|
10
|
-
}
|
11
|
-
export interface CreateOptions {
|
12
|
-
/**
|
13
|
-
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
|
14
|
-
*
|
15
|
-
* The usage of the default pubsub topic is recommended.
|
16
|
-
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
17
|
-
*
|
18
|
-
* @default {@link DefaultPubSubTopic}
|
19
|
-
*/
|
20
|
-
pubSubTopic?: string;
|
21
|
-
}
|
22
|
-
export declare function wakuLightPush(init?: Partial<CreateOptions>): (components: LightPushComponents) => ILightPush;
|
6
|
+
export declare function wakuLightPush(init?: Partial<ProtocolCreateOptions>): (libp2p: Libp2p) => ILightPush;
|