*Use updated rpc client/protocol
This commit is contained in:
parent
5d0f30c49d
commit
d7a17ca163
|
@ -10,17 +10,17 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/kernel-dht-client": "https://github.com/LumeWeb/kernel-dht-client.git",
|
"@lumeweb/kernel-dht-client": "https://github.com/LumeWeb/kernel-dht-client.git",
|
||||||
|
"@lumeweb/kernel-rpc-client": "https://github.com/LumeWeb/kernel-rpc-client.git",
|
||||||
"@lumeweb/kernel-utils": "https://github.com/LumeWeb/kernel-utils.git",
|
"@lumeweb/kernel-utils": "https://github.com/LumeWeb/kernel-utils.git",
|
||||||
"buffer": "^6.0.3",
|
"buffer": "^6.0.3",
|
||||||
"fetch-retry": "^5.0.3",
|
|
||||||
"is-ipfs": "^6.0.2",
|
"is-ipfs": "^6.0.2",
|
||||||
"libkmodule": "^0.2.12",
|
"libkmodule": "^0.2.12",
|
||||||
"libskynet": "^0.0.62",
|
"libskynet": "^0.0.62",
|
||||||
"msgpackr": "^1.6.2",
|
|
||||||
"p-queue": "^7.3.0",
|
"p-queue": "^7.3.0",
|
||||||
"timers-browserify": "^2.0.12"
|
"timers-browserify": "^2.0.12"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@lumeweb/relay-types": "https://github.com/LumeWeb/relay-types.git",
|
||||||
"@types/events": "^3.0.0",
|
"@types/events": "^3.0.0",
|
||||||
"@types/node": "^18.0.3",
|
"@types/node": "^18.0.3",
|
||||||
"@types/read": "^0.0.29",
|
"@types/read": "^0.0.29",
|
||||||
|
|
134
src/index.ts
134
src/index.ts
|
@ -1,12 +1,10 @@
|
||||||
import { addHandler, handleMessage, log } from "libkmodule";
|
import { addHandler, handleMessage } from "libkmodule";
|
||||||
import type { ActiveQuery } from "libkmodule";
|
import type { ActiveQuery } from "libkmodule";
|
||||||
import PQueue from "p-queue";
|
import PQueue from "p-queue";
|
||||||
import { ipfsPath, ipnsPath } from "is-ipfs";
|
import { ipfsPath, ipnsPath } from "is-ipfs";
|
||||||
import { DHT } from "@lumeweb/kernel-dht-client";
|
|
||||||
import { pack, unpack } from "msgpackr";
|
|
||||||
import { DataFn } from "libskynet";
|
import { DataFn } from "libskynet";
|
||||||
|
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
|
||||||
onmessage = handleMessage;
|
import { RPCResponse } from "@lumeweb/relay-types";
|
||||||
|
|
||||||
interface StatFileResponse {
|
interface StatFileResponse {
|
||||||
exists: boolean;
|
exists: boolean;
|
||||||
|
@ -15,6 +13,17 @@ interface StatFileResponse {
|
||||||
directory: boolean;
|
directory: boolean;
|
||||||
files: string[];
|
files: string[];
|
||||||
}
|
}
|
||||||
|
interface PingRPCResponse extends RPCResponse {
|
||||||
|
data?: {
|
||||||
|
ping?: any;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MethodsRPCResponse extends RPCResponse {
|
||||||
|
data?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
onmessage = handleMessage;
|
||||||
|
|
||||||
let blockingGatewayUpdate = Promise.resolve();
|
let blockingGatewayUpdate = Promise.resolve();
|
||||||
|
|
||||||
|
@ -23,7 +32,7 @@ let relays = [
|
||||||
"25c2a0a833782d64213c08879b95dd5a60af244b44a058f3a7a70d6722f4bda7",
|
"25c2a0a833782d64213c08879b95dd5a60af244b44a058f3a7a70d6722f4bda7",
|
||||||
];
|
];
|
||||||
|
|
||||||
let dht: DHT;
|
let network: RpcNetwork;
|
||||||
|
|
||||||
addHandler("presentSeed", handlePresentSeed);
|
addHandler("presentSeed", handlePresentSeed);
|
||||||
addHandler("refreshGatewayList", handleRefreshGatewayList);
|
addHandler("refreshGatewayList", handleRefreshGatewayList);
|
||||||
|
@ -38,11 +47,11 @@ let readyPromise = new Promise((resolve) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handlePresentSeed() {
|
async function handlePresentSeed() {
|
||||||
dht = new DHT(false);
|
network = new RpcNetwork(false);
|
||||||
for (const relay of relays) {
|
for (const relay of relays) {
|
||||||
await dht.addRelay(relay);
|
await network.addRelay(relay);
|
||||||
}
|
}
|
||||||
await dht.ready();
|
|
||||||
refreshGatewayList();
|
refreshGatewayList();
|
||||||
readyPromiseResolve();
|
readyPromiseResolve();
|
||||||
}
|
}
|
||||||
|
@ -141,23 +150,25 @@ async function fetchFromRelays(
|
||||||
await refreshGatewayList();
|
await refreshGatewayList();
|
||||||
}
|
}
|
||||||
for (const relay of activeRelays) {
|
for (const relay of activeRelays) {
|
||||||
let resp;
|
let query;
|
||||||
try {
|
if (stream) {
|
||||||
resp = await rpcCall(relay, "ipfs", method, stream, {
|
query = network.streamingQuery(relay, method, "ipfs", stream, {
|
||||||
hash,
|
hash,
|
||||||
path,
|
path,
|
||||||
});
|
});
|
||||||
} catch (e: any) {
|
|
||||||
if (e instanceof Error) {
|
|
||||||
error = e;
|
|
||||||
} else {
|
} else {
|
||||||
error = new Error(e);
|
query = network.simpleQuery(relay, method, "ipfs", {
|
||||||
|
hash,
|
||||||
|
path,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
continue;
|
let resp = await query.result;
|
||||||
|
if (resp.error) {
|
||||||
|
throw new Error(resp.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resp) {
|
if (!stream) {
|
||||||
return resp;
|
return resp.data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,11 +179,13 @@ async function relayHasMethods(
|
||||||
methodList: string[],
|
methodList: string[],
|
||||||
relay: string
|
relay: string
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
let methods: string | string[] = [];
|
let methods: string[];
|
||||||
try {
|
let query = network.simpleQuery(relay, "get_methods", "core");
|
||||||
methods = (await rpcCall(relay, "misc", "get_methods")) as [];
|
|
||||||
} catch (e) {
|
let resp = (await query.result) as MethodsRPCResponse;
|
||||||
return false;
|
|
||||||
|
if (resp.data) {
|
||||||
|
methods = resp.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
let has = true;
|
let has = true;
|
||||||
|
@ -185,54 +198,6 @@ async function relayHasMethods(
|
||||||
|
|
||||||
return has;
|
return has;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function rpcCall(
|
|
||||||
relay: string,
|
|
||||||
chain: string,
|
|
||||||
query: string,
|
|
||||||
stream?: (data: any) => void,
|
|
||||||
data = {}
|
|
||||||
) {
|
|
||||||
const socket = await dht.connect(relay);
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let dataCount = 0;
|
|
||||||
socket.on("data", (res) => {
|
|
||||||
dataCount++;
|
|
||||||
const response = unpack(res);
|
|
||||||
|
|
||||||
if (!response || response.error) {
|
|
||||||
socket.end();
|
|
||||||
reject(response?.error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!stream && 1 === dataCount) {
|
|
||||||
socket.end();
|
|
||||||
resolve(response?.data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream) {
|
|
||||||
if (response?.data.done) {
|
|
||||||
socket.end();
|
|
||||||
resolve(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
stream(response?.data.data);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
socket.write("rpc");
|
|
||||||
socket.write(
|
|
||||||
pack({
|
|
||||||
query,
|
|
||||||
chain,
|
|
||||||
data,
|
|
||||||
bypassCache: true,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function refreshGatewayList() {
|
async function refreshGatewayList() {
|
||||||
let processResolve: any;
|
let processResolve: any;
|
||||||
blockingGatewayUpdate = new Promise((resolve) => {
|
blockingGatewayUpdate = new Promise((resolve) => {
|
||||||
|
@ -255,33 +220,26 @@ async function refreshGatewayList() {
|
||||||
.map((item: any[]) => item[1]);
|
.map((item: any[]) => item[1]);
|
||||||
processResolve();
|
processResolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkRelayLatency(relay: string, list: any[]) {
|
function checkRelayLatency(relay: string, list: any[]) {
|
||||||
return async () => {
|
return async () => {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
let resp;
|
let query = network.simpleQuery(relay, "ping", "core");
|
||||||
try {
|
|
||||||
resp = await rpcCall(relay, "misc", "ping", undefined, {});
|
let resp = (await query.result) as PingRPCResponse;
|
||||||
} catch {
|
|
||||||
return;
|
if (!resp?.data?.ping) {
|
||||||
}
|
|
||||||
// @ts-ignore
|
|
||||||
if (!resp.pong) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const end = Date.now() - start;
|
const end = Date.now() - start;
|
||||||
|
|
||||||
try {
|
if (
|
||||||
resp = await relayHasMethods(
|
!(await relayHasMethods(
|
||||||
["stat_ipfs", "stat_ipns", "fetch_ipfs", "fetch_ipns"],
|
["stat_ipfs", "stat_ipns", "fetch_ipfs", "fetch_ipns"],
|
||||||
relay
|
relay
|
||||||
);
|
))
|
||||||
if (!resp) {
|
) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue