@leofcoin/peernet 0.14.26 → 0.15.1

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/README.md CHANGED
@@ -1,10 +1,22 @@
1
- # socket-discovery
1
+ # peernet
2
2
 
3
3
  ## Usage
4
4
  ```js
5
- import discovery from 'socket-discovery'
5
+ import Peernet from '@leofcoin/peernet'
6
+
7
+
8
+ const config = {
9
+ network: 'leofcoin:peach',
10
+ stars: ['wss://peach.leofcoin.org']
11
+ }
12
+
13
+ await new Peernet(config)
14
+
15
+ console.log(globalThis.peernet)
6
16
  ```
7
17
 
18
+
19
+
8
20
  ## API
9
21
  #### addRequestHandler
10
22
 
@@ -30,6 +42,12 @@ peernet.addRequestHandler('hello', () => {
30
42
  ```
31
43
 
32
44
  ## Development
45
+
46
+ `note: you need to install jsproject`
47
+ ```sh
48
+ npm i -g @vandeurenglenn/project
49
+ ```
50
+
33
51
  ### watch
34
52
  ```sh
35
53
  npm run w
@@ -43,7 +61,3 @@ npm run c
43
61
  npm run demo
44
62
  ```
45
63
 
46
- `note: you need to install jsproject`
47
- ```sh
48
- npm i -g @vandeurenglenn/project
49
- ```
@@ -571,22 +571,18 @@ class Client {
571
571
  return Object.entries(this.#connections)
572
572
  }
573
573
 
574
- constructor(id, identifiers = ['peernet-v0.1.0'], stars = []) {
574
+ constructor(id, network = 'leofcoin:peach', stars = ['wss://peach.leofcoin.org']) {
575
575
  this.id = id || Math.random().toString(36).slice(-12);
576
- if (!Array.isArray(identifiers)) identifiers = [identifiers]
577
576
  this.peerJoined = this.peerJoined.bind(this)
578
577
  this.peerLeft = this.peerLeft.bind(this)
579
578
  this.starLeft = this.starLeft.bind(this)
580
579
  this.starJoined = this.starJoined.bind(this)
581
580
 
582
- this._init(identifiers, stars)
581
+ this._init(network, stars)
583
582
  }
584
583
 
585
- async _init(identifiers, stars = []) {
586
- if (stars.length === 0) {
587
- stars.push('wss://star.leofcoin.org')
588
- }
589
- this.identifiers = identifiers
584
+ async _init(network, stars = []) {
585
+ this.network = network
590
586
  this.starsConfig = stars
591
587
  // reconnectJob()
592
588
 
@@ -599,7 +595,7 @@ class Client {
599
595
 
600
596
  for (const star of stars) {
601
597
  try {
602
- this.socketClient = await socketRequestClient(star, identifiers[0])
598
+ this.socketClient = await socketRequestClient(star, network)
603
599
  const id = await this.socketClient.request({url: 'id', params: {from: this.id}})
604
600
  this.socketClient.peerId = id
605
601
  this.#stars[id] = this.socketClient
@@ -643,7 +639,7 @@ class Client {
643
639
 
644
640
  for (const star of this.starsConfig) {
645
641
  try {
646
- this.socketClient = await socketRequestClient(star, this.identifiers[0])
642
+ this.socketClient = await socketRequestClient(star, this.network)
647
643
  if (!this.socketClient?.client?._connection.connected) return
648
644
  const id = await this.socketClient.request({url: 'id', params: {from: this.id}})
649
645
  this.#stars[id] = this.socketClient
@@ -702,6 +698,21 @@ class Client {
702
698
  debug(`peer ${id} removed`)
703
699
  }
704
700
 
701
+ async close() {
702
+
703
+ this.socketClient.unsubscribe('peer:joined', this.peerJoined)
704
+ this.socketClient.unsubscribe('peer:left', this.peerLeft)
705
+ this.socketClient.unsubscribe('star:left', this.starLeft)
706
+
707
+ const promises = [
708
+ Object.values(this.#connections).map(connection => connection.close()),
709
+ Object.values(this.#stars).map(connection => connection.close()),
710
+ this.socketClient.close()
711
+ ]
712
+
713
+ return Promise.allSettled(promises)
714
+
715
+ }
705
716
 
706
717
  }
707
718
 
@@ -570,22 +570,18 @@ class Client {
570
570
  return Object.entries(this.#connections)
571
571
  }
572
572
 
573
- constructor(id, identifiers = ['peernet-v0.1.0'], stars = []) {
573
+ constructor(id, network = 'leofcoin:peach', stars = ['wss://peach.leofcoin.org']) {
574
574
  this.id = id || Math.random().toString(36).slice(-12);
575
- if (!Array.isArray(identifiers)) identifiers = [identifiers]
576
575
  this.peerJoined = this.peerJoined.bind(this)
577
576
  this.peerLeft = this.peerLeft.bind(this)
578
577
  this.starLeft = this.starLeft.bind(this)
579
578
  this.starJoined = this.starJoined.bind(this)
580
579
 
581
- this._init(identifiers, stars)
580
+ this._init(network, stars)
582
581
  }
583
582
 
584
- async _init(identifiers, stars = []) {
585
- if (stars.length === 0) {
586
- stars.push('wss://star.leofcoin.org')
587
- }
588
- this.identifiers = identifiers
583
+ async _init(network, stars = []) {
584
+ this.network = network
589
585
  this.starsConfig = stars
590
586
  // reconnectJob()
591
587
 
@@ -598,7 +594,7 @@ class Client {
598
594
 
599
595
  for (const star of stars) {
600
596
  try {
601
- this.socketClient = await socketRequestClient(star, identifiers[0])
597
+ this.socketClient = await socketRequestClient(star, network)
602
598
  const id = await this.socketClient.request({url: 'id', params: {from: this.id}})
603
599
  this.socketClient.peerId = id
604
600
  this.#stars[id] = this.socketClient
@@ -642,7 +638,7 @@ class Client {
642
638
 
643
639
  for (const star of this.starsConfig) {
644
640
  try {
645
- this.socketClient = await socketRequestClient(star, this.identifiers[0])
641
+ this.socketClient = await socketRequestClient(star, this.network)
646
642
  if (!this.socketClient?.client?._connection.connected) return
647
643
  const id = await this.socketClient.request({url: 'id', params: {from: this.id}})
648
644
  this.#stars[id] = this.socketClient
@@ -701,6 +697,21 @@ class Client {
701
697
  debug(`peer ${id} removed`)
702
698
  }
703
699
 
700
+ async close() {
701
+
702
+ this.socketClient.unsubscribe('peer:joined', this.peerJoined)
703
+ this.socketClient.unsubscribe('peer:left', this.peerLeft)
704
+ this.socketClient.unsubscribe('star:left', this.starLeft)
705
+
706
+ const promises = [
707
+ Object.values(this.#connections).map(connection => connection.close()),
708
+ Object.values(this.#stars).map(connection => connection.close()),
709
+ this.socketClient.close()
710
+ ]
711
+
712
+ return Promise.allSettled(promises)
713
+
714
+ }
704
715
 
705
716
  }
706
717
 
@@ -8491,6 +8491,7 @@ class LittlePubSub {
8491
8491
  // EXTERNAL MODULE: ./node_modules/@leofcoin/codec-format-interface/src/index.js + 9 modules
8492
8492
  var src = __webpack_require__(745);
8493
8493
  ;// CONCATENATED MODULE: ./dist/module/peernet.js
8494
+ /* provided dependency */ var process = __webpack_require__(155);
8494
8495
 
8495
8496
 
8496
8497
 
@@ -8914,6 +8915,7 @@ class Peernet {
8914
8915
  * @access public
8915
8916
  * @param {Object} options
8916
8917
  * @param {String} options.network - desired network
8918
+ * @param {String} options.stars - star list for selected network (these should match, don't mix networks)
8917
8919
  * @param {String} options.root - path to root directory
8918
8920
  * @param {String} options.storePrefix - prefix for datatores (lfc)
8919
8921
  *
@@ -8928,14 +8930,17 @@ class Peernet {
8928
8930
  * @property {String} network - current network
8929
8931
  */
8930
8932
  this.network = options.network || 'leofcoin';
8933
+ this.stars = options.stars;
8931
8934
  const parts = this.network.split(':');
8932
-
8935
+ this.networkVersion = parts.length > 1 ? parts[1] : 'mainnet';
8936
+
8933
8937
  if (!options.storePrefix) options.storePrefix = 'lfc';
8934
8938
  if (!options.port) options.port = 2000;
8935
8939
  if (!options.root) {
8936
8940
  if (parts[1]) options.root = `.${parts[0]}/${parts[1]}`;
8937
8941
  else options.root = `.${this.network}`;
8938
8942
  }
8943
+
8939
8944
  globalThis.peernet = this;
8940
8945
  this.bw = {
8941
8946
  up: 0,
@@ -9144,9 +9149,15 @@ class Peernet {
9144
9149
  * @access public
9145
9150
  * @type {PeernetClient}
9146
9151
  */
9147
- this.client = new importee.default(this.id);
9152
+ this.client = new importee.default(this.id, this.networkVersion, this.stars);
9148
9153
  if (globalThis.onbeforeunload) {
9149
9154
  globalThis.addEventListener('beforeunload', async () => this.client.close());
9155
+ } else {
9156
+ process.on('SIGTERM', async () => {
9157
+ process.stdin.resume();
9158
+ await this.client.close();
9159
+ process.exit();
9160
+ });
9150
9161
  }
9151
9162
  return this
9152
9163
  }
@@ -8461,6 +8461,7 @@ class LittlePubSub {
8461
8461
  // EXTERNAL MODULE: ./node_modules/@leofcoin/codec-format-interface/src/index.js + 9 modules
8462
8462
  var src = __webpack_require__(745);
8463
8463
  ;// CONCATENATED MODULE: ./dist/module/peernet.js
8464
+ /* provided dependency */ var process = __webpack_require__(155);
8464
8465
 
8465
8466
 
8466
8467
 
@@ -8884,6 +8885,7 @@ class Peernet {
8884
8885
  * @access public
8885
8886
  * @param {Object} options
8886
8887
  * @param {String} options.network - desired network
8888
+ * @param {String} options.stars - star list for selected network (these should match, don't mix networks)
8887
8889
  * @param {String} options.root - path to root directory
8888
8890
  * @param {String} options.storePrefix - prefix for datatores (lfc)
8889
8891
  *
@@ -8898,14 +8900,17 @@ class Peernet {
8898
8900
  * @property {String} network - current network
8899
8901
  */
8900
8902
  this.network = options.network || 'leofcoin';
8903
+ this.stars = options.stars;
8901
8904
  const parts = this.network.split(':');
8902
-
8905
+ this.networkVersion = parts.length > 1 ? parts[1] : 'mainnet';
8906
+
8903
8907
  if (!options.storePrefix) options.storePrefix = 'lfc';
8904
8908
  if (!options.port) options.port = 2000;
8905
8909
  if (!options.root) {
8906
8910
  if (parts[1]) options.root = `.${parts[0]}/${parts[1]}`;
8907
8911
  else options.root = `.${this.network}`;
8908
8912
  }
8913
+
8909
8914
  globalThis.peernet = this;
8910
8915
  this.bw = {
8911
8916
  up: 0,
@@ -9114,9 +9119,15 @@ class Peernet {
9114
9119
  * @access public
9115
9120
  * @type {PeernetClient}
9116
9121
  */
9117
- this.client = new importee.default(this.id);
9122
+ this.client = new importee.default(this.id, this.networkVersion, this.stars);
9118
9123
  if (globalThis.onbeforeunload) {
9119
9124
  globalThis.addEventListener('beforeunload', async () => this.client.close());
9125
+ } else {
9126
+ process.on('SIGTERM', async () => {
9127
+ process.stdin.resume();
9128
+ await this.client.close();
9129
+ process.exit();
9130
+ });
9120
9131
  }
9121
9132
  return this
9122
9133
  }
@@ -447,6 +447,7 @@ class Peernet {
447
447
  * @access public
448
448
  * @param {Object} options
449
449
  * @param {String} options.network - desired network
450
+ * @param {String} options.stars - star list for selected network (these should match, don't mix networks)
450
451
  * @param {String} options.root - path to root directory
451
452
  * @param {String} options.storePrefix - prefix for datatores (lfc)
452
453
  *
@@ -461,14 +462,17 @@ class Peernet {
461
462
  * @property {String} network - current network
462
463
  */
463
464
  this.network = options.network || 'leofcoin';
465
+ this.stars = options.stars;
464
466
  const parts = this.network.split(':');
465
-
467
+ this.networkVersion = parts.length > 1 ? parts[1] : 'mainnet';
468
+
466
469
  if (!options.storePrefix) options.storePrefix = 'lfc';
467
470
  if (!options.port) options.port = 2000;
468
471
  if (!options.root) {
469
472
  if (parts[1]) options.root = `.${parts[0]}/${parts[1]}`;
470
473
  else options.root = `.${this.network}`;
471
474
  }
475
+
472
476
  globalThis.peernet = this;
473
477
  this.bw = {
474
478
  up: 0,
@@ -677,9 +681,15 @@ class Peernet {
677
681
  * @access public
678
682
  * @type {PeernetClient}
679
683
  */
680
- this.client = new importee.default(this.id);
684
+ this.client = new importee.default(this.id, this.networkVersion, this.stars);
681
685
  if (globalThis.onbeforeunload) {
682
686
  globalThis.addEventListener('beforeunload', async () => this.client.close());
687
+ } else {
688
+ process.on('SIGTERM', async () => {
689
+ process.stdin.resume();
690
+ await this.client.close();
691
+ process.exit();
692
+ });
683
693
  }
684
694
  return this
685
695
  }
@@ -421,6 +421,7 @@ class Peernet {
421
421
  * @access public
422
422
  * @param {Object} options
423
423
  * @param {String} options.network - desired network
424
+ * @param {String} options.stars - star list for selected network (these should match, don't mix networks)
424
425
  * @param {String} options.root - path to root directory
425
426
  * @param {String} options.storePrefix - prefix for datatores (lfc)
426
427
  *
@@ -435,14 +436,17 @@ class Peernet {
435
436
  * @property {String} network - current network
436
437
  */
437
438
  this.network = options.network || 'leofcoin';
439
+ this.stars = options.stars;
438
440
  const parts = this.network.split(':');
439
-
441
+ this.networkVersion = parts.length > 1 ? parts[1] : 'mainnet';
442
+
440
443
  if (!options.storePrefix) options.storePrefix = 'lfc';
441
444
  if (!options.port) options.port = 2000;
442
445
  if (!options.root) {
443
446
  if (parts[1]) options.root = `.${parts[0]}/${parts[1]}`;
444
447
  else options.root = `.${this.network}`;
445
448
  }
449
+
446
450
  globalThis.peernet = this;
447
451
  this.bw = {
448
452
  up: 0,
@@ -651,9 +655,15 @@ class Peernet {
651
655
  * @access public
652
656
  * @type {PeernetClient}
653
657
  */
654
- this.client = new importee.default(this.id);
658
+ this.client = new importee.default(this.id, this.networkVersion, this.stars);
655
659
  if (globalThis.onbeforeunload) {
656
660
  globalThis.addEventListener('beforeunload', async () => this.client.close());
661
+ } else {
662
+ process.on('SIGTERM', async () => {
663
+ process.stdin.resume();
664
+ await this.client.close();
665
+ process.exit();
666
+ });
657
667
  }
658
668
  return this
659
669
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.14.26",
3
+ "version": "0.15.1",
4
4
  "description": "",
5
5
  "source": "src/peernet.js",
6
6
  "main": "dist/commonjs/peernet.js",
@@ -29,7 +29,7 @@
29
29
  "@leofcoin/codec-format-interface": "^1.4.0",
30
30
  "@leofcoin/generate-account": "^1.0.4",
31
31
  "@leofcoin/multi-wallet": "^2.1.2",
32
- "@leofcoin/peernet-swarm": "^0.3.10",
32
+ "@leofcoin/peernet-swarm": "^0.4.0",
33
33
  "@leofcoin/storage": "^3.0.0",
34
34
  "@vandeurenglenn/base32": "^1.1.0",
35
35
  "@vandeurenglenn/base58": "^1.1.0",
package/src/peernet.js CHANGED
@@ -23,6 +23,7 @@ export default class Peernet {
23
23
  * @access public
24
24
  * @param {Object} options
25
25
  * @param {String} options.network - desired network
26
+ * @param {String} options.stars - star list for selected network (these should match, don't mix networks)
26
27
  * @param {String} options.root - path to root directory
27
28
  * @param {String} options.storePrefix - prefix for datatores (lfc)
28
29
  *
@@ -37,14 +38,17 @@ export default class Peernet {
37
38
  * @property {String} network - current network
38
39
  */
39
40
  this.network = options.network || 'leofcoin'
41
+ this.stars = options.stars
40
42
  const parts = this.network.split(':')
41
-
43
+ this.networkVersion = parts.length > 1 ? parts[1] : 'mainnet'
44
+
42
45
  if (!options.storePrefix) options.storePrefix = 'lfc'
43
46
  if (!options.port) options.port = 2000
44
47
  if (!options.root) {
45
48
  if (parts[1]) options.root = `.${parts[0]}/${parts[1]}`
46
49
  else options.root = `.${this.network}`
47
50
  }
51
+
48
52
  globalThis.peernet = this
49
53
  this.bw = {
50
54
  up: 0,
@@ -253,9 +257,15 @@ export default class Peernet {
253
257
  * @access public
254
258
  * @type {PeernetClient}
255
259
  */
256
- this.client = new importee.default(this.id)
260
+ this.client = new importee.default(this.id, this.networkVersion, this.stars)
257
261
  if (globalThis.onbeforeunload) {
258
262
  globalThis.addEventListener('beforeunload', async () => this.client.close());
263
+ } else {
264
+ process.on('SIGTERM', async () => {
265
+ process.stdin.resume();
266
+ await this.client.close()
267
+ process.exit()
268
+ });
259
269
  }
260
270
  return this
261
271
  }