@superhero/core 4.0.1 → 4.0.3

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.
Files changed (2) hide show
  1. package/index.js +48 -30
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -2,6 +2,7 @@ import os from 'node:os'
2
2
  import cluster from 'node:cluster'
3
3
  import EventEmitter from 'node:events'
4
4
  import path from 'node:path'
5
+ import url from 'node:url'
5
6
  import bootstrap from '@superhero/bootstrap'
6
7
  import Locate from '@superhero/locator'
7
8
  import Log from '@superhero/log'
@@ -289,7 +290,7 @@ export default class Core
289
290
  }
290
291
  else if(false === this.#isBooted)
291
292
  {
292
- await this.#confirmAndAddDependencies()
293
+ await this.#addDependencies()
293
294
  await this.#confirmAbsoluteServcieMapPaths()
294
295
 
295
296
  freeze && this.config.freeze()
@@ -331,8 +332,11 @@ export default class Core
331
332
  this.#isForked = true
332
333
 
333
334
  // Set the worker execution script.
334
- const primaryConfig = this.config.find('core/cluster/primary', { exec: './worker.js' })
335
- cluster.setupPrimary(primaryConfig)
335
+ const
336
+ pwd = path.dirname(url.fileURLToPath(import.meta.url)),
337
+ primaryClusterConfig = this.config.find('core/cluster/primary', { exec: path.join(pwd, 'worker.js') })
338
+
339
+ cluster.setupPrimary(primaryClusterConfig)
336
340
 
337
341
  for(let i = 0; i < forks; i++)
338
342
  {
@@ -604,61 +608,75 @@ export default class Core
604
608
  }
605
609
  }
606
610
 
607
- async #confirmAndAddDependencies()
611
+ async #addDependencies()
608
612
  {
609
- const loaded = []
610
-
611
- let postLoaded, dependencies
613
+ let
614
+ dependencies = {},
615
+ loaded = []
612
616
 
613
617
  do
614
618
  {
615
- dependencies = this.#findNotLodadedDependencies(this.config, loaded)
616
- await this.add(dependencies)
617
- postLoaded = this.#findNotLodadedDependencies(this.config, loaded)
618
- loaded.push(...postLoaded)
619
+ loaded = Object.keys(dependencies)
620
+ dependencies = this.#loadNewDependencies(loaded)
621
+ await this.add(Object.values(dependencies))
619
622
  }
620
- while(postLoaded.length)
623
+ while(Object.keys(dependencies).length < loaded.length)
621
624
 
622
625
  return this
623
626
  }
624
627
 
625
- #findNotLodadedDependencies(loaded)
628
+ #loadNewDependencies(loaded)
626
629
  {
627
- const dependencies = this.config.find('core/dependencies',
628
- this.config.find('core/dependency', []))
629
-
630
- if(false === Array.isArray(dependencies))
630
+ const
631
+ dependencies = this.config.find('dependency', {}),
632
+ dependenciesType = Object.prototype.toString.call(dependencies)
633
+
634
+ if('[object Object]' !== dependenciesType)
631
635
  {
632
- const error = new TypeError(`Invalid dependencies type: ${Object.prototype.toString.call(dependencies)}`)
633
- error.code = 'E_CORE_CONFIG_DEPENDENCY_INVALID_TYPE'
634
- error.cause = 'Config core/dependencies must be an array'
636
+ const error = new TypeError(`Invalid configured dependency type: ${dependenciesType}`)
637
+ error.code = 'E_CORE_INVALID_DEPENDENCY_TYPE'
635
638
  throw error
636
639
  }
637
640
 
638
- for(let i = 0; i < dependencies.length; i++)
641
+ for(const id in dependencies)
639
642
  {
640
- const dependency = dependencies[i]
643
+ if(loaded.includes(id))
644
+ {
645
+ continue
646
+ }
647
+
648
+ let dependencyPath = dependencies[id]
649
+
650
+ if(false === dependencyPath)
651
+ {
652
+ continue
653
+ }
654
+
655
+ if(true === path)
656
+ {
657
+ dependencyPath = id
658
+ }
641
659
 
642
- if('string' === typeof dependency)
660
+ if('string' !== typeof dependencyPath)
643
661
  {
644
- const error = new TypeError(`Invalid dependency type: ${Object.prototype.toString.call(dependency)}`)
645
- error.code = 'E_CORE_CONFIG_DEPENDENCY_INVALID_TYPE'
646
- error.cause = 'The dependency values in config core/dependencies must be of type string'
662
+ const error = new TypeError(`Invalid dependency path type: ${Object.prototype.toString.call(dependencyPath)}`)
663
+ error.code = 'E_CORE_INVALID_DEPENDENCY_PATH_TYPE'
664
+ error.cause = 'The values of configured dependency attribute must be of type string'
647
665
  throw error
648
666
  }
649
667
 
650
- if(dependency.startsWith('.'))
668
+ if(dependencyPath.startsWith('.'))
651
669
  {
652
- const absolutePath = this.config.findAbsoluteDirPathByConfigEntry('core/dependencies', [ dependency ])
670
+ const absolutePath = this.config.findAbsoluteDirPathByConfigEntry('dependency', { [id]: dependencies[id] })
653
671
 
654
672
  if('string' === typeof absolutePath)
655
673
  {
656
- dependencies[i] = path.normalize(path.join(absolutePath, dependency))
674
+ dependencies[i] = path.normalize(path.join(absolutePath, dependencyPath))
657
675
  }
658
676
  }
659
677
  }
660
678
 
661
- return dependencies.filter((dependency) => false === loaded.includes(dependency))
679
+ return dependencies
662
680
  }
663
681
 
664
682
  #normalizeConfigPaths(configPaths)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superhero/core",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
4
4
  "description": "Core functionalities for the superhero framework.",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
@@ -9,8 +9,8 @@
9
9
  ".": "./index.js"
10
10
  },
11
11
  "dependencies": {
12
- "@superhero/bootstrap": "^4.1.1",
13
- "@superhero/locator": "^4.2.2"
12
+ "@superhero/locator": "^4.2.3",
13
+ "@superhero/bootstrap": "^4.1.2"
14
14
  },
15
15
  "scripts": {
16
16
  "test": "node --trace-warnings --test --experimental-test-coverage"