Compare commits

...

2 Commits

Author SHA1 Message Date
Derrick Hammer d65d33aa18
*Update dist 2022-12-07 01:06:49 -05:00
Derrick Hammer 37ad6d304a
*Add loglevel with custom module logger 2022-12-07 01:06:29 -05:00
5 changed files with 68 additions and 13 deletions

1
dist/index.d.ts vendored
View File

@ -10,6 +10,7 @@ export default class DHTCache extends EventEmitter {
private connectedTo; private connectedTo;
private heartBeatInterval; private heartBeatInterval;
protected flood: DHTFlood; protected flood: DHTFlood;
private log;
constructor(swarm: any, { id, heartBeatInterval, ...opts }?: { constructor(swarm: any, { id, heartBeatInterval, ...opts }?: {
id?: Buffer; id?: Buffer;
[key: string]: any; [key: string]: any;

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;AAa1C,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;gBAGxB,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;IAqC7C,OAAO,CAAC,MAAM,CAAc;IAE5B,IAAW,KAAK,IAAI,MAAM,EAAE,CAE3B;IAED,IAAW,QAAQ,IAAI,MAAM,EAAE,CAU9B;IAEM,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EA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c,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAwFpD,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;IAwBtB,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,YAAY;IA8CpB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,WAAW;IA0BnB,OAAO,CAAC,eAAe;IA0BvB,OAAO,CAAC,cAAc;CA2BvB"}

28
dist/index.js vendored
View File

@ -11,6 +11,7 @@ const hypercore_crypto_1 = __importDefault(require("hypercore-crypto"));
const b4a_1 = __importDefault(require("b4a")); const b4a_1 = __importDefault(require("b4a"));
const messages_js_1 = require("./messages.js"); const messages_js_1 = require("./messages.js");
const dht_flood_1 = __importDefault(require("@lumeweb/dht-flood")); const dht_flood_1 = __importDefault(require("@lumeweb/dht-flood"));
const loglevel_1 = require("loglevel");
const EntityType = { const EntityType = {
PUBKEY: Symbol.for("PUBKEY"), PUBKEY: Symbol.for("PUBKEY"),
ITEM: Symbol.for("ITEM"), ITEM: Symbol.for("ITEM"),
@ -24,6 +25,7 @@ class DHTCache extends events_1.default {
connectedTo; connectedTo;
heartBeatInterval; heartBeatInterval;
flood; flood;
log;
constructor(swarm, { id = swarm.keyPair.publicKey, heartBeatInterval = 15, ...opts } = {}) { constructor(swarm, { id = swarm.keyPair.publicKey, heartBeatInterval = 15, ...opts } = {}) {
super(); super();
if (!id) if (!id)
@ -37,6 +39,7 @@ class DHTCache extends events_1.default {
this._online = new Set([this._maybeHexify(this.id)]); this._online = new Set([this._maybeHexify(this.id)]);
this.swarm = swarm; this.swarm = swarm;
this.flood = new dht_flood_1.default({ id, swarm, ...opts }); this.flood = new dht_flood_1.default({ id, swarm, ...opts });
this.log = (0, loglevel_1.getLogger)("dht-cache");
this.flood.on("peer-open", (peer) => this.addPeerHandler(peer)); this.flood.on("peer-open", (peer) => this.addPeerHandler(peer));
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer)); this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
this.flood.on("message", (message, id) => this.onGetBroadcast(message, id)); this.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
@ -146,9 +149,11 @@ class DHTCache extends events_1.default {
id, id,
}); });
this._emitHeartbeat(peer); this._emitHeartbeat(peer);
this.log.debug(`Relay peer connected: ${stringId}`);
if (this.bootstrapped) { if (this.bootstrapped) {
return; return;
} }
this.log.debug(`Broadcasting bootstrap request`);
// If this is the first person we've met, get their graph // If this is the first person we've met, get their graph
this._broadcastMessage({ type: messages_js_1.Type.BOOTSTRAP_REQUEST }, 0); this._broadcastMessage({ type: messages_js_1.Type.BOOTSTRAP_REQUEST }, 0);
} }
@ -161,6 +166,7 @@ class DHTCache extends events_1.default {
} }
this.onRemovePeer(peer); this.onRemovePeer(peer);
}, DISCONNECT_SMOOTH); }, DISCONNECT_SMOOTH);
this.log.debug(`Relay peer might have disconnected: ${id.toString("hex")}`);
} }
onRemovePeer(peer) { onRemovePeer(peer) {
const id = peer.remotePublicKey; const id = peer.remotePublicKey;
@ -172,6 +178,7 @@ class DHTCache extends events_1.default {
type: messages_js_1.Type.DISCONNECTED, type: messages_js_1.Type.DISCONNECTED,
id, id,
}, 0); }, 0);
this.log.debug(`Relay peer confirmed disconnected: ${id.toString("hex")}`);
} }
onGetBroadcast(message, id) { onGetBroadcast(message, id) {
let decoded; let decoded;
@ -193,26 +200,32 @@ class DHTCache extends events_1.default {
this._ensureItem(bufData); this._ensureItem(bufData);
this._addEntityConnection(id, bufData); this._addEntityConnection(id, bufData);
this.emit("item-added", id, bufData); this.emit("item-added", id, bufData);
this.log.debug(`New item added: ${bufData.toString("hex")} from ${id.toString("hex")}`);
} }
if (messages_js_1.Type.REMOVE_ITEM === type) { if (messages_js_1.Type.REMOVE_ITEM === type) {
this.removeItem(bufData); this.removeItem(bufData);
this._pruneItems(); this._pruneItems();
this.emit("item-removed", id, bufData); this.emit("item-removed", id, bufData);
this.log.debug(`Item removed: ${bufData.toString("hex")} from ${id.toString("hex")}`);
} }
} }
} }
else if (type === messages_js_1.Type.CONNECTED) { else if (type === messages_js_1.Type.CONNECTED) {
const { id: toId } = decoded; const { id: toId } = decoded;
this._addEntityConnection(id, b4a_1.default.from(toId)); let bufId = b4a_1.default.from(toId);
this.emit("peer-add-seen", id, toId); this._addEntityConnection(id, bufId);
this.emit("peer-add-seen", id, bufId);
this._recalculate(); this._recalculate();
this.log.debug(`Network peer connected: ${bufId.toString("hex")}`);
} }
else if (type === messages_js_1.Type.DISCONNECTED) { else if (type === messages_js_1.Type.DISCONNECTED) {
const { id: toId } = decoded; const { id: toId } = decoded;
let bufId = b4a_1.default.from(toId);
this._removeEntityConnection(id, b4a_1.default.from(toId)); this._removeEntityConnection(id, b4a_1.default.from(toId));
this.emit("peer-remove-seen", id, toId); this.emit("peer-remove-seen", id, toId);
this._recalculate(); this._recalculate();
this._pruneItems(); this._pruneItems();
this.log.debug(`Network peer disconnected: ${bufId.toString("hex")}`);
} }
else if (type === messages_js_1.Type.BOOTSTRAP_REQUEST) { else if (type === messages_js_1.Type.BOOTSTRAP_REQUEST) {
const bootstrap = this._getBootstrapInfo(); const bootstrap = this._getBootstrapInfo();
@ -220,18 +233,20 @@ class DHTCache extends events_1.default {
type: messages_js_1.Type.BOOTSTRAP_RESPONSE, type: messages_js_1.Type.BOOTSTRAP_RESPONSE,
bootstrap, bootstrap,
})), 0); })), 0);
this.log.debug(`Bootstrap request received`);
} }
else if (type === messages_js_1.Type.BOOTSTRAP_RESPONSE) { else if (type === messages_js_1.Type.BOOTSTRAP_RESPONSE) {
const { bootstrap } = decoded; const { bootstrap } = decoded;
this._bootstrapFrom(bootstrap); this._bootstrapFrom(bootstrap);
} }
else if (type === messages_js_1.Type.HEARTBEAT) { else if (type === messages_js_1.Type.HEARTBEAT) {
let { id: toId, signature, data: bufData } = decoded; const { id: toId, signature, data: bufData } = decoded;
toId = b4a_1.default.from(toId); let bufId = b4a_1.default.from(toId);
if (signature && hypercore_crypto_1.default.verify(bufData, signature, id)) { if (signature && hypercore_crypto_1.default.verify(bufData, signature, id)) {
this._addEntityConnection(id, toId); this._addEntityConnection(id, bufId);
this._setEntity(toId, { heartbeat: Date.now() }); this._setEntity(bufId, { heartbeat: Date.now() });
} }
this.log.debug(`Bootstrap response received from ${bufId.toString("hex")}`);
} }
} }
_signItem(item) { _signItem(item) {
@ -342,6 +357,7 @@ class DHTCache extends events_1.default {
} }
this._online = online; this._online = online;
this.emit("online", online); this.emit("online", online);
this.log.debug(`Online list updated: ${online.size} network peers online, ${offline.size} network peers offline and removed from DAG`);
} }
_maybeHexify(data) { _maybeHexify(data) {
if (b4a_1.default.isBuffer(data)) { if (b4a_1.default.isBuffer(data)) {

View File

@ -10,6 +10,7 @@
"compact-encoding": "^2.11.0", "compact-encoding": "^2.11.0",
"hypercore-crypto": "^3.3.0", "hypercore-crypto": "^3.3.0",
"jsnetworkx": "^0.3.4", "jsnetworkx": "^0.3.4",
"loglevel": "^1.8.1",
"lru": "^3.1.0", "lru": "^3.1.0",
"ordered-json": "^0.1.1", "ordered-json": "^0.1.1",
"protocol-buffers-encodings": "^1.2.0", "protocol-buffers-encodings": "^1.2.0",

View File

@ -11,6 +11,7 @@ import { Message, State, Type } from "./messages.js";
import sodium from "sodium-universal"; import sodium from "sodium-universal";
import type { PartialMessage } from "@protobuf-ts/runtime"; import type { PartialMessage } from "@protobuf-ts/runtime";
import DHTFlood from "@lumeweb/dht-flood"; import DHTFlood from "@lumeweb/dht-flood";
import log, { getLogger } from "loglevel";
type Bootstrap = { type Bootstrap = {
[key: string]: State; [key: string]: State;
@ -32,6 +33,7 @@ export default class DHTCache extends EventEmitter {
private heartBeatInterval: number; private heartBeatInterval: number;
protected flood: DHTFlood; protected flood: DHTFlood;
private log: log.Logger;
constructor( constructor(
swarm: any, swarm: any,
@ -53,6 +55,7 @@ export default class DHTCache extends EventEmitter {
this._online = new Set([this._maybeHexify(this.id)]); this._online = new Set([this._maybeHexify(this.id)]);
this.swarm = swarm; this.swarm = swarm;
this.flood = new DHTFlood({ id, swarm, ...opts }); this.flood = new DHTFlood({ id, swarm, ...opts });
this.log = getLogger("dht-cache");
this.flood.on("peer-open", (peer) => this.addPeerHandler(peer)); this.flood.on("peer-open", (peer) => this.addPeerHandler(peer));
this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer)); this.flood.on("peer-remove", (peer) => this.removePeerHandler(peer));
@ -193,10 +196,14 @@ export default class DHTCache extends EventEmitter {
this._emitHeartbeat(peer); this._emitHeartbeat(peer);
this.log.debug(`Relay peer connected: ${stringId}`);
if (this.bootstrapped) { if (this.bootstrapped) {
return; return;
} }
this.log.debug(`Broadcasting bootstrap request`);
// If this is the first person we've met, get their graph // If this is the first person we've met, get their graph
this._broadcastMessage({ type: Type.BOOTSTRAP_REQUEST }, 0); this._broadcastMessage({ type: Type.BOOTSTRAP_REQUEST }, 0);
} }
@ -210,6 +217,8 @@ export default class DHTCache extends EventEmitter {
} }
this.onRemovePeer(peer); this.onRemovePeer(peer);
}, DISCONNECT_SMOOTH); }, DISCONNECT_SMOOTH);
this.log.debug(`Relay peer might have disconnected: ${id.toString("hex")}`);
} }
protected onRemovePeer(peer: any) { protected onRemovePeer(peer: any) {
@ -229,6 +238,8 @@ export default class DHTCache extends EventEmitter {
}, },
0 0
); );
this.log.debug(`Relay peer confirmed disconnected: ${id.toString("hex")}`);
} }
protected onGetBroadcast(message: Buffer, id: Buffer) { protected onGetBroadcast(message: Buffer, id: Buffer) {
@ -252,25 +263,42 @@ export default class DHTCache extends EventEmitter {
this._ensureItem(bufData); this._ensureItem(bufData);
this._addEntityConnection(id, bufData); this._addEntityConnection(id, bufData);
this.emit("item-added", id, bufData); this.emit("item-added", id, bufData);
this.log.debug(
`New item added: ${bufData.toString("hex")} from ${id.toString(
"hex"
)}`
);
} }
if (Type.REMOVE_ITEM === type) { if (Type.REMOVE_ITEM === type) {
this.removeItem(bufData); this.removeItem(bufData);
this._pruneItems(); this._pruneItems();
this.emit("item-removed", id, bufData); this.emit("item-removed", id, bufData);
this.log.debug(
`Item removed: ${bufData.toString("hex")} from ${id.toString(
"hex"
)}`
);
} }
} }
} else if (type === Type.CONNECTED) { } else if (type === Type.CONNECTED) {
const { id: toId } = decoded; const { id: toId } = decoded;
this._addEntityConnection(id, b4a.from(toId as Uint8Array) as Buffer); let bufId = b4a.from(toId as Uint8Array) as Buffer;
this.emit("peer-add-seen", id, toId);
this._addEntityConnection(id, bufId);
this.emit("peer-add-seen", id, bufId);
this._recalculate(); this._recalculate();
this.log.debug(`Network peer connected: ${bufId.toString("hex")}`);
} else if (type === Type.DISCONNECTED) { } else if (type === Type.DISCONNECTED) {
const { id: toId } = decoded; const { id: toId } = decoded;
let bufId = b4a.from(toId as Uint8Array) as Buffer;
this._removeEntityConnection(id, b4a.from(toId as Uint8Array) as Buffer); this._removeEntityConnection(id, b4a.from(toId as Uint8Array) as Buffer);
this.emit("peer-remove-seen", id, toId); this.emit("peer-remove-seen", id, toId);
this._recalculate(); this._recalculate();
this._pruneItems(); this._pruneItems();
this.log.debug(`Network peer disconnected: ${bufId.toString("hex")}`);
} else if (type === Type.BOOTSTRAP_REQUEST) { } else if (type === Type.BOOTSTRAP_REQUEST) {
const bootstrap = this._getBootstrapInfo(); const bootstrap = this._getBootstrapInfo();
this.broadcast( this.broadcast(
@ -282,18 +310,23 @@ export default class DHTCache extends EventEmitter {
), ),
0 0
); );
this.log.debug(`Bootstrap request received`);
} else if (type === Type.BOOTSTRAP_RESPONSE) { } else if (type === Type.BOOTSTRAP_RESPONSE) {
const { bootstrap } = decoded; const { bootstrap } = decoded;
this._bootstrapFrom(bootstrap); this._bootstrapFrom(bootstrap);
} else if (type === Type.HEARTBEAT) { } else if (type === Type.HEARTBEAT) {
let { id: toId, signature, data: bufData } = decoded; const { id: toId, signature, data: bufData } = decoded;
toId = b4a.from(toId as Uint8Array); let bufId = b4a.from(toId as Uint8Array) as Buffer;
if (signature && crypto.verify(bufData, signature, id)) { if (signature && crypto.verify(bufData, signature, id)) {
this._addEntityConnection(id, toId as Buffer); this._addEntityConnection(id, bufId as Buffer);
this._setEntity(toId as Buffer, { heartbeat: Date.now() }); this._setEntity(bufId as Buffer, { heartbeat: Date.now() });
} }
this.log.debug(
`Bootstrap response received from ${bufId.toString("hex")}`
);
} }
} }
@ -439,6 +472,10 @@ export default class DHTCache extends EventEmitter {
this._online = online; this._online = online;
this.emit("online", online); this.emit("online", online);
this.log.debug(
`Online list updated: ${online.size} network peers online, ${offline.size} network peers offline and removed from DAG`
);
} }
private _maybeHexify(data: Buffer | string): string { private _maybeHexify(data: Buffer | string): string {