@middy/appconfig
Advanced tools
Comparing version 5.1.0 to 5.2.0
201
index.js
@@ -1,85 +0,126 @@ | ||
import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util'; | ||
import { StartConfigurationSessionCommand, GetLatestConfigurationCommand, AppConfigDataClient } from '@aws-sdk/client-appconfigdata'; | ||
import { | ||
canPrefetch, | ||
createPrefetchClient, | ||
createClient, | ||
getCache, | ||
getInternal, | ||
processCache, | ||
modifyCache, | ||
jsonSafeParse | ||
} from '@middy/util' | ||
import { | ||
StartConfigurationSessionCommand, | ||
GetLatestConfigurationCommand, | ||
AppConfigDataClient | ||
} from '@aws-sdk/client-appconfigdata' | ||
const defaults = { | ||
AwsClient: AppConfigDataClient, | ||
awsClientOptions: {}, | ||
awsClientAssumeRole: undefined, | ||
awsClientCapture: undefined, | ||
fetchData: {}, | ||
disablePrefetch: false, | ||
cacheKey: 'appconfig', | ||
cacheKeyExpiry: {}, | ||
cacheExpiry: -1, | ||
setToContext: false | ||
}; | ||
const contentTypePattern = /^application\/(.+\+)?json($|;.+)/; | ||
const appConfigMiddleware = (opts = {})=>{ | ||
const options = { | ||
...defaults, | ||
...opts | ||
}; | ||
const configurationTokenCache = {}; | ||
const configurationCache = {}; | ||
function fetchLatestConfiguration(configToken, internalKey) { | ||
return client.send(new GetLatestConfigurationCommand({ | ||
ConfigurationToken: configToken | ||
})).then((configResp)=>{ | ||
configurationTokenCache[internalKey] = configResp.NextPollConfigurationToken; | ||
if (configResp.Configuration.length === 0) { | ||
return configurationCache[internalKey]; | ||
} | ||
let value = String.fromCharCode.apply(null, configResp.Configuration); | ||
if (contentTypePattern.test(configResp.ContentType)) { | ||
value = jsonSafeParse(value); | ||
} | ||
configurationCache[internalKey] = value; | ||
return value; | ||
}).catch((e)=>{ | ||
const value = getCache(options.cacheKey).value ?? {}; | ||
value[internalKey] = undefined; | ||
modifyCache(options.cacheKey, value); | ||
throw e; | ||
}); | ||
} | ||
const fetch = (request, cachedValues = {})=>{ | ||
const values = {}; | ||
for (const internalKey of Object.keys(options.fetchData)){ | ||
if (cachedValues[internalKey]) continue; | ||
if (configurationTokenCache[internalKey] == null) { | ||
values[internalKey] = client.send(new StartConfigurationSessionCommand(options.fetchData[internalKey])).then((configSessionResp)=>fetchLatestConfiguration(configSessionResp.InitialConfigurationToken, internalKey)).catch((e)=>{ | ||
const value = getCache(options.cacheKey).value ?? {}; | ||
value[internalKey] = undefined; | ||
modifyCache(options.cacheKey, value); | ||
throw e; | ||
}); | ||
continue; | ||
} | ||
values[internalKey] = fetchLatestConfiguration(configurationTokenCache[internalKey], internalKey); | ||
AwsClient: AppConfigDataClient, | ||
awsClientOptions: {}, | ||
awsClientAssumeRole: undefined, | ||
awsClientCapture: undefined, | ||
fetchData: {}, | ||
disablePrefetch: false, | ||
cacheKey: 'appconfig', | ||
cacheKeyExpiry: {}, | ||
cacheExpiry: -1, | ||
setToContext: false | ||
} | ||
const contentTypePattern = /^application\/(.+\+)?json($|;.+)/ | ||
const appConfigMiddleware = (opts = {}) => { | ||
const options = { | ||
...defaults, | ||
...opts | ||
} | ||
const configurationTokenCache = {} | ||
const configurationCache = {} | ||
function fetchLatestConfiguration (configToken, internalKey) { | ||
return client | ||
.send( | ||
new GetLatestConfigurationCommand({ | ||
ConfigurationToken: configToken | ||
}) | ||
) | ||
.then((configResp) => { | ||
configurationTokenCache[internalKey] = | ||
configResp.NextPollConfigurationToken | ||
if (configResp.Configuration.length === 0) { | ||
return configurationCache[internalKey] | ||
} | ||
return values; | ||
}; | ||
let client; | ||
if (canPrefetch(options)) { | ||
client = createPrefetchClient(options); | ||
processCache(options, fetch); | ||
let value = String.fromCharCode.apply(null, configResp.Configuration) | ||
if (contentTypePattern.test(configResp.ContentType)) { | ||
value = jsonSafeParse(value) | ||
} | ||
configurationCache[internalKey] = value | ||
return value | ||
}) | ||
.catch((e) => { | ||
const value = getCache(options.cacheKey).value ?? {} | ||
value[internalKey] = undefined | ||
modifyCache(options.cacheKey, value) | ||
throw e | ||
}) | ||
} | ||
const fetch = (request, cachedValues = {}) => { | ||
const values = {} | ||
for (const internalKey of Object.keys(options.fetchData)) { | ||
if (cachedValues[internalKey]) continue | ||
if (configurationTokenCache[internalKey] == null) { | ||
values[internalKey] = client | ||
.send( | ||
new StartConfigurationSessionCommand(options.fetchData[internalKey]) | ||
) | ||
.then((configSessionResp) => | ||
fetchLatestConfiguration( | ||
configSessionResp.InitialConfigurationToken, | ||
internalKey | ||
) | ||
) | ||
.catch((e) => { | ||
const value = getCache(options.cacheKey).value ?? {} | ||
value[internalKey] = undefined | ||
modifyCache(options.cacheKey, value) | ||
throw e | ||
}) | ||
continue | ||
} | ||
values[internalKey] = fetchLatestConfiguration( | ||
configurationTokenCache[internalKey], | ||
internalKey | ||
) | ||
} | ||
const appConfigMiddlewareBefore = async (request)=>{ | ||
if (!client) { | ||
client = await createClient(options, request); | ||
} | ||
const { value } = processCache(options, fetch, request); | ||
Object.assign(request.internal, value); | ||
if (options.setToContext) { | ||
const data = await getInternal(Object.keys(options.fetchData), request); | ||
Object.assign(request.context, data); | ||
} | ||
}; | ||
return { | ||
before: appConfigMiddlewareBefore | ||
}; | ||
}; | ||
export default appConfigMiddleware; | ||
export function appConfigReq(req) { | ||
return req; | ||
return values | ||
} | ||
let client | ||
if (canPrefetch(options)) { | ||
client = createPrefetchClient(options) | ||
processCache(options, fetch) | ||
} | ||
const appConfigMiddlewareBefore = async (request) => { | ||
if (!client) { | ||
client = await createClient(options, request) | ||
} | ||
const { value } = processCache(options, fetch, request) | ||
Object.assign(request.internal, value) | ||
if (options.setToContext) { | ||
const data = await getInternal(Object.keys(options.fetchData), request) | ||
Object.assign(request.context, data) | ||
} | ||
} | ||
return { | ||
before: appConfigMiddlewareBefore | ||
} | ||
} | ||
export default appConfigMiddleware | ||
// used for TS type inference (see index.d.ts) | ||
export function appConfigReq (req) { | ||
return req | ||
} | ||
// # sourceMappingURL=index.js.map |
{ | ||
"name": "@middy/appconfig", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"description": "AppConfig middleware for the middy framework", | ||
@@ -61,11 +61,11 @@ "type": "module", | ||
"dependencies": { | ||
"@middy/util": "5.1.0" | ||
"@middy/util": "5.2.0" | ||
}, | ||
"devDependencies": { | ||
"@aws-sdk/client-appconfigdata": "^3.0.0", | ||
"@middy/core": "5.1.0", | ||
"@middy/core": "5.2.0", | ||
"@types/aws-lambda": "^8.10.101", | ||
"aws-xray-sdk": "^3.3.3" | ||
}, | ||
"gitHead": "bbdaf5843914921804ba085dd58117273febe6b5" | ||
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0" | ||
} |
10593
156
+ Added@middy/util@5.2.0(transitive)
- Removed@middy/util@5.1.0(transitive)
Updated@middy/util@5.2.0