Compare commits

..

No commits in common. "52a0e41ca208f0d7912beb25457f5387d43e548c" and "c67bc2bba7c439bb4157669bbc09ca432d78f9bc" have entirely different histories.

9 changed files with 11 additions and 130 deletions

4
dist/index.d.ts vendored
View File

@ -24,7 +24,7 @@ export default class DHTCache extends EventEmitter {
private _online;
get online(): Set<string>;
broadcast(message: any, ttl?: number): void;
send(peer: any, message: any): void;
send(message: any): void;
protected addPeerHandler(peer: any): void;
removePeerHandler(peer: any): void;
protected onRemovePeer(peer: any): void;
@ -43,7 +43,5 @@ export default class DHTCache extends EventEmitter {
private _recalculate;
private _maybeHexify;
private _pruneItems;
private _heartbeatCheck;
private _emitHeartbeat;
}
//# sourceMappingURL=index.d.ts.map

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;IAE9B,SAAS,CAAC,KAAK,EAAE,QAAQ,CAAC;gBAGxB,KAAK,EAAE,GAAG,EACV,EACE,EAA4B,EAC5B,GAAG,IAAI,EACR,GAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAO;IAoC7C,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,EAAE,IAAI,EAAE,MAAM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C,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;IA+BpB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,WAAW;CAyBpB"}

52
dist/index.js vendored
View File

@ -38,7 +38,7 @@ class DHTCache extends events_1.default {
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));
this.swarm.on("connection", (peer) => this.send(peer, b4a_1.default.from("hello")));
this.swarm.on("connection", (peer) => this.flood.send(peer, b4a_1.default.from("hello"), 0));
[...this.swarm.peers.values()]
.map((item) => {
remotePublicKey: item.publicKey;
@ -48,8 +48,6 @@ class DHTCache extends events_1.default {
this.addPeerHandler(item);
});
this._ensurePeer(this.id);
setInterval(() => this._heartbeatCheck(), 5 * 1000);
setInterval(() => this._emitHeartbeat(), 60 * 1000);
}
_cache;
get cache() {
@ -125,8 +123,8 @@ class DHTCache extends events_1.default {
broadcast(message, ttl) {
this.flood.broadcast(message, ttl);
}
send(peer, message) {
this.flood.send(peer, message, 0);
send(message) {
this.flood.send(message, 0);
}
addPeerHandler(peer) {
const id = peer.remotePublicKey;
@ -143,7 +141,6 @@ class DHTCache extends events_1.default {
type: messages_js_1.Type.CONNECTED,
id,
});
this._emitHeartbeat(peer);
if (this.bootstrapped) {
return;
}
@ -223,14 +220,6 @@ class DHTCache extends events_1.default {
const { bootstrap } = decoded;
this._bootstrapFrom(bootstrap);
}
else if (type === messages_js_1.Type.HEARTBEAT) {
let { id: toId, signature, data: bufData } = decoded;
toId = b4a_1.default.from(toId);
if (signature && hypercore_crypto_1.default.verify(bufData, signature, id)) {
this._addEntityConnection(id, toId);
this._setEntity(toId, { heartbeat: Date.now() });
}
}
}
_signItem(item) {
item = this._maybeHexify(item);
@ -363,40 +352,5 @@ class DHTCache extends events_1.default {
}
})();
}
_heartbeatCheck() {
for (const peer of this.connectedTo) {
const pubkey = b4a_1.default.from(peer, "hex");
const heartbeat = this.graph.node.get(peer)?.heartbeat;
const conn = this.swarm._allConnections.get(pubkey);
if (!conn) {
this.onRemovePeer({ remotePublicKey: pubkey });
continue;
}
if (heartbeat > 0 && Date.now() - heartbeat > 60 * 1000) {
conn.end();
}
}
}
_emitHeartbeat(peer) {
let peers = [...this.connectedTo];
if (peer) {
// @ts-ignore
peers = [b4a_1.default.from(peer.remotePublicKey).toString("hex")];
}
for (const peer of peers) {
const pubkey = b4a_1.default.from(peer, "hex");
const conn = this.swarm._allConnections.get(pubkey);
if (!conn) {
continue;
}
const data = b4a_1.default.from(Uint8Array.from([1]));
this.send(conn, this._compileMessage({
type: messages_js_1.Type.HEARTBEAT,
id: this.id,
data,
signature: hypercore_crypto_1.default.sign(data, this.swarm.keyPair.secretKey),
}));
}
}
}
exports.default = DHTCache;

6
dist/messages.d.ts vendored
View File

@ -71,11 +71,7 @@ export declare enum Type {
/**
* @generated from protobuf enum value: REMOVE_ITEM = 6;
*/
REMOVE_ITEM = 6,
/**
* @generated from protobuf enum value: HEARTBEAT = 7;
*/
HEARTBEAT = 7
REMOVE_ITEM = 6
}
declare class Message$Type extends MessageType<Message> {
constructor();

View File

@ -1 +1 @@
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE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
/**
* @generated from protobuf enum value: HEARTBEAT = 7;
*/
Type[Type["HEARTBEAT"] = 7] = "HEARTBEAT";
})(Type = exports.Type || (exports.Type = {}));
// @generated message type with reflection information, may provide speed optimized methods
class Message$Type extends runtime_5.MessageType {

View File

@ -7,7 +7,6 @@ enum Type {
DISCONNECTED = 4;
ADD_ITEM = 5;
REMOVE_ITEM = 6;
HEARTBEAT = 7;
}
message Message {

View File

@ -56,7 +56,7 @@ export default class DHTCache extends EventEmitter {
this.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
this.swarm.on("connection", (peer: any) =>
this.send(peer, b4a.from("hello"))
this.flood.send(peer, b4a.from("hello"), 0)
);
[...this.swarm.peers.values()]
@ -69,8 +69,6 @@ export default class DHTCache extends EventEmitter {
});
this._ensurePeer(this.id);
setInterval(() => this._heartbeatCheck(), 5 * 1000);
setInterval(() => this._emitHeartbeat(), 60 * 1000);
}
private _cache: Set<string>;
@ -165,8 +163,8 @@ export default class DHTCache extends EventEmitter {
this.flood.broadcast(message, ttl);
}
public send(peer: any, message: any) {
this.flood.send(peer, message, 0);
public send(message: any) {
this.flood.send(message, 0);
}
protected addPeerHandler(peer: any) {
@ -188,8 +186,6 @@ export default class DHTCache extends EventEmitter {
id,
});
this._emitHeartbeat(peer);
if (this.bootstrapped) {
return;
}
@ -282,15 +278,6 @@ export default class DHTCache extends EventEmitter {
} else if (type === Type.BOOTSTRAP_RESPONSE) {
const { bootstrap } = decoded;
this._bootstrapFrom(bootstrap);
} else if (type === Type.HEARTBEAT) {
let { id: toId, signature, data: bufData } = decoded;
toId = b4a.from(toId as Uint8Array);
if (signature && crypto.verify(bufData, signature, id)) {
this._addEntityConnection(id, toId as Buffer);
this._setEntity(toId as Buffer, { heartbeat: Date.now() });
}
}
}
@ -460,49 +447,4 @@ export default class DHTCache extends EventEmitter {
}
})();
}
private _heartbeatCheck() {
for (const peer of this.connectedTo) {
const pubkey = b4a.from(peer, "hex");
const heartbeat = this.graph.node.get(peer)?.heartbeat;
const conn = this.swarm._allConnections.get(pubkey);
if (!conn) {
this.onRemovePeer({ remotePublicKey: pubkey });
continue;
}
if (heartbeat > 0 && Date.now() - heartbeat > 60 * 1000) {
conn.end();
}
}
}
private _emitHeartbeat(peer?: any) {
let peers = [...this.connectedTo];
if (peer) {
// @ts-ignore
peers = [b4a.from(peer.remotePublicKey).toString("hex")];
}
for (const peer of peers) {
const pubkey = b4a.from(peer, "hex");
const conn = this.swarm._allConnections.get(pubkey);
if (!conn) {
continue;
}
const data = b4a.from(Uint8Array.from([1]));
this.send(
conn,
this._compileMessage({
type: Type.HEARTBEAT,
id: this.id,
data,
signature: crypto.sign(data, this.swarm.keyPair.secretKey),
})
);
}
}
}

View File

@ -78,11 +78,7 @@ export enum Type {
/**
* @generated from protobuf enum value: REMOVE_ITEM = 6;
*/
REMOVE_ITEM = 6,
/**
* @generated from protobuf enum value: HEARTBEAT = 7;
*/
HEARTBEAT = 7
REMOVE_ITEM = 6
}
// @generated message type with reflection information, may provide speed optimized methods
class Message$Type extends MessageType<Message> {