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