rpc-client/src/network.ts

151 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-07-19 18:43:28 +00:00
// @ts-ignore
import DHT from "@hyperswarm/dht";
import b4a from "b4a";
import RPC from "@lumeweb/rpc";
import { isPromise } from "./util.js";
import SimpleRpcQuery from "./query/simple.js";
import WisdomRpcQuery from "./query/wisdom.js";
2022-06-27 19:36:29 +00:00
export default class RpcNetwork {
constructor(dht = new DHT()) {
this._dht = dht;
}
private _activeRelay?: RPC;
get activeRelay(): RPC {
return this._activeRelay as RPC;
}
private _dht: typeof DHT;
get dht() {
return this._dht;
2022-06-27 19:36:29 +00:00
}
private _majorityThreshold = 0.75;
get majorityThreshold(): number {
return this._majorityThreshold;
2022-06-27 19:36:29 +00:00
}
set majorityThreshold(value: number) {
this._majorityThreshold = value;
2022-06-27 19:36:29 +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;
}
private _relayTimeout = 2;
get relayTimeout(): number {
return this._relayTimeout;
2022-06-27 19:36:29 +00:00
}
set relayTimeout(value: number) {
this._relayTimeout = value;
2022-06-27 19:36:29 +00:00
}
private _relays: string[] = [];
get relays(): string[] {
return this._relays;
}
private _ready?: Promise<void>;
get ready(): Promise<void> {
2022-07-27 00:58:41 +00:00
if (!this._ready) {
this._ready = this._dht.ready() as Promise<void>;
2022-07-27 00:58:41 +00:00
}
return this._ready;
}
2022-08-18 23:10:07 +00:00
private _bypassCache: boolean = false;
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
}
2022-09-22 13:34:07 +00:00
private _maxRelays: number = 0;
get maxRelays(): number {
return this._maxRelays;
}
set maxRelays(value: number) {
this._maxRelays = value;
}
2022-06-27 19:36:29 +00:00
public addRelay(pubkey: string): void {
this._relays.push(pubkey);
this._relays = [...new Set(this._relays)];
}
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 = [];
}
public wisdomQuery(
method: string,
module: string,
2022-06-27 19:36:29 +00:00
data: object | any[] = {},
bypassCache: boolean = false,
options = {}
): WisdomRpcQuery {
return new WisdomRpcQuery(
this,
{
method,
module,
data,
2022-08-18 23:10:07 +00:00
bypassCache: bypassCache || this._bypassCache,
},
options
).run();
}
public simpleQuery(
relay: string,
method: string,
module: string,
data: object | any[] = {},
2022-08-31 02:48:46 +00:00
bypassCache: boolean = false,
options: {}
): SimpleRpcQuery {
return new SimpleRpcQuery(
this,
relay,
{
method,
module,
data,
2022-08-31 02:48:46 +00:00
bypassCache: bypassCache || this._bypassCache,
},
options
).run();
2022-06-27 19:36:29 +00:00
}
}