Compare commits
No commits in common. "fe99066220f4ca3aa208ded6a6ffb2aa182a468a" and "ddb360cb8af0a9a2631f9c0aa87647f1501a6733" have entirely different histories.
fe99066220
...
ddb360cb8a
|
@ -1,8 +1,6 @@
|
|||
/// <reference types="node" />
|
||||
/// <reference types="node" />
|
||||
import EventEmitter from "events";
|
||||
import DHTFlood from "@lumeweb/dht-flood";
|
||||
import type { Logger } from "pino";
|
||||
export default class DHTCache extends EventEmitter {
|
||||
protected swarm: any;
|
||||
private id;
|
||||
|
@ -12,9 +10,8 @@ export default class DHTCache extends EventEmitter {
|
|||
private heartBeatInterval;
|
||||
protected flood: DHTFlood;
|
||||
private log;
|
||||
constructor(swarm: any, { id, logger, heartBeatInterval, ...opts }?: {
|
||||
constructor(swarm: any, { id, heartBeatInterval, ...opts }?: {
|
||||
id?: Buffer;
|
||||
logger?: Logger;
|
||||
[key: string]: any;
|
||||
});
|
||||
private _cache;
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAYlC,OAAO,QAAQ,MAAM,oBAAoB,CAAC;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAanC,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,YAAY;IAChD,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;IACrB,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAU;IAC9B,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,iBAAiB,CAAS;IAElC,SAAS,CAAC,KAAK,EAAE,QAAQ,CAAC;IAC1B,OAAO,CAAC,GAAG,CAAS;gBAGlB,KAAK,EAAE,GAAG,EACV,EACE,EAA4B,EAC5B,MAAM,EACN,iBAAsB,EACtB,GAAG,IAAI,EACR,GAAE;QAAE,EAAE,C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MAAM,GAAG,OAAO;IA2BjD,OAAO,CAAC,OAAO,CAAc;IAE7B,IAAW,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,CAE/B;IAEM,SAAS,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM;IAIpC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG;IAInC,SAAS,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG;IAqClC,iBAAiB,CAAC,IAAI,EAAE,GAAG;IAa3B,SAAS,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG;IAqBhC,SAAS,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAoGpD,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,uBAAuB;IAa/B,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,cAAc;IAmCtB,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,YAAY;IAgDpB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,WAAW;IA0BnB,OAAO,CAAC,eAAe;IA0BvB,OAAO,CAAC,cAAc;IA4BtB,OAAO,CAAC,MAAM;IAKd,OAAO,CAAC,gBAAgB;IAOxB,OAAO,CAAC,4BAA4B;IAKpC,OAAO,CAAC,qBAAqB;CAS9B"}
|
|
@ -11,28 +11,17 @@ const hypercore_crypto_1 = __importDefault(require("hypercore-crypto"));
|
|||
const b4a_1 = __importDefault(require("b4a"));
|
||||
const messages_js_1 = require("./messages.js");
|
||||
const dht_flood_1 = __importDefault(require("@lumeweb/dht-flood"));
|
||||
const loglevel_1 = require("loglevel");
|
||||
const EntityType = {
|
||||
PUBKEY: Symbol.for("PUBKEY"),
|
||||
ITEM: Symbol.for("ITEM"),
|
||||
};
|
||||
const DISCONNECT_SMOOTH = 500;
|
||||
class DHTCache extends events_1.default {
|
||||
swarm;
|
||||
id;
|
||||
bootstrapped;
|
||||
graph;
|
||||
connectedTo;
|
||||
heartBeatInterval;
|
||||
flood;
|
||||
log;
|
||||
constructor(swarm, { id = swarm.keyPair.publicKey, logger, heartBeatInterval = 60, ...opts } = {}) {
|
||||
constructor(swarm, { id = swarm.keyPair.publicKey, heartBeatInterval = 60, ...opts } = {}) {
|
||||
super();
|
||||
if (!id) {
|
||||
if (!id)
|
||||
throw new TypeError("Must provide id for self");
|
||||
}
|
||||
if (!logger) {
|
||||
throw new TypeError("Must logger for self");
|
||||
}
|
||||
this.id = b4a_1.default.from(id);
|
||||
this.bootstrapped = false;
|
||||
this.graph = new jsnetworkx_1.DiGraph();
|
||||
|
@ -42,7 +31,7 @@ class DHTCache extends events_1.default {
|
|||
this._online = new Set([this._maybeHexify(this.id)]);
|
||||
this.swarm = swarm;
|
||||
this.flood = new dht_flood_1.default({ id, swarm, ...opts });
|
||||
this.log = logger;
|
||||
this.log = loglevel_1.getLogger("dht-cache");
|
||||
this.flood.on("peer-open", (peer) => this.addPeerHandler(peer));
|
||||
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
|
||||
this.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
|
||||
|
@ -59,7 +48,6 @@ class DHTCache extends events_1.default {
|
|||
setInterval(() => this._heartbeatCheck(), (this.heartBeatInterval / 4) * 1000);
|
||||
setInterval(() => this._emitHeartbeat(), this.heartBeatInterval * 1000);
|
||||
}
|
||||
_cache;
|
||||
get cache() {
|
||||
return [...this._cache].sort();
|
||||
}
|
||||
|
@ -122,7 +110,6 @@ class DHTCache extends events_1.default {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
_online;
|
||||
get online() {
|
||||
return this._online;
|
||||
}
|
||||
|
@ -353,7 +340,7 @@ class DHTCache extends events_1.default {
|
|||
if (item?.type !== EntityType.PUBKEY) {
|
||||
continue;
|
||||
}
|
||||
if ((0, jsnetworkx_1.hasPath)(this.graph, {
|
||||
if (jsnetworkx_1.hasPath(this.graph, {
|
||||
source: this._maybeHexify(this.id),
|
||||
target: id,
|
||||
})) {
|
||||
|
|
|
@ -59,7 +59,7 @@ class Message$Type extends runtime_5.MessageType {
|
|||
const message = { type: 0, bootstrap: {} };
|
||||
globalThis.Object.defineProperty(message, runtime_4.MESSAGE_TYPE, { enumerable: false, value: this });
|
||||
if (value !== undefined)
|
||||
(0, runtime_3.reflectionMergePartial)(this, message, value);
|
||||
runtime_3.reflectionMergePartial(this, message, value);
|
||||
return message;
|
||||
}
|
||||
internalBinaryRead(reader, length, options, target) {
|
||||
|
@ -150,7 +150,7 @@ class State$Type extends runtime_5.MessageType {
|
|||
const message = { connectedTo: [] };
|
||||
globalThis.Object.defineProperty(message, runtime_4.MESSAGE_TYPE, { enumerable: false, value: this });
|
||||
if (value !== undefined)
|
||||
(0, runtime_3.reflectionMergePartial)(this, message, value);
|
||||
runtime_3.reflectionMergePartial(this, message, value);
|
||||
return message;
|
||||
}
|
||||
internalBinaryRead(reader, length, options, target) {
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
"dependencies": {
|
||||
"@lumeweb/dht-flood": "https://git.lumeweb.com/LumeWeb/dht-flood.git",
|
||||
"@protobuf-ts/plugin": "^2.8.1",
|
||||
"@protobuf-ts/runtime": "^2.8.2",
|
||||
"b4a": "^1.6.1",
|
||||
"compact-encoding": "^2.11.0",
|
||||
"hypercore-crypto": "^3.3.0",
|
||||
|
@ -14,14 +13,12 @@
|
|||
"loglevel": "^1.8.1",
|
||||
"lru": "^3.1.0",
|
||||
"ordered-json": "^0.1.1",
|
||||
"pino": "^8.8.0",
|
||||
"protocol-buffers-encodings": "^1.2.0",
|
||||
"protomux-rpc": "^1.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/b4a": "^1.6.0",
|
||||
"@types/debug": "^4.1.7",
|
||||
"@types/node": "^18.11.17",
|
||||
"crypto": "^1.0.1",
|
||||
"debug": "^4.3.4",
|
||||
"hyperswarm": "^4.3.5",
|
||||
|
@ -29,7 +26,6 @@
|
|||
"protoc": "^1.1.3",
|
||||
"sodium-universal": "^3.1.0",
|
||||
"tape": "^5.6.1",
|
||||
"ts-proto": "^1.131.2",
|
||||
"typescript": "^4.9.4"
|
||||
"ts-proto": "^1.131.2"
|
||||
}
|
||||
}
|
||||
|
|
16
src/index.ts
16
src/index.ts
|
@ -11,7 +11,7 @@ import { Message, State, Type } from "./messages.js";
|
|||
import sodium from "sodium-universal";
|
||||
import type { PartialMessage } from "@protobuf-ts/runtime";
|
||||
import DHTFlood from "@lumeweb/dht-flood";
|
||||
import type { Logger } from "pino";
|
||||
import log, { getLogger } from "loglevel";
|
||||
|
||||
type Bootstrap = {
|
||||
[key: string]: State;
|
||||
|
@ -33,24 +33,18 @@ export default class DHTCache extends EventEmitter {
|
|||
private heartBeatInterval: number;
|
||||
|
||||
protected flood: DHTFlood;
|
||||
private log: Logger;
|
||||
private log: log.Logger;
|
||||
|
||||
constructor(
|
||||
swarm: any,
|
||||
{
|
||||
id = swarm.keyPair.publicKey,
|
||||
logger,
|
||||
heartBeatInterval = 60,
|
||||
...opts
|
||||
}: { id?: Buffer; logger?: Logger; [key: string]: any } = {}
|
||||
}: { id?: Buffer; [key: string]: any } = {}
|
||||
) {
|
||||
super();
|
||||
if (!id) {
|
||||
throw new TypeError("Must provide id for self");
|
||||
}
|
||||
if (!logger) {
|
||||
throw new TypeError("Must logger for self");
|
||||
}
|
||||
if (!id) throw new TypeError("Must provide id for self");
|
||||
|
||||
this.id = b4a.from(id) as Buffer;
|
||||
this.bootstrapped = false;
|
||||
|
@ -61,7 +55,7 @@ export default class DHTCache extends EventEmitter {
|
|||
this._online = new Set([this._maybeHexify(this.id)]);
|
||||
this.swarm = swarm;
|
||||
this.flood = new DHTFlood({ id, swarm, ...opts });
|
||||
this.log = logger;
|
||||
this.log = getLogger("dht-cache");
|
||||
|
||||
this.flood.on("peer-open", (peer) => this.addPeerHandler(peer));
|
||||
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
|
||||
|
|
Loading…
Reference in New Issue