mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-19 23:58:11 +00:00
Add more comments
This commit is contained in:
parent
971d080ab5
commit
c534bd6cff
@ -7,6 +7,9 @@ export type ContentFilter = {
|
|||||||
contentTopic: string;
|
contentTopic: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FilterRPC represents a message conforming to the Waku Filter protocol
|
||||||
|
*/
|
||||||
export class FilterRPC {
|
export class FilterRPC {
|
||||||
public constructor(public proto: proto.FilterRPC) {}
|
public constructor(public proto: proto.FilterRPC) {}
|
||||||
|
|
||||||
@ -27,11 +30,20 @@ export class FilterRPC {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param bytes Uint8Array of bytes from a FilterRPC message
|
||||||
|
* @returns FilterRPC
|
||||||
|
*/
|
||||||
static decode(bytes: Uint8Array): FilterRPC {
|
static decode(bytes: Uint8Array): FilterRPC {
|
||||||
const res = proto.FilterRPC.decode(Reader.create(bytes));
|
const res = proto.FilterRPC.decode(Reader.create(bytes));
|
||||||
return new FilterRPC(res);
|
return new FilterRPC(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode the current FilterRPC request to bytes
|
||||||
|
* @returns Uint8Array
|
||||||
|
*/
|
||||||
encode(): Uint8Array {
|
encode(): Uint8Array {
|
||||||
return proto.FilterRPC.encode(this.proto).finish();
|
return proto.FilterRPC.encode(this.proto).finish();
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,17 @@ export const FilterCodec = "/vac/waku/filter/2.0.0-beta1";
|
|||||||
const log = debug("waku:filter");
|
const log = debug("waku:filter");
|
||||||
|
|
||||||
type FilterSubscriptionOpts = {
|
type FilterSubscriptionOpts = {
|
||||||
|
/**
|
||||||
|
* The Pubsub topic for the subscription
|
||||||
|
*/
|
||||||
topic?: string;
|
topic?: string;
|
||||||
|
/**
|
||||||
|
* Optionally specify a PeerId for the subscription. If not included, will use a random peer
|
||||||
|
*/
|
||||||
peerId?: PeerId;
|
peerId?: PeerId;
|
||||||
|
/**
|
||||||
|
* Array of ContentTopics to subscribe to. If empty, will subscribe to all messages on the network
|
||||||
|
*/
|
||||||
contentTopics: string[];
|
contentTopics: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,6 +35,11 @@ type FilterCallback = (msg: WakuMessage) => void | Promise<void>;
|
|||||||
|
|
||||||
type UnsubscribeFunction = () => Promise<void>;
|
type UnsubscribeFunction = () => Promise<void>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements part of the [Waku v2 Filter protocol](https://rfc.vac.dev/spec/12/).
|
||||||
|
*
|
||||||
|
* WakuFilter can be used as a light filter node, but cannot currently be used as a full node that pushes messages to clients
|
||||||
|
*/
|
||||||
export class WakuFilter {
|
export class WakuFilter {
|
||||||
private subscriptions: {
|
private subscriptions: {
|
||||||
[requestId: string]: FilterCallback;
|
[requestId: string]: FilterCallback;
|
||||||
@ -36,11 +50,17 @@ export class WakuFilter {
|
|||||||
>;
|
>;
|
||||||
|
|
||||||
constructor(public libp2p: Libp2p) {
|
constructor(public libp2p: Libp2p) {
|
||||||
this.libp2p.handle(FilterCodec, this.onRequest.bind(this));
|
|
||||||
this.subscriptions = {};
|
this.subscriptions = {};
|
||||||
this.decryptionKeys = new Map();
|
this.decryptionKeys = new Map();
|
||||||
|
this.libp2p.handle(FilterCodec, this.onRequest.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to
|
||||||
|
* @param callback A function that will be called on each message returned by the filter
|
||||||
|
* @returns Unsubscribe function that can be used to end the subscription
|
||||||
|
*/
|
||||||
async subscribe(
|
async subscribe(
|
||||||
opts: FilterSubscriptionOpts,
|
opts: FilterSubscriptionOpts,
|
||||||
callback: FilterCallback
|
callback: FilterCallback
|
||||||
@ -144,6 +164,7 @@ export class WakuFilter {
|
|||||||
requestId,
|
requestId,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
const stream = await this.newStream(peer);
|
const stream = await this.newStream(peer);
|
||||||
try {
|
try {
|
||||||
await pipe([unsubscribeRequest.encode()], lp.encode(), stream.sink);
|
await pipe([unsubscribeRequest.encode()], lp.encode(), stream.sink);
|
||||||
@ -167,17 +188,19 @@ export class WakuFilter {
|
|||||||
let peer;
|
let peer;
|
||||||
if (peerId) {
|
if (peerId) {
|
||||||
peer = await this.libp2p.peerStore.get(peerId);
|
peer = await this.libp2p.peerStore.get(peerId);
|
||||||
if (!peer)
|
if (!peer) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to retrieve connection details for provided peer in peer store: ${peerId.toB58String()}`
|
`Failed to retrieve connection details for provided peer in peer store: ${peerId.toB58String()}`
|
||||||
);
|
);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
peer = await this.randomPeer;
|
peer = await this.randomPeer;
|
||||||
if (!peer)
|
if (!peer) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Failed to find known peer that registers waku filter protocol"
|
"Failed to find known peer that registers waku filter protocol"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user