@leofcoin/peernet 1.1.15 → 1.1.18

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 { g as getDefaultExportFromCjs, c as commonjsGlobal } from './peernet-abdf9d7d.js';
1
+ import { g as getDefaultExportFromCjs, c as commonjsGlobal } from './peernet-ea3ab655.js';
2
2
  import './value-157ab062.js';
3
3
 
4
4
  function commonjsRequire(path) {
@@ -40906,6 +40906,14 @@ class P2PTPeer {
40906
40906
  return this.#connection.connected
40907
40907
  }
40908
40908
 
40909
+ get connectionStats() {
40910
+ return {
40911
+ family: this.#connection.remoteFamily || this.#connection.localFamily || 'ipv4',
40912
+ address: this.#connection.remoteAddress || this.#connection.localAddress || '127.0.0.1',
40913
+ port: this.#connection.remotePort || this.#connection.localPort || '0000',
40914
+ }
40915
+ }
40916
+
40909
40917
  constructor(peer, p2pt, options = {}) {
40910
40918
  this.#connection = peer;
40911
40919
  this.p2pt = p2pt;
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './peernet-abdf9d7d.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './peernet-ea3ab655.js';
2
2
  import './value-157ab062.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './peernet-abdf9d7d.js';
1
+ import { F as FormatInterface } from './peernet-ea3ab655.js';
2
2
  import './value-157ab062.js';
3
3
 
4
4
  var proto$b = {
@@ -2123,7 +2123,7 @@ class DhtEarth {
2123
2123
  const address = await getAddress();
2124
2124
  const peerLoc = await this.getCoordinates(address);
2125
2125
  for (const provider of providers) {
2126
- if (provider.address === '127.0.0.1')
2126
+ if (provider.address === '127.0.0.1' || provider.address === '::1')
2127
2127
  all.push({ provider, distance: 0 });
2128
2128
  else
2129
2129
  all.push(this.getDistance(peerLoc, provider));
@@ -16086,7 +16086,7 @@ class Identity {
16086
16086
  globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
16087
16087
  }
16088
16088
  else {
16089
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-674a33f1.js');
16089
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-123daa04.js');
16090
16090
  const { identity, accounts } = await importee.default(password, this.network);
16091
16091
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
16092
16092
  await globalThis.walletStore.put('version', String(1));
@@ -16257,7 +16257,7 @@ class Peernet {
16257
16257
  this.root = options.root;
16258
16258
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
16259
16259
  // FolderMessageResponse
16260
- } = await import(/* webpackChunkName: "messages" */ './messages-b6d79e10.js');
16260
+ } = await import(/* webpackChunkName: "messages" */ './messages-5ddc1083.js');
16261
16261
  /**
16262
16262
  * proto Object containing protos
16263
16263
  * @type {Object}
@@ -16328,7 +16328,7 @@ class Peernet {
16328
16328
  if (this.#starting || this.#started)
16329
16329
  return;
16330
16330
  this.#starting = true;
16331
- const importee = await import('./client-61db7724.js');
16331
+ const importee = await import('./client-033977a1.js');
16332
16332
  /**
16333
16333
  * @access public
16334
16334
  * @type {PeernetClient}
@@ -16420,33 +16420,24 @@ class Peernet {
16420
16420
  if (!hash)
16421
16421
  throw new Error('hash expected, received undefined');
16422
16422
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
16423
- this.client.id;
16424
- const walk = async (peer) => {
16423
+ const walk = async (peer, peerId) => {
16425
16424
  const node = await this.prepareMessage(data);
16426
16425
  let result = await peer.request(node.encoded);
16427
16426
  result = new Uint8Array(Object.values(result));
16428
16427
  const proto = await protoFor(result);
16429
16428
  if (proto.name !== 'peernet-dht-response')
16430
16429
  throw dhtError(proto.name);
16431
- // TODO: give ip and port (just used for location)
16432
- if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
16433
- peer.connection.remoteFamily = 'ipv4';
16434
- peer.connection.remoteAddress = '127.0.0.1';
16435
- peer.connection.remotePort = '0000';
16436
- }
16437
16430
  const peerInfo = {
16438
- family: peer.connection.remoteFamily || peer.connection.localFamily,
16439
- address: peer.connection.remoteAddress || peer.connection.localAddress,
16440
- port: peer.connection.remotePort || peer.connection.localPort,
16441
- id: peer.peerId,
16431
+ ...peer.connectionStats,
16432
+ id: peerId,
16442
16433
  };
16443
16434
  if (proto.decoded.has)
16444
16435
  this.dht.addProvider(peerInfo, proto.decoded.hash);
16445
16436
  };
16446
16437
  let walks = [];
16447
- for (const peer of this.connections) {
16448
- if (peer.peerId !== this.id) {
16449
- walks.push(walk(peer));
16438
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
16439
+ if (peerId !== this.id) {
16440
+ walks.push(walk(peer, peerId));
16450
16441
  }
16451
16442
  }
16452
16443
  return Promise.all(walks);
@@ -16456,7 +16447,7 @@ class Peernet {
16456
16447
  *
16457
16448
  * @param {String} hash
16458
16449
  */
16459
- async providersFor(hash) {
16450
+ async providersFor(hash, store) {
16460
16451
  let providers = await this.dht.providersFor(hash);
16461
16452
  // walk the network to find a provider
16462
16453
  if (!providers || providers.length === 0) {
@@ -16519,23 +16510,25 @@ class Peernet {
16519
16510
  if (!closestPeer || !closestPeer.id)
16520
16511
  return this.requestData(hash, store?.name || store);
16521
16512
  const id = closestPeer.id;
16522
- if (this.connections) {
16523
- let closest = this.connections.filter((peer) => {
16524
- if (peer.peerId === id)
16525
- return peer;
16526
- });
16513
+ if (this.#connections[id]) {
16514
+ const peer = this.#connections[id];
16527
16515
  let data = await new globalThis.peernet.protos['peernet-data']({ hash, store: store?.name || store });
16528
16516
  const node = await this.prepareMessage(data);
16529
- if (closest[0])
16530
- data = await closest[0].request(node.encoded);
16517
+ if (peer)
16518
+ data = await peer.request(node.encoded);
16531
16519
  else {
16532
- closest = this.connections.filter((peer) => {
16533
- if (peer.peerId === id)
16534
- return peer;
16535
- });
16536
- if (closest[0])
16537
- data = await closest[0].request(node.encoded);
16520
+ // fallback and try every provider found
16521
+ const promises = [];
16522
+ const providers = await this.providersFor(hash, store);
16523
+ for (const provider of providers) {
16524
+ const peer = this.#connections[provider.id];
16525
+ if (peer)
16526
+ promises.push(peer.request(node.encoded));
16527
+ }
16528
+ data = await Promise.race(promises);
16538
16529
  }
16530
+ if (data)
16531
+ data = new Uint8Array(Object.values(data));
16539
16532
  const proto = await protoFor(data);
16540
16533
  // TODO: store data automaticly or not
16541
16534
  return BufferToUint8Array(proto.decoded.data);
@@ -16736,8 +16729,8 @@ class Peernet {
16736
16729
  // globalSub.publish(topic, data)
16737
16730
  const id = Math.random().toString(36).slice(-12);
16738
16731
  data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
16739
- for (const peer of this.connections) {
16740
- if (peer.peerId !== this.peerId) {
16732
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
16733
+ if (peerId !== this.id) {
16741
16734
  const node = await this.prepareMessage(data);
16742
16735
  this.sendMessage(peer, id, node.encoded);
16743
16736
  }
@@ -1,2 +1,2 @@
1
- export { P as default } from './peernet-abdf9d7d.js';
1
+ export { P as default } from './peernet-ea3ab655.js';
2
2
  import './value-157ab062.js';
@@ -223,7 +223,7 @@ class DhtEarth {
223
223
  const address = await getAddress();
224
224
  const peerLoc = await this.getCoordinates(address);
225
225
  for (const provider of providers) {
226
- if (provider.address === '127.0.0.1')
226
+ if (provider.address === '127.0.0.1' || provider.address === '::1')
227
227
  all.push({ provider, distance: 0 });
228
228
  else
229
229
  all.push(this.getDistance(peerLoc, provider));
@@ -674,33 +674,24 @@ class Peernet {
674
674
  if (!hash)
675
675
  throw new Error('hash expected, received undefined');
676
676
  const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
677
- this.client.id;
678
- const walk = async (peer) => {
677
+ const walk = async (peer, peerId) => {
679
678
  const node = await this.prepareMessage(data);
680
679
  let result = await peer.request(node.encoded);
681
680
  result = new Uint8Array(Object.values(result));
682
681
  const proto = await protoFor(result);
683
682
  if (proto.name !== 'peernet-dht-response')
684
683
  throw dhtError(proto.name);
685
- // TODO: give ip and port (just used for location)
686
- if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
687
- peer.connection.remoteFamily = 'ipv4';
688
- peer.connection.remoteAddress = '127.0.0.1';
689
- peer.connection.remotePort = '0000';
690
- }
691
684
  const peerInfo = {
692
- family: peer.connection.remoteFamily || peer.connection.localFamily,
693
- address: peer.connection.remoteAddress || peer.connection.localAddress,
694
- port: peer.connection.remotePort || peer.connection.localPort,
695
- id: peer.peerId,
685
+ ...peer.connectionStats,
686
+ id: peerId,
696
687
  };
697
688
  if (proto.decoded.has)
698
689
  this.dht.addProvider(peerInfo, proto.decoded.hash);
699
690
  };
700
691
  let walks = [];
701
- for (const peer of this.connections) {
702
- if (peer.peerId !== this.id) {
703
- walks.push(walk(peer));
692
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
693
+ if (peerId !== this.id) {
694
+ walks.push(walk(peer, peerId));
704
695
  }
705
696
  }
706
697
  return Promise.all(walks);
@@ -710,7 +701,7 @@ class Peernet {
710
701
  *
711
702
  * @param {String} hash
712
703
  */
713
- async providersFor(hash) {
704
+ async providersFor(hash, store) {
714
705
  let providers = await this.dht.providersFor(hash);
715
706
  // walk the network to find a provider
716
707
  if (!providers || providers.length === 0) {
@@ -773,23 +764,25 @@ class Peernet {
773
764
  if (!closestPeer || !closestPeer.id)
774
765
  return this.requestData(hash, store?.name || store);
775
766
  const id = closestPeer.id;
776
- if (this.connections) {
777
- let closest = this.connections.filter((peer) => {
778
- if (peer.peerId === id)
779
- return peer;
780
- });
767
+ if (this.#connections[id]) {
768
+ const peer = this.#connections[id];
781
769
  let data = await new globalThis.peernet.protos['peernet-data']({ hash, store: store?.name || store });
782
770
  const node = await this.prepareMessage(data);
783
- if (closest[0])
784
- data = await closest[0].request(node.encoded);
771
+ if (peer)
772
+ data = await peer.request(node.encoded);
785
773
  else {
786
- closest = this.connections.filter((peer) => {
787
- if (peer.peerId === id)
788
- return peer;
789
- });
790
- if (closest[0])
791
- data = await closest[0].request(node.encoded);
774
+ // fallback and try every provider found
775
+ const promises = [];
776
+ const providers = await this.providersFor(hash, store);
777
+ for (const provider of providers) {
778
+ const peer = this.#connections[provider.id];
779
+ if (peer)
780
+ promises.push(peer.request(node.encoded));
781
+ }
782
+ data = await Promise.race(promises);
792
783
  }
784
+ if (data)
785
+ data = new Uint8Array(Object.values(data));
793
786
  const proto = await protoFor(data);
794
787
  // TODO: store data automaticly or not
795
788
  return BufferToUint8Array(proto.decoded.data);
@@ -990,8 +983,8 @@ class Peernet {
990
983
  // globalSub.publish(topic, data)
991
984
  const id = Math.random().toString(36).slice(-12);
992
985
  data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
993
- for (const peer of this.connections) {
994
- if (peer.peerId !== this.peerId) {
986
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
987
+ if (peerId !== this.id) {
995
988
  const node = await this.prepareMessage(data);
996
989
  this.sendMessage(peer, id, node.encoded);
997
990
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.1.15",
3
+ "version": "1.1.18",
4
4
  "description": "",
5
5
  "main": "src/peernet.js",
6
6
  "exports": {
package/src/dht/dht.js CHANGED
@@ -84,9 +84,8 @@ export default class DhtEarth {
84
84
  let all = []
85
85
  const address = await getAddress();
86
86
  const peerLoc = await this.getCoordinates(address)
87
-
88
87
  for (const provider of providers) {
89
- if (provider.address === '127.0.0.1') all.push({provider, distance: 0})
88
+ if (provider.address === '127.0.0.1' || provider.address === '::1') all.push({provider, distance: 0})
90
89
  else all.push(this.getDistance(peerLoc, provider))
91
90
  }
92
91
 
package/src/peernet.ts CHANGED
@@ -373,34 +373,24 @@ export default class Peernet {
373
373
  async walk(hash) {
374
374
  if (!hash) throw new Error('hash expected, received undefined')
375
375
  const data = await new globalThis.peernet.protos['peernet-dht']({hash})
376
- const clientId = this.client.id
377
- const walk = async peer => {
376
+ const walk = async (peer, peerId) => {
378
377
  const node = await this.prepareMessage(data)
379
378
  let result = await peer.request(node.encoded)
380
379
  result = new Uint8Array(Object.values(result))
381
380
  const proto = await protoFor(result)
382
381
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
383
382
 
384
- // TODO: give ip and port (just used for location)
385
- if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
386
- peer.connection.remoteFamily = 'ipv4'
387
- peer.connection.remoteAddress = '127.0.0.1'
388
- peer.connection.remotePort = '0000'
389
- }
390
-
391
383
  const peerInfo = {
392
- family: peer.connection.remoteFamily || peer.connection.localFamily,
393
- address: peer.connection.remoteAddress || peer.connection.localAddress,
394
- port: peer.connection.remotePort || peer.connection.localPort,
395
- id: peer.peerId,
384
+ ...peer.connectionStats,
385
+ id: peerId,
396
386
  }
397
387
 
398
388
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
399
389
  }
400
390
  let walks = []
401
- for (const peer of this.connections) {
402
- if (peer.peerId !== this.id) {
403
- walks.push(walk(peer))
391
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
392
+ if (peerId !== this.id) {
393
+ walks.push(walk(peer, peerId))
404
394
  }
405
395
  }
406
396
  return Promise.all(walks)
@@ -411,7 +401,7 @@ export default class Peernet {
411
401
  *
412
402
  * @param {String} hash
413
403
  */
414
- async providersFor(hash) {
404
+ async providersFor(hash: string, store?: undefined) {
415
405
  let providers = await this.dht.providersFor(hash)
416
406
  // walk the network to find a provider
417
407
  if (!providers || providers.length === 0) {
@@ -472,21 +462,29 @@ export default class Peernet {
472
462
  if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name || store)
473
463
 
474
464
  const id = closestPeer.id
475
- if (this.connections) {
476
- let closest = this.connections.filter((peer) => {
477
- if (peer.peerId === id) return peer
478
- })
465
+
466
+ if (this.#connections[id]) {
467
+ const peer = this.#connections[id]
479
468
 
480
469
  let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name || store});
481
470
 
482
471
  const node = await this.prepareMessage(data)
483
- if (closest[0]) data = await closest[0].request(node.encoded)
472
+
473
+ if (peer) data = await peer.request(node.encoded)
484
474
  else {
485
- closest = this.connections.filter((peer) => {
486
- if (peer.peerId === id) return peer
487
- })
488
- if (closest[0]) data = await closest[0].request(node.encoded)
475
+ // fallback and try every provider found
476
+ const promises = []
477
+ const providers = await this.providersFor(hash, store)
478
+ for (const provider of providers) {
479
+
480
+ const peer = this.#connections[provider.id]
481
+
482
+ if (peer) promises.push(peer.request(node.encoded))
483
+ }
484
+ data = await Promise.race(promises)
489
485
  }
486
+ if (data) data = new Uint8Array(Object.values(data))
487
+
490
488
  const proto = await protoFor(data)
491
489
  // TODO: store data automaticly or not
492
490
  return BufferToUint8Array(proto.decoded.data)
@@ -690,8 +688,8 @@ export default class Peernet {
690
688
  // globalSub.publish(topic, data)
691
689
  const id = Math.random().toString(36).slice(-12)
692
690
  data = await new globalThis.peernet.protos['peernet-ps']({data, topic})
693
- for (const peer of this.connections) {
694
- if (peer.peerId !== this.peerId) {
691
+ for (const [peerId, peer] of Object.entries(this.#connections)) {
692
+ if (peerId !== this.id) {
695
693
  const node = await this.prepareMessage(data)
696
694
  this.sendMessage(peer, id, node.encoded)
697
695
  }
package/test/client.js CHANGED
@@ -1,7 +1,15 @@
1
1
  const Client = require('./../client.js')
2
2
 
3
- const client = new Client({id: Buffer.from('testtesttesttesttesttesttesttest')})
3
+ const client = new Client()
4
4
 
5
- pubsub.subscribe('peer:connected', async peer => setInterval(async () => {
6
- console.log(await peer.request({type: 'block', index: 0}));
7
- }, 5000))
5
+ console.log(pubsub.subscribers);
6
+ pubsub.subscribe('peer:connected', (data) => {
7
+ console.log(data);
8
+ })
9
+ pubsub.subscribe('peer:data', (data) => {
10
+ console.log(data);
11
+ })
12
+
13
+ // pubsub.subscribe('peer:connected', async peer => setInterval(async () => {
14
+ // // console.log(await peer.request({type: 'block', index: 0}));
15
+ // }, 5000))
package/test.js CHANGED
@@ -2,8 +2,16 @@ import Client from './exports/peernet.js'
2
2
 
3
3
 
4
4
 
5
- const client = await new Client({root: '.peernet/test', network: 'leofcoin:peach', networkVersion: 'peach', stars: ['wss://peach.leofcoin.org']})
6
-
5
+ const client = await new Client({
6
+ network: 'leofcoin:peach', networkVersion: 'peach', stars: ['wss://peach.leofcoin.org']
7
+ })
8
+ console.log(pubsub.subscribers);
9
+ pubsub.subscribe('peer:connected', (data) => {
10
+ console.log(data);
11
+ })
12
+ pubsub.subscribe('peer:data', (data) => {
13
+ console.log(data);
14
+ })
7
15
  // peernet.addFolder([{
8
16
  // path: 'assets/asset.png',
9
17
  // content: Buffer.from('png')