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

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 +128 -111
  2. package/package.json +6 -5
package/index.js CHANGED
@@ -1,159 +1,176 @@
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;
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
4
16
  const defaults = {
5
- AwsClient: SSM,
17
+ AwsClient: SSM, // Allow for XRay
6
18
  awsClientOptions: {},
7
19
  awsClientAssumeRole: undefined,
8
20
  awsClientCapture: undefined,
9
- fetchData: {},
21
+ fetchData: {}, // { contextKey: fetchKey, contextPrefix: fetchPath/ }
10
22
  disablePrefetch: false,
11
23
  cacheKey: 'ssm',
12
24
  cacheExpiry: -1,
13
25
  setToContext: false
14
- };
26
+ }
15
27
 
16
28
  const ssmMiddleware = (opts = {}) => {
17
- const options = { ...defaults,
18
- ...opts
19
- };
29
+ const options = { ...defaults, ...opts }
20
30
 
21
31
  const fetch = (request, cachedValues) => {
22
- return { ...fetchSingle(request, cachedValues),
32
+ return {
33
+ ...fetchSingle(request, cachedValues),
23
34
  ...fetchByPath(request, cachedValues)
24
- };
25
- };
35
+ }
36
+ }
26
37
 
27
38
  const fetchSingle = (request, cachedValues = {}) => {
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);
39
+ const values = {}
40
+ let batchReq = null
41
+ let batchInternalKeys = []
42
+ let batchFetchKeys = []
34
43
 
44
+ const internalKeys = Object.keys(options.fetchData)
45
+ const fetchKeys = Object.values(options.fetchData)
35
46
  for (const [idx, internalKey] of internalKeys.entries()) {
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;
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
44
58
  }
45
59
 
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);
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) }
58
79
  })
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
- });
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
+ })
71
88
 
72
89
  for (const internalKey of batchInternalKeys) {
73
- values[internalKey] = batchReq.then(params => {
74
- return params[options.fetchData[internalKey]];
75
- });
90
+ values[internalKey] = batchReq.then((params) => {
91
+ return params[options.fetchData[internalKey]]
92
+ })
76
93
  }
77
94
 
78
- batchInternalKeys = [];
79
- batchFetchKeys = [];
80
- batchReq = null;
95
+ batchInternalKeys = []
96
+ batchFetchKeys = []
97
+ batchReq = null
81
98
  }
82
99
 
83
- return values;
84
- };
100
+ return values
101
+ }
85
102
 
86
103
  const fetchByPath = (request, cachedValues = {}) => {
87
- const values = {};
88
-
104
+ const values = {}
89
105
  for (const internalKey in options.fetchData) {
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
- });
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
+ })
99
115
  }
100
-
101
- return values;
102
- };
116
+ return values
117
+ }
103
118
 
104
119
  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 => {
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
+ }
141
+
142
+ const parseValue = (param) => {
122
143
  if (param.Type === 'StringList') {
123
- return param.Value.split(',');
144
+ return param.Value.split(',')
124
145
  }
146
+ return jsonSafeParse(param.Value)
147
+ }
125
148
 
126
- return jsonSafeParse(param.Value);
127
- };
128
-
129
- let prefetch, client;
130
-
149
+ let prefetch, client
131
150
  if (canPrefetch(options)) {
132
- client = createPrefetchClient(options);
133
- prefetch = processCache(options, fetch);
151
+ client = createPrefetchClient(options)
152
+ prefetch = processCache(options, fetch)
134
153
  }
135
154
 
136
- const ssmMiddlewareBefore = async request => {
155
+ const ssmMiddlewareBefore = async (request) => {
137
156
  if (!client) {
138
- client = await createClient(options, request);
157
+ client = await createClient(options, request)
139
158
  }
140
159
 
141
- const {
142
- value
143
- } = prefetch ?? processCache(options, fetch, request);
144
- Object.assign(request.internal, value);
160
+ const { value } = prefetch ?? processCache(options, fetch, request)
161
+
162
+ Object.assign(request.internal, value)
145
163
 
146
164
  if (options.setToContext) {
147
- const data = await getInternal(Object.keys(options.fetchData), request);
148
- Object.assign(request.context, data);
165
+ const data = await getInternal(Object.keys(options.fetchData), request)
166
+ Object.assign(request.context, data)
149
167
  }
150
168
 
151
- prefetch = null;
152
- };
169
+ prefetch = null
170
+ }
153
171
 
154
172
  return {
155
173
  before: ssmMiddlewareBefore
156
- };
157
- };
158
-
159
- export default ssmMiddleware;
174
+ }
175
+ }
176
+ export default ssmMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/ssm",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "SSM (EC2 Systems Manager) parameters middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "test": "npm run test:unit",
21
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
22
23
  },
23
24
  "license": "MIT",
24
25
  "keywords": [
@@ -47,12 +48,12 @@
47
48
  },
48
49
  "homepage": "https://github.com/middyjs/middy#readme",
49
50
  "dependencies": {
50
- "@middy/util": "^3.0.0-alpha.2"
51
+ "@middy/util": "^3.0.0-alpha.3"
51
52
  },
52
53
  "devDependencies": {
53
- "@middy/core": "^3.0.0-alpha.2",
54
+ "@middy/core": "^3.0.0-alpha.3",
54
55
  "aws-sdk": "^2.939.0",
55
56
  "aws-xray-sdk": "^3.3.3"
56
57
  },
57
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
58
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
58
59
  }