@leofcoin/peernet 0.11.8 → 0.11.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/peernet.js +51 -46
- package/dist/commonjs/{http-12850e18.js → http-1de9e08b.js} +1 -1
- package/dist/commonjs/peernet.js +34 -47
- package/dist/module/peernet.js +33 -46
- package/package.json +3 -2
- package/src/peernet.js +12 -15
- package/src/utils/utils.js +0 -4
- package/test.js +7 -0
- package/src/client.js +0 -75
- package/src/peer.js +0 -67
|
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
10
10
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
11
11
|
var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
|
|
12
12
|
|
|
13
|
-
var version = "0.11.
|
|
13
|
+
var version = "0.11.10";
|
|
14
14
|
|
|
15
15
|
var api$1 = {
|
|
16
16
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
require('@vandeurenglenn/debug');
|
|
3
4
|
var LeofcoinStorage = require('@leofcoin/storage');
|
|
4
5
|
var peernetMessage = require('./peernet-message.js');
|
|
5
6
|
var dht = require('./dht.js');
|
|
@@ -356,7 +357,7 @@ constructor(options = {}) {
|
|
|
356
357
|
}
|
|
357
358
|
|
|
358
359
|
send(message) {
|
|
359
|
-
this.bw.up += message.length;
|
|
360
|
+
this.bw.up += message.length || message.byteLength;
|
|
360
361
|
this.channel.send(message);
|
|
361
362
|
}
|
|
362
363
|
|
|
@@ -369,16 +370,16 @@ constructor(options = {}) {
|
|
|
369
370
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
370
371
|
if (message.id === id) {
|
|
371
372
|
resolve(message.data);
|
|
372
|
-
pubsub.unsubscribe(
|
|
373
|
+
pubsub.unsubscribe(`peer:data`, _onData);
|
|
373
374
|
}
|
|
374
375
|
};
|
|
375
376
|
|
|
376
|
-
pubsub.subscribe(
|
|
377
|
+
pubsub.subscribe(`peer:data`, _onData);
|
|
377
378
|
|
|
378
379
|
// cleanup subscriptions
|
|
379
|
-
setTimeout(() => {
|
|
380
|
-
|
|
381
|
-
}, 5000);
|
|
380
|
+
// setTimeout(() => {
|
|
381
|
+
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
382
|
+
// }, 5000);
|
|
382
383
|
|
|
383
384
|
this.send(data);
|
|
384
385
|
});
|
|
@@ -407,11 +408,12 @@ constructor(options = {}) {
|
|
|
407
408
|
this.#connected = true;
|
|
408
409
|
pubsub.publish('peer:connected', this);
|
|
409
410
|
};
|
|
410
|
-
message.channel.onclose = () =>
|
|
411
|
+
message.channel.onclose = () => this.close.bind(this);
|
|
412
|
+
|
|
411
413
|
message.channel.onmessage = (message) => {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
414
|
+
pubsub.publish('peer:data', message);
|
|
415
|
+
debug(`incoming message from ${this.id}`);
|
|
416
|
+
debug(message);
|
|
415
417
|
this.bw.down += message.length || message.byteLength;
|
|
416
418
|
};
|
|
417
419
|
this.channel = message.channel;
|
|
@@ -427,9 +429,9 @@ constructor(options = {}) {
|
|
|
427
429
|
this.channel.onclose = () => this.close.bind(this);
|
|
428
430
|
|
|
429
431
|
this.channel.onmessage = (message) => {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
432
|
+
pubsub.publish('peer:data', message);
|
|
433
|
+
debug(`incoming message from ${this.peerId}`);
|
|
434
|
+
debug(message);
|
|
433
435
|
this.bw.down += message.length || message.byteLength;
|
|
434
436
|
};
|
|
435
437
|
|
|
@@ -460,6 +462,8 @@ constructor(options = {}) {
|
|
|
460
462
|
// if (data.videocall) return this._startStream(true, false); // start video and audio stream
|
|
461
463
|
// if (data.call) return this._startStream(true, true); // start audio stream
|
|
462
464
|
if (message.candidate) {
|
|
465
|
+
debug(`incoming candidate ${this.channelName}`);
|
|
466
|
+
debug(message.candidate.candidate);
|
|
463
467
|
this.remoteAddress = message.candidate.address;
|
|
464
468
|
this.remotePort = message.candidate.port;
|
|
465
469
|
this.remoteProtocol = message.candidate.protocol;
|
|
@@ -469,12 +473,14 @@ constructor(options = {}) {
|
|
|
469
473
|
try {
|
|
470
474
|
if (message.sdp) {
|
|
471
475
|
if (message.sdp.type === 'offer') {
|
|
476
|
+
debug(`incoming offer ${this.channelName}`);
|
|
472
477
|
await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
|
|
473
478
|
const answer = await this.#connection.createAnswer();
|
|
474
479
|
await this.#connection.setLocalDescription(answer);
|
|
475
480
|
this._sendMessage({'sdp': this.#connection.localDescription});
|
|
476
481
|
}
|
|
477
482
|
if (message.sdp.type === 'answer') {
|
|
483
|
+
debug(`incoming answer ${this.channelName}`);
|
|
478
484
|
await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
|
|
479
485
|
}
|
|
480
486
|
}
|
|
@@ -484,6 +490,7 @@ constructor(options = {}) {
|
|
|
484
490
|
}
|
|
485
491
|
|
|
486
492
|
close() {
|
|
493
|
+
debug(`closing ${this.peerId}`);
|
|
487
494
|
this.channel?.close();
|
|
488
495
|
this.#connection?.close();
|
|
489
496
|
|
|
@@ -514,7 +521,6 @@ class Client {
|
|
|
514
521
|
async _init(identifiers, stars = []) {
|
|
515
522
|
if (stars.length === 0) {
|
|
516
523
|
stars.push('wss://star.leofcoin.org');
|
|
517
|
-
stars.push('ws://localhost:44444');
|
|
518
524
|
}
|
|
519
525
|
this.identifiers = identifiers;
|
|
520
526
|
this.starsConfig = stars;
|
|
@@ -536,14 +542,6 @@ class Client {
|
|
|
536
542
|
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
537
543
|
}
|
|
538
544
|
this.setupListeners();
|
|
539
|
-
|
|
540
|
-
pubsub.subscribe('peer:connected', (peer) => {
|
|
541
|
-
// peer.send(JSON.stringify({data: 'hello', from: this.id, to: peer.to}))
|
|
542
|
-
console.log({peer: peer.to});
|
|
543
|
-
console.log({id: peer.id});
|
|
544
|
-
console.log({id: this.id});
|
|
545
|
-
});
|
|
546
|
-
// pubsub.subscribe('peer:data', (data) => console.log({data}))
|
|
547
545
|
}
|
|
548
546
|
|
|
549
547
|
setupListeners() {
|
|
@@ -602,10 +600,7 @@ class Client {
|
|
|
602
600
|
}
|
|
603
601
|
}
|
|
604
602
|
}
|
|
605
|
-
|
|
606
|
-
console.log(`star ${id} left`);
|
|
607
|
-
|
|
608
|
-
|
|
603
|
+
debug(`star ${id} left`);
|
|
609
604
|
}
|
|
610
605
|
|
|
611
606
|
peerLeft(id) {
|
|
@@ -613,7 +608,7 @@ class Client {
|
|
|
613
608
|
this.#connections[id].close();
|
|
614
609
|
delete this.#connections[id];
|
|
615
610
|
}
|
|
616
|
-
|
|
611
|
+
debug(`peer ${id} left`);
|
|
617
612
|
}
|
|
618
613
|
|
|
619
614
|
peerJoined(id, signal) {
|
|
@@ -623,7 +618,7 @@ class Client {
|
|
|
623
618
|
}
|
|
624
619
|
// RTCPeerConnection
|
|
625
620
|
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
626
|
-
|
|
621
|
+
debug(`peer ${id} joined`);
|
|
627
622
|
}
|
|
628
623
|
|
|
629
624
|
|
|
@@ -781,10 +776,6 @@ class ChatMessage extends codecFormatInterface {
|
|
|
781
776
|
}
|
|
782
777
|
}
|
|
783
778
|
|
|
784
|
-
const debug = (log) => {
|
|
785
|
-
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
786
|
-
};
|
|
787
|
-
|
|
788
779
|
const protoFor = (data) => {
|
|
789
780
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
790
781
|
const codec$1 = new codec.PeernetCodec(data);
|
|
@@ -1643,7 +1634,7 @@ class Peernet {
|
|
|
1643
1634
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1644
1635
|
});
|
|
1645
1636
|
} else {
|
|
1646
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1637
|
+
const http = await Promise.resolve().then(function () { return require('./http-1de9e08b.js'); });
|
|
1647
1638
|
if (environment !== 'browser') http.default(options);
|
|
1648
1639
|
}
|
|
1649
1640
|
|
|
@@ -1729,7 +1720,6 @@ class Peernet {
|
|
|
1729
1720
|
|
|
1730
1721
|
const {id, proto} = message;
|
|
1731
1722
|
this.bw.down += proto.encoded.length;
|
|
1732
|
-
|
|
1733
1723
|
if (proto.name === 'peernet-dht') {
|
|
1734
1724
|
let { hash, store } = proto.decoded;
|
|
1735
1725
|
let has;
|
|
@@ -1778,7 +1768,7 @@ class Peernet {
|
|
|
1778
1768
|
this.bw.up += node.encoded.length;
|
|
1779
1769
|
}
|
|
1780
1770
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
1781
|
-
globalSub.publish(proto.decoded.topic
|
|
1771
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data);
|
|
1782
1772
|
}
|
|
1783
1773
|
// }
|
|
1784
1774
|
}
|
|
@@ -1797,11 +1787,9 @@ class Peernet {
|
|
|
1797
1787
|
let result = await peer.request(node.encoded);
|
|
1798
1788
|
result = new Uint8Array(Object.values(result));
|
|
1799
1789
|
let proto = protoFor(result);
|
|
1800
|
-
|
|
1801
1790
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
1802
1791
|
const from = proto.decoded.from;
|
|
1803
1792
|
proto = protoFor(proto.decoded.data);
|
|
1804
|
-
|
|
1805
1793
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
1806
1794
|
|
|
1807
1795
|
// TODO: give ip and port (just used for location)
|
|
@@ -1892,15 +1880,15 @@ class Peernet {
|
|
|
1892
1880
|
// get closest peer on earth
|
|
1893
1881
|
const closestPeer = await this.dht.closestPeer(providers);
|
|
1894
1882
|
// get peer instance by id
|
|
1895
|
-
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store
|
|
1883
|
+
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
|
|
1896
1884
|
|
|
1897
|
-
const id = closestPeer.id
|
|
1885
|
+
const id = closestPeer.id;
|
|
1898
1886
|
if (this.connections) {
|
|
1899
1887
|
let closest = this.connections.filter((peer) => {
|
|
1900
1888
|
if (peer.peerId === id) return peer
|
|
1901
1889
|
});
|
|
1902
1890
|
|
|
1903
|
-
let data = new DataMessage({hash, store: store
|
|
1891
|
+
let data = new DataMessage({hash, store: store?.name ? store?.name : store});
|
|
1904
1892
|
|
|
1905
1893
|
const node = await this.prepareMessage(id, data.encoded);
|
|
1906
1894
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
@@ -1910,12 +1898,11 @@ class Peernet {
|
|
|
1910
1898
|
});
|
|
1911
1899
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
1912
1900
|
}
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
}
|
|
1901
|
+
data = new Uint8Array(Object.values(data));
|
|
1902
|
+
let proto = protoFor(data);
|
|
1903
|
+
proto = protoFor(proto.decoded.data);
|
|
1904
|
+
// TODO: store data automaticly or not
|
|
1905
|
+
return proto.decoded.data
|
|
1919
1906
|
|
|
1920
1907
|
// this.put(hash, proto.decoded.data)
|
|
1921
1908
|
}
|
|
@@ -2008,7 +1995,7 @@ class Peernet {
|
|
|
2008
1995
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2009
1996
|
if (data) return data
|
|
2010
1997
|
|
|
2011
|
-
return this.requestData(hash, store
|
|
1998
|
+
return this.requestData(hash, store?.name ? store.name : store)
|
|
2012
1999
|
}
|
|
2013
2000
|
|
|
2014
2001
|
/**
|
package/dist/module/peernet.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import '@vandeurenglenn/debug';
|
|
1
2
|
import LeofcoinStorage from '@leofcoin/storage';
|
|
2
3
|
import protons from 'protons';
|
|
3
4
|
import bs32 from '@vandeurenglenn/base32';
|
|
@@ -313,7 +314,7 @@ constructor(options = {}) {
|
|
|
313
314
|
}
|
|
314
315
|
|
|
315
316
|
send(message) {
|
|
316
|
-
this.bw.up += message.length;
|
|
317
|
+
this.bw.up += message.length || message.byteLength;
|
|
317
318
|
this.channel.send(message);
|
|
318
319
|
}
|
|
319
320
|
|
|
@@ -326,16 +327,16 @@ constructor(options = {}) {
|
|
|
326
327
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
327
328
|
if (message.id === id) {
|
|
328
329
|
resolve(message.data);
|
|
329
|
-
pubsub.unsubscribe(
|
|
330
|
+
pubsub.unsubscribe(`peer:data`, _onData);
|
|
330
331
|
}
|
|
331
332
|
};
|
|
332
333
|
|
|
333
|
-
pubsub.subscribe(
|
|
334
|
+
pubsub.subscribe(`peer:data`, _onData);
|
|
334
335
|
|
|
335
336
|
// cleanup subscriptions
|
|
336
|
-
setTimeout(() => {
|
|
337
|
-
|
|
338
|
-
}, 5000);
|
|
337
|
+
// setTimeout(() => {
|
|
338
|
+
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
339
|
+
// }, 5000);
|
|
339
340
|
|
|
340
341
|
this.send(data);
|
|
341
342
|
});
|
|
@@ -364,11 +365,12 @@ constructor(options = {}) {
|
|
|
364
365
|
this.#connected = true;
|
|
365
366
|
pubsub.publish('peer:connected', this);
|
|
366
367
|
};
|
|
367
|
-
message.channel.onclose = () =>
|
|
368
|
+
message.channel.onclose = () => this.close.bind(this);
|
|
369
|
+
|
|
368
370
|
message.channel.onmessage = (message) => {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
371
|
+
pubsub.publish('peer:data', message);
|
|
372
|
+
debug(`incoming message from ${this.id}`);
|
|
373
|
+
debug(message);
|
|
372
374
|
this.bw.down += message.length || message.byteLength;
|
|
373
375
|
};
|
|
374
376
|
this.channel = message.channel;
|
|
@@ -384,9 +386,9 @@ constructor(options = {}) {
|
|
|
384
386
|
this.channel.onclose = () => this.close.bind(this);
|
|
385
387
|
|
|
386
388
|
this.channel.onmessage = (message) => {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
389
|
+
pubsub.publish('peer:data', message);
|
|
390
|
+
debug(`incoming message from ${this.peerId}`);
|
|
391
|
+
debug(message);
|
|
390
392
|
this.bw.down += message.length || message.byteLength;
|
|
391
393
|
};
|
|
392
394
|
|
|
@@ -417,6 +419,8 @@ constructor(options = {}) {
|
|
|
417
419
|
// if (data.videocall) return this._startStream(true, false); // start video and audio stream
|
|
418
420
|
// if (data.call) return this._startStream(true, true); // start audio stream
|
|
419
421
|
if (message.candidate) {
|
|
422
|
+
debug(`incoming candidate ${this.channelName}`);
|
|
423
|
+
debug(message.candidate.candidate);
|
|
420
424
|
this.remoteAddress = message.candidate.address;
|
|
421
425
|
this.remotePort = message.candidate.port;
|
|
422
426
|
this.remoteProtocol = message.candidate.protocol;
|
|
@@ -426,12 +430,14 @@ constructor(options = {}) {
|
|
|
426
430
|
try {
|
|
427
431
|
if (message.sdp) {
|
|
428
432
|
if (message.sdp.type === 'offer') {
|
|
433
|
+
debug(`incoming offer ${this.channelName}`);
|
|
429
434
|
await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
|
|
430
435
|
const answer = await this.#connection.createAnswer();
|
|
431
436
|
await this.#connection.setLocalDescription(answer);
|
|
432
437
|
this._sendMessage({'sdp': this.#connection.localDescription});
|
|
433
438
|
}
|
|
434
439
|
if (message.sdp.type === 'answer') {
|
|
440
|
+
debug(`incoming answer ${this.channelName}`);
|
|
435
441
|
await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
|
|
436
442
|
}
|
|
437
443
|
}
|
|
@@ -441,6 +447,7 @@ constructor(options = {}) {
|
|
|
441
447
|
}
|
|
442
448
|
|
|
443
449
|
close() {
|
|
450
|
+
debug(`closing ${this.peerId}`);
|
|
444
451
|
this.channel?.close();
|
|
445
452
|
this.#connection?.close();
|
|
446
453
|
|
|
@@ -471,7 +478,6 @@ class Client {
|
|
|
471
478
|
async _init(identifiers, stars = []) {
|
|
472
479
|
if (stars.length === 0) {
|
|
473
480
|
stars.push('wss://star.leofcoin.org');
|
|
474
|
-
stars.push('ws://localhost:44444');
|
|
475
481
|
}
|
|
476
482
|
this.identifiers = identifiers;
|
|
477
483
|
this.starsConfig = stars;
|
|
@@ -493,14 +499,6 @@ class Client {
|
|
|
493
499
|
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
494
500
|
}
|
|
495
501
|
this.setupListeners();
|
|
496
|
-
|
|
497
|
-
pubsub.subscribe('peer:connected', (peer) => {
|
|
498
|
-
// peer.send(JSON.stringify({data: 'hello', from: this.id, to: peer.to}))
|
|
499
|
-
console.log({peer: peer.to});
|
|
500
|
-
console.log({id: peer.id});
|
|
501
|
-
console.log({id: this.id});
|
|
502
|
-
});
|
|
503
|
-
// pubsub.subscribe('peer:data', (data) => console.log({data}))
|
|
504
502
|
}
|
|
505
503
|
|
|
506
504
|
setupListeners() {
|
|
@@ -559,10 +557,7 @@ class Client {
|
|
|
559
557
|
}
|
|
560
558
|
}
|
|
561
559
|
}
|
|
562
|
-
|
|
563
|
-
console.log(`star ${id} left`);
|
|
564
|
-
|
|
565
|
-
|
|
560
|
+
debug(`star ${id} left`);
|
|
566
561
|
}
|
|
567
562
|
|
|
568
563
|
peerLeft(id) {
|
|
@@ -570,7 +565,7 @@ class Client {
|
|
|
570
565
|
this.#connections[id].close();
|
|
571
566
|
delete this.#connections[id];
|
|
572
567
|
}
|
|
573
|
-
|
|
568
|
+
debug(`peer ${id} left`);
|
|
574
569
|
}
|
|
575
570
|
|
|
576
571
|
peerJoined(id, signal) {
|
|
@@ -580,7 +575,7 @@ class Client {
|
|
|
580
575
|
}
|
|
581
576
|
// RTCPeerConnection
|
|
582
577
|
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
583
|
-
|
|
578
|
+
debug(`peer ${id} joined`);
|
|
584
579
|
}
|
|
585
580
|
|
|
586
581
|
|
|
@@ -1378,10 +1373,6 @@ class ChatMessage extends FormatInterface {
|
|
|
1378
1373
|
}
|
|
1379
1374
|
}
|
|
1380
1375
|
|
|
1381
|
-
const debug = (log) => {
|
|
1382
|
-
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
1383
|
-
};
|
|
1384
|
-
|
|
1385
1376
|
const protoFor = (data) => {
|
|
1386
1377
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data);
|
|
1387
1378
|
const codec = new PeernetCodec(data);
|
|
@@ -2318,7 +2309,6 @@ class Peernet {
|
|
|
2318
2309
|
|
|
2319
2310
|
const {id, proto} = message;
|
|
2320
2311
|
this.bw.down += proto.encoded.length;
|
|
2321
|
-
|
|
2322
2312
|
if (proto.name === 'peernet-dht') {
|
|
2323
2313
|
let { hash, store } = proto.decoded;
|
|
2324
2314
|
let has;
|
|
@@ -2367,7 +2357,7 @@ class Peernet {
|
|
|
2367
2357
|
this.bw.up += node.encoded.length;
|
|
2368
2358
|
}
|
|
2369
2359
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
2370
|
-
globalSub.publish(proto.decoded.topic
|
|
2360
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data);
|
|
2371
2361
|
}
|
|
2372
2362
|
// }
|
|
2373
2363
|
}
|
|
@@ -2386,11 +2376,9 @@ class Peernet {
|
|
|
2386
2376
|
let result = await peer.request(node.encoded);
|
|
2387
2377
|
result = new Uint8Array(Object.values(result));
|
|
2388
2378
|
let proto = protoFor(result);
|
|
2389
|
-
|
|
2390
2379
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
2391
2380
|
const from = proto.decoded.from;
|
|
2392
2381
|
proto = protoFor(proto.decoded.data);
|
|
2393
|
-
|
|
2394
2382
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
2395
2383
|
|
|
2396
2384
|
// TODO: give ip and port (just used for location)
|
|
@@ -2481,15 +2469,15 @@ class Peernet {
|
|
|
2481
2469
|
// get closest peer on earth
|
|
2482
2470
|
const closestPeer = await this.dht.closestPeer(providers);
|
|
2483
2471
|
// get peer instance by id
|
|
2484
|
-
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store
|
|
2472
|
+
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
|
|
2485
2473
|
|
|
2486
|
-
const id = closestPeer.id
|
|
2474
|
+
const id = closestPeer.id;
|
|
2487
2475
|
if (this.connections) {
|
|
2488
2476
|
let closest = this.connections.filter((peer) => {
|
|
2489
2477
|
if (peer.peerId === id) return peer
|
|
2490
2478
|
});
|
|
2491
2479
|
|
|
2492
|
-
let data = new DataMessage({hash, store: store
|
|
2480
|
+
let data = new DataMessage({hash, store: store?.name ? store?.name : store});
|
|
2493
2481
|
|
|
2494
2482
|
const node = await this.prepareMessage(id, data.encoded);
|
|
2495
2483
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
@@ -2499,12 +2487,11 @@ class Peernet {
|
|
|
2499
2487
|
});
|
|
2500
2488
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2501
2489
|
}
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
}
|
|
2490
|
+
data = new Uint8Array(Object.values(data));
|
|
2491
|
+
let proto = protoFor(data);
|
|
2492
|
+
proto = protoFor(proto.decoded.data);
|
|
2493
|
+
// TODO: store data automaticly or not
|
|
2494
|
+
return proto.decoded.data
|
|
2508
2495
|
|
|
2509
2496
|
// this.put(hash, proto.decoded.data)
|
|
2510
2497
|
}
|
|
@@ -2597,7 +2584,7 @@ class Peernet {
|
|
|
2597
2584
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2598
2585
|
if (data) return data
|
|
2599
2586
|
|
|
2600
|
-
return this.requestData(hash, store
|
|
2587
|
+
return this.requestData(hash, store?.name ? store.name : store)
|
|
2601
2588
|
}
|
|
2602
2589
|
|
|
2603
2590
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.11",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
27
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
|
-
"@leofcoin/peernet-swarm": "^0.1.
|
|
28
|
+
"@leofcoin/peernet-swarm": "^0.1.16",
|
|
29
29
|
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"@rollup/plugin-eslint": "^8.0.1",
|
|
47
47
|
"@rollup/plugin-json": "^4.1.0",
|
|
48
48
|
"@rollup/plugin-node-resolve": "^13.0.4",
|
|
49
|
+
"@vandeurenglenn/debug": "^1.0.0",
|
|
49
50
|
"coveralls": "^3.1.1",
|
|
50
51
|
"esdoc": "^1.1.0",
|
|
51
52
|
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
|
package/src/peernet.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import '@vandeurenglenn/debug'
|
|
1
2
|
import Client from './../node_modules/@leofcoin/peernet-swarm/dist/es/client.js'
|
|
2
3
|
import LeofcoinStorage from '@leofcoin/storage'
|
|
3
4
|
import LeofcoinStorageClient from './http/client/storage.js'
|
|
@@ -16,7 +17,7 @@ import PeerDiscovery from './discovery/peer-discovery'
|
|
|
16
17
|
import DHT from './dht/dht.js'
|
|
17
18
|
import Hash from './hash/hash'
|
|
18
19
|
import codecs from './codec/codecs'
|
|
19
|
-
import {
|
|
20
|
+
import { protoFor, target } from './utils/utils.js'
|
|
20
21
|
import generateAccount from '@leofcoin/generate-account'
|
|
21
22
|
import MessageHandler from './handlers/message.js'
|
|
22
23
|
import { encapsulatedError, dhtError,
|
|
@@ -260,7 +261,6 @@ export default class Peernet {
|
|
|
260
261
|
|
|
261
262
|
const {id, proto} = message
|
|
262
263
|
this.bw.down += proto.encoded.length
|
|
263
|
-
|
|
264
264
|
if (proto.name === 'peernet-dht') {
|
|
265
265
|
let { hash, store } = proto.decoded
|
|
266
266
|
let has;
|
|
@@ -311,7 +311,7 @@ export default class Peernet {
|
|
|
311
311
|
this.bw.up += node.encoded.length
|
|
312
312
|
}
|
|
313
313
|
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
314
|
-
globalSub.publish(proto.decoded.topic
|
|
314
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data)
|
|
315
315
|
}
|
|
316
316
|
// }
|
|
317
317
|
}
|
|
@@ -330,11 +330,9 @@ export default class Peernet {
|
|
|
330
330
|
let result = await peer.request(node.encoded)
|
|
331
331
|
result = new Uint8Array(Object.values(result))
|
|
332
332
|
let proto = protoFor(result)
|
|
333
|
-
|
|
334
333
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
335
334
|
const from = proto.decoded.from
|
|
336
335
|
proto = protoFor(proto.decoded.data)
|
|
337
|
-
|
|
338
336
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
339
337
|
|
|
340
338
|
// TODO: give ip and port (just used for location)
|
|
@@ -425,15 +423,15 @@ export default class Peernet {
|
|
|
425
423
|
// get closest peer on earth
|
|
426
424
|
const closestPeer = await this.dht.closestPeer(providers)
|
|
427
425
|
// get peer instance by id
|
|
428
|
-
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store
|
|
426
|
+
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
|
|
429
427
|
|
|
430
|
-
const id = closestPeer.id
|
|
428
|
+
const id = closestPeer.id
|
|
431
429
|
if (this.connections) {
|
|
432
430
|
let closest = this.connections.filter((peer) => {
|
|
433
431
|
if (peer.peerId === id) return peer
|
|
434
432
|
})
|
|
435
433
|
|
|
436
|
-
let data = new DataMessage({hash, store: store
|
|
434
|
+
let data = new DataMessage({hash, store: store?.name ? store?.name : store});
|
|
437
435
|
|
|
438
436
|
const node = await this.prepareMessage(id, data.encoded)
|
|
439
437
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
@@ -443,12 +441,11 @@ export default class Peernet {
|
|
|
443
441
|
})
|
|
444
442
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
445
443
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
}
|
|
444
|
+
data = new Uint8Array(Object.values(data))
|
|
445
|
+
let proto = protoFor(data)
|
|
446
|
+
proto = protoFor(proto.decoded.data)
|
|
447
|
+
// TODO: store data automaticly or not
|
|
448
|
+
return proto.decoded.data
|
|
452
449
|
|
|
453
450
|
// this.put(hash, proto.decoded.data)
|
|
454
451
|
}
|
|
@@ -542,7 +539,7 @@ export default class Peernet {
|
|
|
542
539
|
if (store && await store.has(hash)) data = await store.get(hash)
|
|
543
540
|
if (data) return data
|
|
544
541
|
|
|
545
|
-
return this.requestData(hash, store
|
|
542
|
+
return this.requestData(hash, store?.name ? store.name : store)
|
|
546
543
|
}
|
|
547
544
|
|
|
548
545
|
/**
|
package/src/utils/utils.js
CHANGED
|
@@ -16,10 +16,6 @@ export const expected = (expected, actual) => {
|
|
|
16
16
|
${entries.join('\n\t')}`;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export const debug = (log) => {
|
|
20
|
-
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;')
|
|
21
|
-
}
|
|
22
|
-
|
|
23
19
|
export const protoFor = (data) => {
|
|
24
20
|
if (!Buffer.isBuffer(data)) data = Buffer.from(data)
|
|
25
21
|
const codec = new Codec(data)
|
package/test.js
CHANGED
|
@@ -8,6 +8,7 @@ pubsub.subscribe('peer:connected', async peer => {
|
|
|
8
8
|
request:'lastBlock'
|
|
9
9
|
})
|
|
10
10
|
const to = peer.id
|
|
11
|
+
await peernet.data.put('hello', 'hi')
|
|
11
12
|
console.log(request);
|
|
12
13
|
const node = await peernet.prepareMessage(to, request.encoded)
|
|
13
14
|
console.log({node});
|
|
@@ -25,4 +26,10 @@ pubsub.subscribe('peer:connected', async peer => {
|
|
|
25
26
|
|
|
26
27
|
const block = new TextDecoder().decode(response.decoded.response)
|
|
27
28
|
console.log(block);
|
|
29
|
+
const task = () => setTimeout(() => {
|
|
30
|
+
console.log(peernet.connections[0]?.connected);
|
|
31
|
+
console.log(pubsub.subscribers);
|
|
32
|
+
task()
|
|
33
|
+
}, 5000);
|
|
34
|
+
task()
|
|
28
35
|
})
|