@middy/ssm 3.0.0-alpha.3 → 3.0.0-alpha.4

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