@leofcoin/peernet 1.1.94 → 1.1.96

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-CG0sdKM4.js';
1
+ import { L as LittlePubSub } from './peernet-BXonj_o8.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
4
4
 
@@ -16,12 +16,9 @@ declare type Coordinates = {
16
16
  longitude: number;
17
17
  latitude: number;
18
18
  };
19
+ export declare const getAddress: () => Promise<any>;
19
20
  export default class DhtEarth {
20
21
  providerMap: Map<string, DHTProviderMapValue>;
21
- /**
22
- *
23
- */
24
- constructor();
25
22
  getCoordinates(address: string): Promise<Coordinates>;
26
23
  /**
27
24
  * @param {Object} peer
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './peernet-CG0sdKM4.js';
1
+ import { F as FormatInterface } from './peernet-BXonj_o8.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
4
4
 
@@ -7991,12 +7991,6 @@ const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
7991
7991
  };
7992
7992
  class DhtEarth {
7993
7993
  providerMap = new Map();
7994
- /**
7995
- *
7996
- */
7997
- constructor() {
7998
- this.providerMap = new Map();
7999
- }
8000
7994
  async getCoordinates(address) {
8001
7995
  if (!fetchedCoordinates[address]) {
8002
7996
  const request = `https://whereis.leofcoin.org/?ip=${address}`;
@@ -8368,11 +8362,12 @@ class Peernet {
8368
8362
  * @return {Promise} instance of Peernet
8369
8363
  */
8370
8364
  async _init(options, password) {
8365
+ await getAddress();
8371
8366
  this.storePrefix = options.storePrefix;
8372
8367
  this.root = options.root;
8373
8368
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
8374
8369
  // FolderMessageResponse
8375
- } = await import(/* webpackChunkName: "messages" */ './messages-CaxCm2Mx.js');
8370
+ } = await import(/* webpackChunkName: "messages" */ './messages-Y4l0mXGt.js');
8376
8371
  /**
8377
8372
  * proto Object containing protos
8378
8373
  * @type {Object}
@@ -8466,7 +8461,7 @@ class Peernet {
8466
8461
  if (this.#starting || this.#started)
8467
8462
  return;
8468
8463
  this.#starting = true;
8469
- const importee = await import('./client-BmW52yUu.js');
8464
+ const importee = await import('./client-BlYwNU8G.js');
8470
8465
  /**
8471
8466
  * @access public
8472
8467
  * @type {PeernetClient}
@@ -8614,7 +8609,7 @@ class Peernet {
8614
8609
  if (proto.name !== 'peernet-dht-response')
8615
8610
  throw dhtError(proto.name);
8616
8611
  const peerInfo = {
8617
- ...peer.connectionStats,
8612
+ address: peer.remoteAddress,
8618
8613
  id: peerId
8619
8614
  };
8620
8615
  if (proto.decoded.has)
@@ -8688,18 +8683,26 @@ class Peernet {
8688
8683
  throw nothingFoundError(hash);
8689
8684
  debug(`found ${Object.keys(providers).length} provider(s) for ${hash}`);
8690
8685
  // get closest peer on earth
8691
- const closestPeer = Object.values(providers)[0];
8686
+ let closestPeer = await this.dht.closestPeer(Object.values(providers));
8687
+ // fallback to first provider if no closest peer found
8688
+ if (!closestPeer || !closestPeer.id)
8689
+ closestPeer = Object.values(providers)[0];
8690
+ debug(`closest peer for ${hash} is ${closestPeer?.address}`);
8692
8691
  // get peer instance by id
8693
8692
  if (!closestPeer || !closestPeer.id)
8694
8693
  return this.requestData(hash, store?.name || store);
8695
8694
  const id = closestPeer.id;
8696
8695
  const peer = this.connections[id];
8696
+ if (!peer || !peer?.connected) {
8697
+ this.dht.removeProvider(id, hash);
8698
+ return this.requestData(hash, store?.name || store);
8699
+ }
8700
+ let data = await new globalThis.peernet.protos['peernet-data']({
8701
+ hash,
8702
+ store: store?.name || store
8703
+ });
8704
+ const node = await this.prepareMessage(data);
8697
8705
  if (peer?.connected) {
8698
- let data = await new globalThis.peernet.protos['peernet-data']({
8699
- hash,
8700
- store: store?.name || store
8701
- });
8702
- const node = await this.prepareMessage(data);
8703
8706
  try {
8704
8707
  if (peer)
8705
8708
  data = await peer.request(node.encoded);
@@ -8725,10 +8728,10 @@ class Peernet {
8725
8728
  catch (error) {
8726
8729
  debug(`Error while requesting data from ${id}`, error);
8727
8730
  // if error, remove provider
8728
- this.dht.removeProvider(id, hash);
8729
8731
  if (this.#peerAttempts[id] > 1) {
8730
8732
  this.#peerAttempts[id] = 0;
8731
8733
  debug(`Removed provider ${id} for ${hash} after 3 attempts`);
8734
+ this.dht.removeProvider(id, hash);
8732
8735
  console.error(nothingFoundError(hash));
8733
8736
  return undefined;
8734
8737
  }
@@ -1,3 +1,3 @@
1
- export { P as default } from './peernet-CG0sdKM4.js';
1
+ export { P as default } from './peernet-BXonj_o8.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
@@ -182,12 +182,6 @@ const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
182
182
  };
183
183
  class DhtEarth {
184
184
  providerMap = new Map();
185
- /**
186
- *
187
- */
188
- constructor() {
189
- this.providerMap = new Map();
190
- }
191
185
  async getCoordinates(address) {
192
186
  if (!fetchedCoordinates[address]) {
193
187
  const request = `https://whereis.leofcoin.org/?ip=${address}`;
@@ -449,6 +443,7 @@ class Peernet {
449
443
  * @return {Promise} instance of Peernet
450
444
  */
451
445
  async _init(options, password) {
446
+ await getAddress();
452
447
  this.storePrefix = options.storePrefix;
453
448
  this.root = options.root;
454
449
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
@@ -695,7 +690,7 @@ class Peernet {
695
690
  if (proto.name !== 'peernet-dht-response')
696
691
  throw dhtError(proto.name);
697
692
  const peerInfo = {
698
- ...peer.connectionStats,
693
+ address: peer.remoteAddress,
699
694
  id: peerId
700
695
  };
701
696
  if (proto.decoded.has)
@@ -769,18 +764,26 @@ class Peernet {
769
764
  throw nothingFoundError(hash);
770
765
  debug(`found ${Object.keys(providers).length} provider(s) for ${hash}`);
771
766
  // get closest peer on earth
772
- const closestPeer = Object.values(providers)[0];
767
+ let closestPeer = await this.dht.closestPeer(Object.values(providers));
768
+ // fallback to first provider if no closest peer found
769
+ if (!closestPeer || !closestPeer.id)
770
+ closestPeer = Object.values(providers)[0];
771
+ debug(`closest peer for ${hash} is ${closestPeer?.address}`);
773
772
  // get peer instance by id
774
773
  if (!closestPeer || !closestPeer.id)
775
774
  return this.requestData(hash, store?.name || store);
776
775
  const id = closestPeer.id;
777
776
  const peer = this.connections[id];
777
+ if (!peer || !peer?.connected) {
778
+ this.dht.removeProvider(id, hash);
779
+ return this.requestData(hash, store?.name || store);
780
+ }
781
+ let data = await new globalThis.peernet.protos['peernet-data']({
782
+ hash,
783
+ store: store?.name || store
784
+ });
785
+ const node = await this.prepareMessage(data);
778
786
  if (peer?.connected) {
779
- let data = await new globalThis.peernet.protos['peernet-data']({
780
- hash,
781
- store: store?.name || store
782
- });
783
- const node = await this.prepareMessage(data);
784
787
  try {
785
788
  if (peer)
786
789
  data = await peer.request(node.encoded);
@@ -806,10 +809,10 @@ class Peernet {
806
809
  catch (error) {
807
810
  debug(`Error while requesting data from ${id}`, error);
808
811
  // if error, remove provider
809
- this.dht.removeProvider(id, hash);
810
812
  if (this.#peerAttempts[id] > 1) {
811
813
  this.#peerAttempts[id] = 0;
812
814
  debug(`Removed provider ${id} for ${hash} after 3 attempts`);
815
+ this.dht.removeProvider(id, hash);
813
816
  console.error(nothingFoundError(hash));
814
817
  return undefined;
815
818
  }
@@ -16,12 +16,9 @@ declare type Coordinates = {
16
16
  longitude: number;
17
17
  latitude: number;
18
18
  };
19
+ export declare const getAddress: () => Promise<any>;
19
20
  export default class DhtEarth {
20
21
  providerMap: Map<string, DHTProviderMapValue>;
21
- /**
22
- *
23
- */
24
- constructor();
25
22
  getCoordinates(address: string): Promise<Coordinates>;
26
23
  /**
27
24
  * @param {Object} peer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.1.94",
3
+ "version": "1.1.96",
4
4
  "description": "",
5
5
  "browser": "./exports/browser/peernet.js",
6
6
  "exports": {
package/src/dht/dht.ts CHANGED
@@ -36,7 +36,7 @@ const lastFetched = {
36
36
 
37
37
  const fetchedCoordinates = {}
38
38
 
39
- const getAddress = async () => {
39
+ export const getAddress = async () => {
40
40
  const { address } = lastFetched
41
41
  if (address) {
42
42
  address.value = await fetch('https://icanhazip.com/')
@@ -67,14 +67,7 @@ const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
67
67
  }
68
68
 
69
69
  export default class DhtEarth {
70
- providerMap = new Map<string, DHTProviderMapValue>()
71
-
72
- /**
73
- *
74
- */
75
- constructor() {
76
- this.providerMap = new Map()
77
- }
70
+ providerMap: Map<string, DHTProviderMapValue> = new Map()
78
71
 
79
72
  async getCoordinates(address: string): Promise<Coordinates> {
80
73
  if (!fetchedCoordinates[address]) {
package/src/peernet.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import '@vandeurenglenn/debug'
2
2
  import PubSub from '@vandeurenglenn/little-pubsub'
3
3
  import PeerDiscovery from './discovery/peer-discovery.js'
4
- import DHT, { DHTProvider, DHTProviderDistanceResult } from './dht/dht.js'
4
+ import DHT, { DHTProvider, DHTProviderDistanceResult, getAddress } from './dht/dht.js'
5
5
  import { BufferToUint8Array, protoFor, target } from './utils/utils.js'
6
6
  import MessageHandler from './handlers/message.js'
7
7
  import dataHandler from './handlers/data.js'
@@ -188,6 +188,7 @@ export default class Peernet {
188
188
  * @return {Promise} instance of Peernet
189
189
  */
190
190
  async _init(options: { storePrefix?: string; root?: string }, password: string): Promise<Peernet> {
191
+ await getAddress()
191
192
  this.storePrefix = options.storePrefix
192
193
  this.root = options.root
193
194
 
@@ -475,7 +476,7 @@ export default class Peernet {
475
476
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
476
477
 
477
478
  const peerInfo = {
478
- ...peer.connectionStats,
479
+ address: peer.remoteAddress,
479
480
  id: peerId
480
481
  }
481
482
 
@@ -547,22 +548,30 @@ export default class Peernet {
547
548
  if (!providers || (providers && Object.keys(providers).length === 0)) throw nothingFoundError(hash)
548
549
  debug(`found ${Object.keys(providers).length} provider(s) for ${hash}`)
549
550
  // get closest peer on earth
550
- const closestPeer: DHTProvider = Object.values(providers)[0]
551
+ let closestPeer: DHTProvider = await this.dht.closestPeer(Object.values(providers))
552
+ // fallback to first provider if no closest peer found
553
+ if (!closestPeer || !closestPeer.id) closestPeer = Object.values(providers)[0]
551
554
 
555
+ debug(`closest peer for ${hash} is ${closestPeer?.address}`)
552
556
  // get peer instance by id
553
557
  if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name || store)
554
558
 
555
559
  const id = closestPeer.id
556
560
  const peer = this.connections[id]
557
561
 
558
- if (peer?.connected) {
559
- let data = await new globalThis.peernet.protos['peernet-data']({
560
- hash,
561
- store: store?.name || store
562
- })
562
+ if (!peer || !peer?.connected) {
563
+ this.dht.removeProvider(id, hash)
564
+ return this.requestData(hash, store?.name || store)
565
+ }
563
566
 
564
- const node = await this.prepareMessage(data)
567
+ let data = await new globalThis.peernet.protos['peernet-data']({
568
+ hash,
569
+ store: store?.name || store
570
+ })
571
+
572
+ const node = await this.prepareMessage(data)
565
573
 
574
+ if (peer?.connected) {
566
575
  try {
567
576
  if (peer) data = await peer.request(node.encoded)
568
577
  else {
@@ -584,10 +593,11 @@ export default class Peernet {
584
593
  } catch (error) {
585
594
  debug(`Error while requesting data from ${id}`, error)
586
595
  // if error, remove provider
587
- this.dht.removeProvider(id, hash)
588
596
  if (this.#peerAttempts[id] > 1) {
589
597
  this.#peerAttempts[id] = 0
590
598
  debug(`Removed provider ${id} for ${hash} after 3 attempts`)
599
+
600
+ this.dht.removeProvider(id, hash)
591
601
  console.error(nothingFoundError(hash))
592
602
  return undefined
593
603
  }