*Rewrite to use new design

This commit is contained in:
Derrick Hammer 2023-02-01 07:50:45 -05:00
parent 4a62c94ee3
commit d608392532
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 126 additions and 139 deletions

View File

@ -9,41 +9,48 @@
"format": "prettier -w src", "format": "prettier -w src",
"build-script": "tsc --project tsconfig.build.json && mv dist-build/build.js dist-build/build.mjs", "build-script": "tsc --project tsconfig.build.json && mv dist-build/build.js dist-build/build.mjs",
"compile": "npm run build-script && node build.js", "compile": "npm run build-script && node build.js",
"build": "rimraf node_modules/@hyperswarm/secret-stream/node_modules node_modules/@lumeweb/dht-web/node_modules && npm run compile && node ./dist-build/build.mjs dev" "build": "npm run compile && node ./dist-build/build.mjs dev"
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@lumeweb/dht-web": "https://github.com/LumeWeb/dht-web.git", "@lumeweb/hyperswarm-web": "git+https://git.lumeweb.com/LumeWeb/hyperswarm-web.git",
"hyperswarm": "^4.0.2", "@noble/ed25519": "^1.7.1",
"libkmodule": "^0.2.12", "b4a": "^1.6.1",
"hyperswarm": "^4.3.7",
"libkmodule": "^0.2.53",
"libskynet": "^0.0.62", "libskynet": "^0.0.62",
"noise-handshake": "https://github.com/LumeWeb/noise-handshake.git", "noise-handshake": "github:LumeWeb/noise-handshake",
"randombytes": "https://github.com/LumeWeb/randombytes-browser.git" "randombytes": "github:LumeWeb/randombytes-browser"
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^22.0.1", "@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^13.3.0", "@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-typescript": "^8.3.3", "@rollup/plugin-typescript": "^8.5.0",
"@screamingvoid/sodium-universal": "^0.1.1", "@screamingvoid/sodium-universal": "^0.1.1",
"@types/jest": "^28.1.3", "@scure/bip39": "^1.1.0",
"@skynetlabs/skynet-nodejs": "^2.9.0",
"@types/b4a": "^1.6.0",
"@types/jest": "^28.1.8",
"@types/read": "^0.0.29", "@types/read": "^0.0.29",
"buffer": "^6.0.3", "buffer": "^6.0.3",
"cli-progress": "^3.11.2",
"crypto-browserify": "^3.12.0", "crypto-browserify": "^3.12.0",
"esbuild": "^0.14.48", "esbuild": "^0.14.54",
"inspectpack": "^4.7.1", "inspectpack": "^4.7.1",
"jest": "^28.1.1", "jest": "^28.1.3",
"jest-puppeteer": "^6.1.0", "jest-puppeteer": "^6.2.0",
"libskynetnode": "^0.1.3", "libskynetnode": "^0.1.4",
"prettier": "^2.7.1", "prettier": "^2.8.3",
"process": "^0.11.10", "process": "^0.11.10",
"puppeteer": "^15.2.0", "puppeteer": "^15.5.0",
"random-number-csprng": "^1.0.2",
"read": "^1.0.7", "read": "^1.0.7",
"rollup": "^2.75.7", "rollup": "^2.79.1",
"rollup-plugin-polyfill-node": "^0.9.0", "rollup-plugin-polyfill-node": "^0.9.0",
"stream-browserify": "^3.0.0", "stream-browserify": "^3.0.0",
"ts-loader": "^9.3.1", "ts-loader": "^9.4.2",
"typescript": "^4.7.4", "typescript": "^4.9.5",
"webpack": "^5.73.0", "webpack": "^5.75.0",
"webpack-cli": "^4.10.0" "webpack-cli": "^4.10.0"
}, },
"browser": { "browser": {

View File

@ -1,20 +1,21 @@
// @ts-ignore // @ts-ignore
import DHT from "@lumeweb/dht-web"; import Hyperswarm from "@lumeweb/hyperswarm-web";
import type { ActiveQuery } from "libkmodule"; import type { ActiveQuery } from "libkmodule";
import { addHandler, getSeed, handleMessage } from "libkmodule"; import { addHandler, getSeed, handleMessage } from "libkmodule";
import { handlePresentSeed as handlePresentSeedModule } from "libkmodule/dist/seed.js"; import { handlePresentSeed as handlePresentSeedModule } from "libkmodule/dist/seed.js";
import type { Buffer } from "buffer"; import type { Buffer } from "buffer";
import { hexToBuf } from "libskynet"; import * as ed from "@noble/ed25519";
import b4a from "b4a";
interface DhtConnection { interface DhtConnection {
dht: number; swarm: number;
conn: any; conn: any;
} }
const connections = new Map<number, DhtConnection>(); const connections = new Map<number, DhtConnection>();
const dhtInstances = new Map<number, DHT>(); const swarmInstances = new Map<number, Hyperswarm>();
let defaultDht: DHT; let defaultSwarm: Hyperswarm;
let moduleReadyResolve: Function; let moduleReadyResolve: Function;
let moduleReady: Promise<void> = new Promise((resolve) => { let moduleReady: Promise<void> = new Promise((resolve) => {
@ -22,120 +23,67 @@ let moduleReady: Promise<void> = new Promise((resolve) => {
}); });
onmessage = handleMessage; onmessage = handleMessage;
function idFactory(start = 1, step = 1, limit = 2 ** 32) { function idFactory(start = 1) {
let id = start; let id = start;
return function nextId() { return function nextId() {
const nextId = id; const nextId = id;
id += step; id += 1;
if (id >= limit) id = start;
return nextId; return nextId;
}; };
} }
const nextId = idFactory(1); const getSwarmId = idFactory();
const getSocketId = idFactory();
addHandler("presentSeed", handlePresentSeed); addHandler("presentSeed", handlePresentSeed);
addHandler("openDht", handleOpenDht); addHandler("joinPeer", handleJoinPeer);
addHandler("closeDht", handleCloseDht); addHandler("getPeerByPubkey", handleGetPeerByPubkey);
addHandler("connect", handleConnect);
addHandler("listenSocketEvent", handleListenSocketEvent, { addHandler("listenSocketEvent", handleListenSocketEvent, {
receiveUpdates: true, receiveUpdates: true,
}); });
addHandler("socketExists", handleSocketExists); addHandler("socketExists", handleSocketExists);
addHandler("close", handleCloseSocketEvent); addHandler("close", handleCloseSocketEvent);
addHandler("write", handleWriteSocketEvent); addHandler("socketWrite", handleWriteSocketEvent);
addHandler("addRelay", handleAddRelay); addHandler("addRelay", handleAddRelay);
addHandler("removeRelay", handleRemoveRelay); addHandler("removeRelay", handleRemoveRelay);
addHandler("clearRelays", handleClearRelays); addHandler("clearRelays", handleClearRelays);
addHandler("getRelays", handleGetRelays); addHandler("getRelays", handleGetRelays);
addHandler("getRelayServers", handleGetRelayServers);
addHandler("ready", handleReady); addHandler("ready", handleReady);
async function handlePresentSeed(aq: ActiveQuery) { async function handlePresentSeed(aq: ActiveQuery) {
const keyPair = aq.callerInput.myskyRootKeypair; handlePresentSeedModule({
handlePresentSeedModule({ callerInput: { seed: keyPair } } as ActiveQuery); callerInput: {
if (!defaultDht) { seed: {
defaultDht = dhtInstances.get(await createDht()) as DHT; publicKey: await ed.getPublicKey(aq.callerInput.rootKey),
secretKey: aq.callerInput.rootKey,
},
},
} as ActiveQuery);
if (!defaultSwarm) {
defaultSwarm = swarmInstances.get(await createSwarm()) as Hyperswarm;
} }
moduleReadyResolve(); moduleReadyResolve();
} }
async function handleOpenDht(aq: ActiveQuery) { async function createSwarm(): Promise<number> {
const id = await createDht(); const swarmInstance = new Hyperswarm({ keyPair: await getSeed() });
aq.respond({ dht: id }); const id = getSwarmId();
} swarmInstances.set(id, swarmInstance);
async function handleCloseDht(aq: ActiveQuery) { swarmInstance.on("connection", (peer) => {
const { dht = null } = aq.callerInput; const socketId = getSocketId();
connections.set(socketId, { swarm: id, conn: peer });
if (!dht) { peer.on("close", () => {
aq.reject("Invalid DHT id"); connections.delete(socketId);
return; });
}
if (dht === defaultDht) {
aq.reject("Cannot close default DHT");
return;
}
dhtInstances.delete(dht);
Array.from(connections.values())
.filter((item) => item.dht === dht)
.forEach((item) => {
item.conn.end();
}); });
aq.respond();
}
async function createDht(): Promise<number> {
const dhtInstance = new DHT({ keyPair: await getSeed() });
const id = nextId();
dhtInstances.set(id, dhtInstance);
return id; return id;
} }
async function handleConnect(aq: ActiveQuery) {
const { pubkey, options = {} } = aq.callerInput;
let socket: any;
const dht = await getDht(aq);
try {
// @ts-ignore
socket = await dht.connect(
typeof pubkey === "string" ? hexToBuf(pubkey).shift() : pubkey,
options
);
} catch (e: any) {
aq.reject(e);
return;
}
const id = nextId();
socket.on("open", () => {
let dhtId: any = [...dhtInstances.entries()].filter(
(item) => item[1] === dht
);
dhtId = dhtId.shift()[0];
setDhtConnection(id, dhtId as number, socket);
aq.respond({ id });
});
socket.on("end", () => {
deleteDhtConnection(id);
});
socket.on("error", (e: any) => {
deleteDhtConnection(id);
aq.reject(e);
});
}
function handleListenSocketEvent(aq: ActiveQuery) { function handleListenSocketEvent(aq: ActiveQuery) {
const { event = null } = aq.callerInput; const { event = null } = aq.callerInput;
@ -183,7 +131,7 @@ function handleListenSocketEvent(aq: ActiveQuery) {
async function handleSocketExists(aq: ActiveQuery) { async function handleSocketExists(aq: ActiveQuery) {
const { id = null } = aq.callerInput; const { id = null } = aq.callerInput;
aq.respond(hasDhtConnection(Number(id))); aq.respond(connections.has(Number(id)));
} }
function handleCloseSocketEvent(aq: ActiveQuery) { function handleCloseSocketEvent(aq: ActiveQuery) {
@ -219,32 +167,32 @@ function handleWriteSocketEvent(aq: ActiveQuery) {
function validateConnection(aq: ActiveQuery): any | boolean { function validateConnection(aq: ActiveQuery): any | boolean {
const { id = null } = aq.callerInput; const { id = null } = aq.callerInput;
if (!id || !hasDhtConnection(id)) { if (!id || !connections.has(id)) {
aq.reject("Invalid connection id"); aq.reject("Invalid connection id");
return false; return false;
} }
return getDhtConnection(id)?.conn; return connections.get(id)?.conn;
} }
async function getDht(aq: ActiveQuery): Promise<DHT> { async function getSwarm(aq: ActiveQuery): Promise<Hyperswarm> {
await moduleReady; await moduleReady;
let dht; let swarm;
if ("callerInput" in aq && aq.callerInput) { if ("callerInput" in aq && aq.callerInput) {
dht = aq.callerInput.dht ?? null; swarm = aq.callerInput.swarm ?? null;
if (dht && !dhtInstances.has(dht)) { if (swarm && !swarmInstances.has(swarm)) {
const error = "Invalid DHT id"; const error = "Invalid swarm id";
aq.reject(error); aq.reject(error);
throw new Error(error); throw new Error(error);
} }
} }
if (!dht) { if (!swarm) {
return defaultDht; return defaultSwarm;
} }
return dhtInstances.get(dht) as DHT; return swarmInstances.get(swarm) as Hyperswarm;
} }
async function handleAddRelay(aq: ActiveQuery) { async function handleAddRelay(aq: ActiveQuery) {
@ -255,7 +203,7 @@ async function handleAddRelay(aq: ActiveQuery) {
return; return;
} }
const dht = await getDht(aq); const dht = await getSwarm(aq);
aq.respond(await dht.addRelay(pubkey)); aq.respond(await dht.addRelay(pubkey));
} }
@ -268,13 +216,13 @@ async function handleRemoveRelay(aq: ActiveQuery) {
return; return;
} }
const dht = await getDht(aq); const dht = await getSwarm(aq);
aq.respond(dht.removeRelay(pubkey)); aq.respond(dht.removeRelay(pubkey));
} }
async function handleClearRelays(aq: ActiveQuery) { async function handleClearRelays(aq: ActiveQuery) {
const dht = await getDht(aq); const dht = await getSwarm(aq);
dht.clearRelays(); dht.clearRelays();
@ -282,29 +230,61 @@ async function handleClearRelays(aq: ActiveQuery) {
} }
async function handleGetRelays(aq: ActiveQuery) { async function handleGetRelays(aq: ActiveQuery) {
aq.respond(await (await getDht(aq)).relays); aq.respond(await (await getSwarm(aq)).relays);
} }
async function handleGetRelayServers(aq: ActiveQuery) {
aq.respond(await (await getDht(aq)).relayServers); async function handleJoinPeer(aq: ActiveQuery) {
const { topic = null } = aq.callerInput;
const swarm = await getSwarm(aq);
if (!topic) {
aq.reject("invalid topic");
return;
}
if (!b4a.isBuffer(topic)) {
aq.reject("topic must be a buffer");
return;
}
// @ts-ignore
swarm.join(topic);
aq.respond();
}
async function handleGetPeerByPubkey(aq: ActiveQuery) {
const { pubkey = null } = aq.callerInput;
const swarm = await getSwarm(aq);
if (!pubkey) {
aq.reject("invalid topic");
return;
}
if (!b4a.isBuffer(pubkey)) {
aq.reject("pubkey must be a buffer");
return;
}
// @ts-ignore
if (!swarm._allConnections.has(pubkey)) {
aq.reject("peer does not exist");
return;
}
// @ts-ignore
const peer = swarm._allConnections.get(pubkey);
aq.respond(
[...connections.entries()].filter((conn) => {
return conn[1].conn === peer;
})[0][0]
);
} }
async function handleReady(aq: ActiveQuery) { async function handleReady(aq: ActiveQuery) {
await (await getDht(aq)).ready(); const swarm = await getSwarm(aq);
// @ts-ignore
await swarm.ready();
aq.respond(); aq.respond();
} }
function setDhtConnection(id: number, dht: number, conn: any) {
connections.set(id, { dht, conn });
}
function getDhtConnection(id: number) {
return connections.get(id);
}
function hasDhtConnection(id: number) {
return connections.has(id);
}
function deleteDhtConnection(id: number) {
connections.delete(id);
}