@middy/util 4.6.5 → 5.0.0-alpha.1
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.
- package/index.d.ts +72 -5
- package/index.js +272 -255
- package/package.json +4 -10
- package/index.cjs +0 -319
package/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import middy from '@middy/core'
|
|
2
|
+
import { Context as LambdaContext } from 'aws-lambda'
|
|
3
|
+
import { ArrayValues, Choose, DeepAwaited, IsUnknown, SanitizeKey, SanitizeKeys } from './type-utils'
|
|
2
4
|
|
|
3
5
|
interface Options<Client, ClientOptions> {
|
|
4
6
|
AwsClient?: new (...[config]: [any] | any) => Client
|
|
@@ -33,12 +35,77 @@ declare function canPrefetch<Client, ClientOptions> (
|
|
|
33
35
|
options: Options<Client, ClientOptions>
|
|
34
36
|
): boolean
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
type InternalOutput<TVariables> = TVariables extends string[] ? { [key in TVariables[number]]: unknown } : never
|
|
39
|
+
|
|
40
|
+
// get an empty object if false is passed
|
|
41
|
+
declare function getInternal<
|
|
42
|
+
TContext extends LambdaContext,
|
|
43
|
+
TInternal extends Record<string, unknown>
|
|
44
|
+
> (
|
|
45
|
+
variables: false,
|
|
46
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
|
|
47
|
+
): Promise<{}>
|
|
48
|
+
|
|
49
|
+
// get all internal values if true is passed (with promises resolved)
|
|
50
|
+
declare function getInternal<
|
|
51
|
+
TContext extends LambdaContext,
|
|
52
|
+
TInternal extends Record<string, unknown>
|
|
53
|
+
> (
|
|
54
|
+
variables: true,
|
|
55
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
|
|
56
|
+
): Promise<DeepAwaited<TInternal>>
|
|
57
|
+
|
|
58
|
+
// get a single value
|
|
59
|
+
declare function getInternal<
|
|
60
|
+
TContext extends LambdaContext,
|
|
61
|
+
TInternal extends Record<string, unknown>,
|
|
62
|
+
TVars extends keyof TInternal | string
|
|
63
|
+
> (
|
|
64
|
+
variables: TVars,
|
|
65
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
|
|
66
|
+
): TVars extends keyof TInternal
|
|
67
|
+
? Promise<DeepAwaited<{ [_ in SanitizeKey<TVars>]: TInternal[TVars] }>>
|
|
68
|
+
: TVars extends string
|
|
69
|
+
? IsUnknown<Choose<DeepAwaited<TInternal>, TVars>> extends true
|
|
70
|
+
? unknown // could not find the path
|
|
71
|
+
: Promise<{ [_ in SanitizeKey<TVars>]: Choose<DeepAwaited<TInternal>, TVars> }>
|
|
72
|
+
: unknown // path is not a string or a keyof TInternal
|
|
73
|
+
|
|
74
|
+
// get multiple values
|
|
75
|
+
declare function getInternal<
|
|
76
|
+
TContext extends LambdaContext,
|
|
77
|
+
TInternal extends Record<string, unknown>,
|
|
78
|
+
TVars extends Array<keyof TInternal | string>
|
|
79
|
+
> (
|
|
80
|
+
variables: TVars,
|
|
81
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
|
|
82
|
+
): Promise<SanitizeKeys<{
|
|
83
|
+
[TVar in ArrayValues<TVars>]:
|
|
84
|
+
TVar extends keyof TInternal
|
|
85
|
+
? DeepAwaited<TInternal[TVar]>
|
|
86
|
+
: TVar extends string
|
|
87
|
+
? Choose<DeepAwaited<TInternal>, TVar>
|
|
88
|
+
: unknown // path is not a string or a keyof TInternal
|
|
89
|
+
}>>
|
|
90
|
+
|
|
91
|
+
// remap object
|
|
92
|
+
declare function getInternal<
|
|
93
|
+
TContext extends LambdaContext,
|
|
94
|
+
TInternal extends Record<string, unknown>,
|
|
95
|
+
TMap extends Record<string, keyof TInternal | string>
|
|
96
|
+
> (
|
|
97
|
+
variables: TMap,
|
|
98
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
|
|
99
|
+
): Promise<{
|
|
100
|
+
[P in keyof TMap]:
|
|
101
|
+
TMap[P] extends keyof TInternal
|
|
102
|
+
? DeepAwaited<TInternal[TMap[P]]>
|
|
103
|
+
: TMap[P] extends string
|
|
104
|
+
? Choose<DeepAwaited<TInternal>, TMap[P]>
|
|
105
|
+
: unknown // path is not a string or a keyof TInternal
|
|
106
|
+
}>
|
|
40
107
|
|
|
41
|
-
declare function sanitizeKey (key:
|
|
108
|
+
declare function sanitizeKey<T extends string> (key: T): SanitizeKey<T>
|
|
42
109
|
|
|
43
110
|
declare function processCache<Client, ClientOptions> (
|
|
44
111
|
options: Options<Client, ClientOptions>,
|
package/index.js
CHANGED
|
@@ -1,267 +1,284 @@
|
|
|
1
|
-
export const createPrefetchClient = (options)=>{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
export const createPrefetchClient = (options) => {
|
|
2
|
+
const { awsClientOptions } = options
|
|
3
|
+
const client = new options.AwsClient(awsClientOptions)
|
|
4
|
+
|
|
5
|
+
// AWS XRay
|
|
6
|
+
if (options.awsClientCapture && options.disablePrefetch) {
|
|
7
|
+
return options.awsClientCapture(client)
|
|
8
|
+
} else if (options.awsClientCapture) {
|
|
9
|
+
console.warn('Unable to apply X-Ray outside of handler invocation scope.')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return client
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const createClient = async (options, request) => {
|
|
16
|
+
let awsClientCredentials = {}
|
|
17
|
+
|
|
18
|
+
// Role Credentials
|
|
19
|
+
if (options.awsClientAssumeRole) {
|
|
20
|
+
if (!request) {
|
|
21
|
+
throw new Error('Request required when assuming role', {
|
|
22
|
+
cause: { package: '@middy/util' }
|
|
23
|
+
})
|
|
22
24
|
}
|
|
23
|
-
awsClientCredentials =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
awsClientCredentials = await getInternal(
|
|
26
|
+
{ credentials: options.awsClientAssumeRole },
|
|
27
|
+
request
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
awsClientCredentials = {
|
|
32
|
+
...awsClientCredentials,
|
|
33
|
+
...options.awsClientOptions
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return createPrefetchClient({
|
|
37
|
+
...options,
|
|
38
|
+
awsClientOptions: awsClientCredentials
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const canPrefetch = (options = {}) => {
|
|
43
|
+
return !options.awsClientAssumeRole && !options.disablePrefetch
|
|
44
|
+
}
|
|
45
|
+
|
|
35
46
|
// Internal Context
|
|
36
|
-
export const getInternal = async (variables, request)=>{
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
export const getInternal = async (variables, request) => {
|
|
48
|
+
if (!variables || !request) return {}
|
|
49
|
+
let keys = []
|
|
50
|
+
let values = []
|
|
51
|
+
if (variables === true) {
|
|
52
|
+
keys = values = Object.keys(request.internal)
|
|
53
|
+
} else if (typeof variables === 'string') {
|
|
54
|
+
keys = values = [variables]
|
|
55
|
+
} else if (Array.isArray(variables)) {
|
|
56
|
+
keys = values = variables
|
|
57
|
+
} else if (typeof variables === 'object') {
|
|
58
|
+
keys = Object.keys(variables)
|
|
59
|
+
values = Object.values(variables)
|
|
60
|
+
}
|
|
61
|
+
const promises = []
|
|
62
|
+
for (const internalKey of values) {
|
|
63
|
+
// 'internal.key.sub_value' -> { [key]: internal.key.sub_value }
|
|
64
|
+
const pathOptionKey = internalKey.split('.')
|
|
65
|
+
const rootOptionKey = pathOptionKey.shift()
|
|
66
|
+
let valuePromise = request.internal[rootOptionKey]
|
|
67
|
+
if (!isPromise(valuePromise)) {
|
|
68
|
+
valuePromise = Promise.resolve(valuePromise)
|
|
51
69
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
70
|
+
promises.push(
|
|
71
|
+
valuePromise.then((value) =>
|
|
72
|
+
pathOptionKey.reduce((p, c) => p?.[c], value)
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
// ensure promise has resolved by the time it's needed
|
|
77
|
+
// If one of the promises throws it will bubble up to @middy/core
|
|
78
|
+
values = await Promise.allSettled(promises)
|
|
79
|
+
const errors = values
|
|
80
|
+
.filter((res) => res.status === 'rejected')
|
|
81
|
+
.map((res) => res.reason)
|
|
82
|
+
if (errors.length) {
|
|
83
|
+
throw new Error('Failed to resolve internal values', {
|
|
84
|
+
cause: { package: '@middy/util', data: errors }
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
return keys.reduce(
|
|
88
|
+
(obj, key, index) => ({ ...obj, [sanitizeKey(key)]: values[index].value }),
|
|
89
|
+
{}
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const isPromise = (promise) => typeof promise?.then === 'function'
|
|
94
|
+
|
|
95
|
+
const sanitizeKeyPrefixLeadingNumber = /^([0-9])/
|
|
96
|
+
const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g
|
|
97
|
+
export const sanitizeKey = (key) => {
|
|
98
|
+
return key
|
|
99
|
+
.replace(sanitizeKeyPrefixLeadingNumber, '_$1')
|
|
100
|
+
.replace(sanitizeKeyRemoveDisallowedChar, '_')
|
|
101
|
+
}
|
|
102
|
+
|
|
83
103
|
// fetch Cache
|
|
84
104
|
const cache = {} // key: { value:{fetchKey:Promise}, expiry }
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
});
|
|
101
|
-
return cache[cacheKey];
|
|
102
|
-
}
|
|
103
|
-
if (unexpired) {
|
|
104
|
-
return {
|
|
105
|
-
...cached,
|
|
106
|
-
cache: true
|
|
107
|
-
};
|
|
108
|
-
}
|
|
105
|
+
export const processCache = (options, fetch = () => undefined, request) => {
|
|
106
|
+
let { cacheKey, cacheKeyExpiry, cacheExpiry } = options
|
|
107
|
+
cacheExpiry = cacheKeyExpiry?.[cacheKey] ?? cacheExpiry
|
|
108
|
+
if (cacheExpiry) {
|
|
109
|
+
const cached = getCache(cacheKey)
|
|
110
|
+
const unexpired =
|
|
111
|
+
cached.expiry && (cacheExpiry < 0 || cached.expiry > Date.now())
|
|
112
|
+
|
|
113
|
+
if (unexpired && cached.modified) {
|
|
114
|
+
const value = fetch(request, cached.value)
|
|
115
|
+
cache[cacheKey] = Object.create({
|
|
116
|
+
value: { ...cached.value, ...value },
|
|
117
|
+
expiry: cached.expiry
|
|
118
|
+
})
|
|
119
|
+
return cache[cacheKey]
|
|
109
120
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// secrets-manager overrides to unix timestamp
|
|
113
|
-
const expiry = cacheExpiry > 86400000 ? cacheExpiry : now + cacheExpiry;
|
|
114
|
-
const duration = cacheExpiry > 86400000 ? cacheExpiry - now : cacheExpiry;
|
|
115
|
-
if (cacheExpiry) {
|
|
116
|
-
const refresh = duration > 0 ? setInterval(()=>processCache(options, fetch, request), duration) : undefined;
|
|
117
|
-
cache[cacheKey] = {
|
|
118
|
-
value,
|
|
119
|
-
expiry,
|
|
120
|
-
refresh
|
|
121
|
-
};
|
|
121
|
+
if (unexpired) {
|
|
122
|
+
return { ...cached, cache: true }
|
|
122
123
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
124
|
+
}
|
|
125
|
+
const value = fetch(request)
|
|
126
|
+
const now = Date.now()
|
|
127
|
+
// secrets-manager overrides to unix timestamp
|
|
128
|
+
const expiry = cacheExpiry > 86400000 ? cacheExpiry : now + cacheExpiry
|
|
129
|
+
const duration = cacheExpiry > 86400000 ? cacheExpiry - now : cacheExpiry
|
|
130
|
+
if (cacheExpiry) {
|
|
131
|
+
const refresh =
|
|
132
|
+
duration > 0
|
|
133
|
+
? setInterval(() => processCache(options, fetch, request), duration)
|
|
134
|
+
: undefined
|
|
135
|
+
cache[cacheKey] = { value, expiry, refresh }
|
|
136
|
+
}
|
|
137
|
+
return { value, expiry }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export const getCache = (key) => {
|
|
141
|
+
if (!cache[key]) return {}
|
|
142
|
+
return cache[key]
|
|
143
|
+
}
|
|
144
|
+
|
|
132
145
|
// Used to remove parts of a cache
|
|
133
|
-
export const modifyCache = (cacheKey, value)=>{
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
export const normalizeHttpResponse = (request)=>{
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
146
|
+
export const modifyCache = (cacheKey, value) => {
|
|
147
|
+
if (!cache[cacheKey]) return
|
|
148
|
+
clearInterval(cache[cacheKey]?.refresh)
|
|
149
|
+
cache[cacheKey] = { ...cache[cacheKey], value, modified: true }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export const clearCache = (keys = null) => {
|
|
153
|
+
keys = keys ?? Object.keys(cache)
|
|
154
|
+
if (!Array.isArray(keys)) keys = [keys]
|
|
155
|
+
for (const cacheKey of keys) {
|
|
156
|
+
clearInterval(cache[cacheKey]?.refresh)
|
|
157
|
+
cache[cacheKey] = undefined
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export const jsonSafeParse = (text, reviver) => {
|
|
162
|
+
if (typeof text !== 'string') return text
|
|
163
|
+
const firstChar = text[0]
|
|
164
|
+
if (firstChar !== '{' && firstChar !== '[' && firstChar !== '"') return text
|
|
165
|
+
try {
|
|
166
|
+
return JSON.parse(text, reviver)
|
|
167
|
+
} catch (e) {}
|
|
168
|
+
|
|
169
|
+
return text
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export const jsonSafeStringify = (value, replacer, space) => {
|
|
173
|
+
try {
|
|
174
|
+
return JSON.stringify(value, replacer, space)
|
|
175
|
+
} catch (e) {}
|
|
176
|
+
|
|
177
|
+
return value
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const normalizeHttpResponse = (request) => {
|
|
181
|
+
let { response } = request
|
|
182
|
+
if (typeof response === 'undefined') {
|
|
183
|
+
response = {}
|
|
184
|
+
} else if (
|
|
185
|
+
typeof response?.statusCode === 'undefined' &&
|
|
186
|
+
typeof response?.body === 'undefined' &&
|
|
187
|
+
typeof response?.headers === 'undefined'
|
|
188
|
+
) {
|
|
189
|
+
response = { statusCode: 200, body: response }
|
|
190
|
+
}
|
|
191
|
+
response.statusCode ??= 500
|
|
192
|
+
response.headers ??= {}
|
|
193
|
+
request.response = response
|
|
194
|
+
return response
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const createErrorRegexp = /[^a-zA-Z]/g
|
|
183
198
|
export class HttpError extends Error {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
message ??= httpErrorCodes[code];
|
|
190
|
-
super(message, options);
|
|
191
|
-
const name = httpErrorCodes[code].replace(createErrorRegexp, '');
|
|
192
|
-
this.name = name.substr(-5) !== 'Error' ? name + 'Error' : name;
|
|
193
|
-
this.status = this.statusCode = code // setting `status` for backwards compatibility w/ `http-errors`
|
|
194
|
-
;
|
|
195
|
-
this.expose = options.expose ?? code < 500;
|
|
199
|
+
constructor (code, message, options = {}) {
|
|
200
|
+
if (message && typeof message !== 'string') {
|
|
201
|
+
options = message
|
|
202
|
+
message = undefined
|
|
196
203
|
}
|
|
204
|
+
message ??= httpErrorCodes[code]
|
|
205
|
+
super(message, options)
|
|
206
|
+
|
|
207
|
+
const name = httpErrorCodes[code].replace(createErrorRegexp, '')
|
|
208
|
+
this.name = name.substr(-5) !== 'Error' ? name + 'Error' : name
|
|
209
|
+
|
|
210
|
+
this.status = this.statusCode = code // setting `status` for backwards compatibility w/ `http-errors`
|
|
211
|
+
this.expose = options.expose ?? code < 500
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export const createError = (code, message, properties = {}) => {
|
|
216
|
+
return new HttpError(code, message, properties)
|
|
197
217
|
}
|
|
198
|
-
export const createError = (code, message, properties = {})=>{
|
|
199
|
-
return new HttpError(code, message, properties);
|
|
200
|
-
};
|
|
201
|
-
const httpErrorCodes = {
|
|
202
|
-
100: 'Continue',
|
|
203
|
-
101: 'Switching Protocols',
|
|
204
|
-
102: 'Processing',
|
|
205
|
-
103: 'Early Hints',
|
|
206
|
-
200: 'OK',
|
|
207
|
-
201: 'Created',
|
|
208
|
-
202: 'Accepted',
|
|
209
|
-
203: 'Non-Authoritative Information',
|
|
210
|
-
204: 'No Content',
|
|
211
|
-
205: 'Reset Content',
|
|
212
|
-
206: 'Partial Content',
|
|
213
|
-
207: 'Multi-Status',
|
|
214
|
-
208: 'Already Reported',
|
|
215
|
-
226: 'IM Used',
|
|
216
|
-
300: 'Multiple Choices',
|
|
217
|
-
301: 'Moved Permanently',
|
|
218
|
-
302: 'Found',
|
|
219
|
-
303: 'See Other',
|
|
220
|
-
304: 'Not Modified',
|
|
221
|
-
305: 'Use Proxy',
|
|
222
|
-
306: '(Unused)',
|
|
223
|
-
307: 'Temporary Redirect',
|
|
224
|
-
308: 'Permanent Redirect',
|
|
225
|
-
400: 'Bad Request',
|
|
226
|
-
401: 'Unauthorized',
|
|
227
|
-
402: 'Payment Required',
|
|
228
|
-
403: 'Forbidden',
|
|
229
|
-
404: 'Not Found',
|
|
230
|
-
405: 'Method Not Allowed',
|
|
231
|
-
406: 'Not Acceptable',
|
|
232
|
-
407: 'Proxy Authentication Required',
|
|
233
|
-
408: 'Request Timeout',
|
|
234
|
-
409: 'Conflict',
|
|
235
|
-
410: 'Gone',
|
|
236
|
-
411: 'Length Required',
|
|
237
|
-
412: 'Precondition Failed',
|
|
238
|
-
413: 'Payload Too Large',
|
|
239
|
-
414: 'URI Too Long',
|
|
240
|
-
415: 'Unsupported Media Type',
|
|
241
|
-
416: 'Range Not Satisfiable',
|
|
242
|
-
417: 'Expectation Failed',
|
|
243
|
-
418: "I'm a teapot",
|
|
244
|
-
421: 'Misdirected Request',
|
|
245
|
-
422: 'Unprocessable Entity',
|
|
246
|
-
423: 'Locked',
|
|
247
|
-
424: 'Failed Dependency',
|
|
248
|
-
425: 'Unordered Collection',
|
|
249
|
-
426: 'Upgrade Required',
|
|
250
|
-
428: 'Precondition Required',
|
|
251
|
-
429: 'Too Many Requests',
|
|
252
|
-
431: 'Request Header Fields Too Large',
|
|
253
|
-
451: 'Unavailable For Legal Reasons',
|
|
254
|
-
500: 'Internal Server Error',
|
|
255
|
-
501: 'Not Implemented',
|
|
256
|
-
502: 'Bad Gateway',
|
|
257
|
-
503: 'Service Unavailable',
|
|
258
|
-
504: 'Gateway Timeout',
|
|
259
|
-
505: 'HTTP Version Not Supported',
|
|
260
|
-
506: 'Variant Also Negotiates',
|
|
261
|
-
507: 'Insufficient Storage',
|
|
262
|
-
508: 'Loop Detected',
|
|
263
|
-
509: 'Bandwidth Limit Exceeded',
|
|
264
|
-
510: 'Not Extended',
|
|
265
|
-
511: 'Network Authentication Required'
|
|
266
|
-
};
|
|
267
218
|
|
|
219
|
+
const httpErrorCodes = {
|
|
220
|
+
100: 'Continue',
|
|
221
|
+
101: 'Switching Protocols',
|
|
222
|
+
102: 'Processing',
|
|
223
|
+
103: 'Early Hints',
|
|
224
|
+
200: 'OK',
|
|
225
|
+
201: 'Created',
|
|
226
|
+
202: 'Accepted',
|
|
227
|
+
203: 'Non-Authoritative Information',
|
|
228
|
+
204: 'No Content',
|
|
229
|
+
205: 'Reset Content',
|
|
230
|
+
206: 'Partial Content',
|
|
231
|
+
207: 'Multi-Status',
|
|
232
|
+
208: 'Already Reported',
|
|
233
|
+
226: 'IM Used',
|
|
234
|
+
300: 'Multiple Choices',
|
|
235
|
+
301: 'Moved Permanently',
|
|
236
|
+
302: 'Found',
|
|
237
|
+
303: 'See Other',
|
|
238
|
+
304: 'Not Modified',
|
|
239
|
+
305: 'Use Proxy',
|
|
240
|
+
306: '(Unused)',
|
|
241
|
+
307: 'Temporary Redirect',
|
|
242
|
+
308: 'Permanent Redirect',
|
|
243
|
+
400: 'Bad Request',
|
|
244
|
+
401: 'Unauthorized',
|
|
245
|
+
402: 'Payment Required',
|
|
246
|
+
403: 'Forbidden',
|
|
247
|
+
404: 'Not Found',
|
|
248
|
+
405: 'Method Not Allowed',
|
|
249
|
+
406: 'Not Acceptable',
|
|
250
|
+
407: 'Proxy Authentication Required',
|
|
251
|
+
408: 'Request Timeout',
|
|
252
|
+
409: 'Conflict',
|
|
253
|
+
410: 'Gone',
|
|
254
|
+
411: 'Length Required',
|
|
255
|
+
412: 'Precondition Failed',
|
|
256
|
+
413: 'Payload Too Large',
|
|
257
|
+
414: 'URI Too Long',
|
|
258
|
+
415: 'Unsupported Media Type',
|
|
259
|
+
416: 'Range Not Satisfiable',
|
|
260
|
+
417: 'Expectation Failed',
|
|
261
|
+
418: "I'm a teapot",
|
|
262
|
+
421: 'Misdirected Request',
|
|
263
|
+
422: 'Unprocessable Entity',
|
|
264
|
+
423: 'Locked',
|
|
265
|
+
424: 'Failed Dependency',
|
|
266
|
+
425: 'Unordered Collection',
|
|
267
|
+
426: 'Upgrade Required',
|
|
268
|
+
428: 'Precondition Required',
|
|
269
|
+
429: 'Too Many Requests',
|
|
270
|
+
431: 'Request Header Fields Too Large',
|
|
271
|
+
451: 'Unavailable For Legal Reasons',
|
|
272
|
+
500: 'Internal Server Error',
|
|
273
|
+
501: 'Not Implemented',
|
|
274
|
+
502: 'Bad Gateway',
|
|
275
|
+
503: 'Service Unavailable',
|
|
276
|
+
504: 'Gateway Timeout',
|
|
277
|
+
505: 'HTTP Version Not Supported',
|
|
278
|
+
506: 'Variant Also Negotiates',
|
|
279
|
+
507: 'Insufficient Storage',
|
|
280
|
+
508: 'Loop Detected',
|
|
281
|
+
509: 'Bandwidth Limit Exceeded',
|
|
282
|
+
510: 'Not Extended',
|
|
283
|
+
511: 'Network Authentication Required'
|
|
284
|
+
}
|
package/package.json
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/util",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=18"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts",
|
|
32
26
|
"codes.js"
|
|
33
27
|
],
|
|
@@ -61,7 +55,7 @@
|
|
|
61
55
|
},
|
|
62
56
|
"devDependencies": {
|
|
63
57
|
"@aws-sdk/client-ssm": "^3.0.0",
|
|
64
|
-
"@middy/core": "
|
|
58
|
+
"@middy/core": "5.0.0-alpha.1",
|
|
65
59
|
"@types/aws-lambda": "^8.10.76",
|
|
66
60
|
"@types/node": "^20.0.0",
|
|
67
61
|
"aws-xray-sdk": "^3.3.3"
|
|
@@ -71,5 +65,5 @@
|
|
|
71
65
|
"type": "github",
|
|
72
66
|
"url": "https://github.com/sponsors/willfarrell"
|
|
73
67
|
},
|
|
74
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
75
69
|
}
|
package/index.cjs
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
HttpError: function() {
|
|
13
|
-
return HttpError;
|
|
14
|
-
},
|
|
15
|
-
canPrefetch: function() {
|
|
16
|
-
return canPrefetch;
|
|
17
|
-
},
|
|
18
|
-
clearCache: function() {
|
|
19
|
-
return clearCache;
|
|
20
|
-
},
|
|
21
|
-
createClient: function() {
|
|
22
|
-
return createClient;
|
|
23
|
-
},
|
|
24
|
-
createError: function() {
|
|
25
|
-
return createError;
|
|
26
|
-
},
|
|
27
|
-
createPrefetchClient: function() {
|
|
28
|
-
return createPrefetchClient;
|
|
29
|
-
},
|
|
30
|
-
getCache: function() {
|
|
31
|
-
return getCache;
|
|
32
|
-
},
|
|
33
|
-
getInternal: function() {
|
|
34
|
-
return getInternal;
|
|
35
|
-
},
|
|
36
|
-
jsonSafeParse: function() {
|
|
37
|
-
return jsonSafeParse;
|
|
38
|
-
},
|
|
39
|
-
jsonSafeStringify: function() {
|
|
40
|
-
return jsonSafeStringify;
|
|
41
|
-
},
|
|
42
|
-
modifyCache: function() {
|
|
43
|
-
return modifyCache;
|
|
44
|
-
},
|
|
45
|
-
normalizeHttpResponse: function() {
|
|
46
|
-
return normalizeHttpResponse;
|
|
47
|
-
},
|
|
48
|
-
processCache: function() {
|
|
49
|
-
return processCache;
|
|
50
|
-
},
|
|
51
|
-
sanitizeKey: function() {
|
|
52
|
-
return sanitizeKey;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
const createPrefetchClient = (options)=>{
|
|
56
|
-
const { awsClientOptions } = options;
|
|
57
|
-
const client = new options.AwsClient(awsClientOptions);
|
|
58
|
-
// AWS XRay
|
|
59
|
-
if (options.awsClientCapture && options.disablePrefetch) {
|
|
60
|
-
return options.awsClientCapture(client);
|
|
61
|
-
} else if (options.awsClientCapture) {
|
|
62
|
-
console.warn('Unable to apply X-Ray outside of handler invocation scope.');
|
|
63
|
-
}
|
|
64
|
-
return client;
|
|
65
|
-
};
|
|
66
|
-
const createClient = async (options, request)=>{
|
|
67
|
-
let awsClientCredentials = {};
|
|
68
|
-
// Role Credentials
|
|
69
|
-
if (options.awsClientAssumeRole) {
|
|
70
|
-
if (!request) {
|
|
71
|
-
throw new Error('Request required when assuming role');
|
|
72
|
-
}
|
|
73
|
-
awsClientCredentials = await getInternal({
|
|
74
|
-
credentials: options.awsClientAssumeRole
|
|
75
|
-
}, request);
|
|
76
|
-
}
|
|
77
|
-
awsClientCredentials = {
|
|
78
|
-
...awsClientCredentials,
|
|
79
|
-
...options.awsClientOptions
|
|
80
|
-
};
|
|
81
|
-
return createPrefetchClient({
|
|
82
|
-
...options,
|
|
83
|
-
awsClientOptions: awsClientCredentials
|
|
84
|
-
});
|
|
85
|
-
};
|
|
86
|
-
const canPrefetch = (options = {})=>{
|
|
87
|
-
return !options.awsClientAssumeRole && !options.disablePrefetch;
|
|
88
|
-
};
|
|
89
|
-
const getInternal = async (variables, request)=>{
|
|
90
|
-
if (!variables || !request) return {};
|
|
91
|
-
let keys = [];
|
|
92
|
-
let values = [];
|
|
93
|
-
if (variables === true) {
|
|
94
|
-
keys = values = Object.keys(request.internal);
|
|
95
|
-
} else if (typeof variables === 'string') {
|
|
96
|
-
keys = values = [
|
|
97
|
-
variables
|
|
98
|
-
];
|
|
99
|
-
} else if (Array.isArray(variables)) {
|
|
100
|
-
keys = values = variables;
|
|
101
|
-
} else if (typeof variables === 'object') {
|
|
102
|
-
keys = Object.keys(variables);
|
|
103
|
-
values = Object.values(variables);
|
|
104
|
-
}
|
|
105
|
-
const promises = [];
|
|
106
|
-
for (const internalKey of values){
|
|
107
|
-
// 'internal.key.sub_value' -> { [key]: internal.key.sub_value }
|
|
108
|
-
const pathOptionKey = internalKey.split('.');
|
|
109
|
-
const rootOptionKey = pathOptionKey.shift();
|
|
110
|
-
let valuePromise = request.internal[rootOptionKey];
|
|
111
|
-
if (!isPromise(valuePromise)) {
|
|
112
|
-
valuePromise = Promise.resolve(valuePromise);
|
|
113
|
-
}
|
|
114
|
-
promises.push(valuePromise.then((value)=>pathOptionKey.reduce((p, c)=>p?.[c], value)));
|
|
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
|
|
118
|
-
values = await Promise.allSettled(promises);
|
|
119
|
-
const errors = values.filter((res)=>res.status === 'rejected').map((res)=>res.reason);
|
|
120
|
-
if (errors.length) {
|
|
121
|
-
throw new Error('Failed to resolve internal values', {
|
|
122
|
-
cause: errors
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
return keys.reduce((obj, key, index)=>({
|
|
126
|
-
...obj,
|
|
127
|
-
[sanitizeKey(key)]: values[index].value
|
|
128
|
-
}), {});
|
|
129
|
-
};
|
|
130
|
-
const isPromise = (promise)=>typeof promise?.then === 'function';
|
|
131
|
-
const sanitizeKeyPrefixLeadingNumber = /^([0-9])/;
|
|
132
|
-
const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g;
|
|
133
|
-
const sanitizeKey = (key)=>{
|
|
134
|
-
return key.replace(sanitizeKeyPrefixLeadingNumber, '_$1').replace(sanitizeKeyRemoveDisallowedChar, '_');
|
|
135
|
-
};
|
|
136
|
-
// fetch Cache
|
|
137
|
-
const cache = {} // key: { value:{fetchKey:Promise}, expiry }
|
|
138
|
-
;
|
|
139
|
-
const processCache = (options, fetch = ()=>undefined, request)=>{
|
|
140
|
-
let { cacheKey, cacheKeyExpiry, cacheExpiry } = options;
|
|
141
|
-
cacheExpiry = cacheKeyExpiry?.[cacheKey] ?? cacheExpiry;
|
|
142
|
-
if (cacheExpiry) {
|
|
143
|
-
const cached = getCache(cacheKey);
|
|
144
|
-
const unexpired = cached.expiry && (cacheExpiry < 0 || cached.expiry > Date.now());
|
|
145
|
-
if (unexpired && cached.modified) {
|
|
146
|
-
const value = fetch(request, cached.value);
|
|
147
|
-
cache[cacheKey] = Object.create({
|
|
148
|
-
value: {
|
|
149
|
-
...cached.value,
|
|
150
|
-
...value
|
|
151
|
-
},
|
|
152
|
-
expiry: cached.expiry
|
|
153
|
-
});
|
|
154
|
-
return cache[cacheKey];
|
|
155
|
-
}
|
|
156
|
-
if (unexpired) {
|
|
157
|
-
return {
|
|
158
|
-
...cached,
|
|
159
|
-
cache: true
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const value = fetch(request);
|
|
164
|
-
const now = Date.now();
|
|
165
|
-
// secrets-manager overrides to unix timestamp
|
|
166
|
-
const expiry = cacheExpiry > 86400000 ? cacheExpiry : now + cacheExpiry;
|
|
167
|
-
const duration = cacheExpiry > 86400000 ? cacheExpiry - now : cacheExpiry;
|
|
168
|
-
if (cacheExpiry) {
|
|
169
|
-
const refresh = duration > 0 ? setInterval(()=>processCache(options, fetch, request), duration) : undefined;
|
|
170
|
-
cache[cacheKey] = {
|
|
171
|
-
value,
|
|
172
|
-
expiry,
|
|
173
|
-
refresh
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
return {
|
|
177
|
-
value,
|
|
178
|
-
expiry
|
|
179
|
-
};
|
|
180
|
-
};
|
|
181
|
-
const getCache = (key)=>{
|
|
182
|
-
if (!cache[key]) return {};
|
|
183
|
-
return cache[key];
|
|
184
|
-
};
|
|
185
|
-
const modifyCache = (cacheKey, value)=>{
|
|
186
|
-
if (!cache[cacheKey]) return;
|
|
187
|
-
clearInterval(cache[cacheKey]?.refresh);
|
|
188
|
-
cache[cacheKey] = {
|
|
189
|
-
...cache[cacheKey],
|
|
190
|
-
value,
|
|
191
|
-
modified: true
|
|
192
|
-
};
|
|
193
|
-
};
|
|
194
|
-
const clearCache = (keys = null)=>{
|
|
195
|
-
keys = keys ?? Object.keys(cache);
|
|
196
|
-
if (!Array.isArray(keys)) keys = [
|
|
197
|
-
keys
|
|
198
|
-
];
|
|
199
|
-
for (const cacheKey of keys){
|
|
200
|
-
clearInterval(cache[cacheKey]?.refresh);
|
|
201
|
-
cache[cacheKey] = undefined;
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
const jsonSafeParse = (text, reviver)=>{
|
|
205
|
-
if (typeof text !== 'string') return text;
|
|
206
|
-
const firstChar = text[0];
|
|
207
|
-
if (firstChar !== '{' && firstChar !== '[' && firstChar !== '"') return text;
|
|
208
|
-
try {
|
|
209
|
-
return JSON.parse(text, reviver);
|
|
210
|
-
} catch (e) {}
|
|
211
|
-
return text;
|
|
212
|
-
};
|
|
213
|
-
const jsonSafeStringify = (value, replacer, space)=>{
|
|
214
|
-
try {
|
|
215
|
-
return JSON.stringify(value, replacer, space);
|
|
216
|
-
} catch (e) {}
|
|
217
|
-
return value;
|
|
218
|
-
};
|
|
219
|
-
const normalizeHttpResponse = (request)=>{
|
|
220
|
-
let { response } = request;
|
|
221
|
-
if (typeof response === 'undefined') {
|
|
222
|
-
response = {};
|
|
223
|
-
} else if (typeof response?.statusCode === 'undefined' && typeof response?.body === 'undefined' && typeof response?.headers === 'undefined') {
|
|
224
|
-
response = {
|
|
225
|
-
statusCode: 200,
|
|
226
|
-
body: response
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
response.statusCode ??= 500;
|
|
230
|
-
response.headers ??= {};
|
|
231
|
-
request.response = response;
|
|
232
|
-
return response;
|
|
233
|
-
};
|
|
234
|
-
const createErrorRegexp = /[^a-zA-Z]/g;
|
|
235
|
-
class HttpError extends Error {
|
|
236
|
-
constructor(code, message, options = {}){
|
|
237
|
-
if (message && typeof message !== 'string') {
|
|
238
|
-
options = message;
|
|
239
|
-
message = undefined;
|
|
240
|
-
}
|
|
241
|
-
message ??= httpErrorCodes[code];
|
|
242
|
-
super(message, options);
|
|
243
|
-
const name = httpErrorCodes[code].replace(createErrorRegexp, '');
|
|
244
|
-
this.name = name.substr(-5) !== 'Error' ? name + 'Error' : name;
|
|
245
|
-
this.status = this.statusCode = code // setting `status` for backwards compatibility w/ `http-errors`
|
|
246
|
-
;
|
|
247
|
-
this.expose = options.expose ?? code < 500;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
const createError = (code, message, properties = {})=>{
|
|
251
|
-
return new HttpError(code, message, properties);
|
|
252
|
-
};
|
|
253
|
-
const httpErrorCodes = {
|
|
254
|
-
100: 'Continue',
|
|
255
|
-
101: 'Switching Protocols',
|
|
256
|
-
102: 'Processing',
|
|
257
|
-
103: 'Early Hints',
|
|
258
|
-
200: 'OK',
|
|
259
|
-
201: 'Created',
|
|
260
|
-
202: 'Accepted',
|
|
261
|
-
203: 'Non-Authoritative Information',
|
|
262
|
-
204: 'No Content',
|
|
263
|
-
205: 'Reset Content',
|
|
264
|
-
206: 'Partial Content',
|
|
265
|
-
207: 'Multi-Status',
|
|
266
|
-
208: 'Already Reported',
|
|
267
|
-
226: 'IM Used',
|
|
268
|
-
300: 'Multiple Choices',
|
|
269
|
-
301: 'Moved Permanently',
|
|
270
|
-
302: 'Found',
|
|
271
|
-
303: 'See Other',
|
|
272
|
-
304: 'Not Modified',
|
|
273
|
-
305: 'Use Proxy',
|
|
274
|
-
306: '(Unused)',
|
|
275
|
-
307: 'Temporary Redirect',
|
|
276
|
-
308: 'Permanent Redirect',
|
|
277
|
-
400: 'Bad Request',
|
|
278
|
-
401: 'Unauthorized',
|
|
279
|
-
402: 'Payment Required',
|
|
280
|
-
403: 'Forbidden',
|
|
281
|
-
404: 'Not Found',
|
|
282
|
-
405: 'Method Not Allowed',
|
|
283
|
-
406: 'Not Acceptable',
|
|
284
|
-
407: 'Proxy Authentication Required',
|
|
285
|
-
408: 'Request Timeout',
|
|
286
|
-
409: 'Conflict',
|
|
287
|
-
410: 'Gone',
|
|
288
|
-
411: 'Length Required',
|
|
289
|
-
412: 'Precondition Failed',
|
|
290
|
-
413: 'Payload Too Large',
|
|
291
|
-
414: 'URI Too Long',
|
|
292
|
-
415: 'Unsupported Media Type',
|
|
293
|
-
416: 'Range Not Satisfiable',
|
|
294
|
-
417: 'Expectation Failed',
|
|
295
|
-
418: "I'm a teapot",
|
|
296
|
-
421: 'Misdirected Request',
|
|
297
|
-
422: 'Unprocessable Entity',
|
|
298
|
-
423: 'Locked',
|
|
299
|
-
424: 'Failed Dependency',
|
|
300
|
-
425: 'Unordered Collection',
|
|
301
|
-
426: 'Upgrade Required',
|
|
302
|
-
428: 'Precondition Required',
|
|
303
|
-
429: 'Too Many Requests',
|
|
304
|
-
431: 'Request Header Fields Too Large',
|
|
305
|
-
451: 'Unavailable For Legal Reasons',
|
|
306
|
-
500: 'Internal Server Error',
|
|
307
|
-
501: 'Not Implemented',
|
|
308
|
-
502: 'Bad Gateway',
|
|
309
|
-
503: 'Service Unavailable',
|
|
310
|
-
504: 'Gateway Timeout',
|
|
311
|
-
505: 'HTTP Version Not Supported',
|
|
312
|
-
506: 'Variant Also Negotiates',
|
|
313
|
-
507: 'Insufficient Storage',
|
|
314
|
-
508: 'Loop Detected',
|
|
315
|
-
509: 'Bandwidth Limit Exceeded',
|
|
316
|
-
510: 'Not Extended',
|
|
317
|
-
511: 'Network Authentication Required'
|
|
318
|
-
};
|
|
319
|
-
|