@leofcoin/peernet 0.14.26 → 0.15.0

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,6 +8930,7 @@ 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
 
8933
8936
  if (!options.storePrefix) options.storePrefix = 'lfc';
@@ -9144,9 +9147,15 @@ class Peernet {
9144
9147
  * @access public
9145
9148
  * @type {PeernetClient}
9146
9149
  */
9147
- this.client = new importee.default(this.id);
9150
+ this.client = new importee.default(this.id, this.network, this.stars);
9148
9151
  if (globalThis.onbeforeunload) {
9149
9152
  globalThis.addEventListener('beforeunload', async () => this.client.close());
9153
+ } else {
9154
+ process.on('SIGTERM', async () => {
9155
+ process.stdin.resume();
9156
+ await this.client.close();
9157
+ process.exit();
9158
+ });
9150
9159
  }
9151
9160
  return this
9152
9161
  }
@@ -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,6 +8900,7 @@ 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
 
8903
8906
  if (!options.storePrefix) options.storePrefix = 'lfc';
@@ -9114,9 +9117,15 @@ class Peernet {
9114
9117
  * @access public
9115
9118
  * @type {PeernetClient}
9116
9119
  */
9117
- this.client = new importee.default(this.id);
9120
+ this.client = new importee.default(this.id, this.network, this.stars);
9118
9121
  if (globalThis.onbeforeunload) {
9119
9122
  globalThis.addEventListener('beforeunload', async () => this.client.close());
9123
+ } else {
9124
+ process.on('SIGTERM', async () => {
9125
+ process.stdin.resume();
9126
+ await this.client.close();
9127
+ process.exit();
9128
+ });
9120
9129
  }
9121
9130
  return this
9122
9131
  }
@@ -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,6 +462,7 @@ 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
 
466
468
  if (!options.storePrefix) options.storePrefix = 'lfc';
@@ -677,9 +679,15 @@ class Peernet {
677
679
  * @access public
678
680
  * @type {PeernetClient}
679
681
  */
680
- this.client = new importee.default(this.id);
682
+ this.client = new importee.default(this.id, this.network, this.stars);
681
683
  if (globalThis.onbeforeunload) {
682
684
  globalThis.addEventListener('beforeunload', async () => this.client.close());
685
+ } else {
686
+ process.on('SIGTERM', async () => {
687
+ process.stdin.resume();
688
+ await this.client.close();
689
+ process.exit();
690
+ });
683
691
  }
684
692
  return this
685
693
  }
@@ -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,6 +436,7 @@ 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
 
440
442
  if (!options.storePrefix) options.storePrefix = 'lfc';
@@ -651,9 +653,15 @@ class Peernet {
651
653
  * @access public
652
654
  * @type {PeernetClient}
653
655
  */
654
- this.client = new importee.default(this.id);
656
+ this.client = new importee.default(this.id, this.network, this.stars);
655
657
  if (globalThis.onbeforeunload) {
656
658
  globalThis.addEventListener('beforeunload', async () => this.client.close());
659
+ } else {
660
+ process.on('SIGTERM', async () => {
661
+ process.stdin.resume();
662
+ await this.client.close();
663
+ process.exit();
664
+ });
657
665
  }
658
666
  return this
659
667
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.14.26",
3
+ "version": "0.15.0",
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,6 +38,7 @@ 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
 
42
44
  if (!options.storePrefix) options.storePrefix = 'lfc'
@@ -253,9 +255,15 @@ export default class Peernet {
253
255
  * @access public
254
256
  * @type {PeernetClient}
255
257
  */
256
- this.client = new importee.default(this.id)
258
+ this.client = new importee.default(this.id, this.network, this.stars)
257
259
  if (globalThis.onbeforeunload) {
258
260
  globalThis.addEventListener('beforeunload', async () => this.client.close());
261
+ } else {
262
+ process.on('SIGTERM', async () => {
263
+ process.stdin.resume();
264
+ await this.client.close()
265
+ process.exit()
266
+ });
259
267
  }
260
268
  return this
261
269
  }