@toa.io/norm 0.2.1-dev.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.
Files changed (71) hide show
  1. package/features/component.operations.feature +54 -0
  2. package/features/steps/.test/manifest.test.js +110 -0
  3. package/features/steps/.types/context.d.ts +9 -0
  4. package/features/steps/hooks.js +18 -0
  5. package/features/steps/manifest.js +66 -0
  6. package/features/steps/parameters.js +9 -0
  7. package/package.json +31 -0
  8. package/src/.component/.expand/bridge.js +9 -0
  9. package/src/.component/.expand/entity.js +14 -0
  10. package/src/.component/.expand/events.js +14 -0
  11. package/src/.component/.expand/extensions.js +10 -0
  12. package/src/.component/.expand/index.js +15 -0
  13. package/src/.component/.expand/operations.js +17 -0
  14. package/src/.component/.expand/receivers.js +16 -0
  15. package/src/.component/.normalize/events.js +13 -0
  16. package/src/.component/.normalize/extensions.js +35 -0
  17. package/src/.component/.normalize/index.js +9 -0
  18. package/src/.component/.normalize/operations.js +12 -0
  19. package/src/.component/collapse.js +44 -0
  20. package/src/.component/defaults.js +25 -0
  21. package/src/.component/dereference.js +72 -0
  22. package/src/.component/expand.js +14 -0
  23. package/src/.component/index.js +17 -0
  24. package/src/.component/merge.js +55 -0
  25. package/src/.component/normalize.js +13 -0
  26. package/src/.component/schema.yaml +222 -0
  27. package/src/.component/validate.js +40 -0
  28. package/src/.context/.dependencies/connectors.js +44 -0
  29. package/src/.context/.dependencies/describe.js +10 -0
  30. package/src/.context/.dependencies/extensions.js +24 -0
  31. package/src/.context/.dependencies/index.js +9 -0
  32. package/src/.context/.dependencies/resolve.js +45 -0
  33. package/src/.context/complete.js +33 -0
  34. package/src/.context/dependencies.js +16 -0
  35. package/src/.context/dereference.js +31 -0
  36. package/src/.context/expand.js +14 -0
  37. package/src/.context/index.js +15 -0
  38. package/src/.context/normalize.js +19 -0
  39. package/src/.context/schema.yaml +60 -0
  40. package/src/.context/validate.js +19 -0
  41. package/src/component.js +59 -0
  42. package/src/context.js +48 -0
  43. package/src/index.js +9 -0
  44. package/src/shortcuts.js +52 -0
  45. package/test/component/collapse.fixtures.js +134 -0
  46. package/test/component/collapse.test.js +81 -0
  47. package/test/component/dereference.fixtures.js +165 -0
  48. package/test/component/dereference.test.js +31 -0
  49. package/test/component/dummies/extension/index.js +9 -0
  50. package/test/component/dummies/extension/package.json +5 -0
  51. package/test/component/expand.fixtures.js +137 -0
  52. package/test/component/expand.test.js +17 -0
  53. package/test/component/normalize.fixtures.js +22 -0
  54. package/test/component/normalize.test.js +45 -0
  55. package/test/component/validate.fixtures.js +58 -0
  56. package/test/component/validate.test.js +250 -0
  57. package/test/context/complete.fixtures.js +59 -0
  58. package/test/context/complete.test.js +31 -0
  59. package/test/context/dereference.fixtures.js +60 -0
  60. package/test/context/dereference.test.js +24 -0
  61. package/test/context/expand.fixtures.js +5 -0
  62. package/test/context/expand.test.js +41 -0
  63. package/test/context/normalize.fixtures.js +32 -0
  64. package/test/context/normalize.test.js +34 -0
  65. package/test/context/validate.fixtures.js +38 -0
  66. package/test/context/validate.test.js +91 -0
  67. package/test/shortcuts.fixtures.js +17 -0
  68. package/test/shortcuts.test.js +92 -0
  69. package/types/component.d.ts +78 -0
  70. package/types/context.d.ts +75 -0
  71. package/types/index.d.ts +5 -0
@@ -0,0 +1,5 @@
1
+ 'use strict'
2
+
3
+ const { context } = require('./validate.fixtures')
4
+
5
+ exports.context = context
@@ -0,0 +1,41 @@
1
+ 'use strict'
2
+
3
+ const clone = require('clone-deep')
4
+ const { generate } = require('randomstring')
5
+
6
+ const { expand } = require('../../src/.context')
7
+ const fixtures = require('./expand.fixtures')
8
+
9
+ /** @type {toa.norm.context.Declaration | object} */
10
+ let context
11
+
12
+ beforeEach(() => {
13
+ context = clone(fixtures.context)
14
+ })
15
+
16
+ describe('annotations', () => {
17
+ it('should not throw without annotations', () => {
18
+ delete context.annotations
19
+
20
+ expect(() => expand(context)).not.toThrow()
21
+ })
22
+
23
+ it('should expand known annotations', () => {
24
+ const exposition = context.annotations['@toa.io/extensions.exposition']
25
+
26
+ delete context.annotations
27
+ context.exposition = exposition
28
+
29
+ expand(context)
30
+
31
+ expect(context.annotations).toStrictEqual(fixtures.context.annotations)
32
+ })
33
+
34
+ it('should recognize annotations', () => {
35
+ context.annotations.mongodb = generate()
36
+
37
+ expand(context)
38
+
39
+ expect(context.annotations['@toa.io/storages.mongodb']).toBeDefined()
40
+ })
41
+ })
@@ -0,0 +1,32 @@
1
+ 'use strict'
2
+
3
+ const { generate } = require('randomstring')
4
+
5
+ const context = {
6
+ runtime: {
7
+ version: '0.0.0'
8
+ },
9
+ name: 'test',
10
+ description: 'context fixture',
11
+ version: '0.0.0',
12
+ packages: 'namespaces/**/*',
13
+ registry: 'localhost:5000',
14
+ compositions: [
15
+ {
16
+ name: 'foo',
17
+ components: ['a.b', 'b.a']
18
+ },
19
+ {
20
+ name: 'bar',
21
+ components: ['d.c', 'a.b']
22
+ }
23
+ ],
24
+ annotations: {
25
+ test: {
26
+ target: generate(),
27
+ 'target@staging': generate()
28
+ }
29
+ }
30
+ }
31
+
32
+ exports.context = context
@@ -0,0 +1,34 @@
1
+ 'use strict'
2
+
3
+ const clone = require('clone-deep')
4
+ const { generate } = require('randomstring')
5
+
6
+ const fixtures = require('./normalize.fixtures')
7
+ const { normalize } = require('../../src/.context')
8
+
9
+ let context
10
+
11
+ beforeEach(() => {
12
+ context = clone(fixtures.context)
13
+ })
14
+
15
+ it('should resolve local version', () => {
16
+ context.runtime = '.'
17
+
18
+ normalize(context)
19
+
20
+ expect(context.runtime).not.toEqual('.')
21
+ expect(context.runtime.version).toEqual(require('@toa.io/runtime').version)
22
+ })
23
+
24
+ it('should expand registry', () => {
25
+ const base = generate()
26
+
27
+ context.registry = base
28
+
29
+ normalize(context)
30
+
31
+ expect(context.registry).toEqual({
32
+ base
33
+ })
34
+ })
@@ -0,0 +1,38 @@
1
+ 'use strict'
2
+
3
+ const context = {
4
+ runtime: {
5
+ version: '0.0.0'
6
+ },
7
+ name: 'test',
8
+ description: 'context fixture',
9
+ version: '0.0.0',
10
+ packages: 'namespaces/**/*',
11
+ registry: {
12
+ base: 'localhost:5000',
13
+ platforms: ['linux/amd64', 'linux/arm/v7', 'linux/arm64']
14
+ },
15
+ compositions: [
16
+ {
17
+ name: 'foo',
18
+ components: ['a.b', 'b.a']
19
+ },
20
+ {
21
+ name: 'bar',
22
+ components: ['d.c', 'a.b']
23
+ }
24
+ ],
25
+ annotations: {
26
+ '@toa.io/extensions.exposition': {
27
+ host: 'dummies.toa.io',
28
+ class: 'alb',
29
+ annotations: {
30
+ 'alb.ingress.kubernetes.io/scheme': 'internet-facing',
31
+ 'alb.ingress.kubernetes.io/target-type': 'ip',
32
+ 'alb.ingress.kubernetes.io/listen-ports': '[{"HTTPS": 443}]'
33
+ }
34
+ }
35
+ }
36
+ }
37
+
38
+ exports.context = context
@@ -0,0 +1,91 @@
1
+ 'use strict'
2
+
3
+ const clone = require('clone-deep')
4
+
5
+ const { validate } = require('../../src/.context')
6
+ const fixtures = require('./validate.fixtures')
7
+
8
+ let context
9
+
10
+ beforeAll(() => {
11
+ expect(() => validate(fixtures.context)).not.toThrow()
12
+ })
13
+
14
+ beforeEach(() => {
15
+ context = clone(fixtures.context)
16
+ })
17
+
18
+ describe('runtime', () => {
19
+ it('should require', () => {
20
+ delete context.runtime
21
+ expect(() => validate(context)).toThrow(/required/)
22
+ })
23
+
24
+ it('should require version as semver', () => {
25
+ delete context.runtime.version
26
+ expect(() => validate(context)).toThrow(/required/)
27
+
28
+ context.runtime.version = '.'
29
+ expect(() => validate(context)).toThrow(/pattern/)
30
+ })
31
+
32
+ it('should require registry to match uri format', () => {
33
+ context.runtime.registry = 'not-a-uri'
34
+ expect(() => validate(context)).toThrow(/must match format/)
35
+
36
+ context.runtime.registry = 'http://localhost'
37
+ expect(() => validate(context)).not.toThrow()
38
+ })
39
+
40
+ it('should require proxy to match uri format', () => {
41
+ context.runtime.proxy = 'not-a-uri'
42
+ expect(() => validate(context)).toThrow(/must match format/)
43
+
44
+ context.runtime.proxy = 'http://localhost'
45
+ expect(() => validate(context)).not.toThrow()
46
+ })
47
+ })
48
+
49
+ describe('registry', () => {
50
+ it('should require', () => {
51
+ delete context.registry
52
+
53
+ expect(() => validate(context)).toThrow(/required property 'registry'/)
54
+ })
55
+
56
+ it('should require base', () => {
57
+ delete context.registry.base
58
+
59
+ expect(() => validate(context)).toThrow(/required property 'base'/)
60
+ })
61
+
62
+ it('should set default platforms', () => {
63
+ delete context.registry.platforms
64
+
65
+ validate(context)
66
+
67
+ expect(context.registry.platforms).toBeInstanceOf(Array)
68
+ expect(context.registry.platforms).toStrictEqual(['linux/amd64', 'linux/arm/v7', 'linux/arm64'])
69
+ })
70
+ })
71
+
72
+ it('should require name as label', () => {
73
+ delete context.name
74
+ expect(() => validate(context)).toThrow(/required/)
75
+
76
+ context.name = 'foo bar'
77
+ expect(() => validate(context)).toThrow(/pattern/)
78
+
79
+ context.name = 'foo-bar'
80
+ expect(() => validate(context)).not.toThrow(/pattern/)
81
+ })
82
+
83
+ it('should require packages location', () => {
84
+ delete context.packages
85
+ expect(() => validate(context)).toThrow(/required/)
86
+ })
87
+
88
+ it('should require registry url', () => {
89
+ delete context.registry
90
+ expect(() => validate(context)).toThrow(/required/)
91
+ })
@@ -0,0 +1,17 @@
1
+ 'use strict'
2
+
3
+ const { generate } = require('randomstring')
4
+ const { random } = require('@toa.io/generic')
5
+
6
+ const SHORTCUTS = {
7
+ http: '@toa.io/bindings.http',
8
+ amqp: '@toa.io/bindings.amqp',
9
+ mongodb: '@toa.io/storages.mongodb',
10
+ exposition: '@toa.io/extensions.exposition',
11
+ origins: '@toa.io/extensions.origins'
12
+ }
13
+
14
+ const object = { foo: random(), bar: { baz: generate() } }
15
+
16
+ exports.object = object
17
+ exports.SHORTCUTS = SHORTCUTS
@@ -0,0 +1,92 @@
1
+ 'use strict'
2
+
3
+ const clone = require('clone-deep')
4
+ const { generate } = require('randomstring')
5
+
6
+ const { recognize, resolve } = require('../src/shortcuts')
7
+ const fixtures = require('./shortcuts.fixtures')
8
+
9
+ let object
10
+
11
+ beforeEach(() => {
12
+ object = clone(fixtures.object)
13
+ })
14
+
15
+ describe('resolve', () => {
16
+ it('should be defined', () => {
17
+ expect(resolve).toBeDefined()
18
+ })
19
+
20
+ it('should resolve', () => {
21
+ expect(Object.keys(fixtures.SHORTCUTS).length).toBeGreaterThan(0)
22
+
23
+ for (const [key, value] of Object.entries(fixtures.SHORTCUTS)) {
24
+ const resolved = resolve(key)
25
+
26
+ expect(resolved).toStrictEqual(value)
27
+ }
28
+ })
29
+ })
30
+
31
+ describe('recognize', () => {
32
+ it('should not change unknown', () => {
33
+ recognize(object)
34
+
35
+ expect(object).toStrictEqual(fixtures.object)
36
+ })
37
+
38
+ it('should resolve known', () => {
39
+ const known = append()
40
+
41
+ recognize(object)
42
+
43
+ for (const [alias, name] of Object.entries(fixtures.SHORTCUTS)) {
44
+ expect(object[alias]).toBeUndefined()
45
+ expect(object[name]).toStrictEqual(known[name])
46
+ }
47
+ })
48
+
49
+ it('should group known', () => {
50
+ const known = append()
51
+ const group = generate()
52
+
53
+ recognize(object, group)
54
+
55
+ expect(object[group]).toStrictEqual(known)
56
+
57
+ for (const alias of Object.keys(fixtures.SHORTCUTS)) expect(object[alias]).toBeUndefined()
58
+ })
59
+
60
+ it('should not overwrite group', () => {
61
+ append()
62
+ const group = generate()
63
+ const existing = { [generate()]: generate() }
64
+
65
+ object[group] = clone(existing)
66
+
67
+ recognize(object, group)
68
+
69
+ expect(object[group]).toStrictEqual(expect.objectContaining(existing))
70
+ })
71
+
72
+ it('should not create empty group', () => {
73
+ const group = generate()
74
+
75
+ recognize(object, group)
76
+
77
+ expect(object[group]).toBeUndefined()
78
+ })
79
+
80
+ const append = () => {
81
+ const known = {}
82
+
83
+ for (const [alias, name] of Object.entries(fixtures.SHORTCUTS)) {
84
+ const value = { [generate()]: generate() }
85
+
86
+ object[alias] = clone(value)
87
+ known[name] = clone(value)
88
+ }
89
+
90
+ return known
91
+ }
92
+ })
@@ -0,0 +1,78 @@
1
+ import type { Locator } from '@toa.io/core/types'
2
+
3
+ export namespace toa.norm {
4
+
5
+ namespace component {
6
+ namespace operations {
7
+
8
+ type Type = 'transition' | 'observation' | 'assignment'
9
+ type Scope = 'object' | 'objects' | 'changeset' | 'none'
10
+
11
+ }
12
+
13
+ interface Map {
14
+ [id: string]: Component
15
+ }
16
+
17
+ interface Operation {
18
+ type?: operations.Type
19
+ scope?: operations.Scope
20
+ bindings?: string[]
21
+ input?: any
22
+ output?: any
23
+ error?: any
24
+ }
25
+
26
+ interface Operations {
27
+ [key: string]: Operation
28
+ }
29
+
30
+ interface Event {
31
+ binding: string
32
+ }
33
+
34
+ interface Events {
35
+ [key: string]: Event
36
+ }
37
+
38
+ interface Receiver {
39
+ transition: string
40
+ adaptive: boolean
41
+ conditioned: boolean
42
+ bridge: string
43
+ binding: string
44
+ path: string
45
+ }
46
+
47
+ type Entity = {
48
+ schema: Object
49
+ storage?: string
50
+ initialized?: boolean
51
+ }
52
+
53
+ interface Declaration {
54
+ namespace: string
55
+ name: string
56
+ version: string
57
+ entity: Entity
58
+ bindings: string[]
59
+ operations?: Operations
60
+ events?: Events
61
+ receivers: Record<string, Receiver>
62
+ extensions?: Record<string, Object>
63
+ }
64
+
65
+ type Constructor = (path: string) => Promise<Component>
66
+ }
67
+
68
+ interface Component extends component.Declaration {
69
+ locator: Locator
70
+ path: string
71
+ }
72
+ }
73
+
74
+ export type Component = toa.norm.Component
75
+ export type Operation = toa.norm.component.Operation
76
+ export type Declaration = toa.norm.component.Declaration
77
+
78
+ export const component: toa.norm.component.Constructor
@@ -0,0 +1,75 @@
1
+ // noinspection ES6UnusedImports
2
+
3
+ import { Component } from './component'
4
+ import { Locator } from '@toa.io/core/types'
5
+
6
+ declare namespace toa.norm {
7
+
8
+ namespace context {
9
+
10
+ interface Runtime {
11
+ version: string
12
+ registry?: string
13
+ proxy?: string
14
+ }
15
+
16
+ interface Registry {
17
+ base: string
18
+ platforms?: string[] | null
19
+ }
20
+
21
+ interface Composition {
22
+ name: string,
23
+ components: string[] | Component[]
24
+ }
25
+
26
+ namespace dependencies {
27
+
28
+ type Instance = {
29
+ locator: Locator
30
+ manifest?: Object
31
+ }
32
+
33
+ type References = {
34
+ [reference: string]: Component[]
35
+ }
36
+
37
+ }
38
+
39
+ interface Dependencies {
40
+ [reference: string]: dependencies.Instance[]
41
+ }
42
+
43
+ interface Declaration {
44
+ name: string
45
+ description: string
46
+ version: string
47
+ runtime: Runtime | string
48
+ registry: Registry | string
49
+ packages: string
50
+ compositions?: Composition[]
51
+ annotations?: Record<string, object>
52
+ }
53
+
54
+ type Constructor = (path: string, environment?: string) => Promise<Context>
55
+ }
56
+
57
+ interface Context extends context.Declaration {
58
+ locator: Locator
59
+ runtime: context.Runtime
60
+ environment?: string
61
+ registry: context.Registry
62
+ components: Component[]
63
+ dependencies?: context.Dependencies
64
+ }
65
+
66
+ }
67
+
68
+ export type Composition = toa.norm.context.Composition
69
+ export type Context = toa.norm.Context
70
+
71
+ export namespace dependencies {
72
+ export type Instance = toa.norm.context.dependencies.Instance
73
+ }
74
+
75
+ export const context: toa.norm.context.Constructor
@@ -0,0 +1,5 @@
1
+ export { component } from './component'
2
+ export { context } from './context'
3
+
4
+ export type { Composition, dependencies } from './context'
5
+ export type { Component, Operation } from './component'