dht-data/dist/DHTDataBase.js

276 lines
9.9 KiB
JavaScript
Raw Normal View History

2022-11-16 07:47:03 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = __importDefault(require("events"));
// @ts-ignore
const jsnetworkx_1 = require("jsnetworkx");
2022-11-20 00:54:04 +00:00
// @ts-ignore
const ordered_json_1 = __importDefault(require("ordered-json"));
// @ts-ignore
const hypercore_crypto_1 = __importDefault(require("hypercore-crypto"));
const b4a_1 = __importDefault(require("b4a"));
2022-11-16 07:47:03 +00:00
const messages_js_1 = require("./messages.js");
2022-11-20 00:54:04 +00:00
const debug_1 = __importDefault(require("debug"));
class DHTDataBase extends events_1.default {
swarm;
2022-11-16 07:47:03 +00:00
id;
bootstrapped;
graph;
connectedTo;
2022-11-20 00:54:04 +00:00
constructor(id, { swarm } = {}) {
2022-11-16 07:47:03 +00:00
super();
if (!id)
throw new TypeError("Must provide id for self");
this.id = id;
this.bootstrapped = false;
this.graph = new jsnetworkx_1.DiGraph();
this.connectedTo = new Set();
2022-11-16 16:15:00 +00:00
this._data = {};
2022-11-16 07:47:03 +00:00
this._online = [this._maybeHexify(this.id)];
2022-11-20 00:54:04 +00:00
this.swarm = swarm;
2022-11-16 07:47:03 +00:00
}
2022-11-16 16:15:00 +00:00
_data;
get data() {
2022-11-16 16:24:10 +00:00
return { ...this._data };
2022-11-16 16:15:00 +00:00
}
set data(value) {
this._data = value;
2022-11-20 00:54:04 +00:00
const timestamp = BigInt(Date.now());
const rawData = ordered_json_1.default.stringify(value);
const signature = hypercore_crypto_1.default.sign(b4a_1.default.from(`${timestamp}${rawData}`), this.swarm.keyPair.secretKey);
this._setPeer(this.id, value, timestamp, signature);
2022-11-16 16:15:00 +00:00
this._broadcastData();
}
2022-11-16 07:47:03 +00:00
_online;
get online() {
return this._online;
}
broadcast(data, ttl) {
throw new TypeError("Broadcast has not been implemented");
}
2022-11-20 00:54:04 +00:00
getPeerRaw(id) {
2022-11-16 07:47:03 +00:00
return this.graph.node.get(this._maybeHexify(id));
}
2022-11-20 00:54:04 +00:00
getPeerField(id, field) {
return this.getPeerRaw(id)?.[field];
}
getPeerData(id) {
return this.getPeerField(id, "data");
}
getPeerTimestamp(id) {
return this.getPeerField(id, "timestamp");
}
getPeerSignature(id) {
return this.getPeerField(id, "signature");
}
2022-11-16 07:47:03 +00:00
onAddPeer(id) {
const stringId = id.toString("hex");
if (this.connectedTo.has(stringId)) {
return;
} // Already know we're connected here
this.connectedTo.add(stringId);
this._addPeerConnection(this.id, id);
this.emit("peer-add", id);
this._recalculate();
this.broadcast(messages_js_1.Message.toBinary(messages_js_1.Message.create({
type: messages_js_1.Type.CONNECTED,
id,
})));
this._broadcastData();
if (this.bootstrapped) {
return;
}
// If this is the first person we've met, get their graph
this.broadcast(messages_js_1.Message.toBinary(messages_js_1.Message.create({
type: messages_js_1.Type.BOOTSTRAP_REQUEST,
})), 0);
}
onRemovePeer(id) {
this.connectedTo.delete(id.toString("hex"));
this._removePeerConnection(this.id, id);
this.emit("peer-remove");
this._recalculate();
this.broadcast(messages_js_1.Message.toBinary(messages_js_1.Message.create({
type: messages_js_1.Type.DISCONNECTED,
id,
})));
}
onGetBroadcast(message, id) {
let decoded;
try {
decoded = messages_js_1.Message.fromBinary(message);
}
catch {
return;
}
const { type } = decoded;
if (!type) {
throw new Error("Missing Type In Message");
}
if (type === messages_js_1.Type.STATE) {
2022-11-20 00:54:04 +00:00
const { data: rawData, timestamp, signature } = decoded;
if (signature &&
hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${rawData}`), signature, id)) {
2022-11-20 01:36:15 +00:00
if ((timestamp || 0) <= this.getPeerTimestamp(id)) {
(0, debug_1.default)(`Received old data for peer ${id}`);
return;
}
2022-11-20 00:54:04 +00:00
const data = rawData ? ordered_json_1.default.parse(rawData) : null;
this._setPeer(id, data, timestamp, signature);
this.emit("peer-data", data, id);
this._recalculate();
return;
}
(0, debug_1.default)(`Invalid signature received for peer ${id}`);
2022-11-16 07:47:03 +00:00
}
else if (type === messages_js_1.Type.CONNECTED) {
const { id: toId } = decoded;
this._addPeerConnection(id, Buffer.from(toId));
this.emit("peer-add-seen", id, toId);
this._recalculate();
}
else if (type === messages_js_1.Type.DISCONNECTED) {
const { id: toId } = decoded;
this._removePeerConnection(id, Buffer.from(toId));
this.emit("peer-remove-seen", id, toId);
this._recalculate();
}
else if (type === messages_js_1.Type.BOOTSTRAP_REQUEST) {
const bootstrap = this._getBootstrapInfo();
this.broadcast(messages_js_1.Message.toBinary(messages_js_1.Message.create({
type: messages_js_1.Type.BOOTSTRAP_RESPONSE,
bootstrap,
})), 0);
}
else if (type === messages_js_1.Type.BOOTSTRAP_RESPONSE) {
const { bootstrap } = decoded;
this._bootstrapFrom(bootstrap);
}
}
2022-11-16 16:15:00 +00:00
_broadcastData() {
const rawData = this._data;
if (!Object.keys(rawData).length) {
return;
}
2022-11-20 00:54:04 +00:00
const data = ordered_json_1.default.stringify(rawData);
const { timestamp, signature } = this.getPeerRaw(this.id);
2022-11-16 16:15:00 +00:00
this.broadcast(messages_js_1.Message.toBinary(messages_js_1.Message.create({
type: messages_js_1.Type.STATE,
2022-11-20 00:54:04 +00:00
data: b4a_1.default.from(data),
signature,
timestamp,
2022-11-16 16:15:00 +00:00
})));
}
2022-11-16 07:47:03 +00:00
_hasSeenPeer(id) {
return this.graph.hasNode(this._maybeHexify(id));
}
2022-11-20 00:54:04 +00:00
_setPeer(id, data, timestamp, signature) {
this.graph.addNode(this._maybeHexify(id), {
timestamp,
signature,
data,
});
2022-11-16 07:47:03 +00:00
}
_ensurePeer(id) {
id = this._maybeHexify(id);
if (!this._hasSeenPeer(id)) {
this._setPeer(id, {});
}
}
_addPeerConnection(origin, destination) {
this._ensurePeer(origin);
this._ensurePeer(destination);
this.graph.addEdge(this._maybeHexify(origin), this._maybeHexify(destination));
}
_removePeerConnection(origin, destination) {
try {
this._ensurePeer(origin);
this._ensurePeer(destination);
this.graph.removeEdge(origin.toString("hex"), destination.toString("hex"));
}
catch (e) {
if (e.name !== "JSNetworkXError")
throw e;
}
}
_bootstrapFrom(bootstrap) {
if (this.bootstrapped) {
return;
}
for (const id in bootstrap) {
2022-11-20 00:54:04 +00:00
const { data, connectedTo, signature, timestamp } = bootstrap[id];
if (id === this.id.toString("hex")) {
2022-11-16 07:47:03 +00:00
continue;
}
2022-11-20 00:54:04 +00:00
if (signature &&
2022-11-20 01:36:15 +00:00
hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${data}`), signature, b4a_1.default.from(id, "hex"))) {
2022-11-20 00:54:04 +00:00
const parsedData = data ? ordered_json_1.default.parse(data) : null;
let peerData = parsedData || {};
// If we're already tracking them
if (this._hasSeenPeer(id)) {
2022-11-20 01:36:15 +00:00
// Ensure we don't have old data
if ((timestamp || 0) > this.getPeerTimestamp(id)) {
// See what data we already have for them
// Add their existing data to what we got from the bootstrap
const existingPeerData = this.getPeerData(id);
peerData = { ...existingPeerData, ...peerData };
this._setPeer(id, peerData, timestamp, signature);
}
else {
(0, debug_1.default)(`Received old data for peer ${id}`);
}
2022-11-20 00:54:04 +00:00
}
}
else {
(0, debug_1.default)(`Invalid signature received for peer ${id}`);
}
2022-11-16 07:47:03 +00:00
for (const connection of connectedTo) {
this._addPeerConnection(id, Buffer.from(connection));
}
}
this.emit("bootstrapped");
this._recalculate();
}
_getBootstrapInfo() {
const state = {};
for (const [id, rawData] of this.graph.nodes(true)) {
const connectedTo = this.graph
.neighbors(id)
.map((id) => Buffer.from(id, "hex"));
2022-11-20 00:54:04 +00:00
const data = rawData ? ordered_json_1.default.stringify(rawData?.data) : null;
const { timestamp = undefined, signature = undefined } = rawData;
state[id] = { data: b4a_1.default.from(data), connectedTo, timestamp, signature };
2022-11-16 07:47:03 +00:00
}
return state;
}
// Calculate who's online and emit an event
_recalculate() {
const online = this.graph.nodes().filter((id) => {
return (0, jsnetworkx_1.hasPath)(this.graph, {
source: this._maybeHexify(this.id),
target: id,
});
});
const offline = this.graph.nodes().filter((id) => {
return !(0, jsnetworkx_1.hasPath)(this.graph, {
source: this._maybeHexify(this.id),
target: id,
});
});
for (const id of offline) {
this.graph.removeNode(id);
}
this._online = online;
this.emit("online", online);
}
_maybeHexify(data) {
2022-11-20 00:54:04 +00:00
if (b4a_1.default.isBuffer(data)) {
2022-11-16 07:47:03 +00:00
return data.toString("hex");
}
return data;
}
}
2022-11-20 00:54:04 +00:00
exports.default = DHTDataBase;