@middy/ssm 2.5.2 → 2.5.6

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 (3) hide show
  1. package/index.d.ts +2 -13
  2. package/index.js +117 -126
  3. package/package.json +4 -4
package/index.d.ts CHANGED
@@ -1,19 +1,8 @@
1
1
  import { SSM } from 'aws-sdk'
2
- import { captureAWSClient } from 'aws-xray-sdk'
2
+ import { Options as MiddyOptions } from '@middy/util'
3
3
  import middy from '@middy/core'
4
4
 
5
- interface Options<S = SSM> {
6
- AwsClient?: new() => S
7
- awsClientOptions?: Partial<SSM.Types.ClientConfiguration>
8
- awsClientAssumeRole?: string
9
- awsClientCapture?: typeof captureAWSClient
10
- fetchData?: { [key: string]: string }
11
- disablePrefetch?: boolean
12
- cacheKey?: string
13
- cacheExpiry?: number
14
- setToEnv?: boolean
15
- setToContext?: boolean
16
- }
5
+ interface Options<S = SSM> extends MiddyOptions<S, SSM.Types.ClientConfiguration> {}
17
6
 
18
7
  declare function ssm (options?: Options): middy.MiddlewareObj
19
8
 
package/index.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  const {
4
2
  canPrefetch,
5
3
  createPrefetchClient,
@@ -10,178 +8,171 @@ const {
10
8
  jsonSafeParse,
11
9
  getInternal,
12
10
  sanitizeKey
13
- } = require('@middy/util');
14
-
15
- const SSM = require('aws-sdk/clients/ssm'); // v2
11
+ } = require('@middy/util')
12
+ const SSM = require('aws-sdk/clients/ssm') // v2
16
13
  // const { SSM } = require('@aws-sdk/client-ssm') // v3
17
14
 
18
-
19
- const awsRequestLimit = 10;
15
+ const awsRequestLimit = 10
20
16
  const defaults = {
21
- AwsClient: SSM,
22
- // Allow for XRay
17
+ AwsClient: SSM, // Allow for XRay
23
18
  awsClientOptions: {},
24
19
  awsClientAssumeRole: undefined,
25
20
  awsClientCapture: undefined,
26
- fetchData: {},
27
- // { contextKey: fetchKey, contextPrefix: fetchPath/ }
21
+ fetchData: {}, // { contextKey: fetchKey, contextPrefix: fetchPath/ }
28
22
  disablePrefetch: false,
29
23
  cacheKey: 'ssm',
30
24
  cacheExpiry: -1,
31
25
  setToEnv: false,
32
26
  setToContext: false
33
- };
27
+ }
34
28
 
35
29
  const ssmMiddleware = (opts = {}) => {
36
- const options = { ...defaults,
37
- ...opts
38
- };
30
+ const options = { ...defaults, ...opts }
39
31
 
40
32
  const fetch = (request, cachedValues) => {
41
- return { ...fetchSingle(request, cachedValues),
33
+ return {
34
+ ...fetchSingle(request, cachedValues),
42
35
  ...fetchByPath(request, cachedValues)
43
- };
44
- };
36
+ }
37
+ }
45
38
 
46
39
  const fetchSingle = (request, cachedValues = {}) => {
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);
40
+ const values = {}
41
+ let batchReq = null
42
+ let batchInternalKeys = []
43
+ let batchFetchKeys = []
53
44
 
45
+ const internalKeys = Object.keys(options.fetchData)
46
+ const fetchKeys = Object.values(options.fetchData)
54
47
  for (const [idx, internalKey] of internalKeys.entries()) {
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;
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
64
59
  }
65
60
 
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);
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) }
84
80
  })
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
- });
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
+ })
92
89
 
93
90
  for (const internalKey of batchInternalKeys) {
94
- values[internalKey] = batchReq.then(params => {
95
- return params[options.fetchData[internalKey]];
96
- });
91
+ values[internalKey] = batchReq.then((params) => {
92
+ return params[options.fetchData[internalKey]]
93
+ })
97
94
  }
98
95
 
99
- batchInternalKeys = [];
100
- batchFetchKeys = [];
101
- batchReq = null;
96
+ batchInternalKeys = []
97
+ batchFetchKeys = []
98
+ batchReq = null
102
99
  }
103
100
 
104
- return values;
105
- };
101
+ return values
102
+ }
106
103
 
107
104
  const fetchByPath = (request, cachedValues = {}) => {
108
- const values = {};
109
-
105
+ const values = {}
110
106
  for (const internalKey in options.fetchData) {
111
- if (cachedValues[internalKey]) continue;
112
- const fetchKey = options.fetchData[internalKey];
113
- if (fetchKey.substr(-1) !== '/') continue; // Skip not path passed in
114
-
115
- values[internalKey] = fetchPath(fetchKey).catch(e => {
116
- var _getCache$value2, _getCache2;
117
-
118
- const value = (_getCache$value2 = (_getCache2 = getCache(options.cacheKey)) === null || _getCache2 === void 0 ? void 0 : _getCache2.value) !== null && _getCache$value2 !== void 0 ? _getCache$value2 : {};
119
- value[internalKey] = undefined;
120
- modifyCache(options.cacheKey, value);
121
- throw e;
122
- });
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
+ })
123
116
  }
124
-
125
- return values;
126
- };
117
+ return values
118
+ }
127
119
 
128
120
  const fetchPath = (path, nextToken, values = {}) => {
129
- return client.getParametersByPath({
130
- Path: path,
131
- NextToken: nextToken,
132
- Recursive: true,
133
- WithDecryption: true
134
- }).promise() // Required for aws-sdk v2
135
- .then(resp => {
136
- Object.assign(values, ...resp.Parameters.map(param => {
137
- return {
138
- [sanitizeKey(param.Name.replace(path, ''))]: parseValue(param)
139
- };
140
- }));
141
- if (resp.NextToken) return fetchPath(path, resp.NextToken, values);
142
- return values;
143
- });
144
- };
145
-
146
- const parseValue = param => {
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
+ }
142
+
143
+ const parseValue = (param) => {
147
144
  if (param.Type === 'StringList') {
148
- return param.Value.split(',');
145
+ return param.Value.split(',')
149
146
  }
147
+ return jsonSafeParse(param.Value)
148
+ }
150
149
 
151
- return jsonSafeParse(param.Value);
152
- };
153
-
154
- let prefetch, client;
155
-
150
+ let prefetch, client
156
151
  if (canPrefetch(options)) {
157
- client = createPrefetchClient(options);
158
- prefetch = processCache(options, fetch);
152
+ client = createPrefetchClient(options)
153
+ prefetch = processCache(options, fetch)
159
154
  }
160
155
 
161
- const ssmMiddlewareBefore = async request => {
162
- var _prefetch;
163
-
156
+ const ssmMiddlewareBefore = async (request) => {
164
157
  if (!client) {
165
- client = await createClient(options, request);
158
+ client = await createClient(options, request)
166
159
  }
167
160
 
168
- const {
169
- value
170
- } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
171
- Object.assign(request.internal, value);
161
+ const { value } = prefetch ?? processCache(options, fetch, request)
162
+
163
+ Object.assign(request.internal, value)
172
164
 
173
165
  if (options.setToContext || options.setToEnv) {
174
- const data = await getInternal(Object.keys(options.fetchData), request);
175
- if (options.setToEnv) Object.assign(process.env, data);
176
- if (options.setToContext) Object.assign(request.context, data);
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)
177
169
  }
178
170
 
179
- prefetch = null;
180
- };
171
+ prefetch = null
172
+ }
181
173
 
182
174
  return {
183
175
  before: ssmMiddlewareBefore
184
- };
185
- };
186
-
187
- module.exports = ssmMiddleware;
176
+ }
177
+ }
178
+ module.exports = ssmMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/ssm",
3
- "version": "2.5.2",
3
+ "version": "2.5.6",
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.2"
49
+ "@middy/util": "^2.5.6"
50
50
  },
51
51
  "devDependencies": {
52
- "@middy/core": "^2.5.2",
52
+ "@middy/core": "^2.5.6",
53
53
  "aws-sdk": "^2.939.0",
54
54
  "aws-xray-sdk": "^3.3.3"
55
55
  },
56
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
56
+ "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
57
57
  }