@middy/appconfig 5.0.3 → 5.2.0
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.js +121 -80
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,85 +1,126 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
canPrefetch,
|
|
3
|
+
createPrefetchClient,
|
|
4
|
+
createClient,
|
|
5
|
+
getCache,
|
|
6
|
+
getInternal,
|
|
7
|
+
processCache,
|
|
8
|
+
modifyCache,
|
|
9
|
+
jsonSafeParse
|
|
10
|
+
} from '@middy/util'
|
|
11
|
+
import {
|
|
12
|
+
StartConfigurationSessionCommand,
|
|
13
|
+
GetLatestConfigurationCommand,
|
|
14
|
+
AppConfigDataClient
|
|
15
|
+
} from '@aws-sdk/client-appconfigdata'
|
|
16
|
+
|
|
3
17
|
const defaults = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
const contentTypePattern = /^application\/(.+\+)?json($|;.+)
|
|
16
|
-
const appConfigMiddleware = (opts = {})=>{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}).catch((e)=>{
|
|
38
|
-
const value = getCache(options.cacheKey).value ?? {};
|
|
39
|
-
value[internalKey] = undefined;
|
|
40
|
-
modifyCache(options.cacheKey, value);
|
|
41
|
-
throw e;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
const fetch = (request, cachedValues = {})=>{
|
|
45
|
-
const values = {};
|
|
46
|
-
for (const internalKey of Object.keys(options.fetchData)){
|
|
47
|
-
if (cachedValues[internalKey]) continue;
|
|
48
|
-
if (configurationTokenCache[internalKey] == null) {
|
|
49
|
-
values[internalKey] = client.send(new StartConfigurationSessionCommand(options.fetchData[internalKey])).then((configSessionResp)=>fetchLatestConfiguration(configSessionResp.InitialConfigurationToken, internalKey)).catch((e)=>{
|
|
50
|
-
const value = getCache(options.cacheKey).value ?? {};
|
|
51
|
-
value[internalKey] = undefined;
|
|
52
|
-
modifyCache(options.cacheKey, value);
|
|
53
|
-
throw e;
|
|
54
|
-
});
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
values[internalKey] = fetchLatestConfiguration(configurationTokenCache[internalKey], internalKey);
|
|
58
|
-
}
|
|
59
|
-
return values;
|
|
60
|
-
};
|
|
61
|
-
let client;
|
|
62
|
-
if (canPrefetch(options)) {
|
|
63
|
-
client = createPrefetchClient(options);
|
|
64
|
-
processCache(options, fetch);
|
|
65
|
-
}
|
|
66
|
-
const appConfigMiddlewareBefore = async (request)=>{
|
|
67
|
-
if (!client) {
|
|
68
|
-
client = await createClient(options, request);
|
|
18
|
+
AwsClient: AppConfigDataClient,
|
|
19
|
+
awsClientOptions: {},
|
|
20
|
+
awsClientAssumeRole: undefined,
|
|
21
|
+
awsClientCapture: undefined,
|
|
22
|
+
fetchData: {},
|
|
23
|
+
disablePrefetch: false,
|
|
24
|
+
cacheKey: 'appconfig',
|
|
25
|
+
cacheKeyExpiry: {},
|
|
26
|
+
cacheExpiry: -1,
|
|
27
|
+
setToContext: false
|
|
28
|
+
}
|
|
29
|
+
const contentTypePattern = /^application\/(.+\+)?json($|;.+)/
|
|
30
|
+
const appConfigMiddleware = (opts = {}) => {
|
|
31
|
+
const options = {
|
|
32
|
+
...defaults,
|
|
33
|
+
...opts
|
|
34
|
+
}
|
|
35
|
+
const configurationTokenCache = {}
|
|
36
|
+
const configurationCache = {}
|
|
37
|
+
|
|
38
|
+
function fetchLatestConfiguration (configToken, internalKey) {
|
|
39
|
+
return client
|
|
40
|
+
.send(
|
|
41
|
+
new GetLatestConfigurationCommand({
|
|
42
|
+
ConfigurationToken: configToken
|
|
43
|
+
})
|
|
44
|
+
)
|
|
45
|
+
.then((configResp) => {
|
|
46
|
+
configurationTokenCache[internalKey] =
|
|
47
|
+
configResp.NextPollConfigurationToken
|
|
48
|
+
|
|
49
|
+
if (configResp.Configuration.length === 0) {
|
|
50
|
+
return configurationCache[internalKey]
|
|
69
51
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
Object.assign(request.context, data);
|
|
52
|
+
|
|
53
|
+
let value = String.fromCharCode.apply(null, configResp.Configuration)
|
|
54
|
+
if (contentTypePattern.test(configResp.ContentType)) {
|
|
55
|
+
value = jsonSafeParse(value)
|
|
75
56
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
57
|
+
configurationCache[internalKey] = value
|
|
58
|
+
return value
|
|
59
|
+
})
|
|
60
|
+
.catch((e) => {
|
|
61
|
+
const value = getCache(options.cacheKey).value ?? {}
|
|
62
|
+
value[internalKey] = undefined
|
|
63
|
+
modifyCache(options.cacheKey, value)
|
|
64
|
+
throw e
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const fetch = (request, cachedValues = {}) => {
|
|
69
|
+
const values = {}
|
|
70
|
+
for (const internalKey of Object.keys(options.fetchData)) {
|
|
71
|
+
if (cachedValues[internalKey]) continue
|
|
72
|
+
if (configurationTokenCache[internalKey] == null) {
|
|
73
|
+
values[internalKey] = client
|
|
74
|
+
.send(
|
|
75
|
+
new StartConfigurationSessionCommand(options.fetchData[internalKey])
|
|
76
|
+
)
|
|
77
|
+
.then((configSessionResp) =>
|
|
78
|
+
fetchLatestConfiguration(
|
|
79
|
+
configSessionResp.InitialConfigurationToken,
|
|
80
|
+
internalKey
|
|
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
|
+
})
|
|
89
|
+
|
|
90
|
+
continue
|
|
91
|
+
}
|
|
92
|
+
values[internalKey] = fetchLatestConfiguration(
|
|
93
|
+
configurationTokenCache[internalKey],
|
|
94
|
+
internalKey
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
return values
|
|
98
|
+
}
|
|
99
|
+
let client
|
|
100
|
+
if (canPrefetch(options)) {
|
|
101
|
+
client = createPrefetchClient(options)
|
|
102
|
+
processCache(options, fetch)
|
|
103
|
+
}
|
|
104
|
+
const appConfigMiddlewareBefore = async (request) => {
|
|
105
|
+
if (!client) {
|
|
106
|
+
client = await createClient(options, request)
|
|
107
|
+
}
|
|
108
|
+
const { value } = processCache(options, fetch, request)
|
|
109
|
+
Object.assign(request.internal, value)
|
|
110
|
+
if (options.setToContext) {
|
|
111
|
+
const data = await getInternal(Object.keys(options.fetchData), request)
|
|
112
|
+
Object.assign(request.context, data)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
before: appConfigMiddlewareBefore
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
export default appConfigMiddleware
|
|
120
|
+
|
|
121
|
+
// used for TS type inference (see index.d.ts)
|
|
122
|
+
export function appConfigReq (req) {
|
|
123
|
+
return req
|
|
84
124
|
}
|
|
85
125
|
|
|
126
|
+
// # sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/appconfig",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "AppConfig middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -58,13 +58,13 @@
|
|
|
58
58
|
"url": "https://github.com/sponsors/willfarrell"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@middy/util": "5.0
|
|
61
|
+
"@middy/util": "5.2.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@aws-sdk/client-appconfigdata": "^3.0.0",
|
|
65
|
-
"@middy/core": "5.0
|
|
65
|
+
"@middy/core": "5.2.0",
|
|
66
66
|
"@types/aws-lambda": "^8.10.101",
|
|
67
67
|
"aws-xray-sdk": "^3.3.3"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
70
70
|
}
|