2023-03-17 09:36:02 +00:00
|
|
|
import { createHelia } from "helia";
|
|
|
|
// @ts-ignore
|
|
|
|
import Hyperswarm from "hyperswarm";
|
2023-07-06 05:14:10 +00:00
|
|
|
import { MultiSocketProxy } from "@lumeweb/libhyperproxy";
|
2023-03-17 09:36:02 +00:00
|
|
|
import { UnixFS, unixfs } from "@helia/unixfs";
|
2023-07-09 15:29:05 +00:00
|
|
|
import { PROTOCOL } from "./constants.js";
|
2023-07-06 05:14:10 +00:00
|
|
|
import {
|
|
|
|
ActiveQuery,
|
|
|
|
addHandler,
|
|
|
|
handleMessage,
|
|
|
|
} from "@lumeweb/libkernel/module";
|
2023-03-17 09:36:02 +00:00
|
|
|
import { createClient } from "@lumeweb/kernel-swarm-client";
|
2023-07-09 15:29:05 +00:00
|
|
|
import { ipns, IPNS } from "@helia/ipns";
|
2023-03-30 22:05:14 +00:00
|
|
|
import { dht, pubsub } from "@helia/ipns/routing";
|
|
|
|
// @ts-ignore
|
|
|
|
import { gossipsub } from "@chainsafe/libp2p-gossipsub";
|
|
|
|
import { CID } from "multiformats/cid";
|
|
|
|
import { bases } from "multiformats/basics";
|
|
|
|
import { substr } from "runes2";
|
|
|
|
import { MultibaseDecoder } from "multiformats";
|
|
|
|
import { peerIdFromCID } from "@libp2p/peer-id";
|
2023-03-30 22:56:26 +00:00
|
|
|
import { IDBBlockstore } from "blockstore-idb";
|
|
|
|
import { IDBDatastore } from "datastore-idb";
|
2023-03-31 00:57:08 +00:00
|
|
|
import defer from "p-defer";
|
2023-04-17 00:51:32 +00:00
|
|
|
import { Helia } from "@helia/interface";
|
2023-07-06 05:14:10 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import type { Components } from "libp2p/src/components.js";
|
2023-07-09 15:29:05 +00:00
|
|
|
import { libp2pConfig } from "./config.js";
|
2023-03-30 22:05:14 +00:00
|
|
|
|
|
|
|
const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
|
2023-07-06 05:14:10 +00:00
|
|
|
bases,
|
2023-03-30 22:05:14 +00:00
|
|
|
).reduce((acc, curr) => {
|
|
|
|
// @ts-ignore
|
|
|
|
acc[bases[curr].prefix] = bases[curr];
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2022-08-31 19:19:35 +00:00
|
|
|
|
|
|
|
onmessage = handleMessage;
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-31 00:57:08 +00:00
|
|
|
const moduleDefer = defer();
|
2023-04-17 00:51:32 +00:00
|
|
|
let activeIpfsPeersDefer = defer();
|
|
|
|
let networkPeersAvailable = defer();
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
let swarm;
|
2023-04-17 00:51:32 +00:00
|
|
|
let proxy: MultiSocketProxy;
|
2023-03-17 09:36:02 +00:00
|
|
|
let fs: UnixFS;
|
2023-03-30 22:05:14 +00:00
|
|
|
let IPNS: IPNS;
|
2023-04-17 00:51:32 +00:00
|
|
|
let ipfs: Helia;
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
// @ts-ignore
|
|
|
|
BigInt.prototype.toJSON = function () {
|
|
|
|
return this.toString();
|
|
|
|
};
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-07-06 05:14:10 +00:00
|
|
|
addHandler("presentKey", handlePresentKey);
|
2023-04-03 23:02:13 +00:00
|
|
|
addHandler("ready", handleReady);
|
2023-03-17 09:36:02 +00:00
|
|
|
addHandler("stat", handleStat);
|
|
|
|
addHandler("ls", handleLs, { receiveUpdates: true });
|
|
|
|
addHandler("cat", handleCat, { receiveUpdates: true });
|
2023-03-30 22:05:14 +00:00
|
|
|
addHandler("ipnsResolve", handleIpnsResolve);
|
2023-03-31 01:06:04 +00:00
|
|
|
addHandler("getActivePeers", handleGetActivePeers);
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-07-06 05:14:10 +00:00
|
|
|
async function handlePresentKey() {
|
2023-03-17 09:36:02 +00:00
|
|
|
swarm = createClient();
|
|
|
|
|
2023-04-17 00:51:32 +00:00
|
|
|
proxy = new MultiSocketProxy({
|
|
|
|
swarm,
|
|
|
|
listen: true,
|
|
|
|
protocol: PROTOCOL,
|
|
|
|
autostart: true,
|
|
|
|
emulateWebsocket: true,
|
|
|
|
server: false,
|
|
|
|
});
|
|
|
|
|
2023-03-30 22:56:26 +00:00
|
|
|
const blockstore = new IDBBlockstore("ipfs_blocks");
|
|
|
|
const datastore = new IDBDatastore("ipfs_data");
|
|
|
|
|
|
|
|
await blockstore.open();
|
|
|
|
await datastore.open();
|
2023-04-17 00:51:32 +00:00
|
|
|
ipfs = await createHelia({
|
2023-03-30 22:56:26 +00:00
|
|
|
blockstore,
|
2023-07-06 05:14:10 +00:00
|
|
|
// @ts-ignore
|
2023-07-09 15:29:05 +00:00
|
|
|
datastore,
|
|
|
|
libp2p: libp2pConfig(proxy),
|
|
|
|
start: false,
|
2023-03-17 09:36:02 +00:00
|
|
|
});
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-04-17 00:51:32 +00:00
|
|
|
proxy.on("peerChannelOpen", async () => {
|
|
|
|
if (!ipfs.libp2p.isStarted()) {
|
2023-07-09 15:29:05 +00:00
|
|
|
await ipfs.start();
|
2023-04-17 00:51:32 +00:00
|
|
|
networkPeersAvailable.resolve();
|
|
|
|
}
|
2023-03-17 09:36:02 +00:00
|
|
|
});
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
swarm.join(PROTOCOL);
|
|
|
|
await swarm.start();
|
2023-03-30 22:05:14 +00:00
|
|
|
await swarm.ready();
|
2023-03-17 09:36:02 +00:00
|
|
|
// @ts-ignore
|
2023-03-31 01:05:45 +00:00
|
|
|
fs = unixfs(ipfs);
|
|
|
|
IPNS = ipns(ipfs as any, [dht(ipfs), pubsub(ipfs as any)]);
|
|
|
|
|
|
|
|
ipfs.libp2p.addEventListener("peer:connect", () => {
|
|
|
|
if (ipfs.libp2p.getPeers().length > 0) {
|
2023-04-17 00:51:32 +00:00
|
|
|
activeIpfsPeersDefer.resolve();
|
2023-03-31 01:05:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipfs.libp2p.addEventListener("peer:disconnect", () => {
|
|
|
|
if (ipfs.libp2p.getPeers().length === 0) {
|
2023-04-17 00:51:32 +00:00
|
|
|
activeIpfsPeersDefer = defer();
|
2023-03-31 01:05:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-31 00:57:08 +00:00
|
|
|
moduleDefer.resolve();
|
2022-08-05 13:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 23:02:13 +00:00
|
|
|
async function handleReady(aq: ActiveQuery) {
|
|
|
|
await ready();
|
|
|
|
|
|
|
|
aq.respond();
|
|
|
|
}
|
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
async function handleStat(aq: ActiveQuery) {
|
2023-03-30 22:05:14 +00:00
|
|
|
await ready();
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
if (!("cid" in aq.callerInput)) {
|
|
|
|
aq.reject("cid required");
|
2022-08-05 13:25:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
let aborted = false;
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
aq.setReceiveUpdate?.(() => {
|
|
|
|
aborted = true;
|
|
|
|
});
|
2022-08-05 13:25:55 +00:00
|
|
|
|
|
|
|
try {
|
2023-03-17 09:36:02 +00:00
|
|
|
aq.respond(
|
|
|
|
JSON.parse(
|
|
|
|
JSON.stringify(
|
2023-04-01 16:51:11 +00:00
|
|
|
await fs.stat(
|
|
|
|
getCID(aq.callerInput.cid),
|
2023-07-06 05:14:10 +00:00
|
|
|
aq.callerInput.options ?? {},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-03-17 09:36:02 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
aq.reject((e as Error).message);
|
2022-08-05 13:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
async function handleLs(aq: ActiveQuery) {
|
2023-03-30 22:05:14 +00:00
|
|
|
await ready();
|
2023-03-17 09:36:02 +00:00
|
|
|
if (!("cid" in aq.callerInput)) {
|
|
|
|
aq.reject("cid required");
|
2022-08-05 13:25:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
let aborted = false;
|
2023-04-09 21:16:12 +00:00
|
|
|
let nextChunk = defer();
|
|
|
|
|
|
|
|
aq.setReceiveUpdate?.((data: any) => {
|
|
|
|
switch (data) {
|
|
|
|
case "abort":
|
|
|
|
aborted = true;
|
|
|
|
break;
|
|
|
|
case "next":
|
|
|
|
nextChunk.resolve();
|
|
|
|
nextChunk = defer();
|
|
|
|
break;
|
|
|
|
}
|
2023-03-17 09:36:02 +00:00
|
|
|
});
|
2023-04-01 16:51:11 +00:00
|
|
|
const iterable = fs.ls(
|
|
|
|
getCID(aq.callerInput.cid),
|
2023-07-06 05:14:10 +00:00
|
|
|
aq.callerInput.options ?? {},
|
2023-04-01 16:51:11 +00:00
|
|
|
);
|
2023-03-17 09:36:02 +00:00
|
|
|
|
|
|
|
for await (const item of iterable) {
|
|
|
|
if (aborted) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
aq.sendUpdate(JSON.parse(JSON.stringify(item)));
|
2023-04-09 21:16:12 +00:00
|
|
|
|
|
|
|
await nextChunk.promise;
|
2022-08-05 13:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
aq.respond();
|
2022-08-05 13:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
async function handleCat(aq: ActiveQuery) {
|
2023-03-30 22:05:14 +00:00
|
|
|
await ready();
|
2022-08-31 19:19:35 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
if (!("cid" in aq.callerInput)) {
|
|
|
|
aq.reject("cid required");
|
|
|
|
return;
|
2022-08-05 13:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
let aborted = false;
|
2023-04-09 21:16:12 +00:00
|
|
|
let nextChunk = defer();
|
|
|
|
|
|
|
|
aq.setReceiveUpdate?.((data: any) => {
|
|
|
|
switch (data) {
|
|
|
|
case "abort":
|
|
|
|
aborted = true;
|
|
|
|
break;
|
|
|
|
case "next":
|
|
|
|
nextChunk.resolve();
|
|
|
|
nextChunk = defer();
|
|
|
|
break;
|
|
|
|
}
|
2022-08-05 13:25:55 +00:00
|
|
|
});
|
|
|
|
|
2023-04-01 16:51:11 +00:00
|
|
|
const iterable = fs.cat(
|
|
|
|
getCID(aq.callerInput.cid),
|
2023-07-06 05:14:10 +00:00
|
|
|
aq.callerInput.options ?? {},
|
2023-04-01 16:51:11 +00:00
|
|
|
);
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
for await (const chunk of iterable) {
|
|
|
|
if (aborted) {
|
|
|
|
break;
|
|
|
|
}
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
aq.sendUpdate(chunk);
|
2023-04-09 21:16:12 +00:00
|
|
|
|
|
|
|
await nextChunk.promise;
|
2023-03-17 09:36:02 +00:00
|
|
|
}
|
2022-08-05 13:25:55 +00:00
|
|
|
|
2023-03-17 09:36:02 +00:00
|
|
|
aq.respond();
|
2022-08-05 13:25:55 +00:00
|
|
|
}
|
2022-08-31 19:19:35 +00:00
|
|
|
|
2023-03-30 22:05:14 +00:00
|
|
|
async function handleIpnsResolve(aq: ActiveQuery) {
|
|
|
|
await ready();
|
2023-03-31 01:05:45 +00:00
|
|
|
|
2023-04-17 00:51:32 +00:00
|
|
|
await activeIpfsPeersDefer.promise;
|
2023-03-31 01:05:45 +00:00
|
|
|
|
2023-04-17 00:51:32 +00:00
|
|
|
if (ipfs.libp2p.getPeers().length === 0) {
|
|
|
|
activeIpfsPeersDefer = defer();
|
2023-03-31 15:37:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:51:32 +00:00
|
|
|
await activeIpfsPeersDefer.promise;
|
2023-03-31 15:37:04 +00:00
|
|
|
|
2023-03-30 22:05:14 +00:00
|
|
|
if (!aq.callerInput || !("cid" in aq.callerInput)) {
|
|
|
|
aq.reject("cid required");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return aq.respond(
|
2023-03-30 23:01:50 +00:00
|
|
|
(
|
2023-04-01 16:51:11 +00:00
|
|
|
await IPNS.resolve(
|
|
|
|
peerIdFromCID(getCID(aq.callerInput.cid)),
|
2023-07-06 05:14:10 +00:00
|
|
|
aq.callerInput?.options,
|
2023-04-01 16:51:11 +00:00
|
|
|
)
|
2023-07-06 05:14:10 +00:00
|
|
|
).asCID.toString(),
|
2023-03-30 22:05:14 +00:00
|
|
|
);
|
|
|
|
} catch (e: any) {
|
|
|
|
aq.reject((e as Error).message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 16:51:11 +00:00
|
|
|
function getCID(cid: string): CID {
|
2023-04-01 16:57:14 +00:00
|
|
|
try {
|
|
|
|
return CID.parse(cid);
|
|
|
|
} catch {}
|
|
|
|
|
2023-04-01 16:51:11 +00:00
|
|
|
const prefix = substr(cid, 0, 1);
|
|
|
|
|
|
|
|
if (!(prefix in basesByPrefix)) {
|
|
|
|
throw new Error("invalid multibase found in CID");
|
|
|
|
}
|
|
|
|
|
|
|
|
const base = basesByPrefix[prefix];
|
|
|
|
return CID.parse(cid, base);
|
|
|
|
}
|
|
|
|
|
2023-03-31 01:06:04 +00:00
|
|
|
async function handleGetActivePeers(aq: ActiveQuery) {
|
|
|
|
await ready();
|
|
|
|
|
2023-04-17 00:51:32 +00:00
|
|
|
aq.respond(ipfs.libp2p.getPeers());
|
2023-03-31 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 22:05:14 +00:00
|
|
|
async function ready() {
|
2023-03-31 00:57:08 +00:00
|
|
|
await moduleDefer.promise;
|
2023-04-17 00:51:32 +00:00
|
|
|
await networkPeersAvailable.promise;
|
2023-03-30 22:05:14 +00:00
|
|
|
}
|