*Update dist
This commit is contained in:
parent
0016903687
commit
47eba446b7
|
@ -6,20 +6,21 @@ export default class DHTOnlineBase extends EventEmitter {
|
||||||
private bootstrapped;
|
private bootstrapped;
|
||||||
private graph;
|
private graph;
|
||||||
private connectedTo;
|
private connectedTo;
|
||||||
private data;
|
|
||||||
private encoding;
|
private encoding;
|
||||||
constructor(id: Buffer, { encoding }?: {
|
constructor(id: Buffer, { encoding }?: {
|
||||||
encoding?: string | undefined;
|
encoding?: string | undefined;
|
||||||
});
|
});
|
||||||
|
private _data;
|
||||||
|
get data(): {};
|
||||||
|
set data(value: {});
|
||||||
private _online;
|
private _online;
|
||||||
get online(): string[];
|
get online(): string[];
|
||||||
broadcast(data: any, ttl?: number): void;
|
broadcast(data: any, ttl?: number): void;
|
||||||
getPeerData(id: Buffer | string): any;
|
getPeerData(id: Buffer | string): any;
|
||||||
setData(data: any): void;
|
|
||||||
private _broadcastData;
|
|
||||||
protected onAddPeer(id: Buffer): void;
|
protected onAddPeer(id: Buffer): void;
|
||||||
protected onRemovePeer(id: Buffer): void;
|
protected onRemovePeer(id: Buffer): void;
|
||||||
protected onGetBroadcast(message: Buffer, id: Buffer): void;
|
protected onGetBroadcast(message: Buffer, id: Buffer): void;
|
||||||
|
private _broadcastData;
|
||||||
private _hasSeenPeer;
|
private _hasSeenPeer;
|
||||||
private _setPeer;
|
private _setPeer;
|
||||||
private _ensurePeer;
|
private _ensurePeer;
|
||||||
|
|
|
@ -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,IAAI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I,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;IAqC9B,SAAS,CAAC,YAAY,CAAC,EAAE,EAAE,MAAM;IAiBjC,SAAS,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IA6CpD,OAAO,CAAC,cAAc;IAgBtB,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"}
|
|
@ -14,7 +14,6 @@ class DHTOnlineBase extends events_1.default {
|
||||||
bootstrapped;
|
bootstrapped;
|
||||||
graph;
|
graph;
|
||||||
connectedTo;
|
connectedTo;
|
||||||
data;
|
|
||||||
encoding;
|
encoding;
|
||||||
constructor(id, { encoding = DEFAULT_ENCODING } = {}) {
|
constructor(id, { encoding = DEFAULT_ENCODING } = {}) {
|
||||||
super();
|
super();
|
||||||
|
@ -24,10 +23,19 @@ class DHTOnlineBase extends events_1.default {
|
||||||
this.bootstrapped = false;
|
this.bootstrapped = false;
|
||||||
this.graph = new jsnetworkx_1.DiGraph();
|
this.graph = new jsnetworkx_1.DiGraph();
|
||||||
this.connectedTo = new Set();
|
this.connectedTo = new Set();
|
||||||
this.data = {};
|
this._data = {};
|
||||||
this.encoding = (0, codecs_1.default)(encoding || DEFAULT_ENCODING);
|
this.encoding = (0, codecs_1.default)(encoding || DEFAULT_ENCODING);
|
||||||
this._online = [this._maybeHexify(this.id)];
|
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;
|
_online;
|
||||||
get online() {
|
get online() {
|
||||||
return this._online;
|
return this._online;
|
||||||
|
@ -38,22 +46,6 @@ class DHTOnlineBase extends events_1.default {
|
||||||
getPeerData(id) {
|
getPeerData(id) {
|
||||||
return this.graph.node.get(this._maybeHexify(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) {
|
onAddPeer(id) {
|
||||||
const stringId = id.toString("hex");
|
const stringId = id.toString("hex");
|
||||||
if (this.connectedTo.has(stringId)) {
|
if (this.connectedTo.has(stringId)) {
|
||||||
|
@ -129,6 +121,17 @@ class DHTOnlineBase extends events_1.default {
|
||||||
this._bootstrapFrom(bootstrap);
|
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) {
|
_hasSeenPeer(id) {
|
||||||
return this.graph.hasNode(this._maybeHexify(id));
|
return this.graph.hasNode(this._maybeHexify(id));
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ class DHTOnline extends dhtOnlineBase_js_1.default {
|
||||||
this.flood.on("peer-remove", (peer) => this.handlePeerRemove(peer));
|
this.flood.on("peer-remove", (peer) => this.handlePeerRemove(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, Buffer.from("hello"), 0));
|
this.swarm.on("connection", (peer) => this.flood.send(peer, Buffer.from("hello"), 0));
|
||||||
this.setData(data);
|
this.data = data;
|
||||||
[...this.swarm.peers.values()].forEach(this.handlePeerAdd.bind(this));
|
[...this.swarm.peers.values()].forEach(this.handlePeerAdd.bind(this));
|
||||||
}
|
}
|
||||||
handlePeerAdd(peer) {
|
handlePeerAdd(peer) {
|
||||||
|
|
Loading…
Reference in New Issue