@leofcoin/peernet 1.1.86 → 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-CIHG3A1i.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-CIHG3A1i.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-CT983-_h.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-Db0RyH-f.js');
8468
+ const importee = await import('./client-D7CuvpPv.js');
8469
8469
  /**
8470
8470
  * @access public
8471
8471
  * @type {PeernetClient}
@@ -8506,6 +8506,11 @@ class Peernet {
8506
8506
  };
8507
8507
  peer.once('error', onError);
8508
8508
  peer.once('connect', async () => {
8509
+ if (!peer.connected) {
8510
+ peer.removeListener('error', onError);
8511
+ debug('Peer not connected');
8512
+ return;
8513
+ }
8509
8514
  await peer.send(data, id);
8510
8515
  this.bw.up += data.length;
8511
8516
  peer.removeListener('error', onError);
@@ -8601,21 +8606,26 @@ class Peernet {
8601
8606
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
8602
8607
  const walk = async (peer, peerId) => {
8603
8608
  const node = await this.prepareMessage(data);
8604
- let result = await peer.request(node.encoded);
8605
- result = new Uint8Array(Object.values(result));
8606
- const proto = await protoFor(result);
8607
- if (proto.name !== 'peernet-dht-response')
8608
- throw dhtError(proto.name);
8609
- const peerInfo = {
8610
- ...peer.connectionStats,
8611
- id: peerId
8612
- };
8613
- if (proto.decoded.has)
8614
- 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
+ }
8615
8625
  };
8616
8626
  let walks = [];
8617
8627
  for (const [peerId, peer] of Object.entries(this.connections)) {
8618
- if (peerId !== this.id) {
8628
+ if (peerId !== this.id && peer.connected) {
8619
8629
  walks.push(walk(peer, peerId));
8620
8630
  }
8621
8631
  }
@@ -8688,24 +8698,35 @@ class Peernet {
8688
8698
  store: store?.name || store
8689
8699
  });
8690
8700
  const node = await this.prepareMessage(data);
8691
- if (peer)
8692
- data = await peer.request(node.encoded);
8693
- else {
8694
- // fallback and try every provider found
8695
- const promises = [];
8696
- const providers = await this.providersFor(hash, store);
8697
- for (const provider of Object.values(providers)) {
8698
- const peer = this.connections[provider.id];
8699
- if (peer)
8700
- 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);
8701
8714
  }
8702
- 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);
8703
8729
  }
8704
- if (data)
8705
- data = new Uint8Array(Object.values(data));
8706
- const proto = await protoFor(data);
8707
- // TODO: store data automaticly or not
8708
- return BufferToUint8Array(proto.decoded.data);
8709
8730
  // this.put(hash, proto.decoded.data)
8710
8731
  }
8711
8732
  else {
@@ -1,3 +1,3 @@
1
- export { P as default } from './peernet-CIHG3A1i.js';
1
+ export { P as default } from './peernet-CZhvkXXp.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
@@ -587,6 +587,11 @@ class Peernet {
587
587
  };
588
588
  peer.once('error', onError);
589
589
  peer.once('connect', async () => {
590
+ if (!peer.connected) {
591
+ peer.removeListener('error', onError);
592
+ debug('Peer not connected');
593
+ return;
594
+ }
590
595
  await peer.send(data, id);
591
596
  this.bw.up += data.length;
592
597
  peer.removeListener('error', onError);
@@ -682,21 +687,26 @@ class Peernet {
682
687
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
683
688
  const walk = async (peer, peerId) => {
684
689
  const node = await this.prepareMessage(data);
685
- let result = await peer.request(node.encoded);
686
- result = new Uint8Array(Object.values(result));
687
- const proto = await protoFor(result);
688
- if (proto.name !== 'peernet-dht-response')
689
- throw dhtError(proto.name);
690
- const peerInfo = {
691
- ...peer.connectionStats,
692
- id: peerId
693
- };
694
- if (proto.decoded.has)
695
- 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
+ }
696
706
  };
697
707
  let walks = [];
698
708
  for (const [peerId, peer] of Object.entries(this.connections)) {
699
- if (peerId !== this.id) {
709
+ if (peerId !== this.id && peer.connected) {
700
710
  walks.push(walk(peer, peerId));
701
711
  }
702
712
  }
@@ -769,24 +779,35 @@ class Peernet {
769
779
  store: store?.name || store
770
780
  });
771
781
  const node = await this.prepareMessage(data);
772
- if (peer)
773
- data = await peer.request(node.encoded);
774
- else {
775
- // fallback and try every provider found
776
- const promises = [];
777
- const providers = await this.providersFor(hash, store);
778
- for (const provider of Object.values(providers)) {
779
- const peer = this.connections[provider.id];
780
- if (peer)
781
- 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);
782
795
  }
783
- 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);
784
810
  }
785
- if (data)
786
- data = new Uint8Array(Object.values(data));
787
- const proto = await protoFor(data);
788
- // TODO: store data automaticly or not
789
- return BufferToUint8Array(proto.decoded.data);
790
811
  // this.put(hash, proto.decoded.data)
791
812
  }
792
813
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.1.86",
3
+ "version": "1.1.88",
4
4
  "description": "",
5
5
  "browser": "./exports/browser/peernet.js",
6
6
  "exports": {
@@ -37,7 +37,7 @@
37
37
  "@leofcoin/identity-utils": "^1.0.2",
38
38
  "@leofcoin/multi-wallet": "^3.1.8",
39
39
  "@leofcoin/storage": "^3.5.38",
40
- "@netpeer/swarm": "^0.8.19",
40
+ "@netpeer/swarm": "^0.8.20",
41
41
  "@vandeurenglenn/base32": "^1.2.4",
42
42
  "@vandeurenglenn/base58": "^1.1.9",
43
43
  "@vandeurenglenn/debug": "^1.2.6",
package/src/peernet.ts CHANGED
@@ -358,6 +358,11 @@ export default class Peernet {
358
358
  }
359
359
  peer.once('error', onError)
360
360
  peer.once('connect', async () => {
361
+ if (!peer.connected) {
362
+ peer.removeListener('error', onError)
363
+ debug('Peer not connected')
364
+ return
365
+ }
361
366
  await peer.send(data, id)
362
367
  this.bw.up += data.length
363
368
  peer.removeListener('error', onError)
@@ -462,21 +467,25 @@ export default class Peernet {
462
467
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash })
463
468
  const walk = async (peer, peerId) => {
464
469
  const node = await this.prepareMessage(data)
465
- let result = await peer.request(node.encoded)
466
- result = new Uint8Array(Object.values(result))
467
- const proto = await protoFor(result)
468
- if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
469
-
470
- const peerInfo = {
471
- ...peer.connectionStats,
472
- id: peerId
473
- }
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
+ }
474
480
 
475
- 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
+ }
476
485
  }
477
486
  let walks = []
478
487
  for (const [peerId, peer] of Object.entries(this.connections)) {
479
- if (peerId !== this.id) {
488
+ if (peerId !== this.id && peer.connected) {
480
489
  walks.push(walk(peer, peerId))
481
490
  }
482
491
  }
@@ -552,23 +561,31 @@ export default class Peernet {
552
561
 
553
562
  const node = await this.prepareMessage(data)
554
563
 
555
- if (peer) data = await peer.request(node.encoded)
556
- else {
557
- // fallback and try every provider found
558
- const promises = []
559
- const providers = await this.providersFor(hash, store)
560
- for (const provider of Object.values(providers)) {
561
- const peer = this.connections[provider.id]
562
-
563
- if (peer) promises.push(peer.request(node.encoded))
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]
572
+
573
+ if (peer) promises.push(peer.request(node.encoded))
574
+ }
575
+ data = await Promise.race(promises)
564
576
  }
565
- 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)
566
588
  }
567
- if (data) data = new Uint8Array(Object.values(data))
568
-
569
- const proto = await protoFor(data)
570
- // TODO: store data automaticly or not
571
- return BufferToUint8Array(proto.decoded.data)
572
589
 
573
590
  // this.put(hash, proto.decoded.data)
574
591
  } else {