refactor: move to ci, and update deps

This commit is contained in:
Derrick Hammer 2023-07-04 04:47:59 -04:00
parent 039583d77e
commit 062ed87f1e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
30 changed files with 23 additions and 681 deletions

5
.presetterrc.json Normal file
View File

@ -0,0 +1,5 @@
{
"preset": [
"@lumeweb/node-library-preset"
]
}

2
dist/error.d.ts vendored
View File

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

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

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

1
dist/error.js vendored
View File

@ -1 +0,0 @@
export const ERR_INVALID_SIGNATURE = "INVALID_SIGNATURE";

5
dist/index.d.ts vendored
View File

@ -1,5 +0,0 @@
export * from "./types.js";
export * from "./query/index.js";
export * from "./network.js";
export * from "./util.js";
//# sourceMappingURL=index.d.ts.map

1
dist/index.d.ts.map vendored
View File

@ -1 +0,0 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}

4
dist/index.js vendored
View File

@ -1,4 +0,0 @@
export * from "./types.js";
export * from "./query/index.js";
export * from "./network.js";
export * from "./util.js";

34
dist/network.d.ts vendored
View File

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

View File

@ -1 +0,0 @@
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAEA,OAAO,sBAAsB,MAAM,kBAAkB,CAAC;AAItD,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,OAAO,CAAC,uBAAuB,CAAC,CAAgB;IAChD,OAAO,CAAC,uBAAuB,CAAC,CAAW;gBAC/B,KAAK,MAAmB;IAKpC,OAAO,CAAC,QAAQ,CAA4D;IAE5E,IAAI,OAAO,IAAI,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAEtC;IAED,OAAO,CAAC,QAAQ,CAAoC;IAEpD,IAAI,OAAO,IAAI,sBAAsB,CAEpC;IAED,OAAO,CAAC,MAAM,CAAoB;IAElC,IAAI,KAAK,QAER;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,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,CAQzB;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;IAchD,QAAQ,CAAC,MAAM,EAAE,MAAM;IAQ9B,OAAO,CAAC,IAAI;IA+CZ,OAAO,CAAC,iBAAiB;CAK1B"}

124
dist/network.js vendored
View File

@ -1,124 +0,0 @@
// @ts-ignore
import Hyperswarm from "hyperswarm";
import RpcNetworkQueryFactory from "./query/index.js";
import b4a from "b4a";
import { createHash, maybeGetAsyncProperty } from "./util.js";
export default class RpcNetwork {
_relaysAvailablePromise;
_relaysAvailableResolve;
constructor(swarm = new Hyperswarm()) {
this._swarm = swarm;
this.init();
}
_methods = new Map();
get methods() {
return this._methods;
}
_factory = new RpcNetworkQueryFactory(this);
get factory() {
return this._factory;
}
_swarm;
get swarm() {
return this._swarm;
}
_majorityThreshold = 0.75;
get majorityThreshold() {
return this._majorityThreshold;
}
set majorityThreshold(value) {
this._majorityThreshold = value;
}
_queryTimeout = 30;
get queryTimeout() {
return this._queryTimeout;
}
set queryTimeout(value) {
this._queryTimeout = value;
}
_relayTimeout = 2;
get relayTimeout() {
return this._relayTimeout;
}
set relayTimeout(value) {
this._relayTimeout = value;
}
_relays = new Map();
get relays() {
return this._relays;
}
_ready;
get ready() {
if (!this._ready) {
this._ready = maybeGetAsyncProperty(this._swarm.dht).then((dht) => dht.ready());
}
return this._ready;
}
get readyWithRelays() {
return this.ready.then(() => this._relaysAvailablePromise);
}
_bypassCache = false;
get bypassCache() {
return this._bypassCache;
}
set bypassCache(value) {
this._bypassCache = value;
}
getAvailableRelay(module, method) {
method = `${module}.${method}`;
let relays = this._methods.get(method) ?? new Set();
if (!relays.size) {
throw Error("no available relay");
}
return this._relays.get(Array.from(relays)[Math.floor(Math.random() * relays.size)]);
}
getRelay(pubkey) {
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 pubkey = b4a
.from(await maybeGetAsyncProperty(relay.remotePublicKey))
.toString("hex");
relay.once("close", () => {
this._methods.forEach((item) => {
if (item.has(pubkey)) {
item.delete(pubkey);
}
});
this.relays.delete(pubkey);
if (!this._relays.size) {
this.setupRelayPromise();
}
});
const query = this._factory.simple({
relay,
query: { module: "core", method: "get_methods", data: null },
});
const resp = await query.result;
if (resp.error) {
relay.end();
return;
}
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?.();
}
});
}
setupRelayPromise() {
this._relaysAvailablePromise = new Promise((resolve) => {
this._relaysAvailableResolve = resolve;
});
}
}

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

@ -1,38 +0,0 @@
/// <reference types="node" />
import RpcNetwork from "../network.js";
import { RpcQueryOptions } from "../types.js";
import type {
ClientRPCRequest,
RPCRequest,
RPCResponse,
} from "@lumeweb/interface-relay";
export default abstract class RpcQueryBase {
protected _network: RpcNetwork;
protected _query: RPCRequest;
protected _options: RpcQueryOptions;
protected _promise?: Promise<any>;
protected _timeoutTimer?: any;
protected _timeout: boolean;
protected _completed: boolean;
protected _response?: RPCResponse;
protected _error?: string;
protected _promiseResolve?: (data: any) => void;
constructor({
network,
query,
options,
}: {
network: RpcNetwork;
query: ClientRPCRequest | RPCRequest;
options: RpcQueryOptions;
});
get result(): Promise<RPCResponse>;
protected handeTimeout(): void;
protected resolve(data?: RPCResponse, timeout?: boolean): void;
run(): this;
private _doRun;
protected setupRelayTimeout(reject: Function): NodeJS.Timeout;
protected abstract _run(): void;
protected queryRpc(rpc: any, request: RPCRequest): Promise<unknown>;
}
//# sourceMappingURL=base.d.ts.map

View File

@ -1 +0,0 @@
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/query/base.ts"],"names":[],"mappings":";AACA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACV,WAAW,EACZ,MAAM,0BAA0B,CAAC;AAElC,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,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"}

84
dist/query/base.js vendored
View File

@ -1,84 +0,0 @@
import { clearTimeout, setTimeout } from "timers";
export default class RpcQueryBase {
_network;
_query;
_options;
_promise;
_timeoutTimer;
_timeout = false;
_completed = false;
_response;
_error;
_promiseResolve;
constructor({ network, query, options = {}, }) {
this._network = network;
this._query = query;
this._options = options;
}
get result() {
return this._promise;
}
handeTimeout() {
this.resolve(undefined, true);
}
resolve(data, timeout = false) {
clearTimeout(this._timeoutTimer);
this._timeout = timeout;
this._completed = true;
if (timeout) {
data = {
error: "timeout",
};
}
this._promiseResolve?.(data);
}
run() {
this._promise =
this._promise ??
new Promise((resolve) => {
this._promiseResolve = resolve;
});
this._timeoutTimer =
this._timeoutTimer ??
setTimeout(this.handeTimeout.bind(this), (this._options?.queryTimeout || this._network.queryTimeout) * 1000);
this._doRun();
return this;
}
async _doRun() {
try {
await this._network.ready;
await this._run();
}
catch (e) {
this._promiseResolve?.({ error: e?.message || e?.error });
}
}
setupRelayTimeout(reject) {
return setTimeout(() => {
this._error = "timeout";
reject("timeout");
}, (this._options.relayTimeout || this._network.relayTimeout) * 1000);
}
async queryRpc(rpc, request) {
let timer;
return new Promise((resolve, reject) => {
rpc
// @ts-ignore
.request(`${request.module}.${request.method}`, request.data)
.then((resp) => {
if (resp.error) {
throw new Error(resp.error);
}
clearTimeout(timer);
this._response = resp;
resolve(null);
})
.catch((e) => {
this._error = e.message;
reject({ error: e.message });
clearTimeout(timer);
});
timer = this.setupRelayTimeout(reject);
});
}
}

View File

@ -1,21 +0,0 @@
import RpcNetwork from "../network.js";
import { RPCRequest } from "@lumeweb/interface-relay";
import { RpcQueryOptions } from "../types.js";
import SimpleRpcQuery from "./simple.js";
export default class ClearCacheRpcQuery extends SimpleRpcQuery {
protected _relays: string[];
constructor({
network,
relays,
query,
options,
}: {
network: RpcNetwork;
relays: string[];
query: RPCRequest;
options: RpcQueryOptions;
});
protected _run(): Promise<void>;
protected queryRelay(): Promise<any>;
}
//# sourceMappingURL=clearCache.d.ts.map

View File

@ -1 +0,0 @@
{"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,0BAA0B,CAAC;AAC3E,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,29 +0,0 @@
import { hashQuery } from "../util.js";
import SimpleRpcQuery from "./simple.js";
export default class ClearCacheRpcQuery extends SimpleRpcQuery {
_relays;
constructor({ network, relays, query, options, }) {
super({ network, relay: "", query, options });
this._relays = relays;
}
async _run() {
// @ts-ignore
this._relay = getActiveRelay().stream.remotePublicKey;
await this.queryRelay();
await this.checkResponses();
}
async queryRelay() {
return this.queryRpc(this._network.getAvailableRelay("rpc", "broadcast_request"), {
module: "rpc",
method: "broadcast_request",
data: {
request: {
module: "rpc",
method: "clear_cached_item",
data: hashQuery(this._query),
},
relays: this._relays,
},
});
}
}

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

@ -1,31 +0,0 @@
/// <reference types="node" />
import { ClientRPCRequest, RPCRequest } from "@lumeweb/interface-relay";
import { RpcQueryOptions } from "../types.js";
import SimpleRpcQuery from "./simple.js";
import ClearCacheRpcQuery from "./clearCache.js";
import RpcNetwork from "../network.js";
import RpcQueryBase from "./base.js";
export default class RpcNetworkQueryFactory {
private _network;
constructor(network: RpcNetwork);
simple({
relay,
query,
options,
}: {
relay?: string | Buffer;
query: ClientRPCRequest;
options?: RpcQueryOptions;
}): SimpleRpcQuery;
clearCache({
relays,
query,
options,
}: {
relays: string[];
query: RPCRequest;
options?: RpcQueryOptions;
}): ClearCacheRpcQuery;
}
export { RpcNetwork, RpcQueryBase, SimpleRpcQuery };
//# sourceMappingURL=index.d.ts.map

View File

@ -1 +0,0 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/query/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACxE,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,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;QACxB,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"}

30
dist/query/index.js vendored
View File

@ -1,30 +0,0 @@
import SimpleRpcQuery from "./simple.js";
import ClearCacheRpcQuery from "./clearCache.js";
import RpcNetwork from "../network.js";
import RpcQueryBase from "./base.js";
export default class RpcNetworkQueryFactory {
_network;
constructor(network) {
this._network = network;
}
simple({ relay, query, options = {}, }) {
return new SimpleRpcQuery({
network: this._network,
relay,
query: {
...query,
bypassCache: query?.bypassCache || this._network.bypassCache,
},
options,
}).run();
}
clearCache({ relays, query, options = {}, }) {
return new ClearCacheRpcQuery({
network: this._network,
query,
relays,
options,
}).run();
}
}
export { RpcNetwork, RpcQueryBase, SimpleRpcQuery };

View File

@ -1,24 +0,0 @@
/// <reference types="node" />
import RpcNetwork from "../network.js";
import { ClientRPCRequest } from "@lumeweb/interface-relay";
import { RpcQueryOptions } from "../types.js";
import RpcQueryBase from "./base.js";
export default class SimpleRpcQuery extends RpcQueryBase {
protected _relay?: string | any;
protected _query: ClientRPCRequest;
constructor({
network,
relay,
query,
options,
}: {
network: RpcNetwork;
relay?: string | Buffer | any;
query: ClientRPCRequest;
options: RpcQueryOptions;
});
protected _run(): Promise<void>;
protected queryRelay(): Promise<any>;
protected checkResponses(): Promise<void>;
}
//# sourceMappingURL=simple.d.ts.map

View File

@ -1 +0,0 @@
{"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,0BAA0B,CAAC;AACzE,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,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,MAAM,GAAG,GAAG,CAAC;QAC9B,KAAK,EAAE,gBAAgB,CAAC;QACxB,OAAO,EAAE,eAAe,CAAC;KAC1B;cASe,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;cAKrB,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;cAwD1B,cAAc;CAsB/B"}

75
dist/query/simple.js vendored
View File

@ -1,75 +0,0 @@
import b4a from "b4a";
import { hashQuery, isPromise, maybeGetAsyncProperty, setupStream, validateTimestampedResponse, } from "../util.js";
import { ERR_INVALID_SIGNATURE } from "../error.js";
import RpcQueryBase from "./base.js";
export default class SimpleRpcQuery extends RpcQueryBase {
_relay;
constructor({ network, relay, query, options, }) {
super({ network, query, options });
if (b4a.isBuffer(relay)) {
relay = b4a.from(relay).toString("hex");
}
this._relay = relay;
}
async _run() {
await this.queryRelay();
await this.checkResponses();
}
async queryRelay() {
let socket = this._relay;
if (socket) {
if (typeof socket === "string") {
try {
const relay = this._network.getRelay(socket);
if (this._network.getRelay(socket)) {
socket = relay;
}
}
catch { }
}
if (typeof socket === "string") {
try {
socket = this._network.swarm.connect(b4a.from(this._relay, "hex"));
if (isPromise(socket)) {
socket = await socket;
}
}
catch { }
}
}
if (!socket) {
socket = this._network.getAvailableRelay(this._query.module, this._query.method);
}
this._relay = socket;
await socket.opened;
const rpc = await setupStream(socket);
if (this._query.bypassCache) {
delete this._query.bypassCache;
await this.queryRpc(rpc, {
module: "rpc",
method: "clear_cached_item",
data: hashQuery(this._query),
});
}
if ("bypassCache" in this._query) {
delete this._query.bypassCache;
}
try {
await this.queryRpc(rpc, this._query);
}
catch (e) {
throw e;
}
}
async checkResponses() {
let response = this._response;
if (this._error) {
response = { error: this._error };
}
if (!response.error &&
!validateTimestampedResponse(b4a.from(await maybeGetAsyncProperty(this._relay.remotePublicKey), "hex"), response)) {
response = { error: ERR_INVALID_SIGNATURE };
}
this.resolve(response);
}
}

5
dist/types.d.ts vendored
View File

@ -1,5 +0,0 @@
export interface RpcQueryOptions {
queryTimeout?: number;
relayTimeout?: number;
}
//# sourceMappingURL=types.d.ts.map

1
dist/types.d.ts.map vendored
View File

@ -1 +0,0 @@
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB"}

1
dist/types.js vendored
View File

@ -1 +0,0 @@
export {};

19
dist/util.d.ts vendored
View File

@ -1,19 +0,0 @@
/// <reference types="node" />
import type { RPCRequest, RPCResponse } from "@lumeweb/interface-relay";
export declare const RPC_PROTOCOL_SYMBOL: unique symbol;
export declare function isPromise(obj: Promise<any>): boolean;
export declare function flatten(target: any, opts?: any): any[];
export declare function validateResponse(
relay: Buffer,
response: RPCResponse,
timestamped?: boolean
): boolean;
export declare function validateTimestampedResponse(
relay: Buffer,
response: RPCResponse
): boolean;
export declare function hashQuery(query: RPCRequest): string;
export declare function createHash(data: string): Buffer;
export declare function setupStream(stream: any): Promise<any>;
export declare function maybeGetAsyncProperty(object: any): Promise<any>;
//# sourceMappingURL=util.d.ts.map

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

@ -1 +0,0 @@
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAOxE,eAAO,MAAM,mBAAmB,eAAwB,CAAC;AAEzD,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;AAED,wBAAsB,WAAW,CAAC,MAAM,EAAE,GAAG,gBAa5C;AAED,wBAAsB,qBAAqB,CAAC,MAAM,EAAE,GAAG,gBAUtD"}

102
dist/util.js vendored
View File

@ -1,102 +0,0 @@
// @ts-ignore
import stringify from "json-stringify-deterministic";
// @ts-ignore
import crypto from "hypercore-crypto";
// @ts-ignore
import sodium from "sodium-universal";
import b4a from "b4a";
import RPC from "@lumeweb/rpc";
export const RPC_PROTOCOL_SYMBOL = Symbol.for("lumeweb");
export function isPromise(obj) {
return (!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function");
}
/*
Forked from https://github.com/hughsk/flat
*/
export function flatten(target, opts = {}) {
opts = opts || {};
const delimiter = opts.delimiter || ".";
const maxDepth = opts.maxDepth;
const transformKey = opts.transformKey || ((key) => (isNaN(parseInt(key)) ? key : ""));
const output = [];
function step(object, prev, currentDepth) {
currentDepth = currentDepth || 1;
if (!Array.isArray(object)) {
object = Object.keys(object ?? {});
}
object.forEach(function (key) {
const value = object[key];
const isarray = opts.safe && Array.isArray(value);
const type = Object.prototype.toString.call(value);
const isbuffer = b4a.isBuffer(value);
const isobject = type === "[object Object]" || type === "[object Array]";
const newKey = prev
? prev + delimiter + transformKey(key)
: transformKey(key);
if (!isarray &&
!isbuffer &&
isobject &&
Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)) {
return step(value, newKey, currentDepth + 1);
}
output.push(`${newKey}=${value}`);
});
}
step(target);
return output;
}
export function validateResponse(relay, response, timestamped = false) {
const field = response.signedField || "data";
// @ts-ignore
let json = response[field];
if (typeof json !== "string") {
json = stringify(json);
}
const updated = response.updated;
if (timestamped && updated) {
json = updated.toString() + json;
}
return !!crypto.verify(b4a.from(json), b4a.from(response.signature, "hex"), relay);
}
export function validateTimestampedResponse(relay, response) {
return validateResponse(relay, response, true);
}
export function hashQuery(query) {
const clonedQuery = {
module: query.module,
method: query.method,
data: query.data,
};
const queryHash = Buffer.allocUnsafe(32);
sodium.crypto_generichash(queryHash, Buffer.from(stringify(clonedQuery)));
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;
}
export async function setupStream(stream) {
const existing = stream[RPC_PROTOCOL_SYMBOL];
if (existing) {
await existing.ready;
return existing;
}
const rpc = new RPC(stream);
stream[RPC_PROTOCOL_SYMBOL] = rpc;
await rpc.ready;
return rpc;
}
export async function maybeGetAsyncProperty(object) {
if (typeof object === "function") {
object = object();
}
if (isPromise(object)) {
object = await object;
}
return object;
}

View File

@ -2,29 +2,27 @@
"name": "@lumeweb/rpc-client",
"type": "module",
"version": "0.1.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "rimraf dist && tsc"
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/rpc-client.git"
},
"devDependencies": {
"@lumeweb/interface-relay": "git+https://git.lumeweb.com/LumeWeb/interface-relay.git",
"@types/b4a": "^1.6.0",
"@types/express": "^4.17.14",
"@types/node": "^18.0.0",
"node-cache": "^5.1.2",
"prettier": "^2.7.1",
"typescript": "^4.7.4"
"@lumeweb/node-library-preset": "^0.2.7",
"presetter": "*"
},
"readme": "ERROR: No README data found!",
"_id": "@lumeweb/rpc-client@0.1.0",
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"dependencies": {
"@hyperswarm/dht": "^6.0.1",
"@lumeweb/rpc": "git+https://git.lumeweb.com/LumeWeb/rpc.git",
"b4a": "^1.6.1",
"hypercore-crypto": "^3.3.1",
"hyperswarm": "^4.3.7",
"json-stringify-deterministic": "^1.0.7",
"libskynet": "^0.0.61",
"msgpackr": "^1.6.1",
"sodium-universal": "^4.0.0"
"@lumeweb/interface-relay": "0.0.2-develop.1",
"@lumeweb/rpc": "0.0.2-develop.2"
},
"publishConfig": {
"access": "public"
}
}

View File

@ -1,23 +0,0 @@
{
"compilerOptions": {
"declaration": true,
"strict": true,
"module": "esnext",
"target": "esnext",
"esModuleInterop": true,
"sourceMap": false,
"rootDir": "src",
"outDir": "dist",
"typeRoots": [
"node_modules/@types",
],
"moduleResolution": "node",
"declarationMap": true,
"declarationDir": "dist",
"emitDeclarationOnly": false,
"allowJs": true
},
"include": [
"src"
]
}