Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@middy/appconfig

Package Overview
Dependencies
Maintainers
3
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@middy/appconfig - npm Package Compare versions

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"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc