kernel-ipfs/src/index.ts

364 lines
7.7 KiB
TypeScript
Raw Normal View History

import { createHelia } from "helia";
// @ts-ignore
import Hyperswarm from "hyperswarm";
import { MultiSocketProxy } from "@lumeweb/libhyperproxy";
import { UnixFS, unixfs } from "@helia/unixfs";
import { PROTOCOL } from "./constants.js";
import {
ActiveQuery,
addHandler,
handleMessage,
} from "@lumeweb/libkernel/module";
import { createClient } from "@lumeweb/kernel-swarm-client";
import { ipns, IPNS } from "@helia/ipns";
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";
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";
// @ts-ignore
import type { Components } from "libp2p/src/components.js";
import { libp2pConfig } from "./config.js";
import { createClient as createNetworkRegistryClient } from "@lumeweb/kernel-network-registry-client";
const basesByPrefix: { [prefix: string]: MultibaseDecoder<any> } = Object.keys(
bases,
).reduce((acc, curr) => {
// @ts-ignore
acc[bases[curr].prefix] = bases[curr];
return acc;
}, {});
2022-08-31 19:19:35 +00:00
const TYPES = ["content"];
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();
let networkReady = false;
const networkRegistry = createNetworkRegistryClient();
2022-08-05 13:25:55 +00:00
let swarm;
2023-04-17 00:51:32 +00:00
let proxy: MultiSocketProxy;
let fs: UnixFS;
let IPNS: IPNS;
2023-04-17 00:51:32 +00:00
let ipfs: Helia;
2022-08-05 13:25:55 +00:00
// @ts-ignore
BigInt.prototype.toJSON = function () {
return this.toString();
};
2022-08-05 13:25:55 +00:00
addHandler("presentKey", handlePresentKey);
addHandler("register", handleRegister);
2023-07-23 13:53:21 +00:00
addHandler("status", handleStatus, { receiveUpdates: true });
2023-07-29 03:19:43 +00:00
addHandler("name", handleName);
2023-04-03 23:02:13 +00:00
addHandler("ready", handleReady);
addHandler("stat", handleStat);
addHandler("ls", handleLs, { receiveUpdates: true });
addHandler("cat", handleCat, { receiveUpdates: true });
addHandler("ipnsResolve", handleIpnsResolve);
2023-03-31 01:06:04 +00:00
addHandler("getActivePeers", handleGetActivePeers);
2022-08-05 13:25:55 +00:00
async function handlePresentKey() {
swarm = createClient();
2023-04-17 00:51:32 +00:00
proxy = new MultiSocketProxy({
swarm,
listen: true,
protocol: PROTOCOL,
autostart: true,
emulateWebsocket: true,
server: false,
});
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({
blockstore,
// @ts-ignore
datastore,
libp2p: libp2pConfig(proxy),
start: false,
});
2022-08-05 13:25:55 +00:00
2023-04-17 00:51:32 +00:00
proxy.on("peerChannelOpen", async () => {
if (!ipfs.libp2p.isStarted()) {
await ipfs.start();
2023-04-17 00:51:32 +00:00
networkPeersAvailable.resolve();
2023-07-23 13:53:21 +00:00
networkReady = true;
2023-04-17 00:51:32 +00:00
}
});
2022-08-05 13:25:55 +00:00
swarm.join(PROTOCOL);
await swarm.start();
await swarm.ready();
// @ts-ignore
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();
}
});
ipfs.libp2p.addEventListener("peer:disconnect", () => {
if (ipfs.libp2p.getPeers().length === 0) {
2023-04-17 00:51:32 +00:00
activeIpfsPeersDefer = defer();
}
});
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();
}
async function handleStat(aq: ActiveQuery) {
await ready();
2022-08-05 13:25:55 +00:00
if (!("cid" in aq.callerInput)) {
aq.reject("cid required");
2022-08-05 13:25:55 +00:00
return;
}
let aborted = false;
2022-08-05 13:25:55 +00:00
aq.setReceiveUpdate?.(() => {
aborted = true;
});
2022-08-05 13:25:55 +00:00
try {
aq.respond(
JSON.parse(
JSON.stringify(
await fs.stat(
getCID(aq.callerInput.cid),
aq.callerInput.options ?? {},
),
),
),
);
} catch (e) {
aq.reject((e as Error).message);
2022-08-05 13:25:55 +00:00
}
}
async function handleLs(aq: ActiveQuery) {
await ready();
if (!("cid" in aq.callerInput)) {
aq.reject("cid required");
2022-08-05 13:25:55 +00:00
return;
}
let aborted = false;
let nextChunk = defer();
aq.setReceiveUpdate?.((data: any) => {
switch (data) {
case "abort":
aborted = true;
break;
case "next":
nextChunk.resolve();
nextChunk = defer();
break;
}
});
const iterable = fs.ls(
getCID(aq.callerInput.cid),
aq.callerInput.options ?? {},
);
for await (const item of iterable) {
if (aborted) {
break;
}
aq.sendUpdate(JSON.parse(JSON.stringify(item)));
await nextChunk.promise;
2022-08-05 13:25:55 +00:00
}
aq.respond();
2022-08-05 13:25:55 +00:00
}
async function handleCat(aq: ActiveQuery) {
await ready();
2022-08-31 19:19:35 +00:00
if (!("cid" in aq.callerInput)) {
aq.reject("cid required");
return;
2022-08-05 13:25:55 +00:00
}
let aborted = false;
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
});
const iterable = fs.cat(
getCID(aq.callerInput.cid),
aq.callerInput.options ?? {},
);
2022-08-05 13:25:55 +00:00
for await (const chunk of iterable) {
if (aborted) {
break;
}
2022-08-05 13:25:55 +00:00
aq.sendUpdate(chunk);
await nextChunk.promise;
}
2022-08-05 13:25:55 +00:00
aq.respond();
2022-08-05 13:25:55 +00:00
}
2022-08-31 19:19:35 +00:00
async function handleIpnsResolve(aq: ActiveQuery) {
await ready();
2023-04-17 00:51:32 +00:00
await activeIpfsPeersDefer.promise;
2023-04-17 00:51:32 +00:00
if (ipfs.libp2p.getPeers().length === 0) {
activeIpfsPeersDefer = defer();
}
2023-04-17 00:51:32 +00:00
await activeIpfsPeersDefer.promise;
if (!aq.callerInput || !("cid" in aq.callerInput)) {
aq.reject("cid required");
return;
}
try {
return aq.respond(
2023-03-30 23:01:50 +00:00
(
await IPNS.resolve(
peerIdFromCID(getCID(aq.callerInput.cid)),
aq.callerInput?.options,
)
).asCID.toString(),
);
} catch (e: any) {
aq.reject((e as Error).message);
}
}
function getCID(cid: string): CID {
try {
return CID.parse(cid);
} catch {}
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
}
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;
}
async function handleRegister(aq: ActiveQuery) {
await networkRegistry.registerNetwork(TYPES);
aq.respond();
}
2023-07-23 13:53:21 +00:00
async function handleStatus(aq: ActiveQuery) {
function sendUpdate() {
aq.sendUpdate({
peers: netPeers,
ready: netPeers > 0,
});
}
let netPeers = 0;
if (!networkReady) {
sendUpdate();
await ready();
getPeers();
} else {
getPeers();
2023-07-23 13:53:21 +00:00
}
function getPeers() {
netPeers = ipfs.libp2p.getPeers().length;
}
function peersListener() {
getPeers();
sendUpdate();
}
const peerEvents = ["connection:prune", "peer:connect", "peer:disconnect"];
peerEvents.forEach((ev) => {
// @ts-ignore
ipfs.libp2p.components.connectionManager.events.addEventListener(
ev,
peersListener,
);
});
// @ts-ignore
ipfs.libp2p.components.connectionManager.events.addEventListener(
"peer:disconnect",
peersListener,
);
ipfs.libp2p.addEventListener("start", peersListener);
2023-07-23 13:53:21 +00:00
aq.setReceiveUpdate?.(() => {
peerEvents.forEach((ev) => {
// @ts-ignore
ipfs.libp2p.components.connectionManager.events.removeEventListener(
ev,
peersListener,
);
});
ipfs.libp2p.removeEventListener("start", peersListener);
2023-07-23 13:53:21 +00:00
aq.respond();
});
sendUpdate();
}
2023-07-29 03:19:43 +00:00
function handleName(aq: ActiveQuery) {
aq.respond("IPFS");
}