2022-12-04 07:42:04 +00:00
|
|
|
import b4a from "b4a";
|
2023-03-25 15:26:38 +00:00
|
|
|
import { hashQuery, isPromise, maybeGetAsyncProperty, setupStream, validateTimestampedResponse, } from "../util.js";
|
2022-12-04 07:42:04 +00:00
|
|
|
import { ERR_INVALID_SIGNATURE } from "../error.js";
|
2022-08-28 06:33:49 +00:00
|
|
|
import RpcQueryBase from "./base.js";
|
|
|
|
export default class SimpleRpcQuery extends RpcQueryBase {
|
|
|
|
_relay;
|
2023-03-18 16:11:41 +00:00
|
|
|
constructor({ network, relay, query, options, }) {
|
|
|
|
super({ network, query, options });
|
2023-03-18 16:21:38 +00:00
|
|
|
if (b4a.isBuffer(relay)) {
|
|
|
|
relay = b4a.from(relay).toString("hex");
|
|
|
|
}
|
2022-08-28 06:33:49 +00:00
|
|
|
this._relay = relay;
|
|
|
|
}
|
2022-12-04 07:42:04 +00:00
|
|
|
async _run() {
|
|
|
|
await this.queryRelay();
|
|
|
|
await this.checkResponses();
|
|
|
|
}
|
|
|
|
async queryRelay() {
|
2023-03-18 16:11:41 +00:00
|
|
|
let socket = this._relay;
|
|
|
|
if (socket) {
|
2023-03-25 15:26:38 +00:00
|
|
|
if (typeof socket === "string") {
|
2023-03-18 16:11:41 +00:00
|
|
|
try {
|
|
|
|
const relay = this._network.getRelay(socket);
|
|
|
|
if (this._network.getRelay(socket)) {
|
|
|
|
socket = relay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
}
|
2023-03-25 15:26:38 +00:00
|
|
|
if (typeof socket === "string") {
|
2023-03-18 16:11:41 +00:00
|
|
|
try {
|
|
|
|
socket = this._network.swarm.connect(b4a.from(this._relay, "hex"));
|
|
|
|
if (isPromise(socket)) {
|
|
|
|
socket = await socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch { }
|
2022-12-04 07:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-18 16:11:41 +00:00
|
|
|
if (!socket) {
|
|
|
|
socket = this._network.getAvailableRelay(this._query.module, this._query.method);
|
2022-08-28 06:33:49 +00:00
|
|
|
}
|
2023-03-18 16:11:41 +00:00
|
|
|
this._relay = socket;
|
2022-12-04 07:42:04 +00:00
|
|
|
await socket.opened;
|
2023-04-08 18:38:29 +00:00
|
|
|
const rpc = await setupStream(socket);
|
2022-12-04 11:37:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-12-04 07:42:04 +00:00
|
|
|
try {
|
|
|
|
await this.queryRpc(rpc, this._query);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
throw e;
|
2022-08-28 06:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-04 07:42:04 +00:00
|
|
|
async checkResponses() {
|
|
|
|
let response = this._response;
|
|
|
|
if (this._error) {
|
|
|
|
response = { error: this._error };
|
|
|
|
}
|
|
|
|
if (!response.error &&
|
2023-03-25 15:26:38 +00:00
|
|
|
!validateTimestampedResponse(b4a.from(await maybeGetAsyncProperty(this._relay.remotePublicKey), "hex"), response)) {
|
2022-12-04 07:42:04 +00:00
|
|
|
response = { error: ERR_INVALID_SIGNATURE };
|
|
|
|
}
|
|
|
|
this.resolve(response);
|
2022-08-28 06:33:49 +00:00
|
|
|
}
|
|
|
|
}
|