Compare commits
7 Commits
74773f12cd
...
323530c4bc
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 323530c4bc | |
Derrick Hammer | 1910398d44 | |
Derrick Hammer | 734a3640e3 | |
Derrick Hammer | 587739334a | |
Derrick Hammer | 48ceded78f | |
Derrick Hammer | 24b97d5288 | |
Derrick Hammer | eab1d70df4 |
|
@ -16,6 +16,7 @@ export default class DHTCache extends EventEmitter {
|
|||
private _cache;
|
||||
get cache(): string[];
|
||||
get allCache(): string[];
|
||||
peerHasItem(peer: string | Buffer, item: string | Buffer): boolean;
|
||||
addItem(item: string | Buffer): void;
|
||||
private _compileMessage;
|
||||
private _broadcastMessage;
|
||||
|
|
|
@ -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;IAkC7C,OAAO,CAAC,MAAM,CAAc;IAE5B,IAAW,KAAK,IAAI,MAAM,EAAE,CAE3B;IAED,IAAW,QAAQ,IAAI,MAAM,EAAE,CAU9B;IAEM,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAqBpC,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,iBAAiB;IA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CAAC,iBAAiB;IAoBzB,OAAO,CAAC,YAAY;IA+BpB,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,WAAW;CAyBpB"}
|
|
@ -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.flood.send(peer, Buffer.from("hello"), 0));
|
||||
this.swarm.on("connection", (peer) => this.flood.send(peer, b4a_1.default.from("hello"), 0));
|
||||
[...this.swarm.peers.values()]
|
||||
.map((item) => {
|
||||
remotePublicKey: item.publicKey;
|
||||
|
@ -64,10 +64,16 @@ class DHTCache extends events_1.default {
|
|||
}
|
||||
return items.sort();
|
||||
}
|
||||
peerHasItem(peer, item) {
|
||||
peer = this._maybeHexify(peer);
|
||||
item = this._maybeHexify(item);
|
||||
return this.graph.hasSuccessor(peer, item);
|
||||
}
|
||||
addItem(item) {
|
||||
item = this._maybeHexify(item);
|
||||
this._cache.add(item);
|
||||
this._ensureItem(item);
|
||||
this._addEntityConnection(this.id, item);
|
||||
const broadcast = () => {
|
||||
this._broadcastMessage({
|
||||
type: messages_js_1.Type.ADD_ITEM,
|
||||
|
@ -173,22 +179,24 @@ class DHTCache extends events_1.default {
|
|||
if (messages_js_1.Type.ADD_ITEM === type) {
|
||||
this._ensureItem(bufData);
|
||||
this._addEntityConnection(id, bufData);
|
||||
this.emit("item-added", id, bufData);
|
||||
}
|
||||
if (messages_js_1.Type.REMOVE_ITEM === type) {
|
||||
this.removeItem(bufData);
|
||||
this._pruneItems();
|
||||
this.emit("item-removed", id, bufData);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (type === messages_js_1.Type.CONNECTED) {
|
||||
const { id: toId } = decoded;
|
||||
this._addEntityConnection(id, Buffer.from(toId));
|
||||
this._addEntityConnection(id, b4a_1.default.from(toId));
|
||||
this.emit("peer-add-seen", id, toId);
|
||||
this._recalculate();
|
||||
}
|
||||
else if (type === messages_js_1.Type.DISCONNECTED) {
|
||||
const { id: toId } = decoded;
|
||||
this._removeEntityConnection(id, Buffer.from(toId));
|
||||
this._removeEntityConnection(id, b4a_1.default.from(toId));
|
||||
this.emit("peer-remove-seen", id, toId);
|
||||
this._recalculate();
|
||||
}
|
||||
|
@ -272,7 +280,8 @@ class DHTCache extends events_1.default {
|
|||
}
|
||||
const connectedTo = this.graph
|
||||
.neighbors(id)
|
||||
.map((id) => Buffer.from(id, "hex"));
|
||||
.filter((item) => this.graph.node.get(item)?.type === EntityType.PUBKEY)
|
||||
.map((id) => b4a_1.default.from(id, "hex"));
|
||||
state[id] = { connectedTo };
|
||||
}
|
||||
return state;
|
||||
|
|
21
src/index.ts
21
src/index.ts
|
@ -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.flood.send(peer, Buffer.from("hello"), 0)
|
||||
this.flood.send(peer, b4a.from("hello"), 0)
|
||||
);
|
||||
|
||||
[...this.swarm.peers.values()]
|
||||
|
@ -89,11 +89,19 @@ export default class DHTCache extends EventEmitter {
|
|||
return items.sort();
|
||||
}
|
||||
|
||||
public peerHasItem(peer: string | Buffer, item: string | Buffer): boolean {
|
||||
peer = this._maybeHexify(peer);
|
||||
item = this._maybeHexify(item);
|
||||
|
||||
return this.graph.hasSuccessor(peer, item);
|
||||
}
|
||||
|
||||
public addItem(item: string | Buffer) {
|
||||
item = this._maybeHexify(item);
|
||||
this._cache.add(item);
|
||||
|
||||
this._ensureItem(item);
|
||||
this._addEntityConnection(this.id, item);
|
||||
|
||||
const broadcast = () => {
|
||||
this._broadcastMessage({
|
||||
|
@ -228,21 +236,23 @@ export default class DHTCache extends EventEmitter {
|
|||
if (Type.ADD_ITEM === type) {
|
||||
this._ensureItem(bufData);
|
||||
this._addEntityConnection(id, bufData);
|
||||
this.emit("item-added", id, bufData);
|
||||
}
|
||||
|
||||
if (Type.REMOVE_ITEM === type) {
|
||||
this.removeItem(bufData);
|
||||
this._pruneItems();
|
||||
this.emit("item-removed", id, bufData);
|
||||
}
|
||||
}
|
||||
} else if (type === Type.CONNECTED) {
|
||||
const { id: toId } = decoded;
|
||||
this._addEntityConnection(id, Buffer.from(toId as Uint8Array));
|
||||
this._addEntityConnection(id, b4a.from(toId as Uint8Array) as Buffer);
|
||||
this.emit("peer-add-seen", id, toId);
|
||||
this._recalculate();
|
||||
} else if (type === Type.DISCONNECTED) {
|
||||
const { id: toId } = decoded;
|
||||
this._removeEntityConnection(id, Buffer.from(toId as Uint8Array));
|
||||
this._removeEntityConnection(id, b4a.from(toId as Uint8Array) as Buffer);
|
||||
this.emit("peer-remove-seen", id, toId);
|
||||
this._recalculate();
|
||||
} else if (type === Type.BOOTSTRAP_REQUEST) {
|
||||
|
@ -352,7 +362,10 @@ export default class DHTCache extends EventEmitter {
|
|||
}
|
||||
const connectedTo = this.graph
|
||||
.neighbors(id)
|
||||
.map((id: string) => Buffer.from(id, "hex"));
|
||||
.filter(
|
||||
(item: any) => this.graph.node.get(item)?.type === EntityType.PUBKEY
|
||||
)
|
||||
.map((id: string) => b4a.from(id, "hex"));
|
||||
state[id] = { connectedTo };
|
||||
}
|
||||
|
||||
|
|
34
test.js
34
test.js
|
@ -13,7 +13,7 @@ sodium.crypto_generichash(item1, b4a.from("item1"));
|
|||
sodium.crypto_generichash(item2, b4a.from("item2"));
|
||||
|
||||
test("Basic presence test / data propagation", (t) => {
|
||||
t.plan(6);
|
||||
t.plan(10);
|
||||
|
||||
const peer1 = createPeer();
|
||||
const peer2 = createPeer();
|
||||
|
@ -51,15 +51,39 @@ test("Basic presence test / data propagation", (t) => {
|
|||
p1.removeListener("online", handleOnline);
|
||||
p2.removeListener("online", handleOnline);
|
||||
|
||||
setTimeout(() => {
|
||||
t.pass("Seeing everyone online");
|
||||
t.pass("Seeing everyone online");
|
||||
|
||||
t.equals(true, p2.allCache.includes(peerData1[0]));
|
||||
let p1Resolve;
|
||||
let p2Resolve;
|
||||
let p1Promise = new Promise((resolve) => {
|
||||
p1Resolve = resolve;
|
||||
});
|
||||
|
||||
let p2Promise = new Promise((resolve) => {
|
||||
p2Resolve = resolve;
|
||||
});
|
||||
|
||||
p1.on("item-added", () => {
|
||||
t.equals(true, p1.allCache.includes(peerData2[0]));
|
||||
p1Resolve();
|
||||
});
|
||||
|
||||
p2.on("item-added", () => {
|
||||
t.equals(true, p2.allCache.includes(peerData1[0]));
|
||||
p2Resolve();
|
||||
});
|
||||
|
||||
Promise.all([p1Promise, p2Promise]).then(() => {
|
||||
t.equals(true, p1.peerHasItem(peer1.keyPair.publicKey, item1));
|
||||
t.equals(true, p2.peerHasItem(peer1.keyPair.publicKey, item1));
|
||||
|
||||
t.equals(true, p1.peerHasItem(peer2.keyPair.publicKey, item2));
|
||||
t.equals(true, p2.peerHasItem(peer2.keyPair.publicKey, item2));
|
||||
|
||||
hasFinished = true;
|
||||
|
||||
peer2._allConnections.get(peer1.keyPair.publicKey).end();
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue