Compare commits
No commits in common. "323530c4bc136301b94699039452b483360e4c11" and "74773f12cd874775feaa9547036e65f409e85535" have entirely different histories.
323530c4bc
...
74773f12cd
|
@ -16,7 +16,6 @@ export default class DHTCache extends EventEmitter {
|
||||||
private _cache;
|
private _cache;
|
||||||
get cache(): string[];
|
get cache(): string[];
|
||||||
get allCache(): string[];
|
get allCache(): string[];
|
||||||
peerHasItem(peer: string | Buffer, item: string | Buffer): boolean;
|
|
||||||
addItem(item: string | Buffer): void;
|
addItem(item: string | Buffer): void;
|
||||||
private _compileMessage;
|
private _compileMessage;
|
||||||
private _broadcastMessage;
|
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,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO;IAOlE,OAAO,CAAC,IAAI,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B,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-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));
|
||||||
this.swarm.on("connection", (peer) => this.flood.send(peer, b4a_1.default.from("hello"), 0));
|
this.swarm.on("connection", (peer) => this.flood.send(peer, Buffer.from("hello"), 0));
|
||||||
[...this.swarm.peers.values()]
|
[...this.swarm.peers.values()]
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
remotePublicKey: item.publicKey;
|
remotePublicKey: item.publicKey;
|
||||||
|
@ -64,16 +64,10 @@ class DHTCache extends events_1.default {
|
||||||
}
|
}
|
||||||
return items.sort();
|
return items.sort();
|
||||||
}
|
}
|
||||||
peerHasItem(peer, item) {
|
|
||||||
peer = this._maybeHexify(peer);
|
|
||||||
item = this._maybeHexify(item);
|
|
||||||
return this.graph.hasSuccessor(peer, item);
|
|
||||||
}
|
|
||||||
addItem(item) {
|
addItem(item) {
|
||||||
item = this._maybeHexify(item);
|
item = this._maybeHexify(item);
|
||||||
this._cache.add(item);
|
this._cache.add(item);
|
||||||
this._ensureItem(item);
|
this._ensureItem(item);
|
||||||
this._addEntityConnection(this.id, item);
|
|
||||||
const broadcast = () => {
|
const broadcast = () => {
|
||||||
this._broadcastMessage({
|
this._broadcastMessage({
|
||||||
type: messages_js_1.Type.ADD_ITEM,
|
type: messages_js_1.Type.ADD_ITEM,
|
||||||
|
@ -179,24 +173,22 @@ class DHTCache extends events_1.default {
|
||||||
if (messages_js_1.Type.ADD_ITEM === type) {
|
if (messages_js_1.Type.ADD_ITEM === type) {
|
||||||
this._ensureItem(bufData);
|
this._ensureItem(bufData);
|
||||||
this._addEntityConnection(id, bufData);
|
this._addEntityConnection(id, bufData);
|
||||||
this.emit("item-added", id, bufData);
|
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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));
|
this._addEntityConnection(id, Buffer.from(toId));
|
||||||
this.emit("peer-add-seen", id, toId);
|
this.emit("peer-add-seen", id, toId);
|
||||||
this._recalculate();
|
this._recalculate();
|
||||||
}
|
}
|
||||||
else if (type === messages_js_1.Type.DISCONNECTED) {
|
else if (type === messages_js_1.Type.DISCONNECTED) {
|
||||||
const { id: toId } = decoded;
|
const { id: toId } = decoded;
|
||||||
this._removeEntityConnection(id, b4a_1.default.from(toId));
|
this._removeEntityConnection(id, Buffer.from(toId));
|
||||||
this.emit("peer-remove-seen", id, toId);
|
this.emit("peer-remove-seen", id, toId);
|
||||||
this._recalculate();
|
this._recalculate();
|
||||||
}
|
}
|
||||||
|
@ -280,8 +272,7 @@ class DHTCache extends events_1.default {
|
||||||
}
|
}
|
||||||
const connectedTo = this.graph
|
const connectedTo = this.graph
|
||||||
.neighbors(id)
|
.neighbors(id)
|
||||||
.filter((item) => this.graph.node.get(item)?.type === EntityType.PUBKEY)
|
.map((id) => Buffer.from(id, "hex"));
|
||||||
.map((id) => b4a_1.default.from(id, "hex"));
|
|
||||||
state[id] = { connectedTo };
|
state[id] = { connectedTo };
|
||||||
}
|
}
|
||||||
return state;
|
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.flood.on("message", (message, id) => this.onGetBroadcast(message, id));
|
||||||
|
|
||||||
this.swarm.on("connection", (peer: any) =>
|
this.swarm.on("connection", (peer: any) =>
|
||||||
this.flood.send(peer, b4a.from("hello"), 0)
|
this.flood.send(peer, Buffer.from("hello"), 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
[...this.swarm.peers.values()]
|
[...this.swarm.peers.values()]
|
||||||
|
@ -89,19 +89,11 @@ export default class DHTCache extends EventEmitter {
|
||||||
return items.sort();
|
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) {
|
public addItem(item: string | Buffer) {
|
||||||
item = this._maybeHexify(item);
|
item = this._maybeHexify(item);
|
||||||
this._cache.add(item);
|
this._cache.add(item);
|
||||||
|
|
||||||
this._ensureItem(item);
|
this._ensureItem(item);
|
||||||
this._addEntityConnection(this.id, item);
|
|
||||||
|
|
||||||
const broadcast = () => {
|
const broadcast = () => {
|
||||||
this._broadcastMessage({
|
this._broadcastMessage({
|
||||||
|
@ -236,23 +228,21 @@ export default class DHTCache extends EventEmitter {
|
||||||
if (Type.ADD_ITEM === type) {
|
if (Type.ADD_ITEM === type) {
|
||||||
this._ensureItem(bufData);
|
this._ensureItem(bufData);
|
||||||
this._addEntityConnection(id, bufData);
|
this._addEntityConnection(id, bufData);
|
||||||
this.emit("item-added", id, bufData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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);
|
this._addEntityConnection(id, Buffer.from(toId as Uint8Array));
|
||||||
this.emit("peer-add-seen", id, toId);
|
this.emit("peer-add-seen", id, toId);
|
||||||
this._recalculate();
|
this._recalculate();
|
||||||
} else if (type === Type.DISCONNECTED) {
|
} else if (type === Type.DISCONNECTED) {
|
||||||
const { id: toId } = decoded;
|
const { id: toId } = decoded;
|
||||||
this._removeEntityConnection(id, b4a.from(toId as Uint8Array) as Buffer);
|
this._removeEntityConnection(id, Buffer.from(toId as Uint8Array));
|
||||||
this.emit("peer-remove-seen", id, toId);
|
this.emit("peer-remove-seen", id, toId);
|
||||||
this._recalculate();
|
this._recalculate();
|
||||||
} else if (type === Type.BOOTSTRAP_REQUEST) {
|
} else if (type === Type.BOOTSTRAP_REQUEST) {
|
||||||
|
@ -362,10 +352,7 @@ export default class DHTCache extends EventEmitter {
|
||||||
}
|
}
|
||||||
const connectedTo = this.graph
|
const connectedTo = this.graph
|
||||||
.neighbors(id)
|
.neighbors(id)
|
||||||
.filter(
|
.map((id: string) => Buffer.from(id, "hex"));
|
||||||
(item: any) => this.graph.node.get(item)?.type === EntityType.PUBKEY
|
|
||||||
)
|
|
||||||
.map((id: string) => b4a.from(id, "hex"));
|
|
||||||
state[id] = { connectedTo };
|
state[id] = { connectedTo };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
32
test.js
32
test.js
|
@ -13,7 +13,7 @@ sodium.crypto_generichash(item1, b4a.from("item1"));
|
||||||
sodium.crypto_generichash(item2, b4a.from("item2"));
|
sodium.crypto_generichash(item2, b4a.from("item2"));
|
||||||
|
|
||||||
test("Basic presence test / data propagation", (t) => {
|
test("Basic presence test / data propagation", (t) => {
|
||||||
t.plan(10);
|
t.plan(6);
|
||||||
|
|
||||||
const peer1 = createPeer();
|
const peer1 = createPeer();
|
||||||
const peer2 = createPeer();
|
const peer2 = createPeer();
|
||||||
|
@ -51,39 +51,15 @@ test("Basic presence test / data propagation", (t) => {
|
||||||
p1.removeListener("online", handleOnline);
|
p1.removeListener("online", handleOnline);
|
||||||
p2.removeListener("online", handleOnline);
|
p2.removeListener("online", handleOnline);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
t.pass("Seeing everyone online");
|
t.pass("Seeing everyone online");
|
||||||
|
|
||||||
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]));
|
t.equals(true, p2.allCache.includes(peerData1[0]));
|
||||||
p2Resolve();
|
t.equals(true, p1.allCache.includes(peerData2[0]));
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
hasFinished = true;
|
||||||
|
|
||||||
peer2._allConnections.get(peer1.keyPair.publicKey).end();
|
peer2._allConnections.get(peer1.keyPair.publicKey).end();
|
||||||
});
|
}, 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue