*update dist

This commit is contained in:
Derrick Hammer 2023-03-18 12:11:41 -04:00
parent 50f21f0434
commit 0216b0d7ba
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
21 changed files with 175 additions and 111 deletions

2
dist/error.d.ts vendored
View File

@ -1,4 +1,2 @@
export declare const ERR_MAX_TRIES_HIT = "ERR_MAX_TRIES_HIT";
export declare const ERR_INVALID_SIGNATURE = "INVALID_SIGNATURE"; export declare const ERR_INVALID_SIGNATURE = "INVALID_SIGNATURE";
export declare const ERR_NO_RELAYS = "NO_RELAYS";
//# sourceMappingURL=error.d.ts.map //# sourceMappingURL=error.d.ts.map

2
dist/error.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,sBAAsB,CAAC;AACrD,eAAO,MAAM,qBAAqB,sBAAsB,CAAC;AACzD,eAAO,MAAM,aAAa,cAAc,CAAC"} {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,sBAAsB,CAAC"}

2
dist/error.js vendored
View File

@ -1,3 +1 @@
export const ERR_MAX_TRIES_HIT = "ERR_MAX_TRIES_HIT";
export const ERR_INVALID_SIGNATURE = "INVALID_SIGNATURE"; export const ERR_INVALID_SIGNATURE = "INVALID_SIGNATURE";
export const ERR_NO_RELAYS = "NO_RELAYS";

23
dist/network.d.ts vendored
View File

@ -1,10 +1,14 @@
import RpcNetworkQueryFactory from "./query/index.js"; import RpcNetworkQueryFactory from "./query/index.js";
export default class RpcNetwork { export default class RpcNetwork {
constructor(dht?: any); private _relaysAvailablePromise?;
private _relaysAvailableResolve?;
constructor(swarm?: any);
private _methods;
get methods(): Map<string, Set<string>>;
private _factory; private _factory;
get factory(): RpcNetworkQueryFactory; get factory(): RpcNetworkQueryFactory;
private _dht; private _swarm;
get dht(): any; get swarm(): any;
private _majorityThreshold; private _majorityThreshold;
get majorityThreshold(): number; get majorityThreshold(): number;
set majorityThreshold(value: number); set majorityThreshold(value: number);
@ -15,17 +19,16 @@ export default class RpcNetwork {
get relayTimeout(): number; get relayTimeout(): number;
set relayTimeout(value: number); set relayTimeout(value: number);
private _relays; private _relays;
get relays(): string[]; get relays(): Map<string, string[]>;
private _ready?; private _ready?;
get ready(): Promise<void>; get ready(): Promise<void>;
get readyWithRelays(): Promise<void>;
private _bypassCache; private _bypassCache;
get bypassCache(): boolean; get bypassCache(): boolean;
set bypassCache(value: boolean); set bypassCache(value: boolean);
private _maxRelays; getAvailableRelay(module: string, method: string): string;
get maxRelays(): number; getRelay(pubkey: string): any;
set maxRelays(value: number); private init;
addRelay(pubkey: string): void; private setupRelayPromise;
removeRelay(pubkey: string): boolean;
clearRelays(): void;
} }
//# sourceMappingURL=network.d.ts.map //# sourceMappingURL=network.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAUA,OAAO,sBAAsB,MAAM,kBAAkB,CAAC;AAEtD,MAAM,CAAC,OAAO,OAAO,UAAU;gBACjB,GAAG,MAAY;IAI3B,OAAO,CAAC,QAAQ,CAAoC;IAEpD,IAAI,OAAO,IAAI,sBAAsB,CAEpC;IAED,OAAO,CAAC,IAAI,CAAa;IAEzB,IAAI,GAAG,QAEN;IAED,OAAO,CAAC,kBAAkB,CAAQ;IAElC,IAAI,iBAAiB,IAAI,MAAM,CAE9B;IAED,IAAI,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAElC;IAED,OAAO,CAAC,aAAa,CAAM;IAE3B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,aAAa,CAAK;IAE1B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,OAAO,CAA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AO,CAAC,aAAa,CAAK;IAE1B,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAE7B;IAED,OAAO,CAAC,OAAO,CAAiD;IAEhE,IAAI,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAElC;IAED,OAAO,CAAC,MAAM,CAAC,CAAgB;IAE/B,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAMzB;IAED,IAAI,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAEnC;IAED,OAAO,CAAC,YAAY,CAAkB;IAEtC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,IAAI,WAAW,CAAC,KAAK,EAAE,OAAO,EAE7B;IAEM,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAYhD,QAAQ,CAAC,MAAM,EAAE,MAAM;IAQ9B,OAAO,CAAC,IAAI;IAyCZ,OAAO,CAAC,iBAAiB;CAK1B"}

95
dist/network.js vendored
View File

@ -1,17 +1,26 @@
// @ts-ignore // @ts-ignore
import DHT from "@hyperswarm/dht"; import Hyperswarm from "hyperswarm";
import RpcNetworkQueryFactory from "./query/index.js"; import RpcNetworkQueryFactory from "./query/index.js";
import b4a from "b4a";
import { createHash } from "./util.js";
export default class RpcNetwork { export default class RpcNetwork {
constructor(dht = new DHT()) { _relaysAvailablePromise;
this._dht = dht; _relaysAvailableResolve;
constructor(swarm = new Hyperswarm()) {
this._swarm = swarm;
this.init();
}
_methods = new Map();
get methods() {
return this._methods;
} }
_factory = new RpcNetworkQueryFactory(this); _factory = new RpcNetworkQueryFactory(this);
get factory() { get factory() {
return this._factory; return this._factory;
} }
_dht; _swarm;
get dht() { get swarm() {
return this._dht; return this._swarm;
} }
_majorityThreshold = 0.75; _majorityThreshold = 0.75;
get majorityThreshold() { get majorityThreshold() {
@ -34,17 +43,20 @@ export default class RpcNetwork {
set relayTimeout(value) { set relayTimeout(value) {
this._relayTimeout = value; this._relayTimeout = value;
} }
_relays = []; _relays = new Map();
get relays() { get relays() {
return this._relays; return this._relays;
} }
_ready; _ready;
get ready() { get ready() {
if (!this._ready) { if (!this._ready) {
this._ready = this._dht.ready(); this._ready = this._swarm.dht.ready();
} }
return this._ready; return this._ready;
} }
get readyWithRelays() {
return this.ready.then(() => this._relaysAvailablePromise);
}
_bypassCache = false; _bypassCache = false;
get bypassCache() { get bypassCache() {
return this._bypassCache; return this._bypassCache;
@ -52,26 +64,55 @@ export default class RpcNetwork {
set bypassCache(value) { set bypassCache(value) {
this._bypassCache = value; this._bypassCache = value;
} }
_maxRelays = 0; getAvailableRelay(module, method) {
get maxRelays() { method = `${module}.${method}`;
return this._maxRelays; let relays = this._methods.get(method) ?? new Set();
} if (!relays.size) {
set maxRelays(value) { throw Error("no available relay");
this._maxRelays = value;
}
addRelay(pubkey) {
this._relays.push(pubkey);
this._relays = [...new Set(this._relays)];
}
removeRelay(pubkey) {
if (!this._relays.includes(pubkey)) {
return false;
} }
delete this._relays[this._relays.indexOf(pubkey)]; return Array.from(relays)[Math.floor(Math.random() * relays.size)];
this._relays = Object.values(this._relays);
return true;
} }
clearRelays() { getRelay(pubkey) {
this._relays = []; if (this._relays.has(pubkey)) {
return this._relays.get(pubkey);
}
return undefined;
}
init() {
this._swarm.join(createHash("lumeweb"));
this.setupRelayPromise();
this._swarm.on("connection", async (relay) => {
const query = this._factory.simple({
relay,
query: { module: "core", method: "get_methods", data: null },
});
const resp = await query.result;
const pubkey = b4a.from(relay.remotePublicKey).toString("hex");
if (resp.data) {
this._relays.set(pubkey, relay);
resp.data.forEach((item) => {
const methods = this._methods.get(item) ?? new Set();
methods.add(pubkey);
this._methods.set(item, methods);
});
this._relaysAvailableResolve?.();
}
relay.on("close", () => {
this._methods.forEach((item) => {
if (item.has(pubkey)) {
item.delete(pubkey);
}
});
this.relays.delete(pubkey);
if (!this._relays.size) {
this.setupRelayPromise();
}
});
});
}
setupRelayPromise() {
this._relaysAvailablePromise = new Promise((resolve) => {
this._relaysAvailableResolve = resolve;
});
} }
} }

14
dist/query/base.d.ts vendored
View File

@ -17,11 +17,15 @@ export default abstract class RpcQueryBase {
protected _response?: RPCResponse; protected _response?: RPCResponse;
protected _error?: string; protected _error?: string;
protected _promiseResolve?: (data: any) => void; protected _promiseResolve?: (data: any) => void;
constructor( constructor({
network: RpcNetwork, network,
query: ClientRPCRequest | RPCRequest, query,
options?: RpcQueryOptions options,
); }: {
network: RpcNetwork;
query: ClientRPCRequest | RPCRequest;
options: RpcQueryOptions;
});
get result(): Promise<RPCResponse>; get result(): Promise<RPCResponse>;
protected handeTimeout(): void; protected handeTimeout(): void;
protected resolve(data?: RPCResponse, timeout?: boolean): void; protected resolve(data?: RPCResponse, timeout?: boolean): void;

View File

@ -1 +1 @@
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/query/base.ts"],"names":[],"mappings":";AAIA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACV,WAAW,EACZ,MAAM,sBAAsB,CAAC;AAI9B,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,YAAY;IACxC,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC;IAC/B,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC;IAC7B,SAAS,CAAC,QAAQ,EAAE,eAAe,CAAC;IAEpC,SAAS,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAClC,SAAS,CAAC,aAAa,CAAC,EAAE,GAAG,CAAC;IAC9B,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAS;IACpC,SAAS,CAAC,UAAU,EAAE,OAAO,CAAS;IACtC,SAAS,CAAC,SAAS,CAAC,EAAE,WAAW,CAAC;IAClC,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAC1B,SA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C,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,eAAe,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;gBAEpC,EACV,OAAO,EACP,KAAK,EACL,OAAY,GACb,EAAE;QACD,OAAO,EAAE,UAAU,CAAC;QACpB,KAAK,EAAE,gBAAgB,GAAG,UAAU,CAAC;QACrC,OAAO,EAAE,eAAe,CAAC;KAC1B;IAMD,IAAI,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,CAEjC;IAED,SAAS,CAAC,YAAY;IAItB,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,OAAO,GAAE,OAAe,GAAG,IAAI;IAc9D,GAAG,IAAI,IAAI;YAmBJ,MAAM;IASpB,SAAS,CAAC,iBAAiB,CAAC,MAAM,EAAE,QAAQ,GAAG,MAAM,CAAC,OAAO;IAO7D,SAAS,CAAC,QAAQ,CAAC,IAAI,IAAI,IAAI;cAEf,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,UAAU;CAyBvD"}

2
dist/query/base.js vendored
View File

@ -10,7 +10,7 @@ export default class RpcQueryBase {
_response; _response;
_error; _error;
_promiseResolve; _promiseResolve;
constructor(network, query, options = {}) { constructor({ network, query, options = {}, }) {
this._network = network; this._network = network;
this._query = query; this._query = query;
this._options = options; this._options = options;

View File

@ -4,12 +4,17 @@ import { RpcQueryOptions } from "../types.js";
import SimpleRpcQuery from "./simple.js"; import SimpleRpcQuery from "./simple.js";
export default class ClearCacheRpcQuery extends SimpleRpcQuery { export default class ClearCacheRpcQuery extends SimpleRpcQuery {
protected _relays: string[]; protected _relays: string[];
constructor( constructor({
network: RpcNetwork, network,
relays: string[], relays,
query: RPCRequest, query,
options: RpcQueryOptions options,
); }: {
network: RpcNetwork;
relays: string[];
query: RPCRequest;
options: RpcQueryOptions;
});
protected _run(): Promise<void>; protected _run(): Promise<void>;
protected queryRelay(): Promise<any>; protected queryRelay(): Promise<any>;
} }

View File

@ -1 +1 @@
{"version":3,"file":"clearCache.d.ts","sourceRoot":"","sources":["../../src/query/clearCache.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAuB,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C,OAAO,cAAc,MAAM,aAAa,CAAC;AAEzC,MAAM,CAAC,OAAO,OAAO,kBAAmB,SAAQ,cAAc;IAC5D,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;gBAG1B,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,MAAM,EAAE,EAChB,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,eAAe;cAMV,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAQrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;CAc3C"} {"version":3,"file":"clearCache.d.ts","sourceRoot":"","sources":["../../src/query/clearCache.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAuB,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,cAAc,MAAM,aAAa,CAAC;AAEzC,MAAM,CAAC,OAAO,OAAO,kBAAmB,SAAQ,cAAc;IAC5D,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;gBAEhB,EACV,OAAO,EACP,MAAM,EACN,KAAK,EACL,OAAO,GACR,EAAE;QACD,OAAO,EAAE,UAAU,CAAC;QACpB,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,KAAK,EAAE,UAAU,CAAC;QAClB,OAAO,EAAE,eAAe,CAAC;KAC1B;cAKe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAOrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;CAiB3C"}

View File

@ -1,21 +1,19 @@
import { hashQuery } from "../util.js"; import { hashQuery } from "../util.js";
import { getActiveRelay, setupRelay } from "../sharedRelay.js";
import SimpleRpcQuery from "./simple.js"; import SimpleRpcQuery from "./simple.js";
export default class ClearCacheRpcQuery extends SimpleRpcQuery { export default class ClearCacheRpcQuery extends SimpleRpcQuery {
_relays; _relays;
constructor(network, relays, query, options) { constructor({ network, relays, query, options, }) {
super(network, "", query, options); super({ network, relay: "", query, options });
this._relays = relays; this._relays = relays;
} }
async _run() { async _run() {
await setupRelay(this._network);
// @ts-ignore // @ts-ignore
this._relay = getActiveRelay().stream.remotePublicKey; this._relay = getActiveRelay().stream.remotePublicKey;
await this.queryRelay(); await this.queryRelay();
await this.checkResponses(); await this.checkResponses();
} }
async queryRelay() { async queryRelay() {
return this.queryRpc(getActiveRelay(), { return this.queryRpc(this._network.getAvailableRelay("rpc", "broadcast_request"), {
module: "rpc", module: "rpc",
method: "broadcast_request", method: "broadcast_request",
data: { data: {

10
dist/query/index.d.ts vendored
View File

@ -1,6 +1,5 @@
import { ClientRPCRequest, RPCRequest } from "@lumeweb/relay-types"; import { ClientRPCRequest, RPCRequest } from "@lumeweb/relay-types";
import { RpcQueryOptions } from "../types.js"; import { RpcQueryOptions } from "../types.js";
import WisdomRpcQuery from "./wisdom.js";
import SimpleRpcQuery from "./simple.js"; import SimpleRpcQuery from "./simple.js";
import ClearCacheRpcQuery from "./clearCache.js"; import ClearCacheRpcQuery from "./clearCache.js";
import RpcNetwork from "../network.js"; import RpcNetwork from "../network.js";
@ -8,13 +7,6 @@ import RpcQueryBase from "./base.js";
export default class RpcNetworkQueryFactory { export default class RpcNetworkQueryFactory {
private _network; private _network;
constructor(network: RpcNetwork); constructor(network: RpcNetwork);
wisdom({
query,
options,
}: {
query: ClientRPCRequest;
options?: RpcQueryOptions;
}): WisdomRpcQuery;
simple({ simple({
relay, relay,
query, query,
@ -34,5 +26,5 @@ export default class RpcNetworkQueryFactory {
options?: RpcQueryOptions; options?: RpcQueryOptions;
}): ClearCacheRpcQuery; }): ClearCacheRpcQuery;
} }
export { RpcNetwork, RpcQueryBase, SimpleRpcQuery, WisdomRpcQuery }; export { RpcNetwork, RpcQueryBase, SimpleRpcQuery };
//# sourceMappingURL=index.d.ts.map //# sourceMappingURL=index.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/query/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,cAAc,MAAM,aAAa,CAAC;AACzC,OAAO,cAAc,MAAM,aAAa,CAAC;AACzC,OAAO,kBAAkB,MAAM,iBAAiB,CAAC;AACjD,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,YAAY,MAAM,WAAW,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,sBAAsB;IACzC,OAAO,CAAC,QAAQ,CAAa;gBAEjB,OAAO,EAAE,UAAU;IAI/B,MAAM,CAAC,EACL,KAAK,EACL,OAAY,GACb,EAAE;QACD,KAAK,EAAE,gBAAgB,CAAC;QACxB,OAAO,CAAC,EAAE,eAAe,CAAC;KAC3B,GAAG,cAAc;IAWlB,MAAM,CAAC,EACL,KAAK,EACL,KAAK,EACL,OAAY,GACb,EAAE;QACD,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,gBAAgB,CAAC;QACxB,OAAO,CAAC,EAAE,eAAe,CAAC;KAC3B,GAAG,cAAc;IAYlB,UAAU,CAAC,EACT,MAAM,EACN,KAAK,EACL,OAAY,GACb,EAAE;QACD,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,KAAK,EAAE,UAAU,CAAC;QAClB,OAAO,CAAC,EAAE,eAAe,CAAC;KAC3B,GAAG,kBAAkB;CAGvB;AAED,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,CAAC"} {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/query/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,cAAc,MAAM,aAAa,CAAC;AACzC,OAAO,kBAAkB,MAAM,iBAAiB,CAAC;AACjD,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,YAAY,MAAM,WAAW,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,sBAAsB;IACzC,OAAO,CAAC,QAAQ,CAAa;gBAEjB,OAAO,EAAE,UAAU;IAI/B,MAAM,CAAC,EACL,KAAK,EACL,KAAK,EACL,OAAY,GACb,EAAE;QACD,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,gBAAgB,CAAC;QACxB,OAAO,CAAC,EAAE,eAAe,CAAC;KAC3B,GAAG,cAAc;IAYlB,UAAU,CAAC,EACT,MAAM,EACN,KAAK,EACL,OAAY,GACb,EAAE;QACD,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,KAAK,EAAE,UAAU,CAAC;QAClB,OAAO,CAAC,EAAE,eAAe,CAAC;KAC3B,GAAG,kBAAkB;CAQvB;AAED,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC"}

29
dist/query/index.js vendored
View File

@ -1,4 +1,3 @@
import WisdomRpcQuery from "./wisdom.js";
import SimpleRpcQuery from "./simple.js"; import SimpleRpcQuery from "./simple.js";
import ClearCacheRpcQuery from "./clearCache.js"; import ClearCacheRpcQuery from "./clearCache.js";
import RpcNetwork from "../network.js"; import RpcNetwork from "../network.js";
@ -8,20 +7,24 @@ export default class RpcNetworkQueryFactory {
constructor(network) { constructor(network) {
this._network = network; this._network = network;
} }
wisdom({ query, options = {}, }) {
return new WisdomRpcQuery(this._network, {
...query,
bypassCache: query.bypassCache || this._network.bypassCache,
}, options).run();
}
simple({ relay, query, options = {}, }) { simple({ relay, query, options = {}, }) {
return new SimpleRpcQuery(this._network, relay, { return new SimpleRpcQuery({
...query, network: this._network,
bypassCache: query.bypassCache || this._network.bypassCache, relay,
}, options).run(); query: {
...query,
bypassCache: query?.bypassCache || this._network.bypassCache,
},
options,
}).run();
} }
clearCache({ relays, query, options = {}, }) { clearCache({ relays, query, options = {}, }) {
return new ClearCacheRpcQuery(this._network, relays, query, options).run(); return new ClearCacheRpcQuery({
network: this._network,
query,
relays,
options,
}).run();
} }
} }
export { RpcNetwork, RpcQueryBase, SimpleRpcQuery, WisdomRpcQuery }; export { RpcNetwork, RpcQueryBase, SimpleRpcQuery };

View File

@ -3,14 +3,19 @@ import { ClientRPCRequest } from "@lumeweb/relay-types";
import { RpcQueryOptions } from "../types.js"; import { RpcQueryOptions } from "../types.js";
import RpcQueryBase from "./base.js"; import RpcQueryBase from "./base.js";
export default class SimpleRpcQuery extends RpcQueryBase { export default class SimpleRpcQuery extends RpcQueryBase {
protected _relay: string; protected _relay?: string | any;
protected _query: ClientRPCRequest; protected _query: ClientRPCRequest;
constructor( constructor({
network: RpcNetwork, network,
relay: string, relay,
query: ClientRPCRequest, query,
options: RpcQueryOptions options,
); }: {
network: RpcNetwork;
relay?: string | any;
query: ClientRPCRequest;
options: RpcQueryOptions;
});
protected _run(): Promise<void>; protected _run(): Promise<void>;
protected queryRelay(): Promise<any>; protected queryRelay(): Promise<any>;
protected checkResponses(): Promise<void>; protected checkResponses(): Promise<void>;

View File

@ -1 +1 @@
{"version":3,"file":"simple.d.ts","sourceRoot":"","sources":["../../src/query/simple.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EACL,gBAAgB,EAIjB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAW9C,OAAO,YAAY,MAAM,WAAW,CAAC;AAErC,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,YAAY;IACtD,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IACzB,UAAkB,MAAM,EAAE,gBAAgB,CAAC;gBAGzC,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,gBAAgB,EACvB,OAAO,EAAE,eAAe;cAMV,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAKrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cAwC1B,cAAc;CAmB/B"} {"version":3,"file":"simple.d.ts","sourceRoot":"","sources":["../../src/query/simple.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAe,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAK9C,OAAO,YAAY,MAAM,WAAW,CAAC;AAGrC,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,YAAY;IACtD,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IAChC,UAAkB,MAAM,EAAE,gBAAgB,CAAC;gBAE/B,EACV,OAAO,EACP,KAAK,EACL,KAAK,EACL,OAAO,GACR,EAAE;QACD,OAAO,EAAE,UAAU,CAAC;QACpB,KAAK,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;QACrB,KAAK,EAAE,gBAAgB,CAAC;QACxB,OAAO,EAAE,eAAe,CAAC;KAC1B;cAKe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAKrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cAwD1B,cAAc;CAmB/B"}

41
dist/query/simple.js vendored
View File

@ -1,12 +1,12 @@
import b4a from "b4a"; import b4a from "b4a";
import { hashQuery, isPromise, validateTimestampedResponse, } from "../util.js"; import { hashQuery, isPromise, validateTimestampedResponse } from "../util.js";
import RPC from "@lumeweb/rpc"; import RPC from "@lumeweb/rpc";
import { ERR_INVALID_SIGNATURE } from "../error.js"; import { ERR_INVALID_SIGNATURE } from "../error.js";
import RpcQueryBase from "./base.js"; import RpcQueryBase from "./base.js";
export default class SimpleRpcQuery extends RpcQueryBase { export default class SimpleRpcQuery extends RpcQueryBase {
_relay; _relay;
constructor(network, relay, query, options) { constructor({ network, relay, query, options, }) {
super(network, query, options); super({ network, query, options });
this._relay = relay; this._relay = relay;
} }
async _run() { async _run() {
@ -14,16 +14,31 @@ export default class SimpleRpcQuery extends RpcQueryBase {
await this.checkResponses(); await this.checkResponses();
} }
async queryRelay() { async queryRelay() {
let socket; let socket = this._relay;
try { if (socket) {
socket = this._network.dht.connect(b4a.from(this._relay, "hex")); if (socket === "string") {
if (isPromise(socket)) { try {
socket = await socket; const relay = this._network.getRelay(socket);
if (this._network.getRelay(socket)) {
socket = relay;
}
}
catch { }
}
if (socket === "string") {
try {
socket = this._network.swarm.connect(b4a.from(this._relay, "hex"));
if (isPromise(socket)) {
socket = await socket;
}
}
catch { }
} }
} }
catch (e) { if (!socket) {
return; socket = this._network.getAvailableRelay(this._query.module, this._query.method);
} }
this._relay = socket;
await socket.opened; await socket.opened;
const rpc = new RPC(socket); const rpc = new RPC(socket);
if (this._query.bypassCache) { if (this._query.bypassCache) {
@ -41,12 +56,8 @@ export default class SimpleRpcQuery extends RpcQueryBase {
await this.queryRpc(rpc, this._query); await this.queryRpc(rpc, this._query);
} }
catch (e) { catch (e) {
// @ts-ignore
rpc.end();
throw e; throw e;
} }
// @ts-ignore
rpc.end();
} }
async checkResponses() { async checkResponses() {
let response = this._response; let response = this._response;
@ -54,7 +65,7 @@ export default class SimpleRpcQuery extends RpcQueryBase {
response = { error: this._error }; response = { error: this._error };
} }
if (!response.error && if (!response.error &&
!validateTimestampedResponse(b4a.from(this._relay, "hex"), response)) { !validateTimestampedResponse(b4a.from(this._relay.remotePublicKey, "hex"), response)) {
response = { error: ERR_INVALID_SIGNATURE }; response = { error: ERR_INVALID_SIGNATURE };
} }
this.resolve(response); this.resolve(response);

1
dist/util.d.ts vendored
View File

@ -12,4 +12,5 @@ export declare function validateTimestampedResponse(
response: RPCResponse response: RPCResponse
): boolean; ): boolean;
export declare function hashQuery(query: RPCRequest): string; export declare function hashQuery(query: RPCRequest): string;
export declare function createHash(data: string): Buffer;
//# sourceMappingURL=util.d.ts.map //# sourceMappingURL=util.d.ts.map

2
dist/util.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAOpE,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,WAM1C;AAKD,wBAAgB,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,GAAE,GAAQ,GAAG,GAAG,EAAE,CA0C1D;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,EACrB,WAAW,UAAQ,GAClB,OAAO,CAmBT;AAED,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,GACpB,OAAO,CAET;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAUnD"} {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAOpE,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,WAM1C;AAKD,wBAAgB,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,GAAE,GAAQ,GAAG,GAAG,EAAE,CA0C1D;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,EACrB,WAAW,UAAQ,GAClB,OAAO,CAkBT;AAED,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,WAAW,GACpB,OAAO,CAET;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAUnD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM/C"}

9
dist/util.js vendored
View File

@ -49,8 +49,7 @@ export function flatten(target, opts = {}) {
export function validateResponse(relay, response, timestamped = false) { export function validateResponse(relay, response, timestamped = false) {
const field = response.signedField || "data"; const field = response.signedField || "data";
// @ts-ignore // @ts-ignore
const data = response[field]; let json = response[field];
let json = data;
if (typeof json !== "string") { if (typeof json !== "string") {
json = stringify(json); json = stringify(json);
} }
@ -73,3 +72,9 @@ export function hashQuery(query) {
sodium.crypto_generichash(queryHash, Buffer.from(stringify(clonedQuery))); sodium.crypto_generichash(queryHash, Buffer.from(stringify(clonedQuery)));
return queryHash.toString("hex"); return queryHash.toString("hex");
} }
export function createHash(data) {
const buffer = b4a.from(data);
let hash = b4a.allocUnsafe(32);
sodium.crypto_generichash(hash, buffer);
return hash;
}