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