@leofcoin/peernet 1.1.87 → 1.1.88

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.
@@ -1,4 +1,4 @@
1
- import { L as LittlePubSub } from './peernet-P6qb1z0Q.js';
1
+ import { L as LittlePubSub } from './peernet-CZhvkXXp.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
4
4
 
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './peernet-P6qb1z0Q.js';
1
+ import { F as FormatInterface } from './peernet-CZhvkXXp.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
4
4
 
@@ -8371,7 +8371,7 @@ class Peernet {
8371
8371
  this.root = options.root;
8372
8372
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
8373
8373
  // FolderMessageResponse
8374
- } = await import(/* webpackChunkName: "messages" */ './messages-BkkXT6WO.js');
8374
+ } = await import(/* webpackChunkName: "messages" */ './messages-D9OyBKBu.js');
8375
8375
  /**
8376
8376
  * proto Object containing protos
8377
8377
  * @type {Object}
@@ -8465,7 +8465,7 @@ class Peernet {
8465
8465
  if (this.#starting || this.#started)
8466
8466
  return;
8467
8467
  this.#starting = true;
8468
- const importee = await import('./client-MAPwcc7G.js');
8468
+ const importee = await import('./client-D7CuvpPv.js');
8469
8469
  /**
8470
8470
  * @access public
8471
8471
  * @type {PeernetClient}
@@ -8606,17 +8606,22 @@ class Peernet {
8606
8606
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
8607
8607
  const walk = async (peer, peerId) => {
8608
8608
  const node = await this.prepareMessage(data);
8609
- let result = await peer.request(node.encoded);
8610
- result = new Uint8Array(Object.values(result));
8611
- const proto = await protoFor(result);
8612
- if (proto.name !== 'peernet-dht-response')
8613
- throw dhtError(proto.name);
8614
- const peerInfo = {
8615
- ...peer.connectionStats,
8616
- id: peerId
8617
- };
8618
- if (proto.decoded.has)
8619
- this.dht.addProvider(peerInfo, proto.decoded.hash);
8609
+ try {
8610
+ let result = await peer.request(node.encoded);
8611
+ result = new Uint8Array(Object.values(result));
8612
+ const proto = await protoFor(result);
8613
+ if (proto.name !== 'peernet-dht-response')
8614
+ throw dhtError(proto.name);
8615
+ const peerInfo = {
8616
+ ...peer.connectionStats,
8617
+ id: peerId
8618
+ };
8619
+ if (proto.decoded.has)
8620
+ this.dht.addProvider(peerInfo, proto.decoded.hash);
8621
+ }
8622
+ catch (error) {
8623
+ console.error(`Error while walking ${peerId}`, error);
8624
+ }
8620
8625
  };
8621
8626
  let walks = [];
8622
8627
  for (const [peerId, peer] of Object.entries(this.connections)) {
@@ -8693,24 +8698,35 @@ class Peernet {
8693
8698
  store: store?.name || store
8694
8699
  });
8695
8700
  const node = await this.prepareMessage(data);
8696
- if (peer)
8697
- data = await peer.request(node.encoded);
8698
- else {
8699
- // fallback and try every provider found
8700
- const promises = [];
8701
- const providers = await this.providersFor(hash, store);
8702
- for (const provider of Object.values(providers)) {
8703
- const peer = this.connections[provider.id];
8704
- if (peer)
8705
- promises.push(peer.request(node.encoded));
8701
+ try {
8702
+ if (peer)
8703
+ data = await peer.request(node.encoded);
8704
+ else {
8705
+ // fallback and try every provider found
8706
+ const promises = [];
8707
+ const providers = await this.providersFor(hash, store);
8708
+ for (const provider of Object.values(providers)) {
8709
+ const peer = this.connections[provider.id];
8710
+ if (peer)
8711
+ promises.push(peer.request(node.encoded));
8712
+ }
8713
+ data = await Promise.race(promises);
8706
8714
  }
8707
- data = await Promise.race(promises);
8715
+ if (data)
8716
+ data = new Uint8Array(Object.values(data));
8717
+ if (!data || data.length === 0)
8718
+ throw nothingFoundError(hash);
8719
+ const proto = await protoFor(data);
8720
+ // TODO: store data automaticly or not
8721
+ return BufferToUint8Array(proto.decoded.data);
8722
+ }
8723
+ catch (error) {
8724
+ debug(`Error while requesting data from ${id}`, error);
8725
+ // if error, remove provider
8726
+ this.dht.removeProvider(id, hash);
8727
+ // and try again
8728
+ return this.requestData(hash, store?.name || store);
8708
8729
  }
8709
- if (data)
8710
- data = new Uint8Array(Object.values(data));
8711
- const proto = await protoFor(data);
8712
- // TODO: store data automaticly or not
8713
- return BufferToUint8Array(proto.decoded.data);
8714
8730
  // this.put(hash, proto.decoded.data)
8715
8731
  }
8716
8732
  else {
@@ -1,3 +1,3 @@
1
- export { P as default } from './peernet-P6qb1z0Q.js';
1
+ export { P as default } from './peernet-CZhvkXXp.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
@@ -687,17 +687,22 @@ class Peernet {
687
687
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
688
688
  const walk = async (peer, peerId) => {
689
689
  const node = await this.prepareMessage(data);
690
- let result = await peer.request(node.encoded);
691
- result = new Uint8Array(Object.values(result));
692
- const proto = await protoFor(result);
693
- if (proto.name !== 'peernet-dht-response')
694
- throw dhtError(proto.name);
695
- const peerInfo = {
696
- ...peer.connectionStats,
697
- id: peerId
698
- };
699
- if (proto.decoded.has)
700
- this.dht.addProvider(peerInfo, proto.decoded.hash);
690
+ try {
691
+ let result = await peer.request(node.encoded);
692
+ result = new Uint8Array(Object.values(result));
693
+ const proto = await protoFor(result);
694
+ if (proto.name !== 'peernet-dht-response')
695
+ throw dhtError(proto.name);
696
+ const peerInfo = {
697
+ ...peer.connectionStats,
698
+ id: peerId
699
+ };
700
+ if (proto.decoded.has)
701
+ this.dht.addProvider(peerInfo, proto.decoded.hash);
702
+ }
703
+ catch (error) {
704
+ console.error(`Error while walking ${peerId}`, error);
705
+ }
701
706
  };
702
707
  let walks = [];
703
708
  for (const [peerId, peer] of Object.entries(this.connections)) {
@@ -774,24 +779,35 @@ class Peernet {
774
779
  store: store?.name || store
775
780
  });
776
781
  const node = await this.prepareMessage(data);
777
- if (peer)
778
- data = await peer.request(node.encoded);
779
- else {
780
- // fallback and try every provider found
781
- const promises = [];
782
- const providers = await this.providersFor(hash, store);
783
- for (const provider of Object.values(providers)) {
784
- const peer = this.connections[provider.id];
785
- if (peer)
786
- promises.push(peer.request(node.encoded));
782
+ try {
783
+ if (peer)
784
+ data = await peer.request(node.encoded);
785
+ else {
786
+ // fallback and try every provider found
787
+ const promises = [];
788
+ const providers = await this.providersFor(hash, store);
789
+ for (const provider of Object.values(providers)) {
790
+ const peer = this.connections[provider.id];
791
+ if (peer)
792
+ promises.push(peer.request(node.encoded));
793
+ }
794
+ data = await Promise.race(promises);
787
795
  }
788
- data = await Promise.race(promises);
796
+ if (data)
797
+ data = new Uint8Array(Object.values(data));
798
+ if (!data || data.length === 0)
799
+ throw nothingFoundError(hash);
800
+ const proto = await protoFor(data);
801
+ // TODO: store data automaticly or not
802
+ return BufferToUint8Array(proto.decoded.data);
803
+ }
804
+ catch (error) {
805
+ debug(`Error while requesting data from ${id}`, error);
806
+ // if error, remove provider
807
+ this.dht.removeProvider(id, hash);
808
+ // and try again
809
+ return this.requestData(hash, store?.name || store);
789
810
  }
790
- if (data)
791
- data = new Uint8Array(Object.values(data));
792
- const proto = await protoFor(data);
793
- // TODO: store data automaticly or not
794
- return BufferToUint8Array(proto.decoded.data);
795
811
  // this.put(hash, proto.decoded.data)
796
812
  }
797
813
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.1.87",
3
+ "version": "1.1.88",
4
4
  "description": "",
5
5
  "browser": "./exports/browser/peernet.js",
6
6
  "exports": {
package/src/peernet.ts CHANGED
@@ -467,17 +467,21 @@ export default class Peernet {
467
467
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash })
468
468
  const walk = async (peer, peerId) => {
469
469
  const node = await this.prepareMessage(data)
470
- let result = await peer.request(node.encoded)
471
- result = new Uint8Array(Object.values(result))
472
- const proto = await protoFor(result)
473
- if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
474
-
475
- const peerInfo = {
476
- ...peer.connectionStats,
477
- id: peerId
478
- }
470
+ try {
471
+ let result = await peer.request(node.encoded)
472
+ result = new Uint8Array(Object.values(result))
473
+ const proto = await protoFor(result)
474
+ if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
475
+
476
+ const peerInfo = {
477
+ ...peer.connectionStats,
478
+ id: peerId
479
+ }
479
480
 
480
- if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
481
+ if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
482
+ } catch (error) {
483
+ console.error(`Error while walking ${peerId}`, error)
484
+ }
481
485
  }
482
486
  let walks = []
483
487
  for (const [peerId, peer] of Object.entries(this.connections)) {
@@ -557,23 +561,31 @@ export default class Peernet {
557
561
 
558
562
  const node = await this.prepareMessage(data)
559
563
 
560
- if (peer) data = await peer.request(node.encoded)
561
- else {
562
- // fallback and try every provider found
563
- const promises = []
564
- const providers = await this.providersFor(hash, store)
565
- for (const provider of Object.values(providers)) {
566
- const peer = this.connections[provider.id]
564
+ try {
565
+ if (peer) data = await peer.request(node.encoded)
566
+ else {
567
+ // fallback and try every provider found
568
+ const promises = []
569
+ const providers = await this.providersFor(hash, store)
570
+ for (const provider of Object.values(providers)) {
571
+ const peer = this.connections[provider.id]
567
572
 
568
- if (peer) promises.push(peer.request(node.encoded))
573
+ if (peer) promises.push(peer.request(node.encoded))
574
+ }
575
+ data = await Promise.race(promises)
569
576
  }
570
- data = await Promise.race(promises)
577
+ if (data) data = new Uint8Array(Object.values(data))
578
+ if (!data || data.length === 0) throw nothingFoundError(hash)
579
+ const proto = await protoFor(data)
580
+ // TODO: store data automaticly or not
581
+ return BufferToUint8Array(proto.decoded.data)
582
+ } catch (error) {
583
+ debug(`Error while requesting data from ${id}`, error)
584
+ // if error, remove provider
585
+ this.dht.removeProvider(id, hash)
586
+ // and try again
587
+ return this.requestData(hash, store?.name || store)
571
588
  }
572
- if (data) data = new Uint8Array(Object.values(data))
573
-
574
- const proto = await protoFor(data)
575
- // TODO: store data automaticly or not
576
- return BufferToUint8Array(proto.decoded.data)
577
589
 
578
590
  // this.put(hash, proto.decoded.data)
579
591
  } else {