@toa.io/norm 1.0.0-alpha.30 → 1.0.0-alpha.300

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 (98) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/package.json +9 -7
  3. package/src/.component/.expand/bridge.js +2 -6
  4. package/src/.component/.expand/entity.js +2 -9
  5. package/src/.component/.expand/events.js +2 -6
  6. package/src/.component/.expand/extensions.js +5 -8
  7. package/src/.component/.expand/index.js +7 -19
  8. package/src/.component/.expand/operations.js +2 -9
  9. package/src/.component/.expand/receivers.js +4 -7
  10. package/src/.component/.expand/version.js +38 -28
  11. package/src/.component/.normalize/entity.js +7 -0
  12. package/src/.component/.normalize/events.js +16 -4
  13. package/src/.component/.normalize/index.js +4 -9
  14. package/src/.component/.normalize/operations.js +10 -6
  15. package/src/.component/.normalize/receivers.js +4 -5
  16. package/src/.component/collapse.js +60 -9
  17. package/src/.component/defaults.js +16 -10
  18. package/src/.component/dependencies.js +14 -10
  19. package/src/.component/dereference.js +20 -18
  20. package/src/.component/expand.js +3 -9
  21. package/src/.component/extensions.js +9 -10
  22. package/src/.component/index.js +10 -21
  23. package/src/.component/merge.js +25 -13
  24. package/src/.component/migrations.js +57 -0
  25. package/src/.component/normalize.js +10 -6
  26. package/src/.component/schema.yaml +137 -73
  27. package/src/.component/validate.js +37 -17
  28. package/src/.context/.dependencies/connectors.js +5 -9
  29. package/src/.context/.dependencies/describe.js +4 -5
  30. package/src/.context/.dependencies/extensions.js +93 -20
  31. package/src/.context/.dependencies/index.js +3 -9
  32. package/src/.context/.dependencies/resolve.js +44 -7
  33. package/src/.context/complete.js +1 -5
  34. package/src/.context/converge.js +31 -0
  35. package/src/.context/dependencies.js +43 -6
  36. package/src/.context/dereference.js +11 -6
  37. package/src/.context/evict.js +38 -0
  38. package/src/.context/expand.js +11 -6
  39. package/src/.context/index.js +8 -15
  40. package/src/.context/normalize.js +9 -7
  41. package/src/.context/schema.yaml +178 -11
  42. package/src/.context/validate.js +8 -14
  43. package/src/component.js +36 -17
  44. package/src/context.js +33 -14
  45. package/src/definition.js +56 -0
  46. package/src/entity.js +15 -0
  47. package/src/index.js +6 -9
  48. package/src/manifest.js +154 -0
  49. package/src/shortcuts.js +9 -14
  50. package/test/component/collapse.fixtures.js +30 -39
  51. package/test/component/collapse.test.js +90 -10
  52. package/test/component/dependencies.test.js +16 -14
  53. package/test/component/dereference.fixtures.js +15 -24
  54. package/test/component/dereference.test.js +18 -8
  55. package/test/component/dummies/extension/index.js +1 -5
  56. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yaml +2 -0
  57. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yml +2 -0
  58. package/test/component/dummies/migrations/malformed/migrations/0001-one.yaml +1 -0
  59. package/test/component/dummies/migrations/ordered/migrations/0001-first.yml +4 -0
  60. package/test/component/dummies/migrations/ordered/migrations/0002-second.yaml +4 -0
  61. package/test/component/dummies/migrations/ordered/migrations/0010-third.json +1 -0
  62. package/test/component/dummies/migrations/stateless/migrations/0001-one.yaml +2 -0
  63. package/test/component/expand.fixtures.js +13 -13
  64. package/test/component/expand.test.js +17 -13
  65. package/test/component/migrations.test.js +62 -0
  66. package/test/component/normalize.fixtures.js +3 -7
  67. package/test/component/normalize.test.js +86 -28
  68. package/test/component/validate.fixtures.js +15 -13
  69. package/test/component/validate.test.js +180 -109
  70. package/test/context/complete.fixtures.js +2 -8
  71. package/test/context/complete.test.js +14 -8
  72. package/test/context/converge.test.js +57 -0
  73. package/test/context/dereference.fixtures.js +3 -9
  74. package/test/context/dereference.test.js +18 -7
  75. package/test/context/evict.fixtures.js +39 -0
  76. package/test/context/evict.test.js +72 -0
  77. package/test/context/expand.fixtures.js +1 -5
  78. package/test/context/expand.test.js +36 -8
  79. package/test/context/normalize.fixtures.js +2 -7
  80. package/test/context/normalize.test.js +33 -9
  81. package/test/context/validate.fixtures.js +1 -6
  82. package/test/context/validate.test.js +143 -35
  83. package/test/definition.test.js +35 -0
  84. package/test/dummies/bare/index.js +1 -0
  85. package/test/dummies/defined/definition.js +1 -0
  86. package/test/dummies/defined/index.js +1 -0
  87. package/test/dummies/defined/package.json +1 -0
  88. package/test/manifest.test.js +126 -0
  89. package/test/shortcuts.fixtures.js +4 -9
  90. package/test/shortcuts.test.js +17 -15
  91. package/types/component.d.ts +23 -4
  92. package/types/context/declaration.d.ts +33 -3
  93. package/types/context.d.ts +19 -13
  94. package/types/entity.d.ts +7 -0
  95. package/types/index.d.ts +12 -3
  96. package/src/.component/.expand/properties.js +0 -14
  97. package/src/.context/.dependencies/load.js +0 -36
  98. package/test/context/dependencies.load.test.js +0 -24
@@ -1,16 +1,12 @@
1
- 'use strict'
2
-
3
- const source = {
1
+ export const source = {
4
2
  entity: {
5
- schema: {
6
- properties: {
7
- foo: {
8
- type: 'string'
9
- },
10
- bar: {
11
- type: 'string',
12
- default: '.foo'
13
- }
3
+ properties: {
4
+ foo: {
5
+ type: 'string'
6
+ },
7
+ bar: {
8
+ type: 'string',
9
+ default: '.foo'
14
10
  }
15
11
  }
16
12
  },
@@ -66,16 +62,14 @@ const source = {
66
62
  }
67
63
  }
68
64
 
69
- const target = {
65
+ export const target = {
70
66
  entity: {
71
- schema: {
72
- properties: {
73
- foo: {
74
- type: 'string'
75
- },
76
- bar: {
77
- type: 'string'
78
- }
67
+ properties: {
68
+ foo: {
69
+ type: 'string'
70
+ },
71
+ bar: {
72
+ type: 'string'
79
73
  }
80
74
  }
81
75
  },
@@ -160,6 +154,3 @@ const target = {
160
154
  }
161
155
  }
162
156
  }
163
-
164
- exports.source = source
165
- exports.target = target
@@ -1,9 +1,10 @@
1
- 'use strict'
1
+ import { it, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const clone = require('clone-deep')
4
+ import clone from 'clone-deep'
4
5
 
5
- const { dereference } = require('../../src/.component')
6
- const fixtures = require('./dereference.fixtures')
6
+ import { dereference } from '../../src/.component/index.js'
7
+ import * as fixtures from './dereference.fixtures.js'
7
8
 
8
9
  let source
9
10
 
@@ -14,18 +15,27 @@ beforeEach(() => {
14
15
  it('should dereference', () => {
15
16
  dereference(source)
16
17
 
17
- expect(source).toStrictEqual(fixtures.target)
18
+ assert.deepStrictEqual(source, fixtures.target)
18
19
  })
19
20
 
20
21
  it('should throw on invalid schema reference', () => {
21
22
  source.operations.transit.output.properties.baz = { type: 'string', default: '.' }
22
- expect(() => dereference(source)).toThrow(/is not defined/)
23
+ assert.throws(
24
+ () => dereference(source),
25
+ (error) => /is not defined/.test(error.message)
26
+ )
23
27
 
24
28
  source.operations.transit.output.properties.baz = { type: 'string', default: '.baz' }
25
- expect(() => dereference(source)).toThrow(/is not defined/)
29
+ assert.throws(
30
+ () => dereference(source),
31
+ (error) => /is not defined/.test(error.message)
32
+ )
26
33
  })
27
34
 
28
35
  it('should throw on invalid forwarding', () => {
29
36
  source.operations.create.forward = 'foo'
30
- expect(() => dereference(source)).toThrow(/is not defined/)
37
+ assert.throws(
38
+ () => dereference(source),
39
+ (error) => /is not defined/.test(error.message)
40
+ )
31
41
  })
@@ -1,9 +1,5 @@
1
- 'use strict'
2
-
3
- const manifest = (declaration) => {
1
+ export const manifest = (declaration) => {
4
2
  if (declaration.ok !== true) return
5
3
 
6
4
  return declaration
7
5
  }
8
-
9
- exports.manifest = manifest
@@ -0,0 +1,4 @@
1
+ - index:
2
+ name: index_a
3
+ keys:
4
+ a: asc
@@ -0,0 +1,4 @@
1
+ - index:
2
+ name: index_b
3
+ keys:
4
+ b: asc
@@ -0,0 +1 @@
1
+ [{ "delete": { "filter": {} } }]
@@ -1,11 +1,10 @@
1
- 'use strict'
2
-
3
- const source = {
4
- path: '',
1
+ export const source = {
2
+ path: import.meta.dirname, // `version` hashes the component directory
5
3
  entity: {
6
4
  schema: {
5
+ type: 'object',
7
6
  properties: {
8
- foo: 'string'
7
+ foo: { type: 'string' }
9
8
  }
10
9
  },
11
10
  storage: 'mongodb'
@@ -16,26 +15,30 @@ const source = {
16
15
  bridge: 'node',
17
16
  bindings: ['amqp'],
18
17
  input: {
18
+ type: 'object',
19
19
  properties: {
20
- foo: 'integer',
20
+ foo: { type: 'integer' },
21
21
  bar: {
22
22
  type: 'array',
23
23
  items: {
24
+ type: 'object',
24
25
  properties: {
25
- baz: 'string'
26
+ baz: { type: 'string' }
26
27
  }
27
28
  }
28
29
  }
29
30
  }
30
31
  },
31
32
  output: {
33
+ type: 'object',
32
34
  properties: {
33
- foo: 'integer',
35
+ foo: { type: 'integer' },
34
36
  bar: {
35
37
  type: 'array',
36
38
  items: {
39
+ type: 'object',
37
40
  properties: {
38
- baz: 'string'
41
+ baz: { type: 'string' }
39
42
  }
40
43
  }
41
44
  }
@@ -59,7 +62,7 @@ const source = {
59
62
  }
60
63
  }
61
64
 
62
- const target = {
65
+ export const target = {
63
66
  entity: {
64
67
  schema: {
65
68
  type: 'object',
@@ -133,6 +136,3 @@ const target = {
133
136
  }
134
137
  }
135
138
  }
136
-
137
- exports.source = source
138
- exports.target = target
@@ -1,9 +1,10 @@
1
- 'use strict'
1
+ import { it, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const clone = require('clone-deep')
4
+ import clone from 'clone-deep'
4
5
 
5
- const { expand } = require('../../src/.component')
6
- const fixtures = require('./expand.fixtures')
6
+ import { expand } from '../../src/.component/index.js'
7
+ import * as fixtures from './expand.fixtures.js'
7
8
 
8
9
  let source
9
10
 
@@ -11,18 +12,21 @@ beforeEach(() => {
11
12
  source = clone(fixtures.source)
12
13
  })
13
14
 
14
- it('should expand', () => {
15
- expand(source)
16
- expect(source).toMatchObject(fixtures.target)
15
+ it('should expand', async () => {
16
+ await expand(source)
17
+ assert.partialDeepStrictEqual(source, fixtures.target)
17
18
  })
18
19
 
19
- it('should recognize storages.queues', async () => {
20
- const queues = { foo: 'bar' }
20
+ it('should derive version from the component contents', async () => {
21
+ await expand(source)
21
22
 
22
- source.queues = clone(queues)
23
+ assert.match(source.version, /^[0-9a-f]{8}$/)
24
+ })
25
+
26
+ it('should keep declared version', async () => {
27
+ source.version = '1.0.0'
23
28
 
24
- expand(source)
29
+ await expand(source)
25
30
 
26
- expect(source.queues).toBeUndefined()
27
- expect(source.properties['@toa.io/storages.queues']).toMatchObject(queues)
31
+ assert.deepStrictEqual(source.version, '1.0.0')
28
32
  })
@@ -0,0 +1,62 @@
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { join } from 'node:path'
4
+
5
+ import { migrations } from '../../src/.component/index.js'
6
+
7
+ const DUMMIES = join(import.meta.dirname, 'dummies/migrations')
8
+
9
+ const entity = () => ({ entity: { schema: { properties: {} } } })
10
+
11
+ describe('migrations', () => {
12
+ it('should read a directory of migrations', async () => {
13
+ const manifest = entity()
14
+
15
+ await migrations(join(DUMMIES, 'ordered'), manifest)
16
+
17
+ assert.deepEqual(
18
+ manifest.entity.migrations.map(({ id }) => id),
19
+ ['0001-first', '0002-second', '0010-third']
20
+ )
21
+ })
22
+
23
+ it('should read whatever extension each was written with', async () => {
24
+ const manifest = entity()
25
+
26
+ await migrations(join(DUMMIES, 'ordered'), manifest)
27
+
28
+ assert.deepEqual(manifest.entity.migrations[0].steps, [
29
+ { index: { name: 'index_a', keys: { a: 'asc' } } }
30
+ ])
31
+ assert.deepEqual(manifest.entity.migrations[2].steps, [{ delete: { filter: {} } }])
32
+ })
33
+
34
+ it('should leave a component with no migrations alone', async () => {
35
+ const manifest = entity()
36
+
37
+ await migrations(join(DUMMIES, 'nothing-here'), manifest)
38
+
39
+ assert.equal(manifest.entity.migrations, undefined)
40
+ })
41
+
42
+ it('should refuse two files resolving to one id', async () => {
43
+ await assert.rejects(
44
+ migrations(join(DUMMIES, 'conflicting'), entity()),
45
+ /has more than one migrations\/0001-one/
46
+ )
47
+ })
48
+
49
+ it('should refuse migrations on a component that stores nothing', async () => {
50
+ await assert.rejects(
51
+ migrations(join(DUMMIES, 'stateless'), {}),
52
+ /declares migrations but stores nothing/
53
+ )
54
+ })
55
+
56
+ it('should refuse a migration that is not a list of steps', async () => {
57
+ await assert.rejects(
58
+ migrations(join(DUMMIES, 'malformed'), entity()),
59
+ /is not a list of steps/
60
+ )
61
+ })
62
+ })
@@ -1,11 +1,9 @@
1
- 'use strict'
1
+ import { generate } from 'randomstring'
2
2
 
3
- const { generate } = require('randomstring')
4
-
5
- const operations = {
3
+ export const operations = {
6
4
  namespace: 'dummies',
7
5
  name: 'dummy',
8
- path: __dirname,
6
+ path: import.meta.dirname,
9
7
  bindings: ['foo', 'bar'],
10
8
  'bindings@local': ['foo'],
11
9
  operations: {
@@ -22,5 +20,3 @@ const operations = {
22
20
  }
23
21
  }
24
22
  }
25
-
26
- exports.operations = operations
@@ -1,10 +1,10 @@
1
- 'use strict'
1
+ import { describe, it, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const { dirname } = require('node:path')
4
- const clone = require('clone-deep')
4
+ import clone from 'clone-deep'
5
5
 
6
- const { normalize } = require('../../src/.component')
7
- const fixtures = require('./normalize.fixtures')
6
+ import { normalize, extensions } from '../../src/.component/index.js'
7
+ import * as fixtures from './normalize.fixtures.js'
8
8
 
9
9
  let manifest
10
10
 
@@ -13,51 +13,109 @@ beforeEach(() => {
13
13
  })
14
14
 
15
15
  describe('operations', () => {
16
- it('should set default bindings', () => {
17
- normalize(manifest)
16
+ it('should set default bindings', async () => {
17
+ await normalize(manifest)
18
18
 
19
- expect(manifest.operations.add.bindings).toStrictEqual(manifest.bindings)
19
+ assert.deepStrictEqual(manifest.operations.add.bindings, manifest.bindings)
20
+ })
21
+
22
+ it('should state an empty output where none is declared', async () => {
23
+ await normalize(manifest)
24
+
25
+ assert.deepStrictEqual(manifest.operations.add.output, {})
26
+ })
27
+
28
+ it('should scope a computation to none', async () => {
29
+ manifest.operations.compute = { type: 'computation' }
30
+
31
+ await normalize(manifest)
32
+
33
+ assert.deepStrictEqual(manifest.operations.compute.scope, 'none')
34
+ assert.deepStrictEqual(manifest.operations.compute.query, false)
35
+ })
36
+
37
+ it('should scope an effect to none unless it says otherwise', async () => {
38
+ manifest.operations.affect = { type: 'effect' }
39
+ manifest.operations.reach = { type: 'effect', scope: 'objects' }
40
+
41
+ await normalize(manifest)
42
+
43
+ assert.deepStrictEqual(manifest.operations.affect.scope, 'none')
44
+ assert.deepStrictEqual(manifest.operations.reach.scope, 'objects')
20
45
  })
21
46
  })
22
47
 
48
+ describe('namespace', () => {
49
+ it('should default to `default`', async () => {
50
+ delete manifest.namespace
51
+
52
+ await normalize(manifest)
53
+
54
+ assert.deepStrictEqual(manifest.namespace, 'default')
55
+ })
56
+ })
57
+
58
+ describe('entity', () => {
59
+ it('should default storage and flags', async () => {
60
+ manifest.entity = { properties: {} }
61
+
62
+ await normalize(manifest)
63
+
64
+ assert.deepStrictEqual(manifest.entity.storage, '@toa.io/storages.mongodb')
65
+ assert.deepStrictEqual(manifest.entity.associated, false)
66
+ assert.deepStrictEqual(manifest.entity.custom, false)
67
+ })
68
+
69
+ it('should allow a date-time on the record', async () => {
70
+ manifest.entity = { properties: { settled: { type: 'string', format: 'date-time' } } }
71
+
72
+ await normalize(manifest)
73
+
74
+ assert.deepStrictEqual(manifest.entity.properties.settled.format, 'date-time')
75
+ })
76
+
77
+
78
+ })
79
+
23
80
  describe('extensions', () => {
24
- it('should resolve absolute references', () => {
25
- const origins = manifest.extensions['@toa.io/extensions.origins']
26
- const path = dirname(require.resolve('@toa.io/extensions.origins/package.json'))
81
+ it('should add predefined extensions', async () => {
82
+ await extensions(manifest)
27
83
 
28
- normalize(manifest)
84
+ assert.strictEqual(manifest.extensions['@toa.io/extensions.telemetry'], null)
85
+ assert.strictEqual(manifest.extensions['@toa.io/extensions.fetch'], null)
86
+ })
87
+
88
+ it('should add predefined extensions without explicit declarations', async () => {
89
+ delete manifest.extensions
29
90
 
30
- expect(manifest.extensions[path]).toStrictEqual(origins)
91
+ await extensions(manifest)
92
+
93
+ assert.deepStrictEqual(manifest.extensions, {
94
+ '@toa.io/extensions.telemetry': null,
95
+ '@toa.io/extensions.fetch': null,
96
+ '@toa.io/extensions.introspection': {}
97
+ })
31
98
  })
32
99
  })
33
100
 
34
101
  describe('receivers', () => {
35
102
  it('should substitute default namespace', async () => {
36
103
  manifest.receivers = {
37
- 'messages.created': 'add'
104
+ 'messages.created': { operation: 'add' }
38
105
  }
39
106
 
40
- normalize(manifest)
107
+ await normalize(manifest)
41
108
 
42
- expect(manifest.receivers).toStrictEqual({
43
- 'default.messages.created': 'add'
44
- })
109
+ assert.deepStrictEqual(Object.keys(manifest.receivers), ['default.messages.created'])
45
110
  })
46
111
 
47
112
  it('should not substitute default namespace for foreign events', async () => {
48
- const receiver = {
49
- transition: 'add',
50
- source: 'test'
51
- }
52
-
53
113
  manifest.receivers = {
54
- 'messages.created': receiver
114
+ 'messages.created': { transition: 'add', source: 'test' }
55
115
  }
56
116
 
57
- normalize(manifest)
117
+ await normalize(manifest)
58
118
 
59
- expect(manifest.receivers).toStrictEqual({
60
- 'messages.created': receiver
61
- })
119
+ assert.deepStrictEqual(Object.keys(manifest.receivers), ['messages.created'])
62
120
  })
63
121
  })
@@ -1,17 +1,15 @@
1
- 'use strict'
2
-
3
- const ok = {
1
+ export const ok = {
4
2
  namespace: 'foo',
5
3
  name: 'bar',
6
4
  entity: {
7
5
  storage: 'whatever',
8
- schema: {
9
- properties: {
10
- name: {
11
- type: 'string',
12
- minLength: 1,
13
- maxLength: 128
14
- }
6
+ associated: false,
7
+ custom: false,
8
+ properties: {
9
+ name: {
10
+ type: 'string',
11
+ minLength: 1,
12
+ maxLength: 128
15
13
  }
16
14
  }
17
15
  },
@@ -37,11 +35,15 @@ const ok = {
37
35
  },
38
36
  compute: {
39
37
  type: 'computation',
38
+ scope: 'none',
39
+ query: false,
40
40
  bridge: 'whatever',
41
41
  bindings: ['@toa.io/bindings.amqp']
42
42
  },
43
43
  affect: {
44
44
  type: 'effect',
45
+ scope: 'none',
46
+ query: false,
45
47
  bridge: 'whatever',
46
48
  bindings: ['@toa.io/bindings.amqp']
47
49
  }
@@ -60,9 +62,9 @@ const ok = {
60
62
  operation: 'add',
61
63
  bridge: 'whatever',
62
64
  binding: 'amqp',
63
- path: '/somewhere'
65
+ path: '/somewhere',
66
+ conditioned: false,
67
+ adaptive: false
64
68
  }
65
69
  }
66
70
  }
67
-
68
- exports.ok = ok