*Update dist
This commit is contained in:
parent
47daf47f45
commit
79bf18a2ed
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"rpcNetwork.d.ts","sourceRoot":"","sources":["../src/rpcNetwork.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,eAAe,CAAC;AAMrC,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,kBAAkB,CAAQ;IAClC,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,aAAa,CAAM;IAC3B,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,MAAM,CAAkB;gBAEpB,GAAG,MAAY;IAK3B,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAEzB;IAED,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAED,IAAI,GAAG,QAEN;IAED,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,IAAI,MAAM,CAAC,KAAK,EAAE,MAAM,EAEvB;IAED,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED,IAAI,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAElC;IAED,IAAI,KAAK,IAAI,OAAO,CAEnB;IAED,IAAI,KAAK,CAAC,KAAK,EAAE,OAAO,EAEvB;IAEM,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAK9B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAWpC,WAAW,IAAI,IAAI;IAInB,KAAK,CACV,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,KAAK,GAAE,OAAe,GACrB,QAAQ;CAQZ"}
|
{"version":3,"file":"rpcNetwork.d.ts","sourceRoot":"","sources":["../src/rpcNetwork.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,eAAe,CAAC;AAIrC,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,kBAAkB,CAAQ;IAClC,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,aAAa,CAAM;IAC3B,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,MAAM,CAAkB;gBAEpB,GAAG,MAAY;IAK3B,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAEzB;IAED,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAED,IAAI,GAAG,QAEN;IAED,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,IAAI,MAAM,CAAC,KAAK,EAAE,MAAM,EAEvB;IAED,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED,IAAI,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAElC;IAED,IAAI,KAAK,IAAI,OAAO,CAEnB;IAED,IAAI,KAAK,CAAC,KAAK,EAAE,OAAO,EAEvB;IAEM,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAK9B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAWpC,WAAW,IAAI,IAAI;IAInB,KAAK,CACV,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,MAAM,GAAG,GAAG,EAAO,EACzB,KAAK,GAAE,OAAe,GACrB,QAAQ;CAQZ"}
|
|
@ -1,15 +1,15 @@
|
||||||
// tslint:disable:no-var-requires
|
|
||||||
import { createRequire } from "module";
|
|
||||||
import RpcQuery from "./rpcQuery.js";
|
import RpcQuery from "./rpcQuery.js";
|
||||||
const require = createRequire(import.meta.url);
|
// @ts-ignore
|
||||||
const DHT = require("@hyperswarm/dht");
|
import DHT from "@hyperswarm/dht";
|
||||||
export default class RpcNetwork {
|
export default class RpcNetwork {
|
||||||
|
_dht;
|
||||||
|
_majorityThreshold = 0.75;
|
||||||
|
_maxTtl = 12 * 60 * 60;
|
||||||
|
_queryTimeout = 30;
|
||||||
|
_relays = [];
|
||||||
|
_ready;
|
||||||
|
_force = false;
|
||||||
constructor(dht = new DHT()) {
|
constructor(dht = new DHT()) {
|
||||||
this._majorityThreshold = 0.75;
|
|
||||||
this._maxTtl = 12 * 60 * 60;
|
|
||||||
this._queryTimeout = 30;
|
|
||||||
this._relays = [];
|
|
||||||
this._force = false;
|
|
||||||
this._dht = dht;
|
this._dht = dht;
|
||||||
this._ready = this._dht.ready();
|
this._ready = this._dht.ready();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"rpcQuery.d.ts","sourceRoot":"","sources":["../src/rpcQuery.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAe,MAAM,SAAS,CAAC;AAElD,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC3B,OAAO,CAAC,QAAQ,CAAa;IAC7B,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,QAAQ,CAAC,CAAe;IAChC,OAAO,CAAC,aAAa,CAAC,CAAM;IAC5B,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,UAAU,CAAkB;IACpC,OAAO,CAAC,UAAU,CAAwC;IAC1D,OAAO,CAAC,eAAe,CAAC,CAAsB;gBAElC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU;IAMlD,IAAI,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,CAE1B;IAED,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,OAAO;YAQD,IAAI;YA2BJ,UAAU;IAiBxB,OAAO,CAAC,cAAc;IAgCtB,OAAO,CAAC,KAAK;CASd"}
|
{"version":3,"file":"rpcQuery.d.ts","sourceRoot":"","sources":["../src/rpcQuery.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAe,MAAM,SAAS,CAAC;AAIlD,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC3B,OAAO,CAAC,QAAQ,CAAa;IAC7B,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,QAAQ,CAAC,CAAe;IAChC,OAAO,CAAC,aAAa,CAAC,CAAM;IAC5B,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,UAAU,CAAkB;IACpC,OAAO,CAAC,UAAU,CAAwC;IAC1D,OAAO,CAAC,eAAe,CAAC,CAAsB;gBAElC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU;IAMlD,IAAI,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,CAE1B;IAED,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,OAAO;YAQD,IAAI;YA2BJ,UAAU;IA0BxB,OAAO,CAAC,cAAc;IA0CtB,OAAO,CAAC,KAAK;CASd"}
|
|
@ -1,10 +1,17 @@
|
||||||
import { clearTimeout, setTimeout } from "timers";
|
import { clearTimeout, setTimeout } from "timers";
|
||||||
import { pack, unpack } from "msgpackr";
|
import { pack, unpack } from "msgpackr";
|
||||||
|
import { Buffer } from "buffer";
|
||||||
|
import { blake2b } from "libskynet";
|
||||||
export default class RpcQuery {
|
export default class RpcQuery {
|
||||||
|
_network;
|
||||||
|
_query;
|
||||||
|
_promise;
|
||||||
|
_timeoutTimer;
|
||||||
|
_timeout = false;
|
||||||
|
_completed = false;
|
||||||
|
_responses = {};
|
||||||
|
_promiseResolve;
|
||||||
constructor(network, query) {
|
constructor(network, query) {
|
||||||
this._timeout = false;
|
|
||||||
this._completed = false;
|
|
||||||
this._responses = {};
|
|
||||||
this._network = network;
|
this._network = network;
|
||||||
this._query = query;
|
this._query = query;
|
||||||
this.init();
|
this.init();
|
||||||
|
@ -41,7 +48,16 @@ export default class RpcQuery {
|
||||||
this.checkResponses();
|
this.checkResponses();
|
||||||
}
|
}
|
||||||
async queryRelay(relay) {
|
async queryRelay(relay) {
|
||||||
const socket = this._network.dht.connect(Buffer.from(relay, "hex"));
|
let socket;
|
||||||
|
try {
|
||||||
|
socket = this._network.dht.connect(Buffer.from(relay, "hex"));
|
||||||
|
if (isPromise(socket)) {
|
||||||
|
socket = await socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
socket.on("data", (res) => {
|
socket.on("data", (res) => {
|
||||||
socket.end();
|
socket.end();
|
||||||
|
@ -57,25 +73,31 @@ export default class RpcQuery {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
checkResponses() {
|
checkResponses() {
|
||||||
const responses = {};
|
|
||||||
const responseStore = this._responses;
|
const responseStore = this._responses;
|
||||||
const responseStoreKeys = Object.keys(responseStore);
|
const responseStoreData = Object.values(responseStore);
|
||||||
// tslint:disable-next-line:forin
|
const responseObjects = responseStoreData.reduce((output, item) => {
|
||||||
for (const peer in responseStore) {
|
const hash = Buffer.from(blake2b(Buffer.from(JSON.stringify(item?.data)))).toString("hex");
|
||||||
const responseIndex = responseStoreKeys.indexOf(peer);
|
output[hash] = item?.data;
|
||||||
responses[responseIndex] = responses[responseIndex] ?? 0;
|
return output;
|
||||||
responses[responseIndex]++;
|
}, {});
|
||||||
}
|
const responses = responseStoreData.reduce((output, item) => {
|
||||||
for (const responseIndex in responses) {
|
const hash = Buffer.from(blake2b(Buffer.from(JSON.stringify(item?.data)))).toString("hex");
|
||||||
if (responses[responseIndex] / responseStoreKeys.length >=
|
output[hash] = output[hash] ?? 0;
|
||||||
|
output[hash]++;
|
||||||
|
return output;
|
||||||
|
}, {});
|
||||||
|
for (const responseHash in responses) {
|
||||||
|
if (responses[responseHash] / responseStoreData.length >=
|
||||||
this._network.majorityThreshold) {
|
this._network.majorityThreshold) {
|
||||||
const response = responseStore[responseStoreKeys[parseInt(responseIndex, 10)]];
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (null === response || null === response?.data) {
|
const response = responseObjects[responseHash];
|
||||||
|
// @ts-ignore
|
||||||
|
if (null === response) {
|
||||||
this.retry();
|
this.retry();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.resolve(response?.data);
|
this.resolve(response);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,3 +109,8 @@ export default class RpcQuery {
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function isPromise(obj) {
|
||||||
|
return (!!obj &&
|
||||||
|
(typeof obj === "object" || typeof obj === "function") &&
|
||||||
|
typeof obj.then === "function");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue