@middy/ssm 2.5.6 → 2.5.7

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 (2) hide show
  1. package/index.js +133 -117
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ "use strict";
2
+
1
3
  const {
2
4
  canPrefetch,
3
5
  createPrefetchClient,
@@ -8,171 +10,185 @@ const {
8
10
  jsonSafeParse,
9
11
  getInternal,
10
12
  sanitizeKey
11
- } = require('@middy/util')
12
- const SSM = require('aws-sdk/clients/ssm') // v2
13
+ } = require('@middy/util');
14
+
15
+ const SSM = require('aws-sdk/clients/ssm'); // v2
13
16
  // const { SSM } = require('@aws-sdk/client-ssm') // v3
14
17
 
15
- const awsRequestLimit = 10
18
+
19
+ const awsRequestLimit = 10;
16
20
  const defaults = {
17
- AwsClient: SSM, // Allow for XRay
21
+ AwsClient: SSM,
22
+ // Allow for XRay
18
23
  awsClientOptions: {},
19
24
  awsClientAssumeRole: undefined,
20
25
  awsClientCapture: undefined,
21
- fetchData: {}, // { contextKey: fetchKey, contextPrefix: fetchPath/ }
26
+ fetchData: {},
27
+ // { contextKey: fetchKey, contextPrefix: fetchPath/ }
22
28
  disablePrefetch: false,
23
29
  cacheKey: 'ssm',
24
30
  cacheExpiry: -1,
25
31
  setToEnv: false,
26
32
  setToContext: false
27
- }
33
+ };
28
34
 
29
35
  const ssmMiddleware = (opts = {}) => {
30
- const options = { ...defaults, ...opts }
36
+ const options = { ...defaults,
37
+ ...opts
38
+ };
31
39
 
32
40
  const fetch = (request, cachedValues) => {
33
- return {
34
- ...fetchSingle(request, cachedValues),
41
+ return { ...fetchSingle(request, cachedValues),
35
42
  ...fetchByPath(request, cachedValues)
36
- }
37
- }
43
+ };
44
+ };
38
45
 
39
46
  const fetchSingle = (request, cachedValues = {}) => {
40
- const values = {}
41
- let batchReq = null
42
- let batchInternalKeys = []
43
- let batchFetchKeys = []
47
+ const values = {};
48
+ let batchReq = null;
49
+ let batchInternalKeys = [];
50
+ let batchFetchKeys = [];
51
+ const internalKeys = Object.keys(options.fetchData);
52
+ const fetchKeys = Object.values(options.fetchData);
44
53
 
45
- const internalKeys = Object.keys(options.fetchData)
46
- const fetchKeys = Object.values(options.fetchData)
47
54
  for (const [idx, internalKey] of internalKeys.entries()) {
48
- if (cachedValues[internalKey]) continue
49
- const fetchKey = options.fetchData[internalKey]
50
- if (fetchKey.substr(-1) === '/') continue // Skip path passed in
51
- batchInternalKeys.push(internalKey)
52
- batchFetchKeys.push(fetchKey)
53
- // from the first to the batch size skip, unless it's the last entry
54
- if (
55
- (!idx || (idx + 1) % awsRequestLimit !== 0) &&
56
- !(idx + 1 === internalKeys.length)
57
- ) {
58
- continue
55
+ if (cachedValues[internalKey]) continue;
56
+ const fetchKey = options.fetchData[internalKey];
57
+ if (fetchKey.substr(-1) === '/') continue; // Skip path passed in
58
+
59
+ batchInternalKeys.push(internalKey);
60
+ batchFetchKeys.push(fetchKey); // from the first to the batch size skip, unless it's the last entry
61
+
62
+ if ((!idx || (idx + 1) % awsRequestLimit !== 0) && !(idx + 1 === internalKeys.length)) {
63
+ continue;
59
64
  }
60
65
 
61
- batchReq = client
62
- .getParameters({ Names: batchFetchKeys, WithDecryption: true })
63
- .promise() // Required for aws-sdk v2
64
- .then((resp) => {
65
- // Don't sanitize key, mapped to set value in options
66
- return Object.assign(
67
- ...(resp.InvalidParameters ?? []).map((fetchKey) => {
68
- return {
69
- [fetchKey]: new Promise(() => {
70
- const internalKey = internalKeys[fetchKeys.indexOf(fetchKey)]
71
- const value = getCache(options.cacheKey)?.value ?? {}
72
- value[internalKey] = undefined
73
- modifyCache(options.cacheKey, value)
74
- throw new Error('ssm.InvalidParameter ' + fetchKey)
75
- })
76
- }
77
- }),
78
- ...(resp.Parameters ?? []).map((param) => {
79
- return { [param.Name]: parseValue(param) }
66
+ batchReq = client.getParameters({
67
+ Names: batchFetchKeys,
68
+ WithDecryption: true
69
+ }).promise() // Required for aws-sdk v2
70
+ .then(resp => {
71
+ var _resp$InvalidParamete, _resp$Parameters;
72
+
73
+ // Don't sanitize key, mapped to set value in options
74
+ return Object.assign(...((_resp$InvalidParamete = resp.InvalidParameters) !== null && _resp$InvalidParamete !== void 0 ? _resp$InvalidParamete : []).map(fetchKey => {
75
+ return {
76
+ [fetchKey]: new Promise(() => {
77
+ var _getCache$value, _getCache;
78
+
79
+ const internalKey = internalKeys[fetchKeys.indexOf(fetchKey)];
80
+ const value = (_getCache$value = (_getCache = getCache(options.cacheKey)) === null || _getCache === void 0 ? void 0 : _getCache.value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
81
+ value[internalKey] = undefined;
82
+ modifyCache(options.cacheKey, value);
83
+ throw new Error('ssm.InvalidParameter ' + fetchKey);
80
84
  })
81
- )
82
- })
83
- .catch((e) => {
84
- const value = getCache(options.cacheKey).value ?? {}
85
- value[internalKey] = undefined
86
- modifyCache(options.cacheKey, value)
87
- throw e
88
- })
85
+ };
86
+ }), ...((_resp$Parameters = resp.Parameters) !== null && _resp$Parameters !== void 0 ? _resp$Parameters : []).map(param => {
87
+ return {
88
+ [param.Name]: parseValue(param)
89
+ };
90
+ }));
91
+ }).catch(e => {
92
+ var _getCache$value2;
93
+
94
+ const value = (_getCache$value2 = getCache(options.cacheKey).value) !== null && _getCache$value2 !== void 0 ? _getCache$value2 : {};
95
+ value[internalKey] = undefined;
96
+ modifyCache(options.cacheKey, value);
97
+ throw e;
98
+ });
89
99
 
90
100
  for (const internalKey of batchInternalKeys) {
91
- values[internalKey] = batchReq.then((params) => {
92
- return params[options.fetchData[internalKey]]
93
- })
101
+ values[internalKey] = batchReq.then(params => {
102
+ return params[options.fetchData[internalKey]];
103
+ });
94
104
  }
95
105
 
96
- batchInternalKeys = []
97
- batchFetchKeys = []
98
- batchReq = null
106
+ batchInternalKeys = [];
107
+ batchFetchKeys = [];
108
+ batchReq = null;
99
109
  }
100
110
 
101
- return values
102
- }
111
+ return values;
112
+ };
103
113
 
104
114
  const fetchByPath = (request, cachedValues = {}) => {
105
- const values = {}
115
+ const values = {};
116
+
106
117
  for (const internalKey in options.fetchData) {
107
- if (cachedValues[internalKey]) continue
108
- const fetchKey = options.fetchData[internalKey]
109
- if (fetchKey.substr(-1) !== '/') continue // Skip not path passed in
110
- values[internalKey] = fetchPath(fetchKey).catch((e) => {
111
- const value = getCache(options.cacheKey)?.value ?? {}
112
- value[internalKey] = undefined
113
- modifyCache(options.cacheKey, value)
114
- throw e
115
- })
118
+ if (cachedValues[internalKey]) continue;
119
+ const fetchKey = options.fetchData[internalKey];
120
+ if (fetchKey.substr(-1) !== '/') continue; // Skip not path passed in
121
+
122
+ values[internalKey] = fetchPath(fetchKey).catch(e => {
123
+ var _getCache$value3, _getCache2;
124
+
125
+ const value = (_getCache$value3 = (_getCache2 = getCache(options.cacheKey)) === null || _getCache2 === void 0 ? void 0 : _getCache2.value) !== null && _getCache$value3 !== void 0 ? _getCache$value3 : {};
126
+ value[internalKey] = undefined;
127
+ modifyCache(options.cacheKey, value);
128
+ throw e;
129
+ });
116
130
  }
117
- return values
118
- }
119
131
 
120
- const fetchPath = (path, nextToken, values = {}) => {
121
- return client
122
- .getParametersByPath({
123
- Path: path,
124
- NextToken: nextToken,
125
- Recursive: true,
126
- WithDecryption: true
127
- })
128
- .promise() // Required for aws-sdk v2
129
- .then((resp) => {
130
- Object.assign(
131
- values,
132
- ...resp.Parameters.map((param) => {
133
- return {
134
- [sanitizeKey(param.Name.replace(path, ''))]: parseValue(param)
135
- }
136
- })
137
- )
138
- if (resp.NextToken) return fetchPath(path, resp.NextToken, values)
139
- return values
140
- })
141
- }
132
+ return values;
133
+ };
142
134
 
143
- const parseValue = (param) => {
135
+ const fetchPath = (path, nextToken, values = {}) => {
136
+ return client.getParametersByPath({
137
+ Path: path,
138
+ NextToken: nextToken,
139
+ Recursive: true,
140
+ WithDecryption: true
141
+ }).promise() // Required for aws-sdk v2
142
+ .then(resp => {
143
+ Object.assign(values, ...resp.Parameters.map(param => {
144
+ return {
145
+ [sanitizeKey(param.Name.replace(path, ''))]: parseValue(param)
146
+ };
147
+ }));
148
+ if (resp.NextToken) return fetchPath(path, resp.NextToken, values);
149
+ return values;
150
+ });
151
+ };
152
+
153
+ const parseValue = param => {
144
154
  if (param.Type === 'StringList') {
145
- return param.Value.split(',')
155
+ return param.Value.split(',');
146
156
  }
147
- return jsonSafeParse(param.Value)
148
- }
149
157
 
150
- let prefetch, client
158
+ return jsonSafeParse(param.Value);
159
+ };
160
+
161
+ let prefetch, client;
162
+
151
163
  if (canPrefetch(options)) {
152
- client = createPrefetchClient(options)
153
- prefetch = processCache(options, fetch)
164
+ client = createPrefetchClient(options);
165
+ prefetch = processCache(options, fetch);
154
166
  }
155
167
 
156
- const ssmMiddlewareBefore = async (request) => {
168
+ const ssmMiddlewareBefore = async request => {
169
+ var _prefetch;
170
+
157
171
  if (!client) {
158
- client = await createClient(options, request)
172
+ client = await createClient(options, request);
159
173
  }
160
174
 
161
- const { value } = prefetch ?? processCache(options, fetch, request)
162
-
163
- Object.assign(request.internal, value)
175
+ const {
176
+ value
177
+ } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
178
+ Object.assign(request.internal, value);
164
179
 
165
180
  if (options.setToContext || options.setToEnv) {
166
- const data = await getInternal(Object.keys(options.fetchData), request)
167
- if (options.setToEnv) Object.assign(process.env, data)
168
- if (options.setToContext) Object.assign(request.context, data)
181
+ const data = await getInternal(Object.keys(options.fetchData), request);
182
+ if (options.setToEnv) Object.assign(process.env, data);
183
+ if (options.setToContext) Object.assign(request.context, data);
169
184
  }
170
185
 
171
- prefetch = null
172
- }
186
+ prefetch = null;
187
+ };
173
188
 
174
189
  return {
175
190
  before: ssmMiddlewareBefore
176
- }
177
- }
178
- module.exports = ssmMiddleware
191
+ };
192
+ };
193
+
194
+ module.exports = ssmMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/ssm",
3
- "version": "2.5.6",
3
+ "version": "2.5.7",
4
4
  "description": "SSM (EC2 Systems Manager) parameters middleware for the middy framework",
5
5
  "type": "commonjs",
6
6
  "engines": {
@@ -46,12 +46,12 @@
46
46
  },
47
47
  "homepage": "https://github.com/middyjs/middy#readme",
48
48
  "dependencies": {
49
- "@middy/util": "^2.5.6"
49
+ "@middy/util": "^2.5.7"
50
50
  },
51
51
  "devDependencies": {
52
- "@middy/core": "^2.5.6",
52
+ "@middy/core": "^2.5.7",
53
53
  "aws-sdk": "^2.939.0",
54
54
  "aws-xray-sdk": "^3.3.3"
55
55
  },
56
- "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
56
+ "gitHead": "3983c4b138e1a4d7fcb3ed805d3b8832fff06fc1"
57
57
  }