diff --git a/dist/query/wisdom.d.ts b/dist/query/wisdom.d.ts deleted file mode 100644 index c65d77d..0000000 --- a/dist/query/wisdom.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { - ClientRPCRequest, - RPCBroadcastResponse, - RPCResponse, -} from "@lumeweb/relay-types"; -import RpcQueryBase from "./base.js"; -export default class WisdomRpcQuery extends RpcQueryBase { - protected _response?: RPCBroadcastResponse; - protected _query: ClientRPCRequest; - get result(): Promise; - protected _run(): Promise; - protected resolve(data?: RPCResponse, timeout?: boolean): void; - protected queryRelay(): Promise; - protected checkResponse(): Promise; - protected getRelays(): string[]; -} -//# sourceMappingURL=wisdom.d.ts.map diff --git a/dist/query/wisdom.d.ts.map b/dist/query/wisdom.d.ts.map deleted file mode 100644 index ca1b971..0000000 --- a/dist/query/wisdom.d.ts.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"wisdom.d.ts","sourceRoot":"","sources":["../../src/query/wisdom.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAEhB,oBAAoB,EAEpB,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAW9B,OAAO,YAAY,MAAM,WAAW,CAAC;AAUrC,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,YAAY;IACtD,UAAkB,SAAS,CAAC,EAAE,oBAAoB,CAAC;IACnD,UAAkB,MAAM,EAAE,gBAAgB,CAAC;IAC3C,IAAI,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,CAEjC;cAEe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAMrC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,OAAO,GAAE,OAAe,GAAG,IAAI;cAcrD,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cA8B1B,aAAa;IA4E7B,SAAS,CAAC,SAAS,IAAI,MAAM,EAAE;CAmBhC"} \ No newline at end of file diff --git a/dist/query/wisdom.js b/dist/query/wisdom.js deleted file mode 100644 index 565a7f6..0000000 --- a/dist/query/wisdom.js +++ /dev/null @@ -1,122 +0,0 @@ -import { clearTimeout } from "timers"; -import b4a from "b4a"; -import { flatten, validateResponse, validateTimestampedResponse, } from "../util.js"; -import { blake2b } from "libskynet"; -import { ERR_INVALID_SIGNATURE, ERR_NO_RELAYS } from "../error.js"; -import RpcQueryBase from "./base.js"; -import { getActiveRelay, setupRelay } from "../sharedRelay.js"; -function flatHash(data) { - const flattenedData = flatten(data).sort(); - return Buffer.from(blake2b(Buffer.from(JSON.stringify(flattenedData)))).toString("hex"); -} -export default class WisdomRpcQuery extends RpcQueryBase { - get result() { - return this._promise; - } - async _run() { - await setupRelay(this._network); - await this.queryRelay(); - await this.checkResponse(); - } - resolve(data, timeout = false) { - clearTimeout(this._timeoutTimer); - this._timeout = timeout; - this._completed = true; - if (timeout) { - data = { - error: "timeout", - }; - } - this._promiseResolve?.(data); - } - async queryRelay() { - let activeRelay = getActiveRelay(); - let relays = this.getRelays(); - if (!relays.length) { - throw new Error(ERR_NO_RELAYS); - } - if (this._query.bypassCache) { - delete this._query.bypassCache; - const clearCacheQuery = this._network.factory.clearCache({ - relays, - query: this._query, - }); - await clearCacheQuery.result; - } - if ("bypassCache" in this._query) { - delete this._query.bypassCache; - } - return this.queryRpc(activeRelay, { - module: "rpc", - method: "broadcast_request", - data: { - request: this._query, - relays, - }, - }); - } - async checkResponse() { - if (this._error) { - this.resolve({ error: this._error }); - return; - } - if (!validateResponse( - // @ts-ignore - getActiveRelay().stream.remotePublicKey, this._response)) { - this.resolve({ error: ERR_INVALID_SIGNATURE }); - return; - } - let relays = []; - for (const relay in this._response?.relays) { - const resp = this._response?.relays[relay]; - if (resp?.error) { - continue; - } - if (validateTimestampedResponse(b4a.from(relay, "hex"), resp)) { - relays.push(resp); - } - } - if (!relays.length) { - this.resolve({ error: ERR_NO_RELAYS }); - return; - } - const responseObjects = relays.reduce((output, item) => { - const field = item.signedField || "data"; - // @ts-ignore - const hash = flatHash(item[field]); - output[hash] = item?.data; - return output; - }, {}); - const responses = relays.reduce((output, item) => { - const field = item.signedField || "data"; - // @ts-ignore - const hash = flatHash(item[field]); - output[hash] = output[hash] ?? 0; - output[hash]++; - return output; - }, {}); - for (const responseHash in responses) { - if (responses[responseHash] / relays.length >= - this._network.majorityThreshold) { - let response = responseObjects[responseHash]; - response = { data: response }; - this.resolve(response); - break; - } - } - } - getRelays() { - if (this._network.maxRelays === 0 || - this._network.relays.length <= this._network.maxRelays) { - return this._network.relays; - } - const list = []; - let available = this._network.relays; - while (list.length <= this._network.maxRelays) { - const item = Math.floor(Math.random() * available.length); - list.push(available[item]); - available.splice(item, 1); - } - return list; - } -} diff --git a/dist/sharedRelay.d.ts b/dist/sharedRelay.d.ts deleted file mode 100644 index adf5c6e..0000000 --- a/dist/sharedRelay.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import RPC from "@lumeweb/rpc"; -import RpcNetwork from "./network.js"; -export declare function setupRelay(network: RpcNetwork): Promise; -export declare function getActiveRelay(): RPC; -//# sourceMappingURL=sharedRelay.d.ts.map diff --git a/dist/sharedRelay.d.ts.map b/dist/sharedRelay.d.ts.map deleted file mode 100644 index e77c41e..0000000 --- a/dist/sharedRelay.d.ts.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"sharedRelay.d.ts","sourceRoot":"","sources":["../src/sharedRelay.ts"],"names":[],"mappings":"AAGA,OAAO,GAAG,MAAM,cAAc,CAAC;AAC/B,OAAO,UAAU,MAAM,cAAc,CAAC;AAItC,wBAAsB,UAAU,CAAC,OAAO,EAAE,UAAU,iBAqBnD;AAED,wBAAgB,cAAc,IAAI,GAAG,CAEpC"} \ No newline at end of file diff --git a/dist/sharedRelay.js b/dist/sharedRelay.js deleted file mode 100644 index 6253d63..0000000 --- a/dist/sharedRelay.js +++ /dev/null @@ -1,26 +0,0 @@ -import { ERR_NO_RELAYS } from "./error.js"; -import b4a from "b4a"; -import { isPromise } from "./util.js"; -import RPC from "@lumeweb/rpc"; -let activeRelay; -export async function setupRelay(network) { - const relays = network.relays; - if (!activeRelay) { - if (!relays.length) { - throw new Error(ERR_NO_RELAYS); - } - let relay = relays[Math.floor(Math.random() * relays.length)]; - let socket = network.dht.connect(b4a.from(relay, "hex")); - if (isPromise(socket)) { - socket = await socket; - } - await socket.opened; - activeRelay = new RPC(socket); - socket.once("close", () => { - activeRelay = undefined; - }); - } -} -export function getActiveRelay() { - return activeRelay; -}