Compare commits

..

No commits in common. "86ce21a4b4bd7eb434b5c2e0a0eea04daf1754dc" and "00fc999169f774331bc1becc5bdd51162a50d7f0" have entirely different histories.

7 changed files with 28 additions and 41 deletions

View File

@ -6,7 +6,6 @@ import config from "./config.js";
import { loadPlugins } from "./modules/plugin.js";
import { start as startDns } from "./modules/dns.js";
import { start as startSSl } from "./modules/ssl.js";
import { start as startSwarm } from "./modules/swarm.js";
import { generateSeedPhraseDeterministic } from "libskynet";
import * as crypto from "crypto";
@ -21,7 +20,6 @@ if (!config.str("seed")) {
}
async function boot() {
await startSwarm();
await loadPlugins();
await startApp();
await startRpc();

View File

@ -36,7 +36,7 @@ async function ipUpdate() {
}
export async function start() {
const swarm = getSwarm();
const swarm = (await getSwarm()) as any;
await ipUpdate();

View File

@ -20,7 +20,7 @@ import { getSslContext } from "./ssl.js";
export async function start() {
const relayPort = config.uint("port");
const dht = getSwarm();
const dht = await getSwarm();
const statusCodeServer = http.createServer(function (req, res) {
// @ts-ignore

View File

@ -13,13 +13,13 @@ export async function start() {
errorExit("Please set pocket-app-id and pocket-app-key config options.");
}
getSwarm().on("connection", (stream: SecretStream) =>
(await getSwarm()).on("connection", (stream: SecretStream) =>
getRpcServer().setup(stream)
);
}
export async function getRpcByPeer(peer: string) {
const swarm = getSwarm();
const swarm = await getSwarm();
if (swarm._allConnections.has(peer)) {
return swarm._allConnections.get(peer)[RPC_PROTOCOL_SYMBOL];

View File

@ -34,10 +34,7 @@ export class RPCCache extends EventEmitter {
constructor(server: RPCServer) {
super();
this.server = server;
this._swarm = getSwarm();
this.dhtCache = new DHTCache(this._swarm, {
protocol: "lumeweb.rpccache",
});
this.init();
}
public async getNodeQuery(
@ -113,7 +110,6 @@ export class RPCCache extends EventEmitter {
item.signature = this.signResponse(item);
this.dhtCache?.addItem(queryHash);
this._data[queryHash] = item;
}
@ -129,4 +125,11 @@ export class RPCCache extends EventEmitter {
return true;
}
private async init() {
this.dhtCache = new DHTCache(await getSwarm(), {
protocol: "lumeweb.rpccache",
});
this._swarm = await getSwarm();
}
}

View File

@ -133,7 +133,7 @@ export class RPCServer extends EventEmitter {
}
return crypto
.sign(Buffer.from(raw), this._cache.swarm.keyPair.secretKey)
.sign(Buffer.from(raw, this._cache.swarm.keyPair.privateKey))
.toString("hex");
}
@ -147,7 +147,6 @@ export class RPCServer extends EventEmitter {
let cachedRequest = this.getCachedRequest(request) as RPCCacheItem;
if (cachedRequest) {
this.getRequestLock(request)?.release();
return cachedRequest.value;
}
@ -186,15 +185,13 @@ export class RPCServer extends EventEmitter {
this.cache.addItem(request, rpcResult);
}
this.getRequestLock(request)?.release();
return rpcResult;
}
private getCachedRequest(request: RPCRequest): RPCCacheItem | boolean {
const req = RPCServer.hashQuery(request);
if (RPCServer.hashQuery(request) in this._cache.data) {
return this._cache.data[req] as RPCCacheItem;
this._cache.data[req];
}
return false;
@ -228,12 +225,15 @@ export class RPCServer extends EventEmitter {
return;
}
if (!this.getRequestLock(request)) {
this.createRequestLock(request);
}
const reqId = RPCServer.hashQuery(request);
const lock: Mutex = this.getRequestLock(request) as Mutex;
let lock: Mutex = this.pendingRequests.get(reqId) as Mutex;
const lockExists = !!lock;
if (!lockExists) {
lock = new Mutex();
this.pendingRequests.set(reqId, lock);
}
if (lock.isLocked()) {
await lock.waitForUnlock();
@ -244,22 +244,4 @@ export class RPCServer extends EventEmitter {
await lock.acquire();
}
private getRequestLock(request: RPCRequest): Mutex | null {
const reqId = RPCServer.hashQuery(request);
let lock: Mutex = this.pendingRequests.get(reqId) as Mutex;
if (!lock) {
return null;
}
return lock;
}
private createRequestLock(request: RPCRequest) {
const reqId = RPCServer.hashQuery(request);
this.pendingRequests.set(reqId, new Mutex());
}
}

View File

@ -34,7 +34,7 @@ export function getKeyPair() {
return deriveMyskyRootKeypair(seedPhraseToSeed(seed)[0]);
}
export async function start() {
async function start() {
const keyPair = getKeyPair();
node = new Hyperswarm({ keyPair, dht: new DHT({ keyPair }) });
@ -49,6 +49,10 @@ export async function start() {
return node;
}
export function get(): Hyperswarm {
export async function get(): Promise<Hyperswarm> {
if (!node) {
await start();
}
return node;
}