Compare commits

..

No commits in common. "cd7b12e8b3864c8660ad25b0e7d43a1b5862b8cf" and "a87660b678f8b890ea574f11ec4c9ccb54fbff9b" have entirely different histories.

4 changed files with 22 additions and 48 deletions

View File

@ -9,7 +9,7 @@ import {
RPCResponse, RPCResponse,
} from "@lumeweb/relay-types"; } from "@lumeweb/relay-types";
import { getRpcByPeer } from "../rpc"; import { getRpcByPeer } from "../rpc";
import { get as getSwarm, LUMEWEB_TOPIC_HASH } from "../swarm"; import { get as getSwarm } from "../swarm";
import b4a from "b4a"; import b4a from "b4a";
async function broadcastRequest( async function broadcastRequest(
@ -124,21 +124,6 @@ const plugin: Plugin = {
return result; 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,28 +10,9 @@ import crypto from "hypercore-crypto";
import NodeCache from "node-cache"; import NodeCache from "node-cache";
export class RPCCache extends EventEmitter { export class RPCCache extends EventEmitter {
private dhtCache?: DHTCache;
private server: RPCServer; 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; private _swarm?: any;
get swarm(): any { get swarm(): any {
@ -44,6 +25,20 @@ export class RPCCache extends EventEmitter {
return this._data; 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 { public signResponse(item: RPCCacheItem): string {
const field = item.value.signedField || "data"; const field = item.value.signedField || "data";
const updated = item.value.updated; const updated = item.value.updated;
@ -98,18 +93,18 @@ export class RPCCache extends EventEmitter {
item.signature = this.signResponse(item); item.signature = this.signResponse(item);
this._dhtCache?.addItem(queryHash); this.dhtCache?.addItem(queryHash);
this._data.set(queryHash, item); this._data.set(queryHash, item);
} }
public deleteItem(queryHash: string): boolean { public deleteItem(queryHash: string): boolean {
const cache = this._dhtCache?.cache; const cache = this.dhtCache?.cache;
if (!cache?.includes(queryHash)) { if (!cache?.includes(queryHash)) {
throw Error("item does not exist"); throw Error("item does not exist");
} }
this._dhtCache?.removeItem(queryHash); this.dhtCache?.removeItem(queryHash);
this._data.del(queryHash); this._data.del(queryHash);
return true; return true;

View File

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

View File

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