@middy/util 4.6.1 → 4.6.2

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.cjs +11 -2
  2. package/index.js +13 -2
  3. package/package.json +3 -3
package/index.cjs CHANGED
@@ -55,6 +55,7 @@ _export(exports, {
55
55
  const createPrefetchClient = (options)=>{
56
56
  const { awsClientOptions } = options;
57
57
  const client = new options.AwsClient(awsClientOptions);
58
+ // AWS XRay
58
59
  if (options.awsClientCapture && options.disablePrefetch) {
59
60
  return options.awsClientCapture(client);
60
61
  } else if (options.awsClientCapture) {
@@ -64,6 +65,7 @@ const createPrefetchClient = (options)=>{
64
65
  };
65
66
  const createClient = async (options, request)=>{
66
67
  let awsClientCredentials = {};
68
+ // Role Credentials
67
69
  if (options.awsClientAssumeRole) {
68
70
  if (!request) {
69
71
  throw new Error('Request required when assuming role');
@@ -102,6 +104,7 @@ const getInternal = async (variables, request)=>{
102
104
  }
103
105
  const promises = [];
104
106
  for (const internalKey of values){
107
+ // 'internal.key.sub_value' -> { [key]: internal.key.sub_value }
105
108
  const pathOptionKey = internalKey.split('.');
106
109
  const rootOptionKey = pathOptionKey.shift();
107
110
  let valuePromise = request.internal[rootOptionKey];
@@ -110,6 +113,8 @@ const getInternal = async (variables, request)=>{
110
113
  }
111
114
  promises.push(valuePromise.then((value)=>pathOptionKey.reduce((p, c)=>p?.[c], value)));
112
115
  }
116
+ // ensure promise has resolved by the time it's needed
117
+ // If one of the promises throws it will bubble up to @middy/core
113
118
  values = await Promise.allSettled(promises);
114
119
  const errors = values.filter((res)=>res.status === 'rejected').map((res)=>res.reason);
115
120
  if (errors.length) {
@@ -128,7 +133,9 @@ const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g;
128
133
  const sanitizeKey = (key)=>{
129
134
  return key.replace(sanitizeKeyPrefixLeadingNumber, '_$1').replace(sanitizeKeyRemoveDisallowedChar, '_');
130
135
  };
131
- const cache = {};
136
+ // fetch Cache
137
+ const cache = {} // key: { value:{fetchKey:Promise}, expiry }
138
+ ;
132
139
  const processCache = (options, fetch = ()=>undefined, request)=>{
133
140
  let { cacheKey, cacheKeyExpiry, cacheExpiry } = options;
134
141
  cacheExpiry = cacheKeyExpiry?.[cacheKey] ?? cacheExpiry;
@@ -155,6 +162,7 @@ const processCache = (options, fetch = ()=>undefined, request)=>{
155
162
  }
156
163
  const value = fetch(request);
157
164
  const now = Date.now();
165
+ // secrets-manager overrides to unix timestamp
158
166
  const expiry = cacheExpiry > 86400000 ? cacheExpiry : now + cacheExpiry;
159
167
  const duration = cacheExpiry > 86400000 ? cacheExpiry - now : cacheExpiry;
160
168
  if (cacheExpiry) {
@@ -234,7 +242,8 @@ class HttpError extends Error {
234
242
  super(message, options);
235
243
  const name = httpErrorCodes[code].replace(createErrorRegexp, '');
236
244
  this.name = name.substr(-5) !== 'Error' ? name + 'Error' : name;
237
- this.status = this.statusCode = code;
245
+ this.status = this.statusCode = code // setting `status` for backwards compatibility w/ `http-errors`
246
+ ;
238
247
  this.expose = options.expose ?? code < 500;
239
248
  }
240
249
  }
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export const createPrefetchClient = (options)=>{
2
2
  const { awsClientOptions } = options;
3
3
  const client = new options.AwsClient(awsClientOptions);
4
+ // AWS XRay
4
5
  if (options.awsClientCapture && options.disablePrefetch) {
5
6
  return options.awsClientCapture(client);
6
7
  } else if (options.awsClientCapture) {
@@ -10,6 +11,7 @@ export const createPrefetchClient = (options)=>{
10
11
  };
11
12
  export const createClient = async (options, request)=>{
12
13
  let awsClientCredentials = {};
14
+ // Role Credentials
13
15
  if (options.awsClientAssumeRole) {
14
16
  if (!request) {
15
17
  throw new Error('Request required when assuming role');
@@ -30,6 +32,7 @@ export const createClient = async (options, request)=>{
30
32
  export const canPrefetch = (options = {})=>{
31
33
  return !options.awsClientAssumeRole && !options.disablePrefetch;
32
34
  };
35
+ // Internal Context
33
36
  export const getInternal = async (variables, request)=>{
34
37
  if (!variables || !request) return {};
35
38
  let keys = [];
@@ -48,6 +51,7 @@ export const getInternal = async (variables, request)=>{
48
51
  }
49
52
  const promises = [];
50
53
  for (const internalKey of values){
54
+ // 'internal.key.sub_value' -> { [key]: internal.key.sub_value }
51
55
  const pathOptionKey = internalKey.split('.');
52
56
  const rootOptionKey = pathOptionKey.shift();
53
57
  let valuePromise = request.internal[rootOptionKey];
@@ -56,6 +60,8 @@ export const getInternal = async (variables, request)=>{
56
60
  }
57
61
  promises.push(valuePromise.then((value)=>pathOptionKey.reduce((p, c)=>p?.[c], value)));
58
62
  }
63
+ // ensure promise has resolved by the time it's needed
64
+ // If one of the promises throws it will bubble up to @middy/core
59
65
  values = await Promise.allSettled(promises);
60
66
  const errors = values.filter((res)=>res.status === 'rejected').map((res)=>res.reason);
61
67
  if (errors.length) {
@@ -74,7 +80,9 @@ const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g;
74
80
  export const sanitizeKey = (key)=>{
75
81
  return key.replace(sanitizeKeyPrefixLeadingNumber, '_$1').replace(sanitizeKeyRemoveDisallowedChar, '_');
76
82
  };
77
- const cache = {};
83
+ // fetch Cache
84
+ const cache = {} // key: { value:{fetchKey:Promise}, expiry }
85
+ ;
78
86
  export const processCache = (options, fetch = ()=>undefined, request)=>{
79
87
  let { cacheKey, cacheKeyExpiry, cacheExpiry } = options;
80
88
  cacheExpiry = cacheKeyExpiry?.[cacheKey] ?? cacheExpiry;
@@ -101,6 +109,7 @@ export const processCache = (options, fetch = ()=>undefined, request)=>{
101
109
  }
102
110
  const value = fetch(request);
103
111
  const now = Date.now();
112
+ // secrets-manager overrides to unix timestamp
104
113
  const expiry = cacheExpiry > 86400000 ? cacheExpiry : now + cacheExpiry;
105
114
  const duration = cacheExpiry > 86400000 ? cacheExpiry - now : cacheExpiry;
106
115
  if (cacheExpiry) {
@@ -120,6 +129,7 @@ export const getCache = (key)=>{
120
129
  if (!cache[key]) return {};
121
130
  return cache[key];
122
131
  };
132
+ // Used to remove parts of a cache
123
133
  export const modifyCache = (cacheKey, value)=>{
124
134
  if (!cache[cacheKey]) return;
125
135
  clearInterval(cache[cacheKey]?.refresh);
@@ -180,7 +190,8 @@ export class HttpError extends Error {
180
190
  super(message, options);
181
191
  const name = httpErrorCodes[code].replace(createErrorRegexp, '');
182
192
  this.name = name.substr(-5) !== 'Error' ? name + 'Error' : name;
183
- this.status = this.statusCode = code;
193
+ this.status = this.statusCode = code // setting `status` for backwards compatibility w/ `http-errors`
194
+ ;
184
195
  this.expose = options.expose ?? code < 500;
185
196
  }
186
197
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/util",
3
- "version": "4.6.1",
3
+ "version": "4.6.2",
4
4
  "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)",
5
5
  "type": "module",
6
6
  "engines": {
@@ -61,7 +61,7 @@
61
61
  },
62
62
  "devDependencies": {
63
63
  "@aws-sdk/client-ssm": "^3.0.0",
64
- "@middy/core": "4.6.1",
64
+ "@middy/core": "4.6.2",
65
65
  "@types/aws-lambda": "^8.10.76",
66
66
  "@types/node": "^20.0.0",
67
67
  "aws-xray-sdk": "^3.3.3"
@@ -71,5 +71,5 @@
71
71
  "type": "github",
72
72
  "url": "https://github.com/sponsors/willfarrell"
73
73
  },
74
- "gitHead": "253ed0e4ca95623decbade03938a07d837a1eba2"
74
+ "gitHead": "8b03a01abf5a9c08231ec5ced775e87f8be8f67d"
75
75
  }