rpc-client/dist/rpcNetwork.js

82 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-06-27 19:51:51 +00:00
import RpcQuery from "./rpcQuery.js";
2022-07-19 18:48:27 +00:00
// @ts-ignore
import DHT from "@hyperswarm/dht";
2022-06-27 19:51:51 +00:00
export default class RpcNetwork {
constructor(dht = new DHT()) {
this._dht = dht;
}
2022-07-23 15:43:05 +00:00
_dht;
2022-06-27 19:51:51 +00:00
get dht() {
return this._dht;
}
2022-07-23 15:43:05 +00:00
_majorityThreshold = 0.75;
get majorityThreshold() {
return this._majorityThreshold;
}
set majorityThreshold(value) {
this._majorityThreshold = value;
}
_maxTtl = 12 * 60 * 60;
2022-06-27 19:51:51 +00:00
get maxTtl() {
return this._maxTtl;
}
set maxTtl(value) {
this._maxTtl = value;
}
2022-07-23 15:43:05 +00:00
_queryTimeout = 30;
2022-06-27 19:51:51 +00:00
get queryTimeout() {
return this._queryTimeout;
}
set queryTimeout(value) {
this._queryTimeout = value;
}
2022-07-23 15:43:05 +00:00
_relayTimeout = 2;
get relayTimeout() {
return this._relayTimeout;
2022-06-27 19:51:51 +00:00
}
2022-07-23 15:43:05 +00:00
set relayTimeout(value) {
this._relayTimeout = value;
2022-06-27 19:51:51 +00:00
}
2022-07-23 15:43:05 +00:00
_relays = [];
get relays() {
return this._relays;
}
_ready;
get ready() {
2022-07-27 00:59:27 +00:00
if (!this._ready) {
this._ready = this._dht.ready();
}
2022-07-23 15:43:05 +00:00
return this._ready;
}
_force = false;
2022-06-27 19:51:51 +00:00
get force() {
return this._force;
}
set force(value) {
this._force = value;
}
addRelay(pubkey) {
this._relays.push(pubkey);
this._relays = [...new Set(this._relays)];
}
2022-06-27 20:26:13 +00:00
removeRelay(pubkey) {
if (!this._relays.includes(pubkey)) {
return false;
}
delete this._relays[this._relays.indexOf(pubkey)];
this._relays = Object.values(this._relays);
return true;
}
clearRelays() {
this._relays = [];
}
2022-06-27 19:51:51 +00:00
query(query, chain, data = {}, force = false) {
return new RpcQuery(this, {
query,
chain,
data,
force: force || this._force,
});
}
}