Compare commits

...

2 Commits

Author SHA1 Message Date
Derrick Hammer be22b771f5
*Update dht flood
ci/woodpecker/push/woodpecker Pipeline was successful Details
2022-12-30 16:44:02 -05:00
Derrick Hammer 1b78d0e696
*Implement a protocol manager to register protomux based protocols and add it to the plugin api 2022-12-30 01:00:45 -05:00
3 changed files with 55 additions and 3 deletions

View File

@ -13,7 +13,11 @@ import pluginCore from "./plugins/core";
import type Config from "@lumeweb/cfg";
import EventEmitter2 from "eventemitter2";
import log from "../log.js";
import { get as getSwarm } from "./swarm.js";
import {
get as getSwarm,
getProtocolManager,
ProtocolManager,
} from "./swarm.js";
import { get as getSSl, SSLManager } from "./ssl.js";
import type { HDKey } from "micro-ed25519-hdkey";
@ -86,6 +90,10 @@ class PluginAPI extends EventEmitter2 {
return getSSl();
}
get protocols(): ProtocolManager {
return getProtocolManager();
}
public loadPlugin(
moduleName: string
): (moduleName: string) => Promise<Plugin> {

View File

@ -5,6 +5,8 @@
import Hyperswarm from "hyperswarm";
// @ts-ignore
import DHT from "@hyperswarm/dht";
// @ts-ignore
import Protomux from "protomux";
// @ts-ignore
import sodium from "sodium-universal";
@ -20,6 +22,7 @@ sodium.crypto_generichash(LUMEWEB_TOPIC_HASH, LUMEWEB);
export type SecretStream = any;
let node: Hyperswarm;
let protocolManager: ProtocolManager;
export async function start() {
const keyPair = getKeyPair();
@ -51,3 +54,44 @@ export async function start() {
export function get(): Hyperswarm {
return node;
}
export class ProtocolManager {
private _protocols: Map<string, Function> = new Map<string, Function>();
private _swarm;
constructor(swarm: any) {
this._swarm = swarm;
this._swarm.on("connection", (peer: any) => {
for (const protocol of this._protocols) {
Protomux.from(peer).pair(
protocol[0],
this.handler.bind(this, protocol[0], peer)
);
}
});
}
private handler(protocol: string, peer: any) {
if (this._protocols.has(protocol)) {
this._protocols.get(protocol)?.(peer, Protomux.from(peer));
}
}
public register(name: string, handler: Function): boolean {
if (this._protocols.has(name)) {
return false;
}
this._protocols.set(name, handler);
return true;
}
}
export function getProtocolManager(): ProtocolManager {
if (!protocolManager) {
protocolManager = new ProtocolManager(get());
}
return protocolManager;
}

View File

@ -650,13 +650,13 @@ __metadata:
"@lumeweb/dht-flood@https://git.lumeweb.com/LumeWeb/dht-flood.git":
version: 0.1.0
resolution: "@lumeweb/dht-flood@https://git.lumeweb.com/LumeWeb/dht-flood.git#commit=993eff5398ce3018dc52f281d71750676d4cb700"
resolution: "@lumeweb/dht-flood@https://git.lumeweb.com/LumeWeb/dht-flood.git#commit=9cd5e798d91bfb5ffe9665894421e22372852f8f"
dependencies:
compact-encoding: "npm:^2.11.0"
lru: "npm:^3.1.0"
protocol-buffers-encodings: "npm:^1.2.0"
protomux-rpc: "npm:^1.3.0"
checksum: 181d25e531c54185a4db2d6adfdd0b2d506aceee3e7fc9af5217c0038f1e6e291e5b093a45d570febce85210bdb58aa701fda6fa27004dcfb685608c215a0883
checksum: de53558a9e91bcade4136c1566b5ee913b03b5086e91cce64fdff2c5e2e09726e33829d42da6294ad324f8b7ee184fba5e0121efacbb314522a4d928f2705bf5
languageName: node
linkType: hard