Compare commits

..

No commits in common. "47eba446b7a41c426f50277ea31154d1377e1bda" and "866c91c0c6dd51244fbed5fa4c01a0d3b4838ae9" have entirely different histories.

7 changed files with 52 additions and 61 deletions

View File

@ -6,21 +6,20 @@ export default class DHTOnlineBase extends EventEmitter {
private bootstrapped;
private graph;
private connectedTo;
private data;
private encoding;
constructor(id: Buffer, { encoding }?: {
encoding?: string | undefined;
});
private _data;
get data(): {};
set data(value: {});
private _online;
get online(): string[];
broadcast(data: any, ttl?: number): void;
getPeerData(id: Buffer | string): any;
setData(data: any): void;
private _broadcastData;
protected onAddPeer(id: Buffer): void;
protected onRemovePeer(id: Buffer): void;
protected onGetBroadcast(message: Buffer, id: Buffer): void;
private _broadcastData;
private _hasSeenPeer;
private _setPeer;
private _ensurePeer;

View File

@ -1 +1 @@
{"version":3,"file":"dhtOnlineBase.d.ts","sourceRoot":"","sources":["../src/dhtOnlineBase.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAYlC,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,YAAY;IACrD,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAU;IAC9B,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,QAAQ,CAAoB;gBAExB,EAAE,EAAE,MAAM,EAAE,EAAE,QAA2B,EAAE;;KAAK;IAa5D,OAAO,CAAC,KAAK,CAAK;IAElB,IAAI,IAAI,IAAI,EAAE,CAEb;IAED,IAAI,IAAI,CAAC,KAAK,EAAE,EAAE,EAMjB;IAED,OAAO,CAAC,OAAO,CAAW;IAE1B,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAED,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM;IAIjC,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM;IAI/B,SAAS,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM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,MAAM;IA6CpD,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,QAAQ;IAGhB,OAAO,CAAC,WAAW;IAOnB,OAAO,CAAC,kBAAkB;IAY1B,OAAO,CAAC,qBAAqB;IAa7B,OAAO,CAAC,cAAc;IA2BtB,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,YAAY;CAOrB"}

39
dist/dhtOnlineBase.js vendored
View File

@ -14,6 +14,7 @@ class DHTOnlineBase extends events_1.default {
bootstrapped;
graph;
connectedTo;
data;
encoding;
constructor(id, { encoding = DEFAULT_ENCODING } = {}) {
super();
@ -23,19 +24,10 @@ class DHTOnlineBase extends events_1.default {
this.bootstrapped = false;
this.graph = new jsnetworkx_1.DiGraph();
this.connectedTo = new Set();
this._data = {};
this.data = {};
this.encoding = (0, codecs_1.default)(encoding || DEFAULT_ENCODING);
this._online = [this._maybeHexify(this.id)];
}
_data;
get data() {
return this._data;
}
set data(value) {
this._data = value;
this._setPeer(this.id, value);
this._broadcastData();
}
_online;
get online() {
return this._online;
@ -46,6 +38,22 @@ class DHTOnlineBase extends events_1.default {
getPeerData(id) {
return this.graph.node.get(this._maybeHexify(id));
}
setData(data) {
this.data = data;
this._setPeer(this.id, data);
this._broadcastData();
}
_broadcastData() {
const rawData = this.data;
if (!Object.keys(rawData).length) {
return;
}
const data = this.encoding.encode(rawData);
this.broadcast(messages_js_1.Message.toBinary(messages_js_1.Message.create({
type: messages_js_1.Type.STATE,
data,
})));
}
onAddPeer(id) {
const stringId = id.toString("hex");
if (this.connectedTo.has(stringId)) {
@ -121,17 +129,6 @@ class DHTOnlineBase extends events_1.default {
this._bootstrapFrom(bootstrap);
}
}
_broadcastData() {
const rawData = this._data;
if (!Object.keys(rawData).length) {
return;
}
const data = this.encoding.encode(rawData);
this.broadcast(messages_js_1.Message.toBinary(messages_js_1.Message.create({
type: messages_js_1.Type.STATE,
data,
})));
}
_hasSeenPeer(id) {
return this.graph.hasNode(this._maybeHexify(id));
}

2
dist/index.js vendored
View File

@ -17,7 +17,7 @@ class DHTOnline extends dhtOnlineBase_js_1.default {
this.flood.on("peer-remove", (peer) => this.handlePeerRemove(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.data = data;
this.setData(data);
[...this.swarm.peers.values()].forEach(this.handlePeerAdd.bind(this));
}
handlePeerAdd(peer) {

View File

@ -15,6 +15,7 @@ export default class DHTOnlineBase extends EventEmitter {
private bootstrapped: boolean;
private graph: any;
private connectedTo: Set<any>;
private data: {};
private encoding: codecs.Codec<any>;
constructor(id: Buffer, { encoding = DEFAULT_ENCODING } = {}) {
@ -25,25 +26,11 @@ export default class DHTOnlineBase extends EventEmitter {
this.bootstrapped = false;
this.graph = new DiGraph();
this.connectedTo = new Set();
this._data = {};
this.data = {};
this.encoding = codecs(encoding || DEFAULT_ENCODING);
this._online = [this._maybeHexify(this.id)];
}
private _data: {};
get data(): {} {
return this._data;
}
set data(value: {}) {
this._data = value;
this._setPeer(this.id, value);
this._broadcastData();
}
private _online: string[];
get online(): string[] {
@ -58,6 +45,30 @@ export default class DHTOnlineBase extends EventEmitter {
return this.graph.node.get(this._maybeHexify(id));
}
public setData(data: any) {
this.data = data;
this._setPeer(this.id, data);
this._broadcastData();
}
private _broadcastData() {
const rawData = this.data;
if (!Object.keys(rawData).length) {
return;
}
const data = this.encoding.encode(rawData);
this.broadcast(
Message.toBinary(
Message.create({
type: Type.STATE,
data,
})
)
);
}
protected onAddPeer(id: Buffer) {
const stringId = id.toString("hex");
if (this.connectedTo.has(stringId)) {
@ -157,22 +168,6 @@ export default class DHTOnlineBase extends EventEmitter {
}
}
private _broadcastData() {
const rawData = this._data;
if (!Object.keys(rawData).length) {
return;
}
const data = this.encoding.encode(rawData);
this.broadcast(
Message.toBinary(
Message.create({
type: Type.STATE,
data,
})
)
);
}
private _hasSeenPeer(id: Buffer | string) {
return this.graph.hasNode(this._maybeHexify(id));
}

View File

@ -23,7 +23,7 @@ export default class DHTOnline extends DHTOnlineBase {
this.flood.send(peer, Buffer.from("hello"), 0)
);
this.data = data;
this.setData(data);
[...this.swarm.peers.values()].forEach(this.handlePeerAdd.bind(this));
}

View File

@ -22,8 +22,8 @@ test("Basic presence test / data propagation", (t) => {
const p1 = new DHTOnline(peer1);
const p2 = new DHTOnline(peer2);
p1.data = { message: "Hello" };
p2.data = { message: "World!" };
p1.setData({ message: "Hello" });
p2.setData({ message: "World!" });
t.ok(p1.id, "Generated id 1");
t.ok(p2.id, "Generated id 2");