@toa.io/norm 0.7.1 → 0.7.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/norm",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Toa declarations normalization and validation",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -19,7 +19,7 @@
19
19
  "test": "echo \"Error: run tests from root\" && exit 1"
20
20
  },
21
21
  "devDependencies": {
22
- "@toa.io/mock": "0.7.1"
22
+ "@toa.io/mock": "0.7.2"
23
23
  },
24
24
  "dependencies": {
25
25
  "@toa.io/core": "0.7.0",
@@ -28,5 +28,5 @@
28
28
  "@toa.io/schemas": "0.7.0",
29
29
  "@toa.io/yaml": "0.7.0"
30
30
  },
31
- "gitHead": "8a50eb96d584d45797d37a64e9a8e019dd956e86"
31
+ "gitHead": "42b78a9a964d1d8b2925bcf1cc02c737c48d0304"
32
32
  }
@@ -0,0 +1,29 @@
1
+ 'use strict'
2
+
3
+ const { join, dirname } = require('node:path')
4
+
5
+ /**
6
+ * @param {toa.norm.Component} component
7
+ */
8
+ const dependencies = (component) => {
9
+ if ('entity' in component) component.entity.storage = resolve(component.path, component.entity.storage)
10
+ }
11
+
12
+ function resolve (root, reference) {
13
+ const paths = [root, __dirname]
14
+ const options = { paths }
15
+
16
+ let path
17
+
18
+ try { // as package
19
+ const packageJsonRef = join(reference, 'package.json')
20
+
21
+ path = require.resolve(packageJsonRef, options)
22
+ } catch { // as directory
23
+ path = require.resolve(reference, options)
24
+ }
25
+
26
+ return dirname(path)
27
+ }
28
+
29
+ exports.dependencies = dependencies
@@ -2,6 +2,7 @@
2
2
 
3
3
  const { collapse } = require('./collapse')
4
4
  const { defaults } = require('./defaults')
5
+ const { dependencies } = require('./dependencies')
5
6
  const { dereference } = require('./dereference')
6
7
  const { expand } = require('./expand')
7
8
  const { merge } = require('./merge')
@@ -10,6 +11,7 @@ const { validate } = require('./validate')
10
11
 
11
12
  exports.collapse = collapse
12
13
  exports.defaults = defaults
14
+ exports.dependencies = dependencies
13
15
  exports.dereference = dereference
14
16
  exports.expand = expand
15
17
  exports.merge = merge
@@ -0,0 +1,36 @@
1
+ 'use strict'
2
+
3
+ const { join } = require('node:path')
4
+
5
+ /**
6
+ * @param {string} path
7
+ * @returns {{ metadata: object, module: object }}
8
+ */
9
+ function load (path) {
10
+ const metadata = loadMetadata(path)
11
+ const module = loadModule(path)
12
+
13
+ return { metadata, module }
14
+ }
15
+
16
+ function loadMetadata (reference) {
17
+ reference = join(reference, 'package.json')
18
+
19
+ try {
20
+ return require(reference)
21
+ } catch {
22
+ return null
23
+ }
24
+ }
25
+
26
+ /**
27
+ * @param {string} reference
28
+ * @returns {object}
29
+ */
30
+ function loadModule (reference) {
31
+ const path = require.resolve(reference)
32
+
33
+ return require(path)
34
+ }
35
+
36
+ exports.load = load
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { join } = require('node:path')
4
+ const { load } = require('./load')
4
5
 
5
6
  /**
6
7
  * @param {toa.norm.context.dependencies.References} references
@@ -12,7 +13,8 @@ const resolve = (references, annotations) => {
12
13
  const dependencies = {}
13
14
 
14
15
  for (const [dependency, components] of Object.entries(references)) {
15
- const id = name(dependency)
16
+ const { metadata, module } = load(dependency)
17
+ const id = metadata.name
16
18
 
17
19
  const instances = components.map((component) => ({
18
20
  locator: component.locator,
@@ -22,7 +24,6 @@ const resolve = (references, annotations) => {
22
24
  dependencies[dependency] = instances
23
25
 
24
26
  const annotation = annotations?.[id]
25
- const module = require(dependency)
26
27
 
27
28
  if (annotation !== undefined && module.annotation !== undefined) {
28
29
  annotations[id] = module.annotation(annotation, instances)
@@ -32,14 +33,4 @@ const resolve = (references, annotations) => {
32
33
  return dependencies
33
34
  }
34
35
 
35
- /**
36
- * @param {string} dependency
37
- * @returns {string}
38
- */
39
- const name = (dependency) => {
40
- const pkg = require(join(dependency, 'package.json'))
41
-
42
- return pkg.name
43
- }
44
-
45
36
  exports.resolve = resolve
package/src/component.js CHANGED
@@ -13,6 +13,7 @@ const {
13
13
  collapse,
14
14
  dereference,
15
15
  defaults,
16
+ dependencies,
16
17
  normalize
17
18
  } = require('./.component')
18
19
 
@@ -32,7 +33,7 @@ const load = async (path, base) => {
32
33
  if (base !== undefined) path = find(path, base, MANIFEST)
33
34
 
34
35
  const file = join(path, MANIFEST)
35
- const manifest = await yaml(file)
36
+ const manifest = /** @type {toa.norm.Component} */ await yaml(file)
36
37
 
37
38
  manifest.path = path
38
39
 
@@ -50,6 +51,7 @@ const load = async (path, base) => {
50
51
  }
51
52
 
52
53
  dereference(manifest)
54
+ dependencies(manifest)
53
55
 
54
56
  return manifest
55
57
  }
@@ -0,0 +1,38 @@
1
+ 'use strict'
2
+
3
+ const { resolve } = require('node:path')
4
+ const { dependencies } = require('../../src/.component')
5
+
6
+ const NORM = resolve(__dirname, '../../')
7
+
8
+ it('should be', async () => {
9
+ expect(dependencies).toBeInstanceOf(Function)
10
+ })
11
+
12
+ /** @type {toa.norm.Component} */
13
+ let component
14
+
15
+ beforeEach(() => {
16
+ component = /** @type {toa.norm.Component} */ { path: __dirname }
17
+ })
18
+
19
+ describe.each(/** @type {[string, string][]} */ [
20
+ ['package id', '@toa.io/norm'],
21
+ ['relative path', '../../']
22
+ ])('%s', (_, reference) => {
23
+ it('should resolve storage', async () => {
24
+ component.entity = { storage: reference, schema: {} }
25
+
26
+ dependencies(component)
27
+
28
+ expect(component.entity.storage).toStrictEqual(NORM)
29
+ })
30
+ })
31
+
32
+ it('should resolve toa packages', async () => {
33
+ component.entity = { storage: '@toa.io/core', schema: {} }
34
+
35
+ dependencies(component)
36
+
37
+ expect(component.entity.storage).toBeDefined()
38
+ })
@@ -0,0 +1,24 @@
1
+ 'use strict'
2
+
3
+ const { join } = require('node:path')
4
+ const { load } = require('../../src/.context/.dependencies/load')
5
+
6
+ it('should be', async () => {
7
+ expect(load).toBeInstanceOf(Function)
8
+ })
9
+
10
+ it('should load module', async () => {
11
+ const path = join(__dirname, '../../')
12
+ const { metadata, module } = load(path)
13
+
14
+ expect(metadata.name).toStrictEqual('@toa.io/norm')
15
+ expect(module.context).toBeInstanceOf(Function)
16
+ })
17
+
18
+ it('should return null metadata if no package.json', async () => {
19
+ const path = join(__dirname, '../../src')
20
+ const { metadata, module } = load(path)
21
+
22
+ expect(metadata).toBeNull()
23
+ expect(module.context).toBeInstanceOf(Function)
24
+ })
@@ -45,12 +45,13 @@ export namespace toa.norm {
45
45
  }
46
46
 
47
47
  type Entity = {
48
- schema: object
48
+ schema: Object
49
49
  storage?: string
50
50
  initialized?: boolean
51
51
  }
52
52
 
53
53
  interface Declaration {
54
+ prototype: string
54
55
  namespace: string
55
56
  name: string
56
57
  version: string
@@ -60,7 +61,7 @@ export namespace toa.norm {
60
61
  events?: Events
61
62
  receivers: Record<string, Receiver>
62
63
  extensions?: Record<string, object>
63
- properties: Record<string, object>
64
+ properties?: Record<string, object>
64
65
  }
65
66
 
66
67
  type Constructor = (path: string) => Promise<Component>