Compare commits

...

2 Commits

Author SHA1 Message Date
Derrick Hammer fe99066220
*Update dist 2022-12-19 08:44:54 -05:00
Derrick Hammer 67c098cb0d
*Switch to pino logger 2022-12-19 08:43:17 -05:00
6 changed files with 41 additions and 15 deletions

5
dist/index.d.ts vendored
View File

@ -1,6 +1,8 @@
/// <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;
@ -10,8 +12,9 @@ export default class DHTCache extends EventEmitter {
private heartBeatInterval;
protected flood: DHTFlood;
private log;
constructor(swarm: any, { id, heartBeatInterval, ...opts }?: {
constructor(swarm: any, { id, logger, heartBeatInterval, ...opts }?: {
id?: Buffer;
logger?: Logger;
[key: string]: any;
});
private _cache;

2
dist/index.d.ts.map vendored
View File

@ -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,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,CAAa;gBAGtB,KAAK,EAAE,GAAG,EACV,EACE,EAA4B,EAC5B,iBAAsB,EACtB,GAAG,IAAI,EACR,GAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAO;IAuC7C,OAAO,CAAC,MAAM,CAAc;IAE5B,IAAW,K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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"}

23
dist/index.js vendored
View File

@ -11,17 +11,28 @@ 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 {
constructor(swarm, { id = swarm.keyPair.publicKey, heartBeatInterval = 60, ...opts } = {}) {
swarm;
id;
bootstrapped;
graph;
connectedTo;
heartBeatInterval;
flood;
log;
constructor(swarm, { id = swarm.keyPair.publicKey, logger, 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();
@ -31,7 +42,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 = loglevel_1.getLogger("dht-cache");
this.log = logger;
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));
@ -48,6 +59,7 @@ 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();
}
@ -110,6 +122,7 @@ class DHTCache extends events_1.default {
}
return true;
}
_online;
get online() {
return this._online;
}
@ -340,7 +353,7 @@ class DHTCache extends events_1.default {
if (item?.type !== EntityType.PUBKEY) {
continue;
}
if (jsnetworkx_1.hasPath(this.graph, {
if ((0, jsnetworkx_1.hasPath)(this.graph, {
source: this._maybeHexify(this.id),
target: id,
})) {

4
dist/messages.js vendored
View File

@ -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)
runtime_3.reflectionMergePartial(this, message, value);
(0, 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)
runtime_3.reflectionMergePartial(this, message, value);
(0, runtime_3.reflectionMergePartial)(this, message, value);
return message;
}
internalBinaryRead(reader, length, options, target) {

View File

@ -6,6 +6,7 @@
"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",
@ -13,12 +14,14 @@
"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",
@ -26,6 +29,7 @@
"protoc": "^1.1.3",
"sodium-universal": "^3.1.0",
"tape": "^5.6.1",
"ts-proto": "^1.131.2"
"ts-proto": "^1.131.2",
"typescript": "^4.9.4"
}
}

View File

@ -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 log, { getLogger } from "loglevel";
import type { Logger } from "pino";
type Bootstrap = {
[key: string]: State;
@ -33,18 +33,24 @@ export default class DHTCache extends EventEmitter {
private heartBeatInterval: number;
protected flood: DHTFlood;
private log: log.Logger;
private log: Logger;
constructor(
swarm: any,
{
id = swarm.keyPair.publicKey,
logger,
heartBeatInterval = 60,
...opts
}: { id?: Buffer; [key: string]: any } = {}
}: { id?: Buffer; logger?: Logger; [key: string]: any } = {}
) {
super();
if (!id) throw new TypeError("Must provide id for self");
if (!id) {
throw new TypeError("Must provide id for self");
}
if (!logger) {
throw new TypeError("Must logger for self");
}
this.id = b4a.from(id) as Buffer;
this.bootstrapped = false;
@ -55,7 +61,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 = getLogger("dht-cache");
this.log = logger;
this.flood.on("peer-open", (peer) => this.addPeerHandler(peer));
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));