Compare commits

..

4 Commits

4 changed files with 48 additions and 22 deletions

View File

@ -9,7 +9,7 @@ import {
RPCResponse,
} from "@lumeweb/relay-types";
import { getRpcByPeer } from "../rpc";
import { get as getSwarm } from "../swarm";
import { get as getSwarm, LUMEWEB_TOPIC_HASH } from "../swarm";
import b4a from "b4a";
async function broadcastRequest(
@ -124,6 +124,21 @@ const plugin: Plugin = {
return result;
},
});
api.registerMethod("get_peers", {
cacheable: false,
async handler(): Promise<string[]> {
return [...getRpcServer().cache.dhtCache.online];
},
});
api.registerMethod("get_direct_peers", {
cacheable: false,
async handler(): Promise<string[]> {
const topic = LUMEWEB_TOPIC_HASH.toString("hex");
return [...getRpcServer().cache.swarm.peers.values()]
.filter((item: any) => [...item._seenTopics.keys()].includes(topic))
.map((item: any) => item.publicKey.toString("hex"));
},
});
},
};

View File

@ -10,9 +10,28 @@ import crypto from "hypercore-crypto";
import NodeCache from "node-cache";
export class RPCCache extends EventEmitter {
private dhtCache?: DHTCache;
private server: RPCServer;
constructor(server: RPCServer) {
super();
this.server = server;
this._swarm = getSwarm();
this._dhtCache = new DHTCache(this._swarm, {
protocol: "lumeweb.rpccache",
});
this._data.on("del", (key: string) => {
try {
this.deleteItem(key);
} catch {}
});
}
private _dhtCache?: DHTCache;
get dhtCache(): DHTCache {
return this._dhtCache as DHTCache;
}
private _swarm?: any;
get swarm(): any {
@ -25,20 +44,6 @@ export class RPCCache extends EventEmitter {
return this._data;
}
constructor(server: RPCServer) {
super();
this.server = server;
this._swarm = getSwarm();
this.dhtCache = new DHTCache(this._swarm, {
protocol: "lumeweb.rpccache",
});
this._data.on("del", (key: string) => {
try {
this.deleteItem(key);
} catch {}
});
}
public signResponse(item: RPCCacheItem): string {
const field = item.value.signedField || "data";
const updated = item.value.updated;
@ -93,18 +98,18 @@ export class RPCCache extends EventEmitter {
item.signature = this.signResponse(item);
this.dhtCache?.addItem(queryHash);
this._dhtCache?.addItem(queryHash);
this._data.set(queryHash, item);
}
public deleteItem(queryHash: string): boolean {
const cache = this.dhtCache?.cache;
const cache = this._dhtCache?.cache;
if (!cache?.includes(queryHash)) {
throw Error("item does not exist");
}
this.dhtCache?.removeItem(queryHash);
this._dhtCache?.removeItem(queryHash);
this._data.del(queryHash);
return true;

View File

@ -175,6 +175,12 @@ export class RPCServer extends EventEmitter {
let rpcResult: RPCResponse = {};
if (ret === undefined) {
ret = {
data: true,
};
}
if (ret?.data) {
rpcResult = { ...ret };

View File

@ -18,6 +18,8 @@ import sodium from "sodium-universal";
import b4a from "b4a";
const LUMEWEB = b4a.from("lumeweb");
export const LUMEWEB_TOPIC_HASH = b4a.allocUnsafe(32);
sodium.crypto_generichash(LUMEWEB_TOPIC_HASH, LUMEWEB);
export type SecretStream = any;
@ -38,13 +40,11 @@ export async function start() {
const keyPair = getKeyPair();
node = new Hyperswarm({ keyPair, dht: new DHT({ keyPair }) });
const topic = b4a.allocUnsafe(32);
sodium.crypto_generichash(topic, LUMEWEB);
// @ts-ignore
await node.dht.ready();
await node.listen();
node.join(topic);
node.join(LUMEWEB_TOPIC_HASH);
return node;
}