@waku/core 0.0.18 → 0.0.20
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 +40 -0
- package/bundle/index.js +548 -109
- package/bundle/lib/message/version_0.js +1 -1
- package/bundle/{version_0-c6b47311.js → version_0-9c941081.js} +402 -23
- package/dist/index.d.ts +4 -2
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +8 -0
- package/dist/lib/connection_manager.js +74 -6
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/{filter_rpc.d.ts → v1/filter_rpc.d.ts} +1 -3
- package/dist/lib/filter/v1/filter_rpc.js.map +1 -0
- package/dist/lib/filter/{index.d.ts → v1/index.d.ts} +0 -2
- package/dist/lib/filter/{index.js → v1/index.js} +3 -3
- package/dist/lib/filter/v1/index.js.map +1 -0
- package/dist/lib/filter/v2/filter_rpc.d.ts +40 -0
- package/dist/lib/filter/v2/filter_rpc.js +109 -0
- package/dist/lib/filter/v2/filter_rpc.js.map +1 -0
- package/dist/lib/filter/v2/index.d.ts +3 -0
- package/dist/lib/filter/v2/index.js +235 -0
- package/dist/lib/filter/v2/index.js.map +1 -0
- package/dist/lib/wait_for_remote_peer.d.ts +1 -1
- package/dist/lib/wait_for_remote_peer.js +1 -1
- package/dist/lib/waku.d.ts +5 -5
- package/dist/lib/waku.js.map +1 -1
- package/package.json +9 -10
- package/src/index.ts +5 -2
- package/src/lib/connection_manager.ts +96 -8
- package/src/lib/filter/{filter_rpc.ts → v1/filter_rpc.ts} +1 -4
- package/src/lib/filter/{index.ts → v1/index.ts} +5 -6
- package/src/lib/filter/v2/filter_rpc.ts +131 -0
- package/src/lib/filter/v2/index.ts +413 -0
- package/src/lib/wait_for_remote_peer.ts +1 -1
- package/src/lib/waku.ts +5 -7
- package/dist/lib/filter/filter_rpc.js.map +0 -1
- package/dist/lib/filter/index.js.map +0 -1
- /package/dist/lib/filter/{filter_rpc.js → v1/filter_rpc.js} +0 -0
package/dist/lib/waku.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import type { Stream } from "@libp2p/interface-connection";
|
2
2
|
import type { Libp2p } from "@libp2p/interface-libp2p";
|
3
|
-
import
|
4
|
-
import
|
5
|
-
import type { IFilter, ILightPush, IRelay, IStore, Waku } from "@waku/interfaces";
|
3
|
+
import { PeerId } from "@libp2p/interface-peer-id";
|
4
|
+
import { MultiaddrInput } from "@multiformats/multiaddr";
|
5
|
+
import type { IFilter, IFilterV2, ILightPush, IRelay, IStore, Waku } from "@waku/interfaces";
|
6
6
|
import { Protocols } from "@waku/interfaces";
|
7
7
|
import { ConnectionManager } from "./connection_manager.js";
|
8
8
|
export declare const DefaultPingKeepAliveValueSecs = 0;
|
@@ -33,10 +33,10 @@ export declare class WakuNode implements Waku {
|
|
33
33
|
libp2p: Libp2p;
|
34
34
|
relay?: IRelay;
|
35
35
|
store?: IStore;
|
36
|
-
filter?: IFilter;
|
36
|
+
filter?: IFilter | IFilterV2;
|
37
37
|
lightPush?: ILightPush;
|
38
38
|
connectionManager: ConnectionManager;
|
39
|
-
constructor(options: WakuOptions, libp2p: Libp2p, store?: (libp2p: Libp2p) => IStore, lightPush?: (libp2p: Libp2p) => ILightPush, filter?: (libp2p: Libp2p) => IFilter, relay?: (libp2p: Libp2p) => IRelay);
|
39
|
+
constructor(options: WakuOptions, libp2p: Libp2p, store?: (libp2p: Libp2p) => IStore, lightPush?: (libp2p: Libp2p) => ILightPush, filter?: (libp2p: Libp2p) => IFilter | IFilterV2, relay?: (libp2p: Libp2p) => IRelay);
|
40
40
|
/**
|
41
41
|
* Dials to the provided peer.
|
42
42
|
*
|
package/dist/lib/waku.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"waku.js","sourceRoot":"","sources":["../../src/lib/waku.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"waku.js","sourceRoot":"","sources":["../../src/lib/wa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AM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,uBAAuB;QACrB,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM;aAC/B,aAAa,EAAE;aACf,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;QACzD,IAAI,CAAC,cAAc,IAAI,cAAc,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE;YACvD,MAAM,4BAA4B,CAAC;SACpC;QACD,OAAO,cAAc,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IAClE,CAAC;CACF;AACD,SAAS,sBAAsB,CAC7B,MAA+B;IAE/B,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;AACvD,CAAC"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@waku/core",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.20",
|
4
4
|
"description": "TypeScript implementation of the Waku v2 protocol",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -56,7 +56,6 @@
|
|
56
56
|
"build:bundle": "rollup --config rollup.config.js",
|
57
57
|
"fix": "run-s fix:*",
|
58
58
|
"fix:lint": "eslint src *.js --fix",
|
59
|
-
"pretest": "run-s pretest:*",
|
60
59
|
"check": "run-s check:*",
|
61
60
|
"check:tsc": "tsc -p tsconfig.dev.json",
|
62
61
|
"check:lint": "eslint src *.js",
|
@@ -74,13 +73,13 @@
|
|
74
73
|
},
|
75
74
|
"dependencies": {
|
76
75
|
"@noble/hashes": "^1.3.0",
|
77
|
-
"@waku/interfaces": "0.0.
|
78
|
-
"@waku/proto": "
|
79
|
-
"@waku/utils": "0.0.
|
76
|
+
"@waku/interfaces": "0.0.15",
|
77
|
+
"@waku/proto": "0.0.5",
|
78
|
+
"@waku/utils": "0.0.8",
|
80
79
|
"debug": "^4.3.4",
|
81
|
-
"it-all": "^3.0.
|
80
|
+
"it-all": "^3.0.2",
|
82
81
|
"it-length-prefixed": "^9.0.1",
|
83
|
-
"it-pipe": "^
|
82
|
+
"it-pipe": "^3.0.1",
|
84
83
|
"p-event": "^5.0.1",
|
85
84
|
"uint8arraylist": "^2.4.3",
|
86
85
|
"uuid": "^9.0.0"
|
@@ -100,11 +99,11 @@
|
|
100
99
|
"@types/mocha": "^10.0.1",
|
101
100
|
"@types/uuid": "^9.0.1",
|
102
101
|
"@typescript-eslint/eslint-plugin": "^5.57.0",
|
103
|
-
"@typescript-eslint/parser": "^5.
|
102
|
+
"@typescript-eslint/parser": "^5.59.8",
|
104
103
|
"@waku/build-utils": "*",
|
105
104
|
"chai": "^4.3.7",
|
106
105
|
"cspell": "^6.31.1",
|
107
|
-
"eslint": "^8.
|
106
|
+
"eslint": "^8.41.0",
|
108
107
|
"eslint-config-prettier": "^8.6.0",
|
109
108
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
110
109
|
"eslint-plugin-functional": "^5.0.4",
|
@@ -121,7 +120,7 @@
|
|
121
120
|
"npm-run-all": "^4.1.5",
|
122
121
|
"prettier": "^2.8.8",
|
123
122
|
"process": "^0.11.10",
|
124
|
-
"puppeteer": "^
|
123
|
+
"puppeteer": "^20.4.0",
|
125
124
|
"rollup": "^3.21.3",
|
126
125
|
"ts-loader": "^9.4.2",
|
127
126
|
"ts-node": "^10.9.1",
|
package/src/index.ts
CHANGED
@@ -11,8 +11,11 @@ export * as message from "./lib/message/index.js";
|
|
11
11
|
export * as waku from "./lib/waku.js";
|
12
12
|
export { WakuNode, WakuOptions } from "./lib/waku.js";
|
13
13
|
|
14
|
-
export * as
|
15
|
-
export { wakuFilter } from "./lib/filter/index.js";
|
14
|
+
export * as waku_filter_v1 from "./lib/filter/v1/index.js";
|
15
|
+
export { wakuFilter as wakuFilterV1 } from "./lib/filter/v1/index.js";
|
16
|
+
|
17
|
+
export * as waku_filter_v2 from "./lib/filter/v2/index.js";
|
18
|
+
export { wakuFilterV2 } from "./lib/filter/v2/index.js";
|
16
19
|
|
17
20
|
export * as waku_light_push from "./lib/light_push/index.js";
|
18
21
|
export { wakuLightPush, LightPushCodec } from "./lib/light_push/index.js";
|
@@ -12,6 +12,7 @@ const log = debug("waku:connection-manager");
|
|
12
12
|
|
13
13
|
export const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
|
14
14
|
export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
|
15
|
+
export const DEFAULT_MAX_PARALLEL_DIALS = 3;
|
15
16
|
|
16
17
|
export class ConnectionManager {
|
17
18
|
private static instances = new Map<string, ConnectionManager>();
|
@@ -21,6 +22,9 @@ export class ConnectionManager {
|
|
21
22
|
private dialAttemptsForPeer: Map<string, number> = new Map();
|
22
23
|
private dialErrorsForPeer: Map<string, any> = new Map();
|
23
24
|
|
25
|
+
private currentActiveDialCount = 0;
|
26
|
+
private pendingPeerDialQueue: Array<PeerId> = [];
|
27
|
+
|
24
28
|
public static create(
|
25
29
|
peerId: string,
|
26
30
|
libp2p: Libp2p,
|
@@ -52,6 +56,7 @@ export class ConnectionManager {
|
|
52
56
|
this.options = {
|
53
57
|
maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
|
54
58
|
maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
|
59
|
+
maxParallelDials: DEFAULT_MAX_PARALLEL_DIALS,
|
55
60
|
...options,
|
56
61
|
};
|
57
62
|
|
@@ -60,6 +65,31 @@ export class ConnectionManager {
|
|
60
65
|
this.run()
|
61
66
|
.then(() => log(`Connection Manager is now running`))
|
62
67
|
.catch((error) => log(`Unexpected error while running service`, error));
|
68
|
+
|
69
|
+
// libp2p emits `peer:discovery` events during its initialization
|
70
|
+
// which means that before the ConnectionManager is initialized, some peers may have been discovered
|
71
|
+
// we will dial the peers in peerStore ONCE before we start to listen to the `peer:discovery` events within the ConnectionManager
|
72
|
+
this.dialPeerStorePeers();
|
73
|
+
}
|
74
|
+
|
75
|
+
private async dialPeerStorePeers(): Promise<void> {
|
76
|
+
const peerInfos = await this.libp2pComponents.peerStore.all();
|
77
|
+
const dialPromises = [];
|
78
|
+
for (const peerInfo of peerInfos) {
|
79
|
+
if (
|
80
|
+
this.libp2pComponents
|
81
|
+
.getConnections()
|
82
|
+
.find((c) => c.remotePeer === peerInfo.id)
|
83
|
+
)
|
84
|
+
continue;
|
85
|
+
|
86
|
+
dialPromises.push(this.attemptDial(peerInfo.id));
|
87
|
+
}
|
88
|
+
try {
|
89
|
+
await Promise.all(dialPromises);
|
90
|
+
} catch (error) {
|
91
|
+
log(`Unexpected error while dialing peer store peers`, error);
|
92
|
+
}
|
63
93
|
}
|
64
94
|
|
65
95
|
private async run(): Promise<void> {
|
@@ -86,6 +116,7 @@ export class ConnectionManager {
|
|
86
116
|
}
|
87
117
|
|
88
118
|
private async dialPeer(peerId: PeerId): Promise<void> {
|
119
|
+
this.currentActiveDialCount += 1;
|
89
120
|
let dialAttempt = 0;
|
90
121
|
while (dialAttempt <= this.options.maxDialAttemptsForPeer) {
|
91
122
|
try {
|
@@ -105,6 +136,7 @@ export class ConnectionManager {
|
|
105
136
|
return;
|
106
137
|
} catch (e) {
|
107
138
|
const error = e as AggregateError;
|
139
|
+
|
108
140
|
this.dialErrorsForPeer.set(peerId.toString(), error);
|
109
141
|
log(`Error dialing peer ${peerId.toString()} - ${error.errors}`);
|
110
142
|
|
@@ -128,6 +160,33 @@ export class ConnectionManager {
|
|
128
160
|
return await this.libp2pComponents.peerStore.delete(peerId);
|
129
161
|
} catch (error) {
|
130
162
|
throw `Error deleting undialable peer ${peerId.toString()} from peer store - ${error}`;
|
163
|
+
} finally {
|
164
|
+
this.currentActiveDialCount -= 1;
|
165
|
+
this.processDialQueue();
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
async dropConnection(peerId: PeerId): Promise<void> {
|
170
|
+
try {
|
171
|
+
await this.libp2pComponents.hangUp(peerId);
|
172
|
+
log(`Dropped connection with peer ${peerId.toString()}`);
|
173
|
+
} catch (error) {
|
174
|
+
log(
|
175
|
+
`Error dropping connection with peer ${peerId.toString()} - ${error}`
|
176
|
+
);
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
private async processDialQueue(): Promise<void> {
|
181
|
+
if (
|
182
|
+
this.pendingPeerDialQueue.length > 0 &&
|
183
|
+
this.currentActiveDialCount < this.options.maxParallelDials
|
184
|
+
) {
|
185
|
+
const peerId = this.pendingPeerDialQueue.shift();
|
186
|
+
if (!peerId) return;
|
187
|
+
this.attemptDial(peerId).catch((error) => {
|
188
|
+
log(error);
|
189
|
+
});
|
131
190
|
}
|
132
191
|
}
|
133
192
|
|
@@ -164,21 +223,50 @@ export class ConnectionManager {
|
|
164
223
|
);
|
165
224
|
}
|
166
225
|
|
226
|
+
private async attemptDial(peerId: PeerId): Promise<void> {
|
227
|
+
if (this.currentActiveDialCount >= this.options.maxParallelDials) {
|
228
|
+
this.pendingPeerDialQueue.push(peerId);
|
229
|
+
return;
|
230
|
+
}
|
231
|
+
|
232
|
+
if (!(await this.shouldDialPeer(peerId))) return;
|
233
|
+
|
234
|
+
this.dialPeer(peerId).catch((err) => {
|
235
|
+
throw `Error dialing peer ${peerId.toString()} : ${err}`;
|
236
|
+
});
|
237
|
+
}
|
238
|
+
|
167
239
|
private onEventHandlers = {
|
168
240
|
"peer:discovery": async (evt: CustomEvent<PeerInfo>): Promise<void> => {
|
169
241
|
const { id: peerId } = evt.detail;
|
170
|
-
if (!(await this.shouldDialPeer(peerId))) return;
|
171
242
|
|
172
|
-
this.
|
243
|
+
this.attemptDial(peerId).catch((err) =>
|
173
244
|
log(`Error dialing peer ${peerId.toString()} : ${err}`)
|
174
245
|
);
|
175
246
|
},
|
176
|
-
"peer:connect": (evt: CustomEvent<Connection>): void => {
|
177
|
-
{
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
)
|
247
|
+
"peer:connect": async (evt: CustomEvent<Connection>): Promise<void> => {
|
248
|
+
const { remotePeer: peerId } = evt.detail;
|
249
|
+
|
250
|
+
this.keepAliveManager.start(
|
251
|
+
peerId,
|
252
|
+
this.libp2pComponents.ping.bind(this)
|
253
|
+
);
|
254
|
+
|
255
|
+
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
|
256
|
+
Tags.BOOTSTRAP
|
257
|
+
);
|
258
|
+
|
259
|
+
if (isBootstrap) {
|
260
|
+
const bootstrapConnections = this.libp2pComponents
|
261
|
+
.getConnections()
|
262
|
+
.filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
|
263
|
+
|
264
|
+
// If we have too many bootstrap connections, drop one
|
265
|
+
if (
|
266
|
+
bootstrapConnections.length > this.options.maxBootstrapPeersAllowed
|
267
|
+
) {
|
268
|
+
await this.dropConnection(peerId);
|
269
|
+
}
|
182
270
|
}
|
183
271
|
},
|
184
272
|
"peer:disconnect": () => {
|
@@ -1,10 +1,7 @@
|
|
1
|
+
import { ContentFilter } from "@waku/interfaces";
|
1
2
|
import { proto_filter as proto } from "@waku/proto";
|
2
3
|
import { v4 as uuid } from "uuid";
|
3
4
|
|
4
|
-
export type ContentFilter = {
|
5
|
-
contentTopic: string;
|
6
|
-
};
|
7
|
-
|
8
5
|
/**
|
9
6
|
* FilterRPC represents a message conforming to the Waku Filter protocol
|
10
7
|
*/
|
@@ -4,6 +4,7 @@ import type { IncomingStreamData } from "@libp2p/interface-registrar";
|
|
4
4
|
import type {
|
5
5
|
ActiveSubscriptions,
|
6
6
|
Callback,
|
7
|
+
ContentFilter,
|
7
8
|
IAsyncIterator,
|
8
9
|
IDecodedMessage,
|
9
10
|
IDecoder,
|
@@ -19,13 +20,11 @@ import all from "it-all";
|
|
19
20
|
import * as lp from "it-length-prefixed";
|
20
21
|
import { pipe } from "it-pipe";
|
21
22
|
|
22
|
-
import { BaseProtocol } from "
|
23
|
-
import { DefaultPubSubTopic } from "
|
24
|
-
import { toProtoMessage } from "
|
23
|
+
import { BaseProtocol } from "../../base_protocol.js";
|
24
|
+
import { DefaultPubSubTopic } from "../../constants.js";
|
25
|
+
import { toProtoMessage } from "../../to_proto_message.js";
|
25
26
|
|
26
|
-
import {
|
27
|
-
|
28
|
-
export { ContentFilter };
|
27
|
+
import { FilterRpc } from "./filter_rpc.js";
|
29
28
|
|
30
29
|
export const FilterCodec = "/vac/waku/filter/2.0.0-beta1";
|
31
30
|
|
@@ -0,0 +1,131 @@
|
|
1
|
+
import { proto_filter_v2 as proto, WakuMessage } from "@waku/proto";
|
2
|
+
import { v4 as uuid } from "uuid";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* FilterPushRPC represents a message conforming to the Waku FilterPush protocol.
|
6
|
+
* Protocol documentation: https://rfc.vac.dev/spec/12/
|
7
|
+
*/
|
8
|
+
export class FilterPushRpc {
|
9
|
+
public constructor(public proto: proto.MessagePush) {}
|
10
|
+
|
11
|
+
static decode(bytes: Uint8Array): FilterPushRpc {
|
12
|
+
const res = proto.MessagePush.decode(bytes);
|
13
|
+
return new FilterPushRpc(res);
|
14
|
+
}
|
15
|
+
|
16
|
+
encode(): Uint8Array {
|
17
|
+
return proto.MessagePush.encode(this.proto);
|
18
|
+
}
|
19
|
+
|
20
|
+
get wakuMessage(): WakuMessage | undefined {
|
21
|
+
return this.proto.wakuMessage;
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Get the pubsub topic from the FilterPushRpc object.
|
26
|
+
* @returns string
|
27
|
+
*/
|
28
|
+
get pubsubTopic(): string | undefined {
|
29
|
+
return this.proto.pubsubTopic;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
export class FilterSubscribeRpc {
|
34
|
+
public constructor(public proto: proto.FilterSubscribeRequest) {}
|
35
|
+
|
36
|
+
static createSubscribeRequest(
|
37
|
+
pubsubTopic: string,
|
38
|
+
contentTopics: string[]
|
39
|
+
): FilterSubscribeRpc {
|
40
|
+
return new FilterSubscribeRpc({
|
41
|
+
requestId: uuid(),
|
42
|
+
filterSubscribeType:
|
43
|
+
proto.FilterSubscribeRequest.FilterSubscribeType.SUBSCRIBE,
|
44
|
+
pubsubTopic,
|
45
|
+
contentTopics,
|
46
|
+
});
|
47
|
+
}
|
48
|
+
|
49
|
+
static createUnsubscribeRequest(
|
50
|
+
pubsubTopic: string,
|
51
|
+
contentTopics: string[]
|
52
|
+
): FilterSubscribeRpc {
|
53
|
+
return new FilterSubscribeRpc({
|
54
|
+
requestId: uuid(),
|
55
|
+
filterSubscribeType:
|
56
|
+
proto.FilterSubscribeRequest.FilterSubscribeType.UNSUBSCRIBE,
|
57
|
+
pubsubTopic,
|
58
|
+
contentTopics,
|
59
|
+
});
|
60
|
+
}
|
61
|
+
|
62
|
+
static createUnsubscribeAllRequest(pubsubTopic: string): FilterSubscribeRpc {
|
63
|
+
return new FilterSubscribeRpc({
|
64
|
+
requestId: uuid(),
|
65
|
+
filterSubscribeType:
|
66
|
+
proto.FilterSubscribeRequest.FilterSubscribeType.UNSUBSCRIBE_ALL,
|
67
|
+
pubsubTopic,
|
68
|
+
contentTopics: [],
|
69
|
+
});
|
70
|
+
}
|
71
|
+
|
72
|
+
static createSubscriberPingRequest(): FilterSubscribeRpc {
|
73
|
+
return new FilterSubscribeRpc({
|
74
|
+
requestId: uuid(),
|
75
|
+
filterSubscribeType:
|
76
|
+
proto.FilterSubscribeRequest.FilterSubscribeType.SUBSCRIBER_PING,
|
77
|
+
pubsubTopic: "",
|
78
|
+
contentTopics: [],
|
79
|
+
});
|
80
|
+
}
|
81
|
+
|
82
|
+
static decode(bytes: Uint8Array): FilterSubscribeRpc {
|
83
|
+
const res = proto.FilterSubscribeRequest.decode(bytes);
|
84
|
+
return new FilterSubscribeRpc(res);
|
85
|
+
}
|
86
|
+
|
87
|
+
encode(): Uint8Array {
|
88
|
+
return proto.FilterSubscribeRequest.encode(this.proto);
|
89
|
+
}
|
90
|
+
|
91
|
+
get filterSubscribeType(): proto.FilterSubscribeRequest.FilterSubscribeType {
|
92
|
+
return this.proto.filterSubscribeType;
|
93
|
+
}
|
94
|
+
|
95
|
+
get requestId(): string {
|
96
|
+
return this.proto.requestId;
|
97
|
+
}
|
98
|
+
|
99
|
+
get pubsubTopic(): string | undefined {
|
100
|
+
return this.proto.pubsubTopic;
|
101
|
+
}
|
102
|
+
|
103
|
+
get contentTopics(): string[] {
|
104
|
+
return this.proto.contentTopics;
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
export class FilterSubscribeResponse {
|
109
|
+
public constructor(public proto: proto.FilterSubscribeResponse) {}
|
110
|
+
|
111
|
+
static decode(bytes: Uint8Array): FilterSubscribeResponse {
|
112
|
+
const res = proto.FilterSubscribeResponse.decode(bytes);
|
113
|
+
return new FilterSubscribeResponse(res);
|
114
|
+
}
|
115
|
+
|
116
|
+
encode(): Uint8Array {
|
117
|
+
return proto.FilterSubscribeResponse.encode(this.proto);
|
118
|
+
}
|
119
|
+
|
120
|
+
get statusCode(): number {
|
121
|
+
return this.proto.statusCode;
|
122
|
+
}
|
123
|
+
|
124
|
+
get statusDesc(): string | undefined {
|
125
|
+
return this.proto.statusDesc;
|
126
|
+
}
|
127
|
+
|
128
|
+
get requestId(): string {
|
129
|
+
return this.proto.requestId;
|
130
|
+
}
|
131
|
+
}
|