Compare commits
No commits in common. "2e1780e28f8678a52e61d063a07fc7fd3bfe02f2" and "6e61420bfd58c448e93e01d69a3383ccb6c0d28e" have entirely different histories.
2e1780e28f
...
6e61420bfd
|
@ -15,8 +15,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lumeweb/hyperswarm-web": "git+https://git.lumeweb.com/LumeWeb/hyperswarm-web.git",
|
"@lumeweb/hyperswarm-web": "git+https://git.lumeweb.com/LumeWeb/hyperswarm-web.git",
|
||||||
"@noble/ed25519": "^1.7.3",
|
"@noble/ed25519": "^1.7.3",
|
||||||
"b4a": "^1.6.3",
|
"b4a": "^1.6.2",
|
||||||
"eventemitter2": "^6.4.9",
|
|
||||||
"hyperswarm": "^4.3.7",
|
"hyperswarm": "^4.3.7",
|
||||||
"libkmodule": "^0.2.53",
|
"libkmodule": "^0.2.53",
|
||||||
"libskynet": "^0.0.62",
|
"libskynet": "^0.0.62",
|
||||||
|
|
57
src/index.ts
57
src/index.ts
|
@ -7,24 +7,14 @@ import type { Buffer } from "buffer";
|
||||||
import * as ed from "@noble/ed25519";
|
import * as ed from "@noble/ed25519";
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
import { pubKeyToIpv6 } from "./addr.js";
|
import { pubKeyToIpv6 } from "./addr.js";
|
||||||
import { EventEmitter2 as EventEmitter } from "eventemitter2";
|
|
||||||
import { logErr } from "libkmodule/dist";
|
|
||||||
|
|
||||||
const MAX_PEER_LISTENERS = 20;
|
|
||||||
|
|
||||||
interface SwarmConnection {
|
interface SwarmConnection {
|
||||||
swarm: number;
|
swarm: number;
|
||||||
conn: any;
|
conn: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SwarmEvents {
|
|
||||||
swarm: number;
|
|
||||||
events: EventEmitter;
|
|
||||||
}
|
|
||||||
|
|
||||||
const connections = new Map<number, SwarmConnection>();
|
const connections = new Map<number, SwarmConnection>();
|
||||||
const swarmInstances = new Map<number, Hyperswarm>();
|
const swarmInstances = new Map<number, Hyperswarm>();
|
||||||
const swarmEvents = new Map<number, SwarmEvents>();
|
|
||||||
|
|
||||||
let defaultSwarm: Hyperswarm;
|
let defaultSwarm: Hyperswarm;
|
||||||
|
|
||||||
|
@ -91,30 +81,16 @@ async function createSwarm(): Promise<number> {
|
||||||
swarmInstances.set(id, swarmInstance);
|
swarmInstances.set(id, swarmInstance);
|
||||||
|
|
||||||
swarmInstance.onSelf("init", () => {
|
swarmInstance.onSelf("init", () => {
|
||||||
const swarmInstanceEvents = new EventEmitter();
|
|
||||||
swarmInstanceEvents.setMaxListeners(MAX_PEER_LISTENERS);
|
|
||||||
swarmEvents.set(id, { swarm: id, events: swarmInstanceEvents });
|
|
||||||
swarmInstance.on("connection", (peer: any) => {
|
swarmInstance.on("connection", (peer: any) => {
|
||||||
const socketId = getSocketId();
|
const socketId = getSocketId();
|
||||||
connections.set(socketId, {
|
connections.set(socketId, { swarm: id, conn: peer });
|
||||||
swarm: id,
|
|
||||||
conn: peer,
|
|
||||||
});
|
|
||||||
|
|
||||||
peer.once("close", () => {
|
peer.on("close", () => {
|
||||||
connections.delete(socketId);
|
connections.delete(socketId);
|
||||||
});
|
});
|
||||||
|
|
||||||
swarmInstanceEvents.emit("connection", peer);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
swarmInstance.onSelf("close", (...args) => {
|
|
||||||
swarmEvents.get(id)?.events.emit("close", ...args);
|
|
||||||
swarmEvents.get(id)?.events.removeAllListeners();
|
|
||||||
swarmEvents.delete(id);
|
|
||||||
});
|
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -337,36 +313,28 @@ async function handleListenConnections(aq: ActiveQuery) {
|
||||||
aq.sendUpdate(getSwarmToSocketConnectionId(peer));
|
aq.sendUpdate(getSwarmToSocketConnectionId(peer));
|
||||||
};
|
};
|
||||||
|
|
||||||
const swarmEvent = swarmEvents.get(
|
swarm.on("connection", listener);
|
||||||
getSwarmToSwarmId(swarm) as number
|
|
||||||
)?.events;
|
|
||||||
|
|
||||||
if (!swarmEvent) {
|
|
||||||
logErr("swarm event object is missing");
|
|
||||||
}
|
|
||||||
|
|
||||||
swarmEvent?.on("connection", listener);
|
|
||||||
|
|
||||||
aq.setReceiveUpdate?.(() => {
|
aq.setReceiveUpdate?.(() => {
|
||||||
swarmEvent?.off("connection", listener);
|
swarm.off("connection", listener);
|
||||||
aq.respond();
|
aq.respond();
|
||||||
});
|
});
|
||||||
|
|
||||||
const closeCb = () => {
|
const closeCb = () => {
|
||||||
swarmEvent?.off("connection", listener);
|
swarm.off("connection", listener);
|
||||||
swarmEvent?.emit("close");
|
swarm.emit("close");
|
||||||
aq.respond();
|
aq.respond();
|
||||||
};
|
};
|
||||||
|
|
||||||
const hookClose = () => {
|
const hookClose = () => {
|
||||||
swarm.onceSelf("close", closeCb);
|
swarm.activeRelay.dht._protocol._stream.once("close", closeCb);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (swarm.activeRelay) {
|
if (swarm.activeRelay) {
|
||||||
hookClose();
|
hookClose();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
swarm.onceSelf("ready", hookClose);
|
swarm.once("ready", hookClose);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleGetSocketInfo(aq: ActiveQuery) {
|
async function handleGetSocketInfo(aq: ActiveQuery) {
|
||||||
|
@ -396,12 +364,3 @@ function getSwarmToSocketConnectionId(socket: any) {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
function getSwarmToSwarmId(swarm: any) {
|
|
||||||
for (const swarmInstance of swarmInstances) {
|
|
||||||
if (swarmInstance[1] === swarm) {
|
|
||||||
return swarmInstance[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue