@toa.io/pointer 0.20.0-dev.4 → 0.20.0-dev.40

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 (41) hide show
  1. package/package.json +15 -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/tsconfig.json +12 -0
  13. package/source/.deployment/.variables/credentials.js +0 -35
  14. package/source/.deployment/.variables/index.js +0 -7
  15. package/source/.deployment/.variables/pointer.js +0 -18
  16. package/source/.deployment/index.js +0 -8
  17. package/source/.deployment/validate.js +0 -19
  18. package/source/.deployment/variables.js +0 -22
  19. package/source/.pointer/env.js +0 -44
  20. package/source/.pointer/index.js +0 -9
  21. package/source/.pointer/label.js +0 -15
  22. package/source/.pointer/local.js +0 -17
  23. package/source/deployment.js +0 -17
  24. package/source/index.js +0 -11
  25. package/source/pointer.js +0 -34
  26. package/source/uris/construct.js +0 -18
  27. package/source/uris/index.js +0 -9
  28. package/source/uris/normalize.js +0 -12
  29. package/source/uris/resolve.js +0 -18
  30. package/source/uris/schema.yaml +0 -17
  31. package/source/uris/validate.js +0 -17
  32. package/source/variable.js +0 -12
  33. package/test/deployment.test.js +0 -92
  34. package/test/pointer.test.js +0 -213
  35. package/test/uris.fixtures.js +0 -10
  36. package/test/uris.test.js +0 -146
  37. package/test/variable.test.js +0 -37
  38. package/types/deployment.d.ts +0 -21
  39. package/types/pointer.d.ts +0 -19
  40. package/types/uris.d.ts +0 -27
  41. package/types/variable.d.ts +0 -8
@@ -0,0 +1,7 @@
1
+ export { createVariables } from './createVariables'
2
+ export { resolve, resolveRecord } from './resolve'
3
+ export { normalize } from './annotation'
4
+ export * as naming from './naming'
5
+
6
+ export type { Declaration } from './annotation'
7
+ export type { Request, URIMap } from './Deployment'
@@ -0,0 +1,11 @@
1
+ export function nameVariable (...segments: string[]): string {
2
+ return 'TOA_' + segments.join('_')
3
+ .replaceAll(/[-.]/g, '_')
4
+ .toUpperCase()
5
+ }
6
+
7
+ export function nameSecret (...segments: string[]): string {
8
+ return 'toa-' + segments.join('-')
9
+ .replaceAll('.', '-')
10
+ .replace(/--$/, '.default')
11
+ }
@@ -0,0 +1,63 @@
1
+ import { dedupe } from '@toa.io/dns'
2
+ import { nameVariable } from './naming'
3
+ import { type AnnotationRecord, type URIMap } from './Deployment'
4
+
5
+ export async function resolve (id: string, selector: string): Promise<string[]> {
6
+ const variable = nameVariable(id, selector)
7
+ const value = process.env[variable]
8
+
9
+ if (value === undefined) throw new Error(`${variable} is not set.`)
10
+
11
+ let urls = value.split(' ')
12
+
13
+ const protocol = getProtocol(urls)
14
+
15
+ if (protocol !== 'http:')
16
+ urls = await dedupe(urls)
17
+
18
+ return withCredentials(variable, urls)
19
+ }
20
+
21
+ export function resolveRecord (uris: URIMap, selector: string): AnnotationRecord {
22
+ if (selector in uris) return getRecord(uris, selector)
23
+
24
+ const segments = selector.split('.')
25
+
26
+ while (segments.pop() !== undefined) {
27
+ const current = segments.join('.')
28
+
29
+ if (current in uris) return getRecord(uris, current)
30
+ }
31
+
32
+ if ('.' in uris) return getRecord(uris, '.')
33
+ else throw new Error(`Selector '${selector}' cannot be resolved.`)
34
+ }
35
+
36
+ function withCredentials (variable: string, urls: string[]): string[] {
37
+ const username = process.env[variable + '_USERNAME'] ?? ''
38
+ const password = process.env[variable + '_PASSWORD'] ?? ''
39
+
40
+ return urls.map((url) => addCredentials(url, username, password))
41
+ }
42
+
43
+ function addCredentials (ref: string, username: string, password: string): string {
44
+ const url = new URL(ref)
45
+
46
+ url.username = username
47
+ url.password = password
48
+
49
+ return url.href
50
+ }
51
+
52
+ function getProtocol (urls: string[]): string {
53
+ const url = new URL(urls[0])
54
+
55
+ return url.protocol
56
+ }
57
+
58
+ function getRecord (uris: URIMap, key: string): AnnotationRecord {
59
+ return {
60
+ key,
61
+ references: uris[key]
62
+ }
63
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./transpiled"
5
+ },
6
+ "include": [
7
+ "source"
8
+ ],
9
+ "exclude": [
10
+ "**/*.test.ts"
11
+ ]
12
+ }
@@ -1,35 +0,0 @@
1
- 'use strict'
2
-
3
- const { concat, letters: { up, down } } = require('@toa.io/generic')
4
-
5
- /**
6
- * @param {string} prefix
7
- * @param {toa.pointer.URIs} uris
8
- * @returns {toa.deployment.dependency.Variable[]}
9
- */
10
- const credentials = (prefix, uris) => {
11
- const variables = []
12
-
13
- for (const key of Object.keys(uris)) {
14
- const [namespace, name] = key.split('.')
15
- const entry = namespace + concat('-', name)
16
-
17
- const env = `TOA_${up(prefix)}_${up(entry)}_`
18
- const sec = `toa-${down(prefix)}-${down(entry)}`
19
-
20
- for (const property of ['username', 'password']) {
21
- const name = env + up(property)
22
-
23
- const secret = {
24
- name: sec,
25
- key: property
26
- }
27
-
28
- variables.push({ name, secret })
29
- }
30
- }
31
-
32
- return variables
33
- }
34
-
35
- exports.credentials = credentials
@@ -1,7 +0,0 @@
1
- 'use strict'
2
-
3
- const { credentials } = require('./credentials')
4
- const { pointer } = require('./pointer')
5
-
6
- exports.credentials = credentials
7
- exports.pointer = pointer
@@ -1,18 +0,0 @@
1
- 'use strict'
2
-
3
- const { encode, letters: { up } } = require('@toa.io/generic')
4
-
5
- /**
6
- * @param {string} prefix
7
- * @param {toa.pointer.URIs} uris
8
- * @returns {toa.deployment.dependency.Variable}
9
- */
10
- const pointer = (prefix, uris) => {
11
- const name = `TOA_${up(prefix)}_POINTER`
12
- const json = JSON.stringify(uris)
13
- const value = encode(json)
14
-
15
- return { name, value }
16
- }
17
-
18
- exports.pointer = pointer
@@ -1,8 +0,0 @@
1
- 'use strict'
2
-
3
- const { validate } = require('./validate')
4
- const { variables } = require('./variables')
5
-
6
- exports.validate = validate
7
- exports.variables = variables
8
-
@@ -1,19 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * @param {toa.pointer.URIs} uris
5
- */
6
- const validate = (uris) => {
7
- Object.values(uris).forEach(check)
8
- }
9
-
10
- /**
11
- * @param {string} uri
12
- */
13
- const check = (uri) => {
14
- const url = new URL(uri)
15
-
16
- if (url.hostname === '') throw new Error(`URI ${url.href} must contain a hostname`)
17
- }
18
-
19
- exports.validate = validate
@@ -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 | string} 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,17 +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
-
14
- type: object
15
- patternProperties:
16
- '.*':
17
- $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
- })