2022-08-27 19:09:34 +00:00
|
|
|
import WisdomRpcQuery from "./query/wisdom.js";
|
2022-07-19 18:43:28 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import DHT from "@hyperswarm/dht";
|
2022-08-27 19:09:34 +00:00
|
|
|
import StreamingRpcQuery from "./query/streaming.js";
|
|
|
|
import { RpcQueryOptions, StreamHandlerFunction } from "./types.js";
|
|
|
|
import SimpleRpcQuery from "./query/simple.js";
|
2022-06-27 19:36:29 +00:00
|
|
|
|
|
|
|
export default class RpcNetwork {
|
|
|
|
constructor(dht = new DHT()) {
|
|
|
|
this._dht = dht;
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:39:17 +00:00
|
|
|
private _dht: typeof DHT;
|
|
|
|
|
|
|
|
get dht() {
|
|
|
|
return this._dht;
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 15:39:17 +00:00
|
|
|
private _majorityThreshold = 0.75;
|
|
|
|
|
|
|
|
get majorityThreshold(): number {
|
|
|
|
return this._majorityThreshold;
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 15:39:17 +00:00
|
|
|
set majorityThreshold(value: number) {
|
|
|
|
this._majorityThreshold = value;
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 15:39:17 +00:00
|
|
|
private _queryTimeout = 30;
|
|
|
|
|
2022-06-27 19:36:29 +00:00
|
|
|
get queryTimeout(): number {
|
|
|
|
return this._queryTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
set queryTimeout(value: number) {
|
|
|
|
this._queryTimeout = value;
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:39:17 +00:00
|
|
|
private _relayTimeout = 2;
|
|
|
|
|
|
|
|
get relayTimeout(): number {
|
|
|
|
return this._relayTimeout;
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 15:39:17 +00:00
|
|
|
set relayTimeout(value: number) {
|
|
|
|
this._relayTimeout = value;
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 15:39:17 +00:00
|
|
|
private _relays: string[] = [];
|
|
|
|
|
|
|
|
get relays(): string[] {
|
|
|
|
return this._relays;
|
|
|
|
}
|
|
|
|
|
2022-07-27 01:25:38 +00:00
|
|
|
private _ready?: Promise<void>;
|
2022-07-23 15:39:17 +00:00
|
|
|
|
|
|
|
get ready(): Promise<void> {
|
2022-07-27 00:58:41 +00:00
|
|
|
if (!this._ready) {
|
2022-07-27 01:25:38 +00:00
|
|
|
this._ready = this._dht.ready() as Promise<void>;
|
2022-07-27 00:58:41 +00:00
|
|
|
}
|
2022-07-23 15:39:17 +00:00
|
|
|
return this._ready;
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:10:07 +00:00
|
|
|
private _bypassCache: boolean = false;
|
2022-07-23 15:39:17 +00:00
|
|
|
|
2022-08-18 23:10:07 +00:00
|
|
|
get bypassCache(): boolean {
|
|
|
|
return this._bypassCache;
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 23:10:07 +00:00
|
|
|
set bypassCache(value: boolean) {
|
|
|
|
this._bypassCache = value;
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public addRelay(pubkey: string): void {
|
|
|
|
this._relays.push(pubkey);
|
|
|
|
this._relays = [...new Set(this._relays)];
|
|
|
|
}
|
|
|
|
|
2022-06-27 20:25:53 +00:00
|
|
|
public removeRelay(pubkey: string): boolean {
|
|
|
|
if (!this._relays.includes(pubkey)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this._relays[this._relays.indexOf(pubkey)];
|
|
|
|
this._relays = Object.values(this._relays);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public clearRelays(): void {
|
|
|
|
this._relays = [];
|
|
|
|
}
|
|
|
|
|
2022-08-27 19:09:34 +00:00
|
|
|
public wisdomQuery(
|
|
|
|
method: string,
|
|
|
|
module: string,
|
2022-06-27 19:36:29 +00:00
|
|
|
data: object | any[] = {},
|
2022-08-27 19:09:34 +00:00
|
|
|
bypassCache: boolean = false,
|
|
|
|
options: RpcQueryOptions = {}
|
|
|
|
): WisdomRpcQuery {
|
|
|
|
return new WisdomRpcQuery(
|
|
|
|
this,
|
|
|
|
{
|
|
|
|
method,
|
|
|
|
module,
|
|
|
|
data,
|
2022-08-18 23:10:07 +00:00
|
|
|
bypassCache: bypassCache || this._bypassCache,
|
2022-08-27 19:09:34 +00:00
|
|
|
},
|
|
|
|
options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public streamingQuery(
|
|
|
|
relay: Buffer | string,
|
|
|
|
method: string,
|
|
|
|
module: string,
|
|
|
|
streamHandler: StreamHandlerFunction,
|
|
|
|
data: object | any[] = {},
|
|
|
|
options: RpcQueryOptions = {}
|
|
|
|
): StreamingRpcQuery {
|
|
|
|
return new StreamingRpcQuery(
|
|
|
|
this,
|
|
|
|
relay,
|
|
|
|
{ method, module, data },
|
|
|
|
{ streamHandler, ...options }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public simpleQuery(
|
|
|
|
relay: Buffer | string,
|
|
|
|
method: string,
|
|
|
|
module: string,
|
|
|
|
data: object | any[] = {},
|
|
|
|
options: RpcQueryOptions = {}
|
|
|
|
): SimpleRpcQuery {
|
|
|
|
return new SimpleRpcQuery(
|
|
|
|
this,
|
|
|
|
relay,
|
|
|
|
{
|
|
|
|
method,
|
|
|
|
module,
|
|
|
|
data,
|
|
|
|
},
|
|
|
|
options
|
|
|
|
);
|
2022-06-27 19:36:29 +00:00
|
|
|
}
|
|
|
|
}
|