parent
564306bed2
commit
afdeca0bca
19
src/index.ts
19
src/index.ts
|
@ -4,7 +4,7 @@ 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 { DHT } from "@lumeweb/kernel-dht-client";
|
||||||
import { pack, unpack } from "msgpackr";
|
import { pack, unpack } from "msgpackr";
|
||||||
import { clearTimeout } from "timers";
|
import { DataFn } from "libskynet";
|
||||||
|
|
||||||
onmessage = handleMessage;
|
onmessage = handleMessage;
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ interface StatFileResponse {
|
||||||
|
|
||||||
let blockingGatewayUpdate = Promise.resolve();
|
let blockingGatewayUpdate = Promise.resolve();
|
||||||
|
|
||||||
let activeRelays = [];
|
let activeRelays: string | any[] = [];
|
||||||
let relays = [
|
let relays = [
|
||||||
"25c2a0a833782d64213c08879b95dd5a60af244b44a058f3a7a70d6722f4bda7",
|
"25c2a0a833782d64213c08879b95dd5a60af244b44a058f3a7a70d6722f4bda7",
|
||||||
];
|
];
|
||||||
|
@ -106,7 +106,7 @@ async function handleStat(
|
||||||
try {
|
try {
|
||||||
let resp = (await fetchFromRelays(hash, path, method)) as StatFileResponse;
|
let resp = (await fetchFromRelays(hash, path, method)) as StatFileResponse;
|
||||||
aq.respond(resp);
|
aq.respond(resp);
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
aq.reject(e);
|
aq.reject(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ async function handleFetch(
|
||||||
try {
|
try {
|
||||||
await fetchFromRelays(hash, path, method, aq.sendUpdate);
|
await fetchFromRelays(hash, path, method, aq.sendUpdate);
|
||||||
aq.respond();
|
aq.respond();
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
aq.reject(e);
|
aq.reject(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +134,7 @@ async function fetchFromRelays(
|
||||||
hash: string,
|
hash: string,
|
||||||
path: string,
|
path: string,
|
||||||
method: string,
|
method: string,
|
||||||
stream = undefined
|
stream: DataFn | undefined = undefined
|
||||||
) {
|
) {
|
||||||
let error = new Error("NOT_FOUND");
|
let error = new Error("NOT_FOUND");
|
||||||
if (0 == activeRelays.length) {
|
if (0 == activeRelays.length) {
|
||||||
|
@ -168,7 +168,7 @@ async function relayHasMethods(
|
||||||
methodList: string[],
|
methodList: string[],
|
||||||
relay: string
|
relay: string
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
let methods = [];
|
let methods: string | string[] = [];
|
||||||
try {
|
try {
|
||||||
methods = (await rpcCall(relay, "misc", "get_methods")) as [];
|
methods = (await rpcCall(relay, "misc", "get_methods")) as [];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -190,7 +190,7 @@ async function rpcCall(
|
||||||
relay: string,
|
relay: string,
|
||||||
chain: string,
|
chain: string,
|
||||||
query: string,
|
query: string,
|
||||||
stream?: (data) => void,
|
stream?: (data: any) => void,
|
||||||
data = {}
|
data = {}
|
||||||
) {
|
) {
|
||||||
const socket = await dht.connect(relay);
|
const socket = await dht.connect(relay);
|
||||||
|
@ -246,7 +246,7 @@ async function refreshGatewayList() {
|
||||||
});
|
});
|
||||||
const queue = new PQueue({ concurrency: 10 });
|
const queue = new PQueue({ concurrency: 10 });
|
||||||
|
|
||||||
let latencies = [];
|
let latencies: any[] = [];
|
||||||
|
|
||||||
relays.forEach((item) => {
|
relays.forEach((item) => {
|
||||||
queue.add(checkRelayLatency(item, latencies));
|
queue.add(checkRelayLatency(item, latencies));
|
||||||
|
@ -268,10 +268,11 @@ function checkRelayLatency(relay: string, list: any[]) {
|
||||||
|
|
||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
resp = await rpcCall(relay, "misc", "ping", null, {});
|
resp = await rpcCall(relay, "misc", "ping", undefined, {});
|
||||||
} catch {
|
} catch {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// @ts-ignore
|
||||||
if (!resp.pong) {
|
if (!resp.pong) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue