@leofcoin/peernet 1.2.10 → 1.2.12
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/exports/browser/{client-CwKuEJRO.js → client-DN_Y7L6h.js} +1 -1
- package/exports/browser/{messages-odneBhMk.js → messages-C-BwUsHC.js} +1 -1
- package/exports/browser/{peernet-CCZ65IdF.js → peernet-BpKdfmda.js} +74 -66
- package/exports/browser/peernet.js +1 -1
- package/exports/peernet.js +72 -64
- package/package.json +1 -1
- package/src/peernet.ts +68 -61
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-
|
|
1
|
+
import { L as LittlePubSub, d as deflate_1, i as inflate_1, c as createDebugger } from './peernet-BpKdfmda.js';
|
|
2
2
|
import './identity-CqSnKXWH.js';
|
|
3
3
|
import './value-C3vAp-wb.js';
|
|
4
4
|
|
|
@@ -8473,7 +8473,7 @@ class Peernet {
|
|
|
8473
8473
|
await getAddress();
|
|
8474
8474
|
this.storePrefix = options.storePrefix;
|
|
8475
8475
|
this.root = options.root;
|
|
8476
|
-
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-
|
|
8476
|
+
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile } = await import(/* webpackChunkName: "messages" */ './messages-C-BwUsHC.js');
|
|
8477
8477
|
/**
|
|
8478
8478
|
* proto Object containing protos
|
|
8479
8479
|
* @type {Object}
|
|
@@ -8520,7 +8520,7 @@ class Peernet {
|
|
|
8520
8520
|
if (this.#starting || this.#started)
|
|
8521
8521
|
return;
|
|
8522
8522
|
this.#starting = true;
|
|
8523
|
-
const importee = await import('./client-
|
|
8523
|
+
const importee = await import('./client-DN_Y7L6h.js');
|
|
8524
8524
|
/**
|
|
8525
8525
|
* @access public
|
|
8526
8526
|
* @type {PeernetClient}
|
|
@@ -8740,7 +8740,9 @@ class Peernet {
|
|
|
8740
8740
|
walks.push(walk(peer, peerId));
|
|
8741
8741
|
}
|
|
8742
8742
|
}
|
|
8743
|
-
return
|
|
8743
|
+
// walk the network and return first result.
|
|
8744
|
+
// not waiting for all walks to finish, this is faster and more efficient
|
|
8745
|
+
return Promise.any(walks);
|
|
8744
8746
|
}
|
|
8745
8747
|
/**
|
|
8746
8748
|
* Override DHT behavior, try's finding the content three times
|
|
@@ -8792,74 +8794,80 @@ class Peernet {
|
|
|
8792
8794
|
};
|
|
8793
8795
|
}
|
|
8794
8796
|
async requestData(hash, store) {
|
|
8795
|
-
|
|
8796
|
-
|
|
8797
|
-
|
|
8798
|
-
|
|
8799
|
-
|
|
8800
|
-
|
|
8801
|
-
|
|
8802
|
-
|
|
8803
|
-
closestPeer
|
|
8804
|
-
|
|
8805
|
-
|
|
8806
|
-
|
|
8807
|
-
|
|
8808
|
-
|
|
8809
|
-
|
|
8810
|
-
|
|
8811
|
-
|
|
8812
|
-
|
|
8813
|
-
|
|
8814
|
-
|
|
8815
|
-
|
|
8816
|
-
|
|
8817
|
-
|
|
8818
|
-
|
|
8819
|
-
|
|
8820
|
-
|
|
8821
|
-
|
|
8822
|
-
|
|
8823
|
-
|
|
8824
|
-
|
|
8825
|
-
|
|
8826
|
-
|
|
8827
|
-
|
|
8828
|
-
const
|
|
8829
|
-
|
|
8830
|
-
|
|
8797
|
+
try {
|
|
8798
|
+
const providers = await this.providersFor(hash);
|
|
8799
|
+
if (!providers || (providers && Object.keys(providers).length === 0))
|
|
8800
|
+
throw nothingFoundError(hash);
|
|
8801
|
+
debug(`found ${Object.keys(providers).length} provider(s) for ${hash}`);
|
|
8802
|
+
// get closest peer on earth
|
|
8803
|
+
let closestPeer = await this.dht.closestPeer(Object.values(providers));
|
|
8804
|
+
// fallback to first provider if no closest peer found
|
|
8805
|
+
if (!closestPeer || !closestPeer.id)
|
|
8806
|
+
closestPeer = Object.values(providers)[0];
|
|
8807
|
+
debug(`closest peer for ${hash} is ${closestPeer?.address}`);
|
|
8808
|
+
// get peer instance by id
|
|
8809
|
+
if (!closestPeer || !closestPeer.id || !closestPeer.address)
|
|
8810
|
+
return undefined;
|
|
8811
|
+
const id = closestPeer.id;
|
|
8812
|
+
const peer = this.connections[id];
|
|
8813
|
+
if (!peer || !peer?.connected) {
|
|
8814
|
+
this.dht.removeProvider(id, hash);
|
|
8815
|
+
return this.requestData(hash, store?.name || store);
|
|
8816
|
+
}
|
|
8817
|
+
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
8818
|
+
hash,
|
|
8819
|
+
store: store?.name || store
|
|
8820
|
+
});
|
|
8821
|
+
const node = await this.prepareMessage(data);
|
|
8822
|
+
if (peer?.connected) {
|
|
8823
|
+
try {
|
|
8824
|
+
if (peer)
|
|
8825
|
+
data = await peer.request(node.encoded);
|
|
8826
|
+
else {
|
|
8827
|
+
// fallback and try every provider found
|
|
8828
|
+
const promises = [];
|
|
8829
|
+
const providers = await this.providersFor(hash, store);
|
|
8830
|
+
for (const provider of Object.values(providers)) {
|
|
8831
|
+
const peer = this.connections[provider.id];
|
|
8832
|
+
if (peer)
|
|
8833
|
+
promises.push(peer.request(node.encoded));
|
|
8834
|
+
}
|
|
8835
|
+
data = await Promise.race(promises);
|
|
8831
8836
|
}
|
|
8832
|
-
|
|
8837
|
+
if (data)
|
|
8838
|
+
data = new Uint8Array(Object.values(data));
|
|
8839
|
+
if (!data || data.length === 0)
|
|
8840
|
+
throw nothingFoundError(hash);
|
|
8841
|
+
const proto = await protoFor(data);
|
|
8842
|
+
// TODO: store data automaticly or not
|
|
8843
|
+
return BufferToUint8Array(proto.decoded.data);
|
|
8833
8844
|
}
|
|
8834
|
-
|
|
8835
|
-
data
|
|
8836
|
-
|
|
8837
|
-
|
|
8838
|
-
|
|
8839
|
-
|
|
8840
|
-
|
|
8841
|
-
|
|
8842
|
-
|
|
8843
|
-
|
|
8844
|
-
|
|
8845
|
-
|
|
8846
|
-
this.#peerAttempts[id]
|
|
8847
|
-
|
|
8848
|
-
this.dht.removeProvider(id, hash);
|
|
8849
|
-
console.error(nothingFoundError(hash));
|
|
8850
|
-
return undefined;
|
|
8845
|
+
catch (error) {
|
|
8846
|
+
debug(`Error while requesting data from ${id}`, error);
|
|
8847
|
+
// if error, remove provider
|
|
8848
|
+
if (this.#peerAttempts[id] > 1) {
|
|
8849
|
+
this.#peerAttempts[id] = 0;
|
|
8850
|
+
debug(`Removed provider ${id} for ${hash} after 3 attempts`);
|
|
8851
|
+
this.dht.removeProvider(id, hash);
|
|
8852
|
+
console.error(nothingFoundError(hash));
|
|
8853
|
+
return undefined;
|
|
8854
|
+
}
|
|
8855
|
+
if (this.#peerAttempts[id] === undefined)
|
|
8856
|
+
this.#peerAttempts[id] = 0;
|
|
8857
|
+
this.#peerAttempts[id]++;
|
|
8858
|
+
return this.requestData(hash, store?.name || store);
|
|
8851
8859
|
}
|
|
8852
|
-
|
|
8853
|
-
|
|
8854
|
-
|
|
8855
|
-
|
|
8860
|
+
// this.put(hash, proto.decoded.data)
|
|
8861
|
+
}
|
|
8862
|
+
else {
|
|
8863
|
+
this.dht.removeProvider(id, hash);
|
|
8856
8864
|
}
|
|
8857
|
-
|
|
8865
|
+
return undefined;
|
|
8858
8866
|
}
|
|
8859
|
-
|
|
8860
|
-
|
|
8867
|
+
catch (error) {
|
|
8868
|
+
console.error(`Error while requesting data for ${hash} from the network`, error);
|
|
8869
|
+
return undefined;
|
|
8861
8870
|
}
|
|
8862
|
-
return;
|
|
8863
8871
|
}
|
|
8864
8872
|
get message() {
|
|
8865
8873
|
return {
|
|
@@ -8959,7 +8967,7 @@ class Peernet {
|
|
|
8959
8967
|
const data = await shareStore.has(hash);
|
|
8960
8968
|
if (data)
|
|
8961
8969
|
return await shareStore.get(hash);
|
|
8962
|
-
return
|
|
8970
|
+
return this.requestData(hash, 'share');
|
|
8963
8971
|
},
|
|
8964
8972
|
/**
|
|
8965
8973
|
* put share content
|
package/exports/peernet.js
CHANGED
|
@@ -718,7 +718,9 @@ class Peernet {
|
|
|
718
718
|
walks.push(walk(peer, peerId));
|
|
719
719
|
}
|
|
720
720
|
}
|
|
721
|
-
return
|
|
721
|
+
// walk the network and return first result.
|
|
722
|
+
// not waiting for all walks to finish, this is faster and more efficient
|
|
723
|
+
return Promise.any(walks);
|
|
722
724
|
}
|
|
723
725
|
/**
|
|
724
726
|
* Override DHT behavior, try's finding the content three times
|
|
@@ -770,74 +772,80 @@ class Peernet {
|
|
|
770
772
|
};
|
|
771
773
|
}
|
|
772
774
|
async requestData(hash, store) {
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
closestPeer
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
const
|
|
807
|
-
|
|
808
|
-
|
|
775
|
+
try {
|
|
776
|
+
const providers = await this.providersFor(hash);
|
|
777
|
+
if (!providers || (providers && Object.keys(providers).length === 0))
|
|
778
|
+
throw nothingFoundError(hash);
|
|
779
|
+
debug(`found ${Object.keys(providers).length} provider(s) for ${hash}`);
|
|
780
|
+
// get closest peer on earth
|
|
781
|
+
let closestPeer = await this.dht.closestPeer(Object.values(providers));
|
|
782
|
+
// fallback to first provider if no closest peer found
|
|
783
|
+
if (!closestPeer || !closestPeer.id)
|
|
784
|
+
closestPeer = Object.values(providers)[0];
|
|
785
|
+
debug(`closest peer for ${hash} is ${closestPeer?.address}`);
|
|
786
|
+
// get peer instance by id
|
|
787
|
+
if (!closestPeer || !closestPeer.id || !closestPeer.address)
|
|
788
|
+
return undefined;
|
|
789
|
+
const id = closestPeer.id;
|
|
790
|
+
const peer = this.connections[id];
|
|
791
|
+
if (!peer || !peer?.connected) {
|
|
792
|
+
this.dht.removeProvider(id, hash);
|
|
793
|
+
return this.requestData(hash, store?.name || store);
|
|
794
|
+
}
|
|
795
|
+
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
796
|
+
hash,
|
|
797
|
+
store: store?.name || store
|
|
798
|
+
});
|
|
799
|
+
const node = await this.prepareMessage(data);
|
|
800
|
+
if (peer?.connected) {
|
|
801
|
+
try {
|
|
802
|
+
if (peer)
|
|
803
|
+
data = await peer.request(node.encoded);
|
|
804
|
+
else {
|
|
805
|
+
// fallback and try every provider found
|
|
806
|
+
const promises = [];
|
|
807
|
+
const providers = await this.providersFor(hash, store);
|
|
808
|
+
for (const provider of Object.values(providers)) {
|
|
809
|
+
const peer = this.connections[provider.id];
|
|
810
|
+
if (peer)
|
|
811
|
+
promises.push(peer.request(node.encoded));
|
|
812
|
+
}
|
|
813
|
+
data = await Promise.race(promises);
|
|
809
814
|
}
|
|
810
|
-
|
|
815
|
+
if (data)
|
|
816
|
+
data = new Uint8Array(Object.values(data));
|
|
817
|
+
if (!data || data.length === 0)
|
|
818
|
+
throw nothingFoundError(hash);
|
|
819
|
+
const proto = await protoFor(data);
|
|
820
|
+
// TODO: store data automaticly or not
|
|
821
|
+
return BufferToUint8Array(proto.decoded.data);
|
|
811
822
|
}
|
|
812
|
-
|
|
813
|
-
data
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
this.#peerAttempts[id]
|
|
825
|
-
|
|
826
|
-
this.dht.removeProvider(id, hash);
|
|
827
|
-
console.error(nothingFoundError(hash));
|
|
828
|
-
return undefined;
|
|
823
|
+
catch (error) {
|
|
824
|
+
debug(`Error while requesting data from ${id}`, error);
|
|
825
|
+
// if error, remove provider
|
|
826
|
+
if (this.#peerAttempts[id] > 1) {
|
|
827
|
+
this.#peerAttempts[id] = 0;
|
|
828
|
+
debug(`Removed provider ${id} for ${hash} after 3 attempts`);
|
|
829
|
+
this.dht.removeProvider(id, hash);
|
|
830
|
+
console.error(nothingFoundError(hash));
|
|
831
|
+
return undefined;
|
|
832
|
+
}
|
|
833
|
+
if (this.#peerAttempts[id] === undefined)
|
|
834
|
+
this.#peerAttempts[id] = 0;
|
|
835
|
+
this.#peerAttempts[id]++;
|
|
836
|
+
return this.requestData(hash, store?.name || store);
|
|
829
837
|
}
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
838
|
+
// this.put(hash, proto.decoded.data)
|
|
839
|
+
}
|
|
840
|
+
else {
|
|
841
|
+
this.dht.removeProvider(id, hash);
|
|
834
842
|
}
|
|
835
|
-
|
|
843
|
+
return undefined;
|
|
836
844
|
}
|
|
837
|
-
|
|
838
|
-
|
|
845
|
+
catch (error) {
|
|
846
|
+
console.error(`Error while requesting data for ${hash} from the network`, error);
|
|
847
|
+
return undefined;
|
|
839
848
|
}
|
|
840
|
-
return;
|
|
841
849
|
}
|
|
842
850
|
get message() {
|
|
843
851
|
return {
|
|
@@ -937,7 +945,7 @@ class Peernet {
|
|
|
937
945
|
const data = await shareStore.has(hash);
|
|
938
946
|
if (data)
|
|
939
947
|
return await shareStore.get(hash);
|
|
940
|
-
return
|
|
948
|
+
return this.requestData(hash, 'share');
|
|
941
949
|
},
|
|
942
950
|
/**
|
|
943
951
|
* put share content
|
package/package.json
CHANGED
package/src/peernet.ts
CHANGED
|
@@ -505,7 +505,9 @@ export default class Peernet {
|
|
|
505
505
|
walks.push(walk(peer, peerId))
|
|
506
506
|
}
|
|
507
507
|
}
|
|
508
|
-
return
|
|
508
|
+
// walk the network and return first result.
|
|
509
|
+
// not waiting for all walks to finish, this is faster and more efficient
|
|
510
|
+
return Promise.any(walks)
|
|
509
511
|
}
|
|
510
512
|
|
|
511
513
|
/**
|
|
@@ -557,73 +559,78 @@ export default class Peernet {
|
|
|
557
559
|
}
|
|
558
560
|
|
|
559
561
|
async requestData(hash, store) {
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
562
|
+
try {
|
|
563
|
+
const providers = await this.providersFor(hash)
|
|
564
|
+
if (!providers || (providers && Object.keys(providers).length === 0)) throw nothingFoundError(hash)
|
|
565
|
+
debug(`found ${Object.keys(providers).length} provider(s) for ${hash}`)
|
|
566
|
+
// get closest peer on earth
|
|
567
|
+
let closestPeer: DHTProvider = await this.dht.closestPeer(Object.values(providers))
|
|
568
|
+
// fallback to first provider if no closest peer found
|
|
569
|
+
if (!closestPeer || !closestPeer.id) closestPeer = Object.values(providers)[0]
|
|
570
|
+
|
|
571
|
+
debug(`closest peer for ${hash} is ${closestPeer?.address}`)
|
|
572
|
+
// get peer instance by id
|
|
573
|
+
if (!closestPeer || !closestPeer.id || !closestPeer.address) return undefined
|
|
574
|
+
const id = closestPeer.id
|
|
575
|
+
const peer = this.connections[id]
|
|
576
|
+
|
|
577
|
+
if (!peer || !peer?.connected) {
|
|
578
|
+
this.dht.removeProvider(id, hash)
|
|
579
|
+
return this.requestData(hash, store?.name || store)
|
|
580
|
+
}
|
|
578
581
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
582
|
+
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
583
|
+
hash,
|
|
584
|
+
store: store?.name || store
|
|
585
|
+
})
|
|
583
586
|
|
|
584
|
-
|
|
587
|
+
const node = await this.prepareMessage(data)
|
|
585
588
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
589
|
+
if (peer?.connected) {
|
|
590
|
+
try {
|
|
591
|
+
if (peer) data = await peer.request(node.encoded)
|
|
592
|
+
else {
|
|
593
|
+
// fallback and try every provider found
|
|
594
|
+
const promises = []
|
|
595
|
+
const providers = await this.providersFor(hash, store)
|
|
596
|
+
for (const provider of Object.values(providers)) {
|
|
597
|
+
const peer = this.connections[provider.id]
|
|
598
|
+
|
|
599
|
+
if (peer) promises.push(peer.request(node.encoded))
|
|
600
|
+
}
|
|
601
|
+
data = await Promise.race(promises)
|
|
597
602
|
}
|
|
598
|
-
data =
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
603
|
+
if (data) data = new Uint8Array(Object.values(data))
|
|
604
|
+
if (!data || data.length === 0) throw nothingFoundError(hash)
|
|
605
|
+
const proto = await protoFor(data)
|
|
606
|
+
// TODO: store data automaticly or not
|
|
607
|
+
return BufferToUint8Array(proto.decoded.data)
|
|
608
|
+
} catch (error) {
|
|
609
|
+
debug(`Error while requesting data from ${id}`, error)
|
|
610
|
+
// if error, remove provider
|
|
611
|
+
if (this.#peerAttempts[id] > 1) {
|
|
612
|
+
this.#peerAttempts[id] = 0
|
|
613
|
+
debug(`Removed provider ${id} for ${hash} after 3 attempts`)
|
|
614
|
+
|
|
615
|
+
this.dht.removeProvider(id, hash)
|
|
616
|
+
console.error(nothingFoundError(hash))
|
|
617
|
+
return undefined
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
if (this.#peerAttempts[id] === undefined) this.#peerAttempts[id] = 0
|
|
621
|
+
this.#peerAttempts[id]++
|
|
622
|
+
return this.requestData(hash, store?.name || store)
|
|
615
623
|
}
|
|
616
624
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
625
|
+
// this.put(hash, proto.decoded.data)
|
|
626
|
+
} else {
|
|
627
|
+
this.dht.removeProvider(id, hash)
|
|
620
628
|
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
629
|
+
return undefined
|
|
630
|
+
} catch (error) {
|
|
631
|
+
console.error(`Error while requesting data for ${hash} from the network`, error)
|
|
632
|
+
return undefined
|
|
625
633
|
}
|
|
626
|
-
return
|
|
627
634
|
}
|
|
628
635
|
|
|
629
636
|
get message() {
|
|
@@ -723,7 +730,7 @@ export default class Peernet {
|
|
|
723
730
|
debug(`get share ${hash}`)
|
|
724
731
|
const data = await shareStore.has(hash)
|
|
725
732
|
if (data) return await shareStore.get(hash)
|
|
726
|
-
return
|
|
733
|
+
return this.requestData(hash, 'share')
|
|
727
734
|
},
|
|
728
735
|
/**
|
|
729
736
|
* put share content
|