Compare commits
8 Commits
e2f46d5bd2
...
1d61ec5e55
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 1d61ec5e55 | |
Derrick Hammer | 71e41ae003 | |
Derrick Hammer | 09825ea3c7 | |
Derrick Hammer | b5b7c9e0fe | |
Derrick Hammer | 7dc18eb090 | |
Derrick Hammer | 26f1164815 | |
Derrick Hammer | 15a4ef2eac | |
Derrick Hammer | 1a844804ec |
|
@ -1,50 +1,27 @@
|
||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
import { ErrTuple } from "libskynet";
|
import { ErrTuple } from "libskynet";
|
||||||
import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types";
|
import type {
|
||||||
import {
|
RPCRequest,
|
||||||
RpcQueryOptions,
|
RPCResponse,
|
||||||
StreamHandlerFunction,
|
ClientRPCRequest,
|
||||||
StreamingRpcQueryOptions,
|
} from "@lumeweb/interface-relay";
|
||||||
} from "@lumeweb/dht-rpc-client";
|
import { RpcQueryOptions } from "@lumeweb/rpc-client";
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
export declare class RpcNetwork {
|
import { Client } from "@lumeweb/libkernel-universal";
|
||||||
private _actionQueue;
|
export declare class RpcNetwork extends Client {
|
||||||
private _addQueue;
|
|
||||||
private _removeQueue;
|
|
||||||
private _def;
|
private _def;
|
||||||
constructor(def?: boolean);
|
constructor(def?: boolean);
|
||||||
private _networkId;
|
private _networkId;
|
||||||
get networkId(): number;
|
get networkId(): number;
|
||||||
get ready(): Promise<ErrTuple>;
|
get ready(): Promise<ErrTuple>;
|
||||||
private static deleteItem;
|
|
||||||
addRelay(pubkey: string): void;
|
|
||||||
removeRelay(pubkey: string): void;
|
|
||||||
clearRelays(): void;
|
|
||||||
wisdomQuery(
|
|
||||||
method: string,
|
|
||||||
module: string,
|
|
||||||
data?: object | any[],
|
|
||||||
bypassCache?: boolean,
|
|
||||||
options?: RpcQueryOptions
|
|
||||||
): WisdomRpcQuery;
|
|
||||||
streamingQuery(
|
|
||||||
relay: Buffer | string,
|
|
||||||
method: string,
|
|
||||||
module: string,
|
|
||||||
streamHandler: StreamHandlerFunction,
|
|
||||||
data?: object | any[],
|
|
||||||
options?: RpcQueryOptions
|
|
||||||
): StreamingRpcQuery;
|
|
||||||
simpleQuery(
|
simpleQuery(
|
||||||
relay: Buffer | string,
|
relay: Buffer | string,
|
||||||
method: string,
|
query: ClientRPCRequest,
|
||||||
module: string,
|
|
||||||
data?: object | any[],
|
data?: object | any[],
|
||||||
options?: RpcQueryOptions
|
options?: RpcQueryOptions
|
||||||
): SimpleRpcQuery;
|
): SimpleRpcQuery;
|
||||||
processQueue(): Promise<void>;
|
|
||||||
}
|
}
|
||||||
export declare abstract class RpcQueryBase {
|
export declare abstract class RpcQueryBase extends Client {
|
||||||
protected _promise?: Promise<any>;
|
protected _promise?: Promise<any>;
|
||||||
protected _network: RpcNetwork;
|
protected _network: RpcNetwork;
|
||||||
protected _query: RPCRequest;
|
protected _query: RPCRequest;
|
||||||
|
@ -61,32 +38,18 @@ export declare abstract class RpcQueryBase {
|
||||||
}
|
}
|
||||||
export declare class SimpleRpcQuery extends RpcQueryBase {
|
export declare class SimpleRpcQuery extends RpcQueryBase {
|
||||||
protected _relay: string | Buffer;
|
protected _relay: string | Buffer;
|
||||||
constructor(
|
constructor({
|
||||||
network: RpcNetwork,
|
network,
|
||||||
relay: string | Buffer,
|
relay,
|
||||||
query: RPCRequest,
|
query,
|
||||||
options: RpcQueryOptions
|
options,
|
||||||
);
|
}: {
|
||||||
|
network: RpcNetwork;
|
||||||
|
relay: string | Buffer;
|
||||||
|
query: RPCRequest;
|
||||||
|
options: RpcQueryOptions;
|
||||||
|
});
|
||||||
run(): this;
|
run(): this;
|
||||||
}
|
}
|
||||||
export declare class StreamingRpcQuery extends SimpleRpcQuery {
|
export declare const createClient: (...args: any) => RpcNetwork;
|
||||||
protected _options: StreamingRpcQueryOptions;
|
|
||||||
private _sendUpdate?;
|
|
||||||
constructor(
|
|
||||||
network: RpcNetwork,
|
|
||||||
relay: string | Buffer,
|
|
||||||
query: RPCRequest,
|
|
||||||
options: StreamingRpcQueryOptions
|
|
||||||
);
|
|
||||||
cancel(): void;
|
|
||||||
run(): this;
|
|
||||||
get result(): Promise<RPCResponse>;
|
|
||||||
}
|
|
||||||
export declare class WisdomRpcQuery extends RpcQueryBase {
|
|
||||||
constructor(
|
|
||||||
network: RpcNetwork,
|
|
||||||
query: RPCRequest,
|
|
||||||
options?: RpcQueryOptions
|
|
||||||
);
|
|
||||||
}
|
|
||||||
//# sourceMappingURL=index.d.ts.map
|
//# sourceMappingURL=index.d.ts.map
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,wBAAwB,EACzB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAuBhC,qBAAa,UAAU;IACrB,OAAO,CAAC,YAAY,CAAuB;IAC3C,OAAO,CAAC,SAAS,CAAgB;IACjC,OAAO,CAAC,YAAY,CAAgB;IACpC,OAAO,CAAC,IAAI,CAAU;gBAEV,GAAG,GAAE,OAAc;IAI/B,OAAO,CAAC,UAAU,CAAa;IAE/B,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED,IAAI,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC,CAc7B;IAED,OAAO,CAAC,MAAM,CAAC,UAAU;IAQlB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAM9B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAMjC,WAAW,IAAI,IAAI;IAInB,WAAW,CAChB,MAAM,EAAE,MAAM,EACd,MAA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C,GAAG,CAAC,CAAC;IAClC,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC;IAC/B,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC;IAC7B,SAAS,CAAC,QAAQ,EAAE,eAAe,CAAC;IACpC,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC;gBAG3B,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,UAAU,EACjB,OAAO,6BAAsB,EAC7B,SAAS,EAAE,MAAM;IASnB,IAAI,MAAM,IAAI,OAAO,CAAC,WAAW,CAAC,CAQjC;IAEM,GAAG,IAAI,IAAI;CASnB;AAED,qBAAa,cAAe,SAAQ,YAAY;IAC9C,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;gBACtB,EACV,OAAO,EACP,KAAK,EACL,KAAK,EACL,OAAO,GACR,EAAE;QACD,OAAO,EAAE,UAAU,CAAC;QACpB,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;QACvB,KAAK,EAAE,UAAU,CAAC;QAClB,OAAO,EAAE,eAAe,CAAC;KAC1B;IAIM,GAAG,IAAI,IAAI;CAUnB;AAED,eAAO,MAAM,YAAY,8BAA8C,CAAC"}
|
|
@ -1,26 +1,9 @@
|
||||||
const RPC_MODULE = "AQDaEPIo_lpdvz7AKbeafERBHR331RiyvweJ6OrFTplzyg";
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
||||||
let callModule, connectModule;
|
const RPC_MODULE = "fAAgZfXMqN3YOn0-b9DICt8OPsOFeWw3YKY2p84aytzBww";
|
||||||
async function loadLibs() {
|
export class RpcNetwork extends Client {
|
||||||
if (callModule && connectModule) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (typeof window !== "undefined" && window?.document) {
|
|
||||||
const pkg = await import("libkernel");
|
|
||||||
callModule = pkg.callModule;
|
|
||||||
connectModule = pkg.connectModule;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const pkg = await import("libkmodule");
|
|
||||||
callModule = pkg.callModule;
|
|
||||||
connectModule = pkg.connectModule;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class RpcNetwork {
|
|
||||||
_actionQueue = [];
|
|
||||||
_addQueue = [];
|
|
||||||
_removeQueue = [];
|
|
||||||
_def;
|
_def;
|
||||||
constructor(def = true) {
|
constructor(def = true) {
|
||||||
|
super();
|
||||||
this._def = def;
|
this._def = def;
|
||||||
}
|
}
|
||||||
_networkId = 0;
|
_networkId = 0;
|
||||||
|
@ -28,163 +11,72 @@ export class RpcNetwork {
|
||||||
return this._networkId;
|
return this._networkId;
|
||||||
}
|
}
|
||||||
get ready() {
|
get ready() {
|
||||||
let promise = loadLibs();
|
|
||||||
if (this._def) {
|
if (this._def) {
|
||||||
this._networkId = 1;
|
this._networkId = 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
promise = promise
|
Promise.resolve()
|
||||||
.then(() => callModule(RPC_MODULE, "createNetwork"))
|
.then(() => this.callModuleReturn(RPC_MODULE, "createNetwork"))
|
||||||
.then((ret) => (this._networkId = ret[0]));
|
.then((ret) => (this._networkId = ret[0]));
|
||||||
}
|
}
|
||||||
return promise.then(() => callModule(RPC_MODULE, "ready", { network: this._networkId }));
|
return this.callModuleReturn("ready", {
|
||||||
}
|
|
||||||
static deleteItem(array, item) {
|
|
||||||
if (array.includes(item)) {
|
|
||||||
let queue = new Set(array);
|
|
||||||
queue.delete(item);
|
|
||||||
[].splice.apply(array, [0, array.length].concat([...queue]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addRelay(pubkey) {
|
|
||||||
this._addQueue.push(pubkey);
|
|
||||||
this._addQueue = [...new Set(this._addQueue)];
|
|
||||||
RpcNetwork.deleteItem(this._removeQueue, pubkey);
|
|
||||||
}
|
|
||||||
removeRelay(pubkey) {
|
|
||||||
this._removeQueue.push(pubkey);
|
|
||||||
this._removeQueue = [...new Set(this._removeQueue)];
|
|
||||||
RpcNetwork.deleteItem(this._addQueue, pubkey);
|
|
||||||
}
|
|
||||||
clearRelays() {
|
|
||||||
this._actionQueue.push(["clearRelays", {}]);
|
|
||||||
}
|
|
||||||
wisdomQuery(method, module, data = {}, bypassCache = false, options = {}) {
|
|
||||||
return new WisdomRpcQuery(this, {
|
|
||||||
method,
|
|
||||||
module,
|
|
||||||
data,
|
|
||||||
bypassCache,
|
|
||||||
}, options).run();
|
|
||||||
}
|
|
||||||
streamingQuery(relay, method, module, streamHandler, data = {}, options = {}) {
|
|
||||||
return new StreamingRpcQuery(this, relay, { method, module, data }, { streamHandler, ...options }).run();
|
|
||||||
}
|
|
||||||
simpleQuery(relay, method, module, data = {}, options = {}) {
|
|
||||||
return new SimpleRpcQuery(this, relay, {
|
|
||||||
method,
|
|
||||||
module,
|
|
||||||
data,
|
|
||||||
}, options).run();
|
|
||||||
}
|
|
||||||
async processQueue() {
|
|
||||||
await loadLibs();
|
|
||||||
for (const action of this._actionQueue) {
|
|
||||||
try {
|
|
||||||
await callModule(RPC_MODULE, action[0], {
|
|
||||||
...action[1],
|
|
||||||
network: this._networkId,
|
network: this._networkId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (e) { }
|
simpleQuery(relay, query, data = {}, options = {}) {
|
||||||
}
|
return new SimpleRpcQuery({
|
||||||
await Promise.allSettled(this._removeQueue.map((item) => callModule(RPC_MODULE, "removeRelay", {
|
network: this,
|
||||||
pubkey: item,
|
relay,
|
||||||
network: this._networkId,
|
query,
|
||||||
})));
|
options,
|
||||||
await Promise.allSettled(this._addQueue.map((item) => callModule(RPC_MODULE, "addRelay", {
|
}).run();
|
||||||
pubkey: item,
|
|
||||||
network: this._networkId,
|
|
||||||
})));
|
|
||||||
this._actionQueue = [];
|
|
||||||
this._removeQueue = [];
|
|
||||||
this._addQueue = [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class RpcQueryBase {
|
export class RpcQueryBase extends Client {
|
||||||
_promise;
|
_promise;
|
||||||
_network;
|
_network;
|
||||||
_query;
|
_query;
|
||||||
_options;
|
_options;
|
||||||
_queryType;
|
_queryType;
|
||||||
constructor(network, query, options = {}, queryType) {
|
constructor(network, query, options = {}, queryType) {
|
||||||
|
super();
|
||||||
this._network = network;
|
this._network = network;
|
||||||
this._query = query;
|
this._query = query;
|
||||||
this._options = options;
|
this._options = options;
|
||||||
this._queryType = queryType;
|
this._queryType = queryType;
|
||||||
}
|
}
|
||||||
get result() {
|
get result() {
|
||||||
return this._promise.then((result) => {
|
return this._promise
|
||||||
if (result[1]) {
|
.then((result) => {
|
||||||
return { error: result[1] };
|
|
||||||
}
|
|
||||||
return result[0];
|
return result[0];
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
return { error: error.message };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
run() {
|
run() {
|
||||||
this._promise = this._network.processQueue().then(() => callModule(RPC_MODULE, this._queryType, {
|
this._promise = this.callModule(this._queryType, {
|
||||||
query: this._query,
|
query: this._query,
|
||||||
options: this._options,
|
options: this._options,
|
||||||
network: this._network.networkId,
|
network: this._network.networkId,
|
||||||
}));
|
});
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class SimpleRpcQuery extends RpcQueryBase {
|
export class SimpleRpcQuery extends RpcQueryBase {
|
||||||
_relay;
|
_relay;
|
||||||
constructor(network, relay, query, options) {
|
constructor({ network, relay, query, options, }) {
|
||||||
super(network, query, options, "simpleQuery");
|
super(network, query, options, "simpleQuery");
|
||||||
this._relay = relay;
|
this._relay = relay;
|
||||||
}
|
}
|
||||||
run() {
|
run() {
|
||||||
this._promise = this._network.processQueue().then(() => callModule(RPC_MODULE, this._queryType, {
|
this._promise = this.callModule(this._queryType, {
|
||||||
relay: this._relay,
|
relay: this._relay,
|
||||||
query: this._query,
|
query: this._query,
|
||||||
options: this._options,
|
options: this._options,
|
||||||
network: this._network.networkId,
|
network: this._network.networkId,
|
||||||
}));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class StreamingRpcQuery extends SimpleRpcQuery {
|
|
||||||
_options;
|
|
||||||
_sendUpdate;
|
|
||||||
constructor(network, relay, query, options) {
|
|
||||||
super(network, relay, query, options);
|
|
||||||
this._options = options;
|
|
||||||
this._queryType = "streamingQuery";
|
|
||||||
}
|
|
||||||
cancel() {
|
|
||||||
if (this._sendUpdate) {
|
|
||||||
this._sendUpdate({ cancel: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
run() {
|
|
||||||
this._promise = this._network.processQueue().then(() => {
|
|
||||||
const ret = connectModule(RPC_MODULE, this._queryType, {
|
|
||||||
relay: this._relay,
|
|
||||||
query: this._query,
|
|
||||||
options: { ...this._options, streamHandler: true },
|
|
||||||
network: this._network.networkId,
|
|
||||||
}, this._options.streamHandler);
|
|
||||||
this._sendUpdate = ret[0];
|
|
||||||
return ret[1];
|
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
get result() {
|
|
||||||
return this._promise
|
|
||||||
.then((result) => result)
|
|
||||||
.then((response) => {
|
|
||||||
if (response[1]) {
|
|
||||||
return { error: response[1] };
|
|
||||||
}
|
|
||||||
return response[0];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class WisdomRpcQuery extends RpcQueryBase {
|
|
||||||
constructor(network, query, options = {}) {
|
|
||||||
super(network, query, options, "wisdomQuery");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
export const createClient = factory(RpcNetwork, RPC_MODULE);
|
||||||
|
|
17
package.json
17
package.json
|
@ -4,15 +4,20 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/dht-rpc-client": "https://github.com/LumeWeb/dht-rpc-client.git",
|
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
|
||||||
|
"@lumeweb/rpc-client": "git+https://git.lumeweb.com/LumeWeb/rpc-client.git",
|
||||||
"buffer": "^6.0.3",
|
"buffer": "^6.0.3",
|
||||||
"libkernel": "^0.1.43",
|
"libkernel": "^0.1.48",
|
||||||
"libkmodule": "^0.2.44",
|
"libkmodule": "^0.2.53",
|
||||||
"libskynet": "^0.0.62"
|
"libskynet": "^0.0.62"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@lumeweb/relay-types": "https://github.com/LumeWeb/relay-types.git",
|
"@lumeweb/cfg": "git+https://git.lumeweb.com/LumeWeb/cfg.git",
|
||||||
"@types/node": "^18.0.6",
|
"@lumeweb/interface-relay": "git+https://git.lumeweb.com/LumeWeb/interface-relay.git",
|
||||||
"prettier": "^2.7.1"
|
"@types/b4a": "^1.6.0",
|
||||||
|
"@types/node": "^18.15.5",
|
||||||
|
"@types/node-cache": "^4.2.5",
|
||||||
|
"prettier": "^2.8.6",
|
||||||
|
"typescript": "^5.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
264
src/index.ts
264
src/index.ts
|
@ -1,40 +1,20 @@
|
||||||
import { ErrTuple } from "libskynet";
|
import { ErrTuple } from "libskynet";
|
||||||
import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types";
|
import type {
|
||||||
import {
|
RPCRequest,
|
||||||
RpcQueryOptions,
|
RPCResponse,
|
||||||
StreamHandlerFunction,
|
ClientRPCRequest,
|
||||||
StreamingRpcQueryOptions,
|
} from "@lumeweb/interface-relay";
|
||||||
} from "@lumeweb/dht-rpc-client";
|
import { RpcQueryOptions } from "@lumeweb/rpc-client";
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { DataFn } from "libskynet/dist";
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
||||||
|
|
||||||
const RPC_MODULE = "AQDaEPIo_lpdvz7AKbeafERBHR331RiyvweJ6OrFTplzyg";
|
const RPC_MODULE = "fAAgZfXMqN3YOn0-b9DICt8OPsOFeWw3YKY2p84aytzBww";
|
||||||
|
|
||||||
let callModule: any, connectModule: any;
|
export class RpcNetwork extends Client {
|
||||||
|
|
||||||
async function loadLibs() {
|
|
||||||
if (callModule && connectModule) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof window !== "undefined" && window?.document) {
|
|
||||||
const pkg = await import("libkernel");
|
|
||||||
callModule = pkg.callModule;
|
|
||||||
connectModule = pkg.connectModule;
|
|
||||||
} else {
|
|
||||||
const pkg = await import("libkmodule");
|
|
||||||
callModule = pkg.callModule;
|
|
||||||
connectModule = pkg.connectModule;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class RpcNetwork {
|
|
||||||
private _actionQueue: [string, any][] = [];
|
|
||||||
private _addQueue: string[] = [];
|
|
||||||
private _removeQueue: string[] = [];
|
|
||||||
private _def: boolean;
|
private _def: boolean;
|
||||||
|
|
||||||
constructor(def: boolean = true) {
|
constructor(def: boolean = true) {
|
||||||
|
super();
|
||||||
this._def = def;
|
this._def = def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,134 +25,34 @@ export class RpcNetwork {
|
||||||
}
|
}
|
||||||
|
|
||||||
get ready(): Promise<ErrTuple> {
|
get ready(): Promise<ErrTuple> {
|
||||||
let promise = loadLibs();
|
|
||||||
|
|
||||||
if (this._def) {
|
if (this._def) {
|
||||||
this._networkId = 1;
|
this._networkId = 1;
|
||||||
} else {
|
} else {
|
||||||
promise = promise
|
Promise.resolve()
|
||||||
.then(() => callModule(RPC_MODULE, "createNetwork"))
|
.then(() => this.callModuleReturn(RPC_MODULE, "createNetwork"))
|
||||||
.then((ret: ErrTuple) => (this._networkId = ret[0]));
|
.then((ret: ErrTuple) => (this._networkId = ret[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return promise.then(() =>
|
return this.callModuleReturn("ready", {
|
||||||
callModule(RPC_MODULE, "ready", { network: this._networkId })
|
network: this._networkId,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static deleteItem(array: Array<any>, item: string): void {
|
|
||||||
if (array.includes(item)) {
|
|
||||||
let queue = new Set(array);
|
|
||||||
queue.delete(item);
|
|
||||||
[].splice.apply(array, [0, array.length].concat([...queue]) as any);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public addRelay(pubkey: string): void {
|
|
||||||
this._addQueue.push(pubkey);
|
|
||||||
this._addQueue = [...new Set(this._addQueue)];
|
|
||||||
RpcNetwork.deleteItem(this._removeQueue, pubkey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public removeRelay(pubkey: string): void {
|
|
||||||
this._removeQueue.push(pubkey);
|
|
||||||
this._removeQueue = [...new Set(this._removeQueue)];
|
|
||||||
RpcNetwork.deleteItem(this._addQueue, pubkey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public clearRelays(): void {
|
|
||||||
this._actionQueue.push(["clearRelays", {}]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public wisdomQuery(
|
|
||||||
method: string,
|
|
||||||
module: string,
|
|
||||||
data: object | any[] = {},
|
|
||||||
bypassCache: boolean = false,
|
|
||||||
options: RpcQueryOptions = {}
|
|
||||||
): WisdomRpcQuery {
|
|
||||||
return new WisdomRpcQuery(
|
|
||||||
this,
|
|
||||||
{
|
|
||||||
method,
|
|
||||||
module,
|
|
||||||
data,
|
|
||||||
bypassCache,
|
|
||||||
},
|
|
||||||
options
|
|
||||||
).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
public streamingQuery(
|
|
||||||
relay: Buffer | string,
|
|
||||||
method: string,
|
|
||||||
module: string,
|
|
||||||
streamHandler: StreamHandlerFunction,
|
|
||||||
data: object | any[] = {},
|
|
||||||
options: RpcQueryOptions = {}
|
|
||||||
): StreamingRpcQuery {
|
|
||||||
return new StreamingRpcQuery(
|
|
||||||
this,
|
|
||||||
relay,
|
|
||||||
{ method, module, data },
|
|
||||||
{ streamHandler, ...options }
|
|
||||||
).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
public simpleQuery(
|
public simpleQuery(
|
||||||
relay: Buffer | string,
|
relay: Buffer | string,
|
||||||
method: string,
|
query: ClientRPCRequest,
|
||||||
module: string,
|
|
||||||
data: object | any[] = {},
|
data: object | any[] = {},
|
||||||
options: RpcQueryOptions = {}
|
options: RpcQueryOptions = {}
|
||||||
): SimpleRpcQuery {
|
): SimpleRpcQuery {
|
||||||
return new SimpleRpcQuery(
|
return new SimpleRpcQuery({
|
||||||
this,
|
network: this,
|
||||||
relay,
|
relay,
|
||||||
{
|
query,
|
||||||
method,
|
options,
|
||||||
module,
|
}).run();
|
||||||
data,
|
|
||||||
},
|
|
||||||
options
|
|
||||||
).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async processQueue(): Promise<void> {
|
|
||||||
await loadLibs();
|
|
||||||
for (const action of this._actionQueue) {
|
|
||||||
try {
|
|
||||||
await callModule(RPC_MODULE, action[0], {
|
|
||||||
...action[1],
|
|
||||||
network: this._networkId,
|
|
||||||
});
|
|
||||||
} catch (e: any) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.allSettled(
|
|
||||||
this._removeQueue.map((item: string) =>
|
|
||||||
callModule(RPC_MODULE, "removeRelay", {
|
|
||||||
pubkey: item,
|
|
||||||
network: this._networkId,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
await Promise.allSettled(
|
|
||||||
this._addQueue.map((item: string) =>
|
|
||||||
callModule(RPC_MODULE, "addRelay", {
|
|
||||||
pubkey: item,
|
|
||||||
network: this._networkId,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
this._actionQueue = [];
|
|
||||||
this._removeQueue = [];
|
|
||||||
this._addQueue = [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class RpcQueryBase {
|
export abstract class RpcQueryBase extends Client {
|
||||||
protected _promise?: Promise<any>;
|
protected _promise?: Promise<any>;
|
||||||
protected _network: RpcNetwork;
|
protected _network: RpcNetwork;
|
||||||
protected _query: RPCRequest;
|
protected _query: RPCRequest;
|
||||||
|
@ -185,6 +65,7 @@ export abstract class RpcQueryBase {
|
||||||
options: RpcQueryOptions = {},
|
options: RpcQueryOptions = {},
|
||||||
queryType: string
|
queryType: string
|
||||||
) {
|
) {
|
||||||
|
super();
|
||||||
this._network = network;
|
this._network = network;
|
||||||
this._query = query;
|
this._query = query;
|
||||||
this._options = options;
|
this._options = options;
|
||||||
|
@ -192,22 +73,21 @@ export abstract class RpcQueryBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
get result(): Promise<RPCResponse> {
|
get result(): Promise<RPCResponse> {
|
||||||
return (this._promise as Promise<any>).then((result): RPCResponse => {
|
return (this._promise as Promise<any>)
|
||||||
if (result[1]) {
|
.then((result: ErrTuple): RPCResponse => {
|
||||||
return { error: result[1] };
|
|
||||||
}
|
|
||||||
return result[0];
|
return result[0];
|
||||||
|
})
|
||||||
|
.catch((error: Error) => {
|
||||||
|
return { error: error.message };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public run(): this {
|
public run(): this {
|
||||||
this._promise = this._network.processQueue().then(() =>
|
this._promise = this.callModule(this._queryType, {
|
||||||
callModule(RPC_MODULE, this._queryType, {
|
|
||||||
query: this._query,
|
query: this._query,
|
||||||
options: this._options,
|
options: this._options,
|
||||||
network: this._network.networkId,
|
network: this._network.networkId,
|
||||||
})
|
});
|
||||||
);
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -215,88 +95,30 @@ export abstract class RpcQueryBase {
|
||||||
|
|
||||||
export class SimpleRpcQuery extends RpcQueryBase {
|
export class SimpleRpcQuery extends RpcQueryBase {
|
||||||
protected _relay: string | Buffer;
|
protected _relay: string | Buffer;
|
||||||
constructor(
|
constructor({
|
||||||
network: RpcNetwork,
|
network,
|
||||||
relay: string | Buffer,
|
relay,
|
||||||
query: RPCRequest,
|
query,
|
||||||
options: RpcQueryOptions
|
options,
|
||||||
) {
|
}: {
|
||||||
|
network: RpcNetwork;
|
||||||
|
relay: string | Buffer;
|
||||||
|
query: RPCRequest;
|
||||||
|
options: RpcQueryOptions;
|
||||||
|
}) {
|
||||||
super(network, query, options, "simpleQuery");
|
super(network, query, options, "simpleQuery");
|
||||||
this._relay = relay;
|
this._relay = relay;
|
||||||
}
|
}
|
||||||
public run(): this {
|
public run(): this {
|
||||||
this._promise = this._network.processQueue().then(() =>
|
this._promise = this.callModule(this._queryType, {
|
||||||
callModule(RPC_MODULE, this._queryType, {
|
|
||||||
relay: this._relay,
|
relay: this._relay,
|
||||||
query: this._query,
|
query: this._query,
|
||||||
options: this._options,
|
options: this._options,
|
||||||
network: this._network.networkId,
|
network: this._network.networkId,
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class StreamingRpcQuery extends SimpleRpcQuery {
|
|
||||||
protected _options: StreamingRpcQueryOptions;
|
|
||||||
private _sendUpdate?: DataFn;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
network: RpcNetwork,
|
|
||||||
relay: string | Buffer,
|
|
||||||
query: RPCRequest,
|
|
||||||
options: StreamingRpcQueryOptions
|
|
||||||
) {
|
|
||||||
super(network, relay, query, options);
|
|
||||||
this._options = options;
|
|
||||||
this._queryType = "streamingQuery";
|
|
||||||
}
|
|
||||||
|
|
||||||
public cancel() {
|
|
||||||
if (this._sendUpdate) {
|
|
||||||
this._sendUpdate({ cancel: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public run(): this {
|
|
||||||
this._promise = this._network.processQueue().then(() => {
|
|
||||||
const ret = connectModule(
|
|
||||||
RPC_MODULE,
|
|
||||||
this._queryType,
|
|
||||||
{
|
|
||||||
relay: this._relay,
|
|
||||||
query: this._query,
|
|
||||||
options: { ...this._options, streamHandler: true },
|
|
||||||
network: this._network.networkId,
|
|
||||||
},
|
|
||||||
this._options.streamHandler
|
|
||||||
);
|
|
||||||
this._sendUpdate = ret[0];
|
|
||||||
return ret[1];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
get result(): Promise<RPCResponse> {
|
|
||||||
return (this._promise as Promise<any>)
|
|
||||||
.then((result): Promise<ErrTuple> => result)
|
|
||||||
.then((response: ErrTuple) => {
|
|
||||||
if (response[1]) {
|
|
||||||
return { error: response[1] };
|
|
||||||
}
|
|
||||||
return response[0];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WisdomRpcQuery extends RpcQueryBase {
|
export const createClient = factory<RpcNetwork>(RpcNetwork, RPC_MODULE);
|
||||||
constructor(
|
|
||||||
network: RpcNetwork,
|
|
||||||
query: RPCRequest,
|
|
||||||
options: RpcQueryOptions = {}
|
|
||||||
) {
|
|
||||||
super(network, query, options, "wisdomQuery");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue