Compare commits
No commits in common. "d65d33aa184239e05f6d65fee6b16f3ae0921f39" and "90aeec6802219b61eb055561bf3e8356e68b30c0" have entirely different histories.
d65d33aa18
...
90aeec6802
|
@ -10,7 +10,6 @@ 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;
|
||||||
|
|
|
@ -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;IAsC7C,OAAO,CAAC,MAAM,CAAc;IAE5B,IAAW,KAAK,IAAI,MAAM,EAAE,CAE3B;IAED,IAAW,QAAQ,IAAI,MAAM,EAAE,CAU9B;IAEM,WAAW,C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CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAkEpD,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;IA0CpB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,WAAW;IA0BnB,OAAO,CAAC,eAAe;IA0BvB,OAAO,CAAC,cAAc;CA2BvB"}
|
|
@ -11,7 +11,6 @@ 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"),
|
||||||
|
@ -25,7 +24,6 @@ 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)
|
||||||
|
@ -39,7 +37,6 @@ 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));
|
||||||
|
@ -149,11 +146,9 @@ 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);
|
||||||
}
|
}
|
||||||
|
@ -166,7 +161,6 @@ 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;
|
||||||
|
@ -178,7 +172,6 @@ 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;
|
||||||
|
@ -200,32 +193,26 @@ 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;
|
||||||
let bufId = b4a_1.default.from(toId);
|
this._addEntityConnection(id, b4a_1.default.from(toId));
|
||||||
this._addEntityConnection(id, bufId);
|
this.emit("peer-add-seen", id, toId);
|
||||||
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();
|
||||||
|
@ -233,20 +220,18 @@ 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) {
|
||||||
const { id: toId, signature, data: bufData } = decoded;
|
let { id: toId, signature, data: bufData } = decoded;
|
||||||
let bufId = b4a_1.default.from(toId);
|
toId = 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, bufId);
|
this._addEntityConnection(id, toId);
|
||||||
this._setEntity(bufId, { heartbeat: Date.now() });
|
this._setEntity(toId, { heartbeat: Date.now() });
|
||||||
}
|
}
|
||||||
this.log.debug(`Bootstrap response received from ${bufId.toString("hex")}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_signItem(item) {
|
_signItem(item) {
|
||||||
|
@ -357,7 +342,6 @@ 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)) {
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
"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",
|
||||||
|
|
49
src/index.ts
49
src/index.ts
|
@ -11,7 +11,6 @@ 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;
|
||||||
|
@ -33,7 +32,6 @@ 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,
|
||||||
|
@ -55,7 +53,6 @@ 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));
|
||||||
|
@ -196,14 +193,10 @@ 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);
|
||||||
}
|
}
|
||||||
|
@ -217,8 +210,6 @@ 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) {
|
||||||
|
@ -238,8 +229,6 @@ 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) {
|
||||||
|
@ -263,42 +252,25 @@ 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;
|
||||||
let bufId = b4a.from(toId as Uint8Array) as Buffer;
|
this._addEntityConnection(id, 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(
|
||||||
|
@ -310,23 +282,18 @@ 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) {
|
||||||
const { id: toId, signature, data: bufData } = decoded;
|
let { id: toId, signature, data: bufData } = decoded;
|
||||||
|
|
||||||
let bufId = b4a.from(toId as Uint8Array) as Buffer;
|
toId = b4a.from(toId as Uint8Array);
|
||||||
|
|
||||||
if (signature && crypto.verify(bufData, signature, id)) {
|
if (signature && crypto.verify(bufData, signature, id)) {
|
||||||
this._addEntityConnection(id, bufId as Buffer);
|
this._addEntityConnection(id, toId as Buffer);
|
||||||
this._setEntity(bufId as Buffer, { heartbeat: Date.now() });
|
this._setEntity(toId as Buffer, { heartbeat: Date.now() });
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log.debug(
|
|
||||||
`Bootstrap response received from ${bufId.toString("hex")}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -472,10 +439,6 @@ 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 {
|
||||||
|
|
Loading…
Reference in New Issue