@toa.io/pointer 0.20.0-dev.30 → 0.20.0-dev.34

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 (67) hide show
  1. package/package.json +15 -8
  2. package/readme.md +113 -76
  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 +53 -0
  12. package/transpiled/Deployment.d.ts +7 -1
  13. package/transpiled/Deployment.js +37 -16
  14. package/transpiled/Deployment.js.map +1 -1
  15. package/transpiled/annotation.d.ts +1 -1
  16. package/transpiled/annotation.js +3 -3
  17. package/transpiled/annotation.js.map +1 -1
  18. package/transpiled/index.d.ts +3 -1
  19. package/transpiled/index.js +28 -1
  20. package/transpiled/index.js.map +1 -1
  21. package/transpiled/{env.d.ts → naming.d.ts} +1 -0
  22. package/transpiled/naming.js +16 -0
  23. package/transpiled/naming.js.map +1 -0
  24. package/transpiled/resolve.d.ts +3 -1
  25. package/transpiled/resolve.js +28 -5
  26. package/transpiled/resolve.js.map +1 -1
  27. package/tsconfig.json +12 -0
  28. package/source/.deployment/.variables/credentials.js +0 -35
  29. package/source/.deployment/.variables/index.js +0 -7
  30. package/source/.deployment/.variables/pointer.js +0 -18
  31. package/source/.deployment/index.js +0 -8
  32. package/source/.deployment/validate.js +0 -19
  33. package/source/.deployment/variables.js +0 -22
  34. package/source/.pointer/env.js +0 -44
  35. package/source/.pointer/index.js +0 -9
  36. package/source/.pointer/label.js +0 -15
  37. package/source/.pointer/local.js +0 -17
  38. package/source/deployment.js +0 -17
  39. package/source/index.js +0 -11
  40. package/source/pointer.js +0 -34
  41. package/source/uris/construct.js +0 -18
  42. package/source/uris/index.js +0 -9
  43. package/source/uris/normalize.js +0 -12
  44. package/source/uris/resolve.js +0 -18
  45. package/source/uris/schema.yaml +0 -17
  46. package/source/uris/validate.js +0 -17
  47. package/source/variable.js +0 -12
  48. package/test/deployment.test.js +0 -92
  49. package/test/pointer.test.js +0 -213
  50. package/test/uris.fixtures.js +0 -10
  51. package/test/uris.test.js +0 -146
  52. package/test/variable.test.js +0 -37
  53. package/transpiled/Variables.d.ts +0 -15
  54. package/transpiled/Variables.js +0 -42
  55. package/transpiled/Variables.js.map +0 -1
  56. package/transpiled/createDeployment.d.ts +0 -4
  57. package/transpiled/createDeployment.js +0 -12
  58. package/transpiled/createDeployment.js.map +0 -1
  59. package/transpiled/env.js +0 -10
  60. package/transpiled/env.js.map +0 -1
  61. package/transpiled/tsconfig.tsbuildinfo +0 -1
  62. package/types/_uris.d.ts +0 -27
  63. package/types/deployment.d.ts +0 -21
  64. package/types/index.d.ts +0 -1
  65. package/types/pointer.d.ts +0 -19
  66. package/types/uris.d.ts +0 -19
  67. 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,53 @@
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
+ const urls = value.split(' ')
12
+ const unique = await dedupe(urls)
13
+
14
+ return withCredentials(variable, unique)
15
+ }
16
+
17
+ export function resolveRecord (uris: URIMap, selector: string): AnnotationRecord {
18
+ if (selector in uris) return getRecord(uris, selector)
19
+
20
+ const segments = selector.split('.')
21
+
22
+ while (segments.pop() !== undefined) {
23
+ const current = segments.join('.')
24
+
25
+ if (current in uris) return getRecord(uris, current)
26
+ }
27
+
28
+ if ('.' in uris) return getRecord(uris, '.')
29
+ else throw new Error(`Selector '${selector}' cannot be resolved.`)
30
+ }
31
+
32
+ function withCredentials (variable: string, urls: string[]): string[] {
33
+ const username = process.env[variable + '_USERNAME'] ?? ''
34
+ const password = process.env[variable + '_PASSWORD'] ?? ''
35
+
36
+ return urls.map((url) => addCredentials(url, username, password))
37
+ }
38
+
39
+ function addCredentials (ref: string, username: string, password: string): string {
40
+ const url = new URL(ref)
41
+
42
+ url.username = username
43
+ url.password = password
44
+
45
+ return url.href
46
+ }
47
+
48
+ function getRecord (uris: URIMap, key: string): AnnotationRecord {
49
+ return {
50
+ key,
51
+ references: uris[key]
52
+ }
53
+ }
@@ -6,10 +6,16 @@ export declare class Deployment {
6
6
  export(requests: Request[]): Variables;
7
7
  private createVariables;
8
8
  private createVariable;
9
- private resolve;
9
+ private createSecrets;
10
+ private resolveRecord;
11
+ private insecure;
10
12
  }
11
13
  export interface Request {
12
14
  group: string;
13
15
  selectors: string[];
14
16
  }
17
+ export interface AnnotationRecord {
18
+ key: string;
19
+ references: string[];
20
+ }
15
21
  export type URIMap = Record<string, string[]>;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Deployment = void 0;
4
- const env_1 = require("./env");
4
+ const naming_1 = require("./naming");
5
+ const resolve_1 = require("./resolve");
5
6
  class Deployment {
6
7
  id;
7
8
  annotation;
@@ -16,27 +17,47 @@ class Deployment {
16
17
  return variables;
17
18
  }
18
19
  createVariables(selectors) {
19
- return selectors.map((selector) => this.createVariable(selector));
20
+ const variables = [];
21
+ for (const selector of selectors) {
22
+ const variable = this.createVariable(selector);
23
+ const secrets = this.createSecrets(selector);
24
+ variables.push(variable, ...secrets);
25
+ }
26
+ return variables;
20
27
  }
21
28
  createVariable(selector) {
22
- const name = (0, env_1.nameVariable)(this.id, selector);
23
- const values = this.resolve(selector);
24
- const value = values.join(' ');
29
+ const name = (0, naming_1.nameVariable)(this.id, selector);
30
+ const { references } = this.resolveRecord(selector);
31
+ const value = references.join(' ');
25
32
  return { name, value };
26
33
  }
27
- resolve(selector) {
28
- if (selector in this.annotation)
29
- return this.annotation[selector];
30
- const segments = selector.split('.');
31
- while (segments.pop() !== undefined) {
32
- const current = segments.join('.');
33
- if (current in this.annotation)
34
- return this.annotation[current];
34
+ createSecrets(selector) {
35
+ const varialbes = [];
36
+ const { key, references } = this.resolveRecord(selector);
37
+ if (this.insecure(references))
38
+ return [];
39
+ for (const token of ['username', 'password']) {
40
+ const varName = (0, naming_1.nameVariable)(this.id, selector, token);
41
+ const secretName = (0, naming_1.nameSecret)(this.id, key);
42
+ varialbes.push({
43
+ name: varName,
44
+ secret: {
45
+ name: secretName,
46
+ key: token
47
+ }
48
+ });
35
49
  }
36
- if (this.annotation['.'] === undefined)
37
- throw new Error(`Selector '${selector}' cannot be resolved.`);
38
- return this.annotation['.'];
50
+ return varialbes;
51
+ }
52
+ resolveRecord(selector) {
53
+ return (0, resolve_1.resolveRecord)(this.annotation, selector);
54
+ }
55
+ insecure(references) {
56
+ const reference = references[0];
57
+ const url = new URL(reference);
58
+ return insecureProtocols.includes(url.protocol);
39
59
  }
40
60
  }
41
61
  exports.Deployment = Deployment;
62
+ const insecureProtocols = ['http:', 'https:', 'redis:'];
42
63
  //# sourceMappingURL=Deployment.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Deployment.js","sourceRoot":"","sources":["../source/Deployment.ts"],"names":[],"mappings":";;;AAAA,+BAAoC;AAGpC,MAAa,UA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CAAA;IACjD,CAAC;CACF;AAvED,gCAuEC;AAYD,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAA"}
@@ -1,3 +1,3 @@
1
1
  import type { URIMap } from './Deployment';
2
2
  export declare function normalize(declaration: Declaration): URIMap;
3
- export type Declaration = string | string[] | Record<string, string | string[]>;
3
+ export type Declaration = string | string[] | Record<string, string | string[]> | undefined;
@@ -29,6 +29,8 @@ const schemas = __importStar(require("@toa.io/schemas"));
29
29
  const generic_1 = require("@toa.io/generic");
30
30
  function normalize(declaration) {
31
31
  const map = {};
32
+ if (declaration === undefined)
33
+ return map;
32
34
  if (typeof declaration === 'string' || Array.isArray(declaration))
33
35
  declaration = { '.': declaration };
34
36
  for (const [key, value] of Object.entries(declaration))
@@ -41,9 +43,7 @@ function normalize(declaration) {
41
43
  }
42
44
  exports.normalize = normalize;
43
45
  function format(values) {
44
- return values.map(generic_1.shards)
45
- .flat()
46
- .map((value) => (0, generic_1.echo)(value));
46
+ return values.map(generic_1.shards).flat();
47
47
  }
48
48
  function validate(map) {
49
49
  schema.validate(map);
@@ -1 +1 @@
1
- {"version":3,"file":"annotation.js","sourceRoot":"","sources":["../source/annotation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAmC;AACnC,yDAA0C;AAC1C,6CAA8C;AAG9C,SAAgB,SAAS,CAAE,WAAwB;IACjD,MAAM,GAAG,GAAW,EAAE,CAAA;IAEtB,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC;QAC/D,WAAW,GAAG,EAAE,GAAG,EAAE,WAAW,EAAE,CAAA;IAEpC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC;QACpD,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAA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EAAE,4BAA4B,CAAC,CAAA;AAC7D,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA"}
@@ -1,4 +1,6 @@
1
1
  export { createVariables } from './createVariables';
2
- export { resolve } from './resolve';
2
+ export { resolve, resolveRecord } from './resolve';
3
+ export { normalize } from './annotation';
4
+ export * as naming from './naming';
3
5
  export type { Declaration } from './annotation';
4
6
  export type { Request, URIMap } from './Deployment';
@@ -1,8 +1,35 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolve = exports.createVariables = void 0;
26
+ exports.naming = exports.normalize = exports.resolveRecord = exports.resolve = exports.createVariables = void 0;
4
27
  var createVariables_1 = require("./createVariables");
5
28
  Object.defineProperty(exports, "createVariables", { enumerable: true, get: function () { return createVariables_1.createVariables; } });
6
29
  var resolve_1 = require("./resolve");
7
30
  Object.defineProperty(exports, "resolve", { enumerable: true, get: function () { return resolve_1.resolve; } });
31
+ Object.defineProperty(exports, "resolveRecord", { enumerable: true, get: function () { return resolve_1.resolveRecord; } });
32
+ var annotation_1 = require("./annotation");
33
+ Object.defineProperty(exports, "normalize", { enumerable: true, get: function () { return annotation_1.normalize; } });
34
+ exports.naming = __importStar(require("./naming"));
8
35
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":";;;AAAA,qDAAmD;AAA1C,kHAAA,eAAe,OAAA;AACxB,qCAAmC;AAA1B,kGAAA,OAAO,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAmD;AAA1C,kHAAA,eAAe,OAAA;AACxB,qCAAkD;AAAzC,kGAAA,OAAO,OAAA;AAAE,wGAAA,aAAa,OAAA;AAC/B,2CAAwC;AAA/B,uGAAA,SAAS,OAAA;AAClB,mDAAkC"}
@@ -1 +1,2 @@
1
1
  export declare function nameVariable(...segments: string[]): string;
2
+ export declare function nameSecret(...segments: string[]): string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nameSecret = exports.nameVariable = void 0;
4
+ function nameVariable(...segments) {
5
+ return 'TOA_' + segments.join('_')
6
+ .replaceAll(/[-.]/g, '_')
7
+ .toUpperCase();
8
+ }
9
+ exports.nameVariable = nameVariable;
10
+ function nameSecret(...segments) {
11
+ return 'toa-' + segments.join('-')
12
+ .replaceAll('.', '-')
13
+ .replace(/--$/, '.default');
14
+ }
15
+ exports.nameSecret = nameSecret;
16
+ //# sourceMappingURL=naming.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"naming.js","sourceRoot":"","sources":["../source/naming.ts"],"names":[],"mappings":";;;AAAA,SAAgB,YAAY,CAAE,GAAG,QAAkB;IACjD,OAAO,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC;SAC/B,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC;SACxB,WAAW,EAAE,CAAA;AAClB,CAAC;AAJD,oCAIC;AAED,SAAgB,UAAU,CAAE,GAAG,QAAkB;IAC/C,OAAO,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC;SAC/B,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC;SACpB,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,CAAA;AAC/B,CAAC;AAJD,gCAIC"}
@@ -1 +1,3 @@
1
- export declare function resolve(id: string, selector: string): string[];
1
+ import { type AnnotationRecord, type URIMap } from './Deployment';
2
+ export declare function resolve(id: string, selector: string): Promise<string[]>;
3
+ export declare function resolveRecord(uris: URIMap, selector: string): AnnotationRecord;
@@ -1,16 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolve = void 0;
4
- const env_1 = require("./env");
5
- function resolve(id, selector) {
6
- const variable = (0, env_1.nameVariable)(id, selector);
3
+ exports.resolveRecord = exports.resolve = void 0;
4
+ const dns_1 = require("@toa.io/dns");
5
+ const naming_1 = require("./naming");
6
+ async function resolve(id, selector) {
7
+ const variable = (0, naming_1.nameVariable)(id, selector);
7
8
  const value = process.env[variable];
8
9
  if (value === undefined)
9
10
  throw new Error(`${variable} is not set.`);
10
11
  const urls = value.split(' ');
11
- return withCredentials(variable, urls);
12
+ const unique = await (0, dns_1.dedupe)(urls);
13
+ return withCredentials(variable, unique);
12
14
  }
13
15
  exports.resolve = resolve;
16
+ function resolveRecord(uris, selector) {
17
+ if (selector in uris)
18
+ return getRecord(uris, selector);
19
+ const segments = selector.split('.');
20
+ while (segments.pop() !== undefined) {
21
+ const current = segments.join('.');
22
+ if (current in uris)
23
+ return getRecord(uris, current);
24
+ }
25
+ if ('.' in uris)
26
+ return getRecord(uris, '.');
27
+ else
28
+ throw new Error(`Selector '${selector}' cannot be resolved.`);
29
+ }
30
+ exports.resolveRecord = resolveRecord;
14
31
  function withCredentials(variable, urls) {
15
32
  const username = process.env[variable + '_USERNAME'] ?? '';
16
33
  const password = process.env[variable + '_PASSWORD'] ?? '';
@@ -22,4 +39,10 @@ function addCredentials(ref, username, password) {
22
39
  url.password = password;
23
40
  return url.href;
24
41
  }
42
+ function getRecord(uris, key) {
43
+ return {
44
+ key,
45
+ references: uris[key]
46
+ };
47
+ }
25
48
  //# sourceMappingURL=resolve.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolve.js","sourceRoot":"","sources":["../source/resolve.ts"],"names":[],"mappings":";;;AAAA,+BAAoC;AAEpC,SAAgB,OAAO,CAAE,EAAU,EAAE,QAAgB;IACnD,MAAM,QAAQ,GAAG,IAAA,kBAAY,EAAC,EAAE,EAAE,QAAQ,CAAC,CAAA;IAC3C,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAEnC,IAAI,KAAK,KAAK,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,GAAG,QAAQ,cAAc,CAAC,CAAA;IAEnE,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAE7B,OAAO,eAAe,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;AACxC,CAAC;AATD,0BASC;AAED,SAAS,eAAe,CAAE,QAAgB,EAAE,IAAc;IACxD,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,CAAA;IAC1D,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,CAAA;IAE1D,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,cAAc,CAAC,GAAG,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAA;AACnE,CAAC;AAED,SAAS,cAAc,CAAE,GAAW,EAAE,QAAgB,EAAE,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AAI,GAAG,IAAI,IAAI;QAAE,OAAO,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;;QACvC,MAAM,IAAI,KAAK,CAAC,aAAa,QAAQ,uBAAuB,CAAC,CAAA;AACpE,CAAC;AAbD,sCAaC;AAED,SAAS,eAAe,CAAE,QAAgB,EAAE,IAAc;IACxD,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,CAAA;IAC1D,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,CAAA;IAE1D,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,cAAc,CAAC,GAAG,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAA;AACnE,CAAC;AAED,SAAS,cAAc,CAAE,GAAW,EAAE,QAAgB,EAAE,QAAgB;IACtE,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAA;IAExB,GAAG,CAAC,QAAQ,GAAG,QAAQ,CAAA;IACvB,GAAG,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAEvB,OAAO,GAAG,CAAC,IAAI,CAAA;AACjB,CAAC;AAED,SAAS,SAAS,CAAE,IAAY,EAAE,GAAW;IAC3C,OAAO;QACL,GAAG;QACH,UAAU,EAAE,IAAI,CAAC,GAAG,CAAC;KACtB,CAAA;AACH,CAAC"}
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