@allthings/cloud-toolkit 5.1.0 → 5.2.0-beta.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.
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const btoa = (stringToEncode) => Buffer.from(stringToEncode).toString('base64');
4
+ const atob = (stringToDecode) => Buffer.from(stringToDecode, 'base64').toString('utf8');
5
+
6
+ exports.atob = atob;
7
+ exports.btoa = btoa;
@@ -0,0 +1,4 @@
1
+ const btoa = (stringToEncode) => Buffer.from(stringToEncode).toString('base64');
2
+ const atob = (stringToDecode) => Buffer.from(stringToDecode, 'base64').toString('utf8');
3
+
4
+ export { atob, btoa };
@@ -0,0 +1,84 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var clientSsm = require('@aws-sdk/client-ssm');
6
+
7
+ const DEFAULT_AWS_CONFIGURATION = {
8
+ region: 'eu-central-2',
9
+ };
10
+
11
+ const DEFAULT_CLIENT_CONFIG = {
12
+ clientConfig: DEFAULT_AWS_CONFIGURATION,
13
+ onlyDefault: false,
14
+ prefix: process.env.SSM_PARAM_PATH,
15
+ };
16
+ const getSecrets = async (ssm, config, parameters_, defaultValues) => {
17
+ if (config.onlyDefault) {
18
+ return defaultValues;
19
+ }
20
+ try {
21
+ const command = new clientSsm.GetParametersCommand({
22
+ Names: parameters_.map((parameter) => `${config.prefix ?? ''}${parameter}`),
23
+ WithDecryption: true,
24
+ });
25
+ const parameters = await ssm.send(command);
26
+ return (parameters?.Parameters?.map((parameter) => {
27
+ const defaultIfNoParameter = !parameter.Value && parameter.Name
28
+ ? defaultValues.find((defaultValue) => defaultValue.name === parameter.Name)
29
+ : defaultValues[0];
30
+ return {
31
+ name: parameter.Name ?? defaultIfNoParameter?.name ?? '',
32
+ value: parameter.Value ?? defaultIfNoParameter?.value ?? '',
33
+ };
34
+ }) ?? defaultValues);
35
+ }
36
+ catch (error) {
37
+ if (config.logger) {
38
+ config.logger.error(error);
39
+ }
40
+ return defaultValues;
41
+ }
42
+ };
43
+ const getSecret = async (ssm, config, parameter_, defaultValue) => {
44
+ if (config.onlyDefault) {
45
+ if (defaultValue === undefined) {
46
+ throw new Error(`Cannot retrieve parameter ${parameter_}: onlyDefault is enabled and no default value was provided`);
47
+ }
48
+ return defaultValue;
49
+ }
50
+ try {
51
+ const parameter = await ssm.send(new clientSsm.GetParameterCommand({
52
+ Name: `${config.prefix ?? ''}${parameter_}`,
53
+ WithDecryption: true,
54
+ }));
55
+ const value = parameter?.Parameter?.Value;
56
+ if (value === undefined) {
57
+ throw new Error(`Could not retrieve parameter ${parameter_} from AWS SSM parameter store`);
58
+ }
59
+ return value;
60
+ }
61
+ catch (error) {
62
+ if (config.logger) {
63
+ config.logger.error(error);
64
+ }
65
+ if (defaultValue === undefined) {
66
+ throw error;
67
+ }
68
+ return defaultValue;
69
+ }
70
+ };
71
+ const parameterStore = (userConfig) => {
72
+ const { clientConfig, ...config } = {
73
+ ...DEFAULT_CLIENT_CONFIG,
74
+ ...userConfig,
75
+ };
76
+ const ssm = new clientSsm.SSMClient(clientConfig);
77
+ return {
78
+ getSecret: (parameter, defaultValue) => getSecret(ssm, config, parameter, defaultValue),
79
+ getSecrets: (parameters, defaultValues = []) => getSecrets(ssm, config, parameters, defaultValues),
80
+ };
81
+ };
82
+
83
+ exports.DEFAULT_CLIENT_CONFIG = DEFAULT_CLIENT_CONFIG;
84
+ exports.default = parameterStore;
@@ -0,0 +1,79 @@
1
+ import { SSMClient, GetParametersCommand, GetParameterCommand } from '@aws-sdk/client-ssm';
2
+
3
+ const DEFAULT_AWS_CONFIGURATION = {
4
+ region: 'eu-central-2',
5
+ };
6
+
7
+ const DEFAULT_CLIENT_CONFIG = {
8
+ clientConfig: DEFAULT_AWS_CONFIGURATION,
9
+ onlyDefault: false,
10
+ prefix: process.env.SSM_PARAM_PATH,
11
+ };
12
+ const getSecrets = async (ssm, config, parameters_, defaultValues) => {
13
+ if (config.onlyDefault) {
14
+ return defaultValues;
15
+ }
16
+ try {
17
+ const command = new GetParametersCommand({
18
+ Names: parameters_.map((parameter) => `${config.prefix ?? ''}${parameter}`),
19
+ WithDecryption: true,
20
+ });
21
+ const parameters = await ssm.send(command);
22
+ return (parameters?.Parameters?.map((parameter) => {
23
+ const defaultIfNoParameter = !parameter.Value && parameter.Name
24
+ ? defaultValues.find((defaultValue) => defaultValue.name === parameter.Name)
25
+ : defaultValues[0];
26
+ return {
27
+ name: parameter.Name ?? defaultIfNoParameter?.name ?? '',
28
+ value: parameter.Value ?? defaultIfNoParameter?.value ?? '',
29
+ };
30
+ }) ?? defaultValues);
31
+ }
32
+ catch (error) {
33
+ if (config.logger) {
34
+ config.logger.error(error);
35
+ }
36
+ return defaultValues;
37
+ }
38
+ };
39
+ const getSecret = async (ssm, config, parameter_, defaultValue) => {
40
+ if (config.onlyDefault) {
41
+ if (defaultValue === undefined) {
42
+ throw new Error(`Cannot retrieve parameter ${parameter_}: onlyDefault is enabled and no default value was provided`);
43
+ }
44
+ return defaultValue;
45
+ }
46
+ try {
47
+ const parameter = await ssm.send(new GetParameterCommand({
48
+ Name: `${config.prefix ?? ''}${parameter_}`,
49
+ WithDecryption: true,
50
+ }));
51
+ const value = parameter?.Parameter?.Value;
52
+ if (value === undefined) {
53
+ throw new Error(`Could not retrieve parameter ${parameter_} from AWS SSM parameter store`);
54
+ }
55
+ return value;
56
+ }
57
+ catch (error) {
58
+ if (config.logger) {
59
+ config.logger.error(error);
60
+ }
61
+ if (defaultValue === undefined) {
62
+ throw error;
63
+ }
64
+ return defaultValue;
65
+ }
66
+ };
67
+ const parameterStore = (userConfig) => {
68
+ const { clientConfig, ...config } = {
69
+ ...DEFAULT_CLIENT_CONFIG,
70
+ ...userConfig,
71
+ };
72
+ const ssm = new SSMClient(clientConfig);
73
+ return {
74
+ getSecret: (parameter, defaultValue) => getSecret(ssm, config, parameter, defaultValue),
75
+ getSecrets: (parameters, defaultValues = []) => getSecrets(ssm, config, parameters, defaultValues),
76
+ };
77
+ };
78
+
79
+ export { DEFAULT_CLIENT_CONFIG, parameterStore as default };
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ const splitEvery = (every, list) => Array.from({ length: Math.ceil(list.length / every) }).map((_, count) => list.slice(count * every, count * every + every));
4
+ const until = async (predicate, transformer, initialValue, iterationCount = 0) => {
5
+ const transformed = await transformer(initialValue, iterationCount);
6
+ return (await predicate(transformed, iterationCount))
7
+ ? transformed
8
+ : until(predicate, transformer, transformed, iterationCount + 1);
9
+ };
10
+ const partial = (function_, ...partialArguments) => (...arguments_) => function_(...partialArguments, ...arguments_);
11
+ const times = (function_, n) => Array.from({ length: n }, (_, index) => function_(index, index));
12
+ const deepFlatten = (arrayOfArrays) => arrayOfArrays.reduce((flatArray, currentArray) => [
13
+ ...flatArray,
14
+ ...(Array.isArray(currentArray)
15
+ ?
16
+ currentArray.some(Array.isArray)
17
+ ? deepFlatten(currentArray)
18
+ : currentArray
19
+ : [currentArray]),
20
+ ], []);
21
+ const flatten = (arrayOfArrays, depth = 1) => arrayOfArrays.reduce((flatArray, currentArray) => [
22
+ ...flatArray,
23
+ ...(Array.isArray(currentArray)
24
+ ?
25
+ currentArray.some(Array.isArray) && depth > 1
26
+ ? flatten(currentArray, depth - 1)
27
+ : currentArray
28
+ : [currentArray]),
29
+ ], []);
30
+ const asyncReduce = async (iterable, reducer, initialValue) => iterable.reduce(async (accumulatorPromise, value, index, array) => {
31
+ const accumulator = await accumulatorPromise;
32
+ return reducer(accumulator, value, index, array);
33
+ }, Promise.resolve(initialValue));
34
+
35
+ exports.asyncReduce = asyncReduce;
36
+ exports.deepFlatten = deepFlatten;
37
+ exports.flatten = flatten;
38
+ exports.partial = partial;
39
+ exports.splitEvery = splitEvery;
40
+ exports.times = times;
41
+ exports.until = until;
@@ -0,0 +1,33 @@
1
+ const splitEvery = (every, list) => Array.from({ length: Math.ceil(list.length / every) }).map((_, count) => list.slice(count * every, count * every + every));
2
+ const until = async (predicate, transformer, initialValue, iterationCount = 0) => {
3
+ const transformed = await transformer(initialValue, iterationCount);
4
+ return (await predicate(transformed, iterationCount))
5
+ ? transformed
6
+ : until(predicate, transformer, transformed, iterationCount + 1);
7
+ };
8
+ const partial = (function_, ...partialArguments) => (...arguments_) => function_(...partialArguments, ...arguments_);
9
+ const times = (function_, n) => Array.from({ length: n }, (_, index) => function_(index, index));
10
+ const deepFlatten = (arrayOfArrays) => arrayOfArrays.reduce((flatArray, currentArray) => [
11
+ ...flatArray,
12
+ ...(Array.isArray(currentArray)
13
+ ?
14
+ currentArray.some(Array.isArray)
15
+ ? deepFlatten(currentArray)
16
+ : currentArray
17
+ : [currentArray]),
18
+ ], []);
19
+ const flatten = (arrayOfArrays, depth = 1) => arrayOfArrays.reduce((flatArray, currentArray) => [
20
+ ...flatArray,
21
+ ...(Array.isArray(currentArray)
22
+ ?
23
+ currentArray.some(Array.isArray) && depth > 1
24
+ ? flatten(currentArray, depth - 1)
25
+ : currentArray
26
+ : [currentArray]),
27
+ ], []);
28
+ const asyncReduce = async (iterable, reducer, initialValue) => iterable.reduce(async (accumulatorPromise, value, index, array) => {
29
+ const accumulator = await accumulatorPromise;
30
+ return reducer(accumulator, value, index, array);
31
+ }, Promise.resolve(initialValue));
32
+
33
+ export { asyncReduce, deepFlatten, flatten, partial, splitEvery, times, until };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allthings/cloud-toolkit",
3
- "version": "5.1.0",
3
+ "version": "5.2.0-beta.1",
4
4
  "description": "",
5
5
  "author": "Allthings GmbH",
6
6
  "keywords": [
@@ -27,9 +27,37 @@
27
27
  "import": "./dist/lib.esm.mjs",
28
28
  "require": "./dist/lib.cjs.js"
29
29
  },
30
+ "./base64": {
31
+ "types": "./dist/src/base64.d.ts",
32
+ "import": "./dist/base64.esm.mjs",
33
+ "require": "./dist/base64.cjs.js"
34
+ },
35
+ "./parameterStore": {
36
+ "types": "./dist/src/aws/parameterStore.d.ts",
37
+ "import": "./dist/parameterStore.esm.mjs",
38
+ "require": "./dist/parameterStore.cjs.js"
39
+ },
40
+ "./utils": {
41
+ "types": "./dist/src/utils.d.ts",
42
+ "import": "./dist/utils.esm.mjs",
43
+ "require": "./dist/utils.cjs.js"
44
+ },
30
45
  "./package.json": "./package.json"
31
46
  },
32
47
  "sideEffects": false,
48
+ "typesVersions": {
49
+ "*": {
50
+ "base64": [
51
+ "dist/src/base64.d.ts"
52
+ ],
53
+ "parameterStore": [
54
+ "dist/src/aws/parameterStore.d.ts"
55
+ ],
56
+ "utils": [
57
+ "dist/src/utils.d.ts"
58
+ ]
59
+ }
60
+ },
33
61
  "files": [
34
62
  "dist/*.js",
35
63
  "dist/*.mjs",