@toa.io/pointer 0.8.4-dev.9 → 0.20.0-alpha.0

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
@@ -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
+ }
@@ -0,0 +1,21 @@
1
+ import type { Variables } from '@toa.io/operations';
2
+ export declare class Deployment {
3
+ private readonly id;
4
+ private readonly annotation;
5
+ constructor(id: string, annotation: URIMap);
6
+ export(requests: Request[]): Variables;
7
+ private createVariables;
8
+ private createVariable;
9
+ private createSecrets;
10
+ private resolveRecord;
11
+ private insecure;
12
+ }
13
+ export interface Request {
14
+ group: string;
15
+ selectors: string[];
16
+ }
17
+ export interface AnnotationRecord {
18
+ key: string;
19
+ references: string[];
20
+ }
21
+ export type URIMap = Record<string, string[]>;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Deployment = void 0;
4
+ const naming_1 = require("./naming");
5
+ const resolve_1 = require("./resolve");
6
+ class Deployment {
7
+ id;
8
+ annotation;
9
+ constructor(id, annotation) {
10
+ this.id = id;
11
+ this.annotation = annotation;
12
+ }
13
+ export(requests) {
14
+ const variables = {};
15
+ for (const request of requests)
16
+ variables[request.group] = this.createVariables(request.selectors);
17
+ return variables;
18
+ }
19
+ createVariables(selectors) {
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;
27
+ }
28
+ createVariable(selector) {
29
+ const name = (0, naming_1.nameVariable)(this.id, selector);
30
+ const { references } = this.resolveRecord(selector);
31
+ const value = references.join(' ');
32
+ return { name, value };
33
+ }
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
+ });
49
+ }
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);
59
+ }
60
+ }
61
+ exports.Deployment = Deployment;
62
+ const insecureProtocols = ['http:', 'https:', 'redis:'];
63
+ //# sourceMappingURL=Deployment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Deployment.js","sourceRoot":"","sources":["../source/Deployment.ts"],"names":[],"mappings":";;;AAAA,qCAAmD;AACnD,uCAAyC;AAGzC,MAAa,UAAU;IACJ,EAAE,CAAQ;IACV,UAAU,CAAQ;IAEnC,YAAoB,EAAU,EAAE,UAAkB;QAChD,IAAI,CAAC,EAAE,GAAG,EAAE,CAAA;QACZ,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAEM,MAAM,CAAE,QAAmB;QAChC,MAAM,SAAS,GAAc,EAAE,CAAA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,CAAC,CAAA;YACtD,MAAM,UAAU,GAAG,IAAA,mBAAU,EAAC,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,CAAA;YAE3C,SAAS,CAAC,IAAI,CAAC;gBACb,IAAI,EAAE,OAAO;gBACb,MAAM,EAAE;oBACN,IAAI,EAAE,UAAU;oBAChB,GAAG,EAAE,KAAK;iBACX;aACF,CAAC,CAAA;SACH;QAED,OAAO,SAAS,CAAA;IAClB,CAAC;IAEO,aAAa,CAAE,QAAgB;QACrC,OAAO,IAAA,uBAAa,EAAC,IAAI,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAA;IACjD,CAAC;IAEO,QAAQ,CAAE,UAAoB;QACpC,MAAM,SAAS,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;QAC/B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAA;QAE9B,OAAO,iBAAiB,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IACjD,CAAC;CACF;AAvED,gCAuEC;AAYD,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ import type { URIMap } from './Deployment';
2
+ export declare function normalize(declaration: Declaration): URIMap;
3
+ export type Declaration = string | string[] | Record<string, string | string[]> | undefined;
@@ -0,0 +1,62 @@
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
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.normalize = void 0;
27
+ const node_path_1 = require("node:path");
28
+ const schemas = __importStar(require("@toa.io/schemas"));
29
+ const generic_1 = require("@toa.io/generic");
30
+ function normalize(declaration) {
31
+ const map = {};
32
+ if (declaration === undefined)
33
+ return map;
34
+ if (typeof declaration === 'string' || Array.isArray(declaration))
35
+ declaration = { '.': declaration };
36
+ for (const [key, value] of Object.entries(declaration))
37
+ if (typeof value === 'string')
38
+ map[key] = format([value]);
39
+ else
40
+ map[key] = format(value);
41
+ validate(map);
42
+ return map;
43
+ }
44
+ exports.normalize = normalize;
45
+ function format(values) {
46
+ return values.map(generic_1.shards).flat();
47
+ }
48
+ function validate(map) {
49
+ schema.validate(map);
50
+ for (const uris of Object.values(map))
51
+ for (const uri of uris)
52
+ checkCredentials(uri);
53
+ }
54
+ function checkCredentials(uri) {
55
+ const url = new URL(uri);
56
+ if (url.username !== '' || url.password !== '')
57
+ throw new Error(`Pointer URI '${uri}' must not contain credentials. ` +
58
+ 'Please refer to the "Credentials" section in the documentation for more information.');
59
+ }
60
+ const path = (0, node_path_1.resolve)(__dirname, '../schemas/urimap.cos.yaml');
61
+ const schema = schemas.schema(path);
62
+ //# sourceMappingURL=annotation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"annotation.js","sourceRoot":"","sources":["../source/annotation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAmC;AACnC,yDAA0C;AAC1C,6CAAwC;AAGxC,SAAgB,SAAS,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IAAI,GAAG,IAAA,mBAAO,EAAC,SAAS,EAAE,4BAA4B,CAAC,CAAA;AAC7D,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA"}
@@ -0,0 +1,4 @@
1
+ import { type Request } from './Deployment';
2
+ import type { Variables } from '@toa.io/operations';
3
+ import type { Declaration } from './annotation';
4
+ export declare function createVariables(id: string, declaration: Declaration, requests: Request[]): Variables;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createVariables = void 0;
4
+ const Deployment_1 = require("./Deployment");
5
+ const annotation_1 = require("./annotation");
6
+ function createVariables(id, declaration, requests) {
7
+ const annotation = (0, annotation_1.normalize)(declaration);
8
+ const deployment = new Deployment_1.Deployment(id, annotation);
9
+ return deployment.export(requests);
10
+ }
11
+ exports.createVariables = createVariables;
12
+ //# sourceMappingURL=createVariables.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createVariables.js","sourceRoot":"","sources":["../source/createVariables.ts"],"names":[],"mappings":";;;AAAA,6CAAuD;AACvD,6CAAwC;AAIxC,SAAgB,eAAe,CAC9B,EAAU,EAAE,WAAwB,EAAE,QAAmB;IACxD,MAAM,UAAU,GAAG,IAAA,sBAAS,EAAC,WAAW,CAAC,CAAA;IACzC,MAAM,UAAU,GAAG,IAAI,uBAAU,CAAC,EAAE,EAAE,UAAU,CAAC,CAAA;IAEjD,OAAO,UAAU,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;AACpC,CAAC;AAND,0CAMC"}
@@ -0,0 +1,6 @@
1
+ export { createVariables } from './createVariables';
2
+ export { resolve, resolveRecord } from './resolve';
3
+ export { normalize } from './annotation';
4
+ export * as naming from './naming';
5
+ export type { Declaration } from './annotation';
6
+ export type { Request, URIMap } from './Deployment';
@@ -0,0 +1,35 @@
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
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.naming = exports.normalize = exports.resolveRecord = exports.resolve = exports.createVariables = void 0;
27
+ var createVariables_1 = require("./createVariables");
28
+ Object.defineProperty(exports, "createVariables", { enumerable: true, get: function () { return createVariables_1.createVariables; } });
29
+ var resolve_1 = require("./resolve");
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"));
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,2 @@
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"}
@@ -0,0 +1,3 @@
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;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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);
8
+ const value = process.env[variable];
9
+ if (value === undefined)
10
+ throw new Error(`${variable} is not set.`);
11
+ let urls = value.split(' ');
12
+ const protocol = getProtocol(urls);
13
+ if (protocol !== 'http:')
14
+ urls = await (0, dns_1.dedupe)(urls);
15
+ return withCredentials(variable, urls);
16
+ }
17
+ exports.resolve = resolve;
18
+ function resolveRecord(uris, selector) {
19
+ if (selector in uris)
20
+ return getRecord(uris, selector);
21
+ const segments = selector.split('.');
22
+ while (segments.pop() !== undefined) {
23
+ const current = segments.join('.');
24
+ if (current in uris)
25
+ return getRecord(uris, current);
26
+ }
27
+ if ('.' in uris)
28
+ return getRecord(uris, '.');
29
+ else
30
+ throw new Error(`Selector '${selector}' cannot be resolved.`);
31
+ }
32
+ exports.resolveRecord = resolveRecord;
33
+ function withCredentials(variable, urls) {
34
+ const username = process.env[variable + '_USERNAME'] ?? '';
35
+ const password = process.env[variable + '_PASSWORD'] ?? '';
36
+ return urls.map((url) => addCredentials(url, username, password));
37
+ }
38
+ function addCredentials(ref, username, password) {
39
+ const url = new URL(ref);
40
+ url.username = username;
41
+ url.password = password;
42
+ return url.href;
43
+ }
44
+ function getProtocol(urls) {
45
+ const url = new URL(urls[0]);
46
+ return url.protocol;
47
+ }
48
+ function getRecord(uris, key) {
49
+ return {
50
+ key,
51
+ references: uris[key]
52
+ };
53
+ }
54
+ //# sourceMappingURL=resolve.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve.js","sourceRoot":"","sources":["../source/resolve.ts"],"names":[],"mappings":";;;AAAA,qCAAoC;AACpC,qCAAuC;AAGhC,KAAK,UAAU,OAAO,CAAE,EAAU,EAAE,QAAgB;IACzD,MAAM,QAAQ,GAAG,IAAA,qBAAY,EAAC,EAAE,EAAE,QAAQ,CAAC,CAAA;IAC3C,MAAM,KAAK,GA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AC,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,WAAW,CAAE,IAAc;IAClC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;IAE5B,OAAO,GAAG,CAAC,QAAQ,CAAA;AACrB,CAAC;AAED,SAAS,SAAS,CAAE,IAAY,EAAE,GAAW;IAC3C,OAAO;QACL,GAAG;QACH,UAAU,EAAE,IAAI,CAAC,GAAG,CAAC;KACtB,CAAA;AACH,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":"4.9.5"}
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