@toa.io/pointer 0.9.0-canary.2 → 0.20.0-alpha.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 (60) hide show
  1. package/package.json +14 -7
  2. package/readme.md +113 -78
  3. package/schemas/urimap.cos.yaml +2 -0
  4. package/source/Deployment.ts +90 -0
  5. package/source/annotation.test.ts +66 -0
  6. package/source/annotation.ts +46 -0
  7. package/source/createVariables.test.ts +186 -0
  8. package/source/createVariables.ts +12 -0
  9. package/source/index.ts +7 -0
  10. package/source/naming.ts +11 -0
  11. package/source/resolve.ts +63 -0
  12. package/transpiled/Deployment.d.ts +21 -0
  13. package/transpiled/Deployment.js +63 -0
  14. package/transpiled/Deployment.js.map +1 -0
  15. package/transpiled/annotation.d.ts +3 -0
  16. package/transpiled/annotation.js +62 -0
  17. package/transpiled/annotation.js.map +1 -0
  18. package/transpiled/createVariables.d.ts +4 -0
  19. package/transpiled/createVariables.js +12 -0
  20. package/transpiled/createVariables.js.map +1 -0
  21. package/transpiled/index.d.ts +6 -0
  22. package/transpiled/index.js +35 -0
  23. package/transpiled/index.js.map +1 -0
  24. package/transpiled/naming.d.ts +2 -0
  25. package/transpiled/naming.js +16 -0
  26. package/transpiled/naming.js.map +1 -0
  27. package/transpiled/resolve.d.ts +3 -0
  28. package/transpiled/resolve.js +54 -0
  29. package/transpiled/resolve.js.map +1 -0
  30. package/transpiled/tsconfig.tsbuildinfo +1 -0
  31. package/tsconfig.json +12 -0
  32. package/source/.deployment/.variables/credentials.js +0 -35
  33. package/source/.deployment/.variables/index.js +0 -7
  34. package/source/.deployment/.variables/pointer.js +0 -18
  35. package/source/.deployment/index.js +0 -8
  36. package/source/.deployment/validate.js +0 -19
  37. package/source/.deployment/variables.js +0 -22
  38. package/source/.pointer/env.js +0 -44
  39. package/source/.pointer/index.js +0 -9
  40. package/source/.pointer/label.js +0 -15
  41. package/source/.pointer/local.js +0 -17
  42. package/source/deployment.js +0 -17
  43. package/source/index.js +0 -11
  44. package/source/pointer.js +0 -34
  45. package/source/uris/construct.js +0 -18
  46. package/source/uris/index.js +0 -9
  47. package/source/uris/normalize.js +0 -12
  48. package/source/uris/resolve.js +0 -18
  49. package/source/uris/schema.yaml +0 -18
  50. package/source/uris/validate.js +0 -17
  51. package/source/variable.js +0 -12
  52. package/test/deployment.test.js +0 -92
  53. package/test/pointer.test.js +0 -213
  54. package/test/uris.fixtures.js +0 -10
  55. package/test/uris.test.js +0 -144
  56. package/test/variable.test.js +0 -37
  57. package/types/deployment.d.ts +0 -21
  58. package/types/pointer.d.ts +0 -19
  59. package/types/uris.d.ts +0 -27
  60. package/types/variable.d.ts +0 -8
@@ -1,22 +0,0 @@
1
- 'use strict'
2
-
3
- const declare = require('./.variables')
4
-
5
- /**
6
- * @param {toa.pointer.URIs} uris
7
- * @param {string} prefix
8
- * @returns {toa.deployment.dependency.Variable[]}
9
- */
10
- const variables = (uris, prefix) => {
11
- /** @type {toa.deployment.dependency.Variable[]} */
12
- const variables = []
13
- const pointer = declare.pointer(prefix, uris)
14
- const credentials = declare.credentials(prefix, uris)
15
-
16
- variables.push(pointer)
17
- variables.push(...credentials)
18
-
19
- return variables
20
- }
21
-
22
- exports.variables = variables
@@ -1,44 +0,0 @@
1
- 'use strict'
2
-
3
- const { concat, decode, letters: { up, capitalize } } = require('@toa.io/generic')
4
- const { console } = require('@toa.io/console')
5
-
6
- const { resolve } = require('../uris')
7
-
8
- /**
9
- * @param {string} prefix
10
- * @param {toa.core.Locator} locator
11
- */
12
- const env = (prefix, locator) => {
13
- const key = `TOA_${up(prefix)}_POINTER`
14
- const value = process.env[key]
15
-
16
- if (value === undefined) throw new Error(`Environment variable ${key} is not set`)
17
-
18
- const uris = decode(value)
19
- const { url, entry } = resolve(locator, uris)
20
-
21
- const suffix = convert(entry)
22
- const env = `TOA_${up(prefix)}_${suffix}`
23
-
24
- for (const property of ['username', 'password']) {
25
- const value = process.env[env + '_' + up(property)]
26
-
27
- if (value !== undefined) url[property] = value
28
- else console.warn(`${capitalize(property)} for ${env} is not set`)
29
- }
30
-
31
- return url
32
- }
33
-
34
- /**
35
- * @param {string} entry
36
- * @returns {string}
37
- */
38
- const convert = (entry) => {
39
- const [namespace, name] = entry.toUpperCase().split('.')
40
-
41
- return namespace + concat('_', name)
42
- }
43
-
44
- exports.env = env
@@ -1,9 +0,0 @@
1
- 'use strict'
2
-
3
- const { env } = require('./env')
4
- const { local } = require('./local')
5
- const { label } = require('./label')
6
-
7
- exports.env = env
8
- exports.local = local
9
- exports.label = label
@@ -1,15 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * @param {URL} url
5
- * @returns {string}
6
- */
7
- const label = (url) => {
8
- const safe = new URL(url.href)
9
-
10
- safe.password = ''
11
-
12
- return safe.href
13
- }
14
-
15
- exports.label = label
@@ -1,17 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * @param {string} protocol
5
- * @returns {URL}
6
- */
7
- const local = (protocol) => {
8
- const url = new URL(protocol + '//')
9
-
10
- url.hostname = 'localhost'
11
- url.username = 'developer'
12
- url.password = 'secret'
13
-
14
- return url
15
- }
16
-
17
- exports.local = local
@@ -1,17 +0,0 @@
1
- 'use strict'
2
-
3
- const get = require('./.deployment')
4
-
5
- /** @type {toa.pointer.Deployment} */
6
- const deployment = (instances, uris, options) => {
7
- if (typeof uris === 'string') uris = { default: uris }
8
-
9
- get.validate(uris)
10
-
11
- const global = get.variables(uris, options.prefix)
12
- const variables = { global }
13
-
14
- return { variables }
15
- }
16
-
17
- exports.deployment = deployment
package/source/index.js DELETED
@@ -1,11 +0,0 @@
1
- 'use strict'
2
-
3
- const { Pointer } = require('./pointer')
4
- const { deployment } = require('./deployment')
5
- const { variable } = require('./variable')
6
-
7
- exports.uris = require('./uris')
8
-
9
- exports.Pointer = Pointer
10
- exports.deployment = deployment
11
- exports.variable = variable
package/source/pointer.js DELETED
@@ -1,34 +0,0 @@
1
- 'use strict'
2
-
3
- const get = require('./.pointer')
4
-
5
- class Pointer {
6
- protocol
7
- host
8
- port
9
- hostname
10
- path
11
- reference
12
- label
13
-
14
- /**
15
- * @param {string} prefix
16
- * @param {toa.core.Locator} locator
17
- * @param {toa.pointer.Options} options
18
- */
19
- constructor (prefix, locator, options) {
20
- const dev = process.env.TOA_DEV === '1'
21
- const url = dev ? get.local(options.protocol) : get.env(prefix, locator)
22
-
23
- this.protocol = url.protocol
24
- this.hostname = url.hostname
25
- this.port = Number(url.port)
26
- this.path = url.pathname
27
- this.username = url.username
28
- this.password = url.password
29
- this.reference = url.href
30
- this.label = get.label(url)
31
- }
32
- }
33
-
34
- exports.Pointer = Pointer
@@ -1,18 +0,0 @@
1
- 'use strict'
2
-
3
- const { normalize } = require('./normalize')
4
- const { validate } = require('./validate')
5
-
6
- /**
7
- * @param {toa.pointer.URIs} declaration
8
- * @returns {toa.pointer.URIs}
9
- */
10
- const construct = (declaration) => {
11
- const normalized = normalize(declaration)
12
-
13
- validate(normalized)
14
-
15
- return normalized
16
- }
17
-
18
- exports.construct = construct
@@ -1,9 +0,0 @@
1
- 'use strict'
2
-
3
- const { construct } = require('./construct')
4
- const { resolve } = require('./resolve')
5
- const { validate } = require('./validate')
6
-
7
- exports.construct = construct
8
- exports.resolve = resolve
9
- exports.validate = validate
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * @param {toa.pointer.URIs | string} declaration
5
- * @returns {toa.pointer.URIs}
6
- */
7
- const normalize = (declaration) => {
8
- if (typeof declaration === 'string') return { default: declaration }
9
- return declaration
10
- }
11
-
12
- exports.normalize = normalize
@@ -1,18 +0,0 @@
1
- 'use strict'
2
-
3
- /** @type {toa.pointer.uris.Resolver} */
4
- const resolve = (locator, uris) => {
5
- const keys = [locator.id, locator.namespace, 'default']
6
- const entry = keys.find((key) => key in uris)
7
- const uri = uris[entry]
8
-
9
- if (entry === undefined) throw new Error(`URI annotation for '${locator.id}' is not found`)
10
-
11
- const url = new URL(uri)
12
-
13
- if (url.hostname === '') throw new Error(`URI annotation for '${locator.id}' must contain a hostname`)
14
-
15
- return { url, entry }
16
- }
17
-
18
- exports.resolve = resolve
@@ -1,18 +0,0 @@
1
- $schema: https://json-schema.org/draft/2019-09/schema
2
-
3
- definitions:
4
- node:
5
- oneOf:
6
- - $ref: '#/definitions/locator'
7
- - type: object
8
- patternProperties:
9
- '.*':
10
- $ref: '#/definitions/node'
11
- locator:
12
- type: string
13
- format: uri
14
-
15
- type: object
16
- patternProperties:
17
- '.*':
18
- $ref: '#/definitions/node'
@@ -1,17 +0,0 @@
1
- 'use strict'
2
-
3
- const path = require('path')
4
-
5
- const schemas = require('@toa.io/schemas')
6
- const { load } = require('@toa.io/yaml')
7
-
8
- const cos = load.sync(path.resolve(__dirname, 'schema.yaml'))
9
- const schema = schemas.schema(cos)
10
-
11
- const validate = (declaration) => {
12
- const error = schema.fit(declaration)
13
-
14
- if (error) throw new TypeError(error.message)
15
- }
16
-
17
- exports.validate = validate
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- const { letters: { up } } = require('@toa.io/generic')
4
-
5
- /** @type {toa.pointer.Variable} */
6
- const variable = (scope, locator, label, value) => {
7
- const name = `TOA_${up(scope)}_${locator.uppercase}_${up(label)}`
8
-
9
- return { name, value }
10
- }
11
-
12
- exports.variable = variable
@@ -1,92 +0,0 @@
1
- 'use strict'
2
-
3
- const { encode, letters: { up, down } } = require('@toa.io/generic')
4
- const mock = require('@toa.io/mock')
5
-
6
- const { deployment } = require('../')
7
-
8
- const prefix = 'foo-bar'
9
-
10
- /** @type {toa.norm.context.dependencies.Instance[]} */
11
- let instances
12
-
13
- /** @type {toa.pointer.deployment.Options} */
14
- let options
15
-
16
- beforeEach(() => {
17
- instances = mock.dependencies.instances()
18
- options = { prefix }
19
- })
20
-
21
- it('should be', () => {
22
- expect(deployment).toBeInstanceOf(Function)
23
- })
24
-
25
- it('should throw if protocol is omitted', () => {
26
- const annotation = { default: 'no-protocol' }
27
-
28
- expect(() => deployment(instances, annotation, options)).toThrow('Invalid URL')
29
- })
30
-
31
- it('should throw if hostname is omitted', () => {
32
- const annotation = { default: 'amqps:///' }
33
-
34
- expect(() => deployment(instances, annotation, options)).toThrow('must contain a hostname')
35
- })
36
-
37
- it('should consider string annotation as default', () => {
38
- const annotation = 'pg://host0'
39
-
40
- const output = deployment(instances, annotation, options)
41
-
42
- const name = `TOA_${up(prefix)}_POINTER`
43
- const value = encode({ default: annotation })
44
-
45
- expect(output.variables.global).toStrictEqual(expect.arrayContaining([{ name, value }]))
46
- })
47
-
48
- describe('variables', () => {
49
- it('should export POINTER variable', () => {
50
- const annotation = { default: 'amqps://host0' }
51
- const json = JSON.stringify(annotation)
52
- const value = encode(json)
53
- const output = deployment(instances, annotation, options)
54
-
55
- expect(output.variables.global).toStrictEqual(expect.arrayContaining([{
56
- name: `TOA_${up(prefix)}_POINTER`,
57
- value
58
- }]))
59
- })
60
-
61
- it('should export USERNAME and PASSWORD for each entry of URI Set', () => {
62
- const annotation = {
63
- default: 'amqps://host0',
64
- system: 'amqps://host2',
65
- dummies: 'amqps://host3',
66
- 'dummies.one': 'amqps://host4'
67
- }
68
-
69
- const expected = []
70
-
71
- const env = `TOA_${up(prefix)}_`
72
- const sec = `toa-${down(prefix)}-`
73
-
74
- for (const key of Object.keys(annotation)) {
75
- const label = key.replaceAll('.', '-').toLowerCase()
76
-
77
- for (const property of ['username', 'password']) {
78
- expected.push({
79
- name: `${env}${up(label)}_${up(property)}`,
80
- secret: {
81
- name: `${sec}${down(label)}`,
82
- key: property
83
- }
84
- })
85
- }
86
- }
87
-
88
- const output = deployment(instances, annotation, options)
89
-
90
- expect(output.variables.global).toStrictEqual(expect.arrayContaining(expected))
91
- })
92
- })
@@ -1,213 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
- const { encode, letters: { up } } = require('@toa.io/generic')
5
-
6
- const mock = { console: { info: jest.fn(), warn: jest.fn() } }
7
-
8
- jest.mock('@toa.io/console', () => mock)
9
-
10
- const { Locator } = require('@toa.io/core')
11
- const { Pointer } = require('../')
12
-
13
- it('should be', () => {
14
- expect(Pointer).toBeDefined()
15
- expect(Pointer).toBeInstanceOf(Function)
16
- })
17
-
18
- const prefix = 'bindings-amqp'
19
-
20
- /** @type {toa.core.Locator} */
21
- let locator
22
-
23
- /** @type {toa.pointer.Options} */
24
- const options = { protocol: 'protocol:' }
25
-
26
- /** @type {toa.pointer.Pointer} */
27
- let pointer
28
-
29
- /** @type {URL} */
30
- let url
31
-
32
- const uris = (uris) => (process.env[`TOA_${up(prefix)}_POINTER`] = encode(uris))
33
-
34
- beforeEach(() => {
35
- jest.clearAllMocks()
36
-
37
- const name = generate()
38
- const namespace = generate()
39
-
40
- locator = new Locator(name, namespace)
41
-
42
- url = new URL('amqp://whatever:3000')
43
-
44
- uris({ default: url.href })
45
- })
46
-
47
- it('should throw if env not set', () => {
48
- const name = `TOA_${up(prefix)}_POINTER`
49
- delete process.env[name]
50
-
51
- expect(() => new Pointer(prefix, locator, options)).toThrow(`Environment variable ${name} is not set`)
52
- })
53
-
54
- it('should use declared protocol', () => {
55
- pointer = new Pointer(prefix, locator, options)
56
-
57
- expect(pointer.protocol).toStrictEqual(url.protocol)
58
- })
59
-
60
- it('should point to host', () => {
61
- pointer = new Pointer(prefix, locator, options)
62
-
63
- expect(pointer.hostname).toStrictEqual(url.hostname)
64
- })
65
-
66
- it('should resolve credentials', () => {
67
- const check = (entry) => {
68
- const env = `TOA_${up(prefix)}_${up(entry)}_`
69
- const username = generate()
70
- const password = generate()
71
-
72
- process.env[env + 'USERNAME'] = username
73
- process.env[env + 'PASSWORD'] = password
74
-
75
- pointer = new Pointer(prefix, locator, options)
76
-
77
- delete process.env[env + 'USERNAME']
78
- delete process.env[env + 'PASSWORD']
79
-
80
- expect(pointer.username).toStrictEqual(username)
81
- expect(pointer.password).toStrictEqual(password)
82
- }
83
-
84
- // default resolution
85
- check('default')
86
-
87
- // namespace resolution
88
- uris({ [locator.namespace]: 'pg://host0/db' })
89
- check(locator.namespace)
90
-
91
- // id resolution
92
- uris({ [locator.id]: 'pg://host0/db' })
93
- check(locator.label)
94
- })
95
-
96
- it('should expose port as number', () => {
97
- pointer = new Pointer(prefix, locator, options)
98
-
99
- expect(pointer.port).toStrictEqual(Number(url.port))
100
- })
101
-
102
- it('should expose reference', () => {
103
- pointer = new Pointer(prefix, locator, options)
104
-
105
- expect(pointer.reference).toStrictEqual(url.href)
106
- })
107
-
108
- it('should use localhost and given protocol on local environment', () => {
109
- process.env.TOA_DEV = '1'
110
-
111
- pointer = new Pointer(prefix, locator, options)
112
-
113
- delete process.env.TOA_DEV
114
-
115
- expect(pointer.hostname).toStrictEqual('localhost')
116
- expect(pointer.protocol).toStrictEqual(options.protocol)
117
- })
118
-
119
- it('should use default protocol and credentials on local environment', () => {
120
- process.env.TOA_DEV = '1'
121
-
122
- pointer = new Pointer(prefix, locator, options)
123
-
124
- delete process.env.TOA_DEV
125
-
126
- expect(pointer.reference).toStrictEqual(`${options.protocol}//developer:secret@localhost`)
127
- expect(pointer.label).toStrictEqual(`${options.protocol}//developer@localhost`)
128
- })
129
-
130
- describe('environment variables', () => {
131
- const username = generate()
132
- const password = generate()
133
-
134
- const env = `TOA_${up(prefix)}_DEFAULT`
135
-
136
- let set
137
- let unset
138
-
139
- beforeEach(() => {
140
- jest.clearAllMocks()
141
-
142
- set = (name, value, prefix = env) => {
143
- const key = prefix + '_' + up(name)
144
-
145
- process.env[key] = value
146
- url[name] = value
147
- }
148
-
149
- unset = (name, prefix = env) => {
150
- const key = prefix + '_' + up(name)
151
-
152
- delete process.env[key]
153
- url[name] = ''
154
- }
155
-
156
- set('username', username)
157
- set('password', password)
158
- })
159
-
160
- afterEach(() => {
161
- unset('username')
162
- unset('password')
163
- })
164
-
165
- it('should use env variables for credentials', () => {
166
- pointer = new Pointer(prefix, locator, options)
167
-
168
- expect(pointer.reference).toStrictEqual(url.href)
169
-
170
- url.password = ''
171
-
172
- expect(pointer.label).toStrictEqual(url.href)
173
- })
174
-
175
- it('should warn if username is not set', () => {
176
- url.hostname = locator.hostname(prefix)
177
-
178
- unset('username')
179
- url.password = ''
180
-
181
- const pointer = new Pointer(prefix, locator, options)
182
-
183
- expect(pointer).toBeDefined()
184
- expect(mock.console.warn).toHaveBeenCalledWith(`Username for ${env} is not set`)
185
- })
186
-
187
- it('should warn if password is not set', () => {
188
- url.hostname = locator.hostname(prefix)
189
-
190
- unset('password')
191
-
192
- const pointer = new Pointer(prefix, locator, options)
193
-
194
- expect(pointer).toBeDefined()
195
- expect(mock.console.warn).toHaveBeenCalledWith(`Password for ${env} is not set`)
196
- })
197
-
198
- it('should not warn it credentials are set', () => {
199
- uris({ [locator.id]: 'amqp://host0' })
200
-
201
- const username = generate()
202
- const password = generate()
203
- const env = `TOA_${up(prefix)}_${locator.uppercase}`
204
-
205
- set('username', username, env)
206
- set('password', password, env)
207
-
208
- const pointer = new Pointer(prefix, locator, options)
209
-
210
- expect(pointer).toBeDefined()
211
- expect(mock.console.warn).not.toHaveBeenCalled()
212
- })
213
- })
@@ -1,10 +0,0 @@
1
- 'use strict'
2
-
3
- const annotations = {
4
- default: 'http://host.docker.internal/',
5
- credits: {
6
- balance: 'amqp://host.docker.internal'
7
- }
8
- }
9
-
10
- exports.annotations = annotations