aws-core-utils
Advanced tools
Comparing version 3.0.2 to 4.0.0
{ | ||
"stageHandlingOptions": { | ||
"envStageName": "STAGE", | ||
"streamNameStageSeparator": "_", | ||
@@ -4,0 +5,0 @@ "resourceNameStageSeparator": "_", |
{ | ||
"name": "aws-core-utils", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"description": "Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc.", | ||
@@ -15,3 +15,3 @@ "author": "Byron du Preez", | ||
"core-functions": "^2.0.3", | ||
"logging-utils": "^2.0.3", | ||
"logging-utils": "^2.0.4", | ||
"deep-equal": "^1.0.1" | ||
@@ -18,0 +18,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# aws-core-utils v3.0.2 | ||
# aws-core-utils v4.0.0 | ||
@@ -10,6 +10,8 @@ Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc. | ||
- Utilities for working with AWS errors. | ||
- dynamodb-doc-clients.js | ||
- Utilities for working with AWS.DynamoDB.DocumentClients and a module-scope cache of AWS.DynamoDB.DocumentClient instances by region for Lambda. | ||
- kinesis-utils.js | ||
- Utilities for working with AWS.Kinesis and a module-scope cache of AWS.Kinesis instances by region for Lambda. | ||
- dynamodb-doc-client-cache.js | ||
- A module-scope cache of AWS.DynamoDB.DocumentClient instances by region for Lambda. | ||
- dynamodb-utils.js | ||
- Utilities for working with AWS DynamoDB. | ||
- kinesis-cache.js | ||
- A module-scope cache of AWS.Kinesis instances by region for Lambda. | ||
- lambdas.js | ||
@@ -68,5 +70,5 @@ - Utilities for working with AWS Lambda, which enable extraction of function names, versions and, most importantly, | ||
* To use the DynamoDB.DocumentClient utilities to cache and configure an AWS DynamoDB.DocumentClient instance per region | ||
* To use the DynamoDB.DocumentClient cache to configure and cache an AWS DynamoDB.DocumentClient instance per region | ||
```js | ||
const dynamoDBDocClients = require('aws-core-utils/dynamodb-doc-clients'); | ||
const dynamoDBDocClientCache = require('aws-core-utils/dynamodb-doc-client-cache'); | ||
@@ -89,24 +91,24 @@ // Preamble to create a context and configure logging on the context | ||
// DynamoDB.DocumentClient instance (if any) that is compatible with the given options | ||
const dynamoDBDocClient = dynamoDBDocClients.setDynamoDBDocClient(dynamoDBDocClientOptions, context); | ||
const dynamoDBDocClient = dynamoDBDocClientCache.setDynamoDBDocClient(dynamoDBDocClientOptions, context); | ||
// To configure a new AWS.DynamoDB.DocumentClient instance (or re-use a cached instance) on a context | ||
dynamoDBDocClients.configureDynamoDBDocClient(context, dynamoDBDocClientOptions); | ||
dynamoDBDocClientCache.configureDynamoDBDocClient(context, dynamoDBDocClientOptions); | ||
console.log(context.dynamoDBDocClient); | ||
// To get a previously set or configured AWS DynamoDB.DocumentClient instance for the current AWS region | ||
const dynamoDBDocClient1 = dynamoDBDocClients.getDynamoDBDocClient(); | ||
const dynamoDBDocClient1 = dynamoDBDocClientCache.getDynamoDBDocClient(); | ||
// ... or for a specified region | ||
const dynamoDBDocClient2 = dynamoDBDocClients.getDynamoDBDocClient('us-west-2'); | ||
const dynamoDBDocClient2 = dynamoDBDocClientCache.getDynamoDBDocClient('us-west-2'); | ||
// To get the original options that were used to construct a cached AWS DynamoDB.DocumentClient instance for the current or specified AWS region | ||
const optionsUsed1 = dynamoDBDocClients.getDynamoDBDocClientOptionsUsed(); | ||
const optionsUsed2 = dynamoDBDocClients.getDynamoDBDocClientOptionsUsed('us-west-1'); | ||
const optionsUsed1 = dynamoDBDocClientCache.getDynamoDBDocClientOptionsUsed(); | ||
const optionsUsed2 = dynamoDBDocClientCache.getDynamoDBDocClientOptionsUsed('us-west-1'); | ||
// To delete and remove a cached DynamoDB.DocumentClient instance from the cache | ||
const deleted = dynamoDBDocClients.deleteDynamoDBDocClient('eu-west-1'); | ||
const deleted = dynamoDBDocClientCache.deleteDynamoDBDocClient('eu-west-1'); | ||
``` | ||
* To use the Kinesis utilities to cache and configure an AWS Kinesis instance per region | ||
* To use the Kinesis cache to configure and cache an AWS Kinesis instance per region | ||
```js | ||
const kinesisUtils = require('aws-core-utils/kinesis-utils'); | ||
const kinesisCache = require('aws-core-utils/kinesis-cache'); | ||
@@ -128,19 +130,19 @@ // Preamble to create a context and configure logging on the context | ||
// compatible with the given options | ||
const kinesis = kinesisUtils.setKinesis(kinesisOptions, context); | ||
const kinesis = kinesisCache.setKinesis(kinesisOptions, context); | ||
// To configure a new AWS.Kinesis instance (or re-use a cached instance) on a context | ||
kinesisUtils.configureKinesis(context, kinesisOptions); | ||
kinesisCache.configureKinesis(context, kinesisOptions); | ||
console.log(context.kinesis); | ||
// To get a previously set or configured AWS Kinesis instance for the current AWS region | ||
const kinesis1 = kinesisUtils.getKinesis(); | ||
const kinesis1 = kinesisCache.getKinesis(); | ||
// ... or for a specified region | ||
const kinesis2 = kinesisUtils.getKinesis('us-west-2'); | ||
const kinesis2 = kinesisCache.getKinesis('us-west-2'); | ||
// To get the original options that were used to construct a cached AWS Kinesis instance for the current or specified AWS region | ||
const optionsUsed1 = kinesisUtils.getKinesisOptionsUsed(); | ||
const optionsUsed2 = kinesisUtils.getKinesisOptionsUsed('us-west-1'); | ||
const optionsUsed1 = kinesisCache.getKinesisOptionsUsed(); | ||
const optionsUsed2 = kinesisCache.getKinesisOptionsUsed('us-west-1'); | ||
// To delete and remove a cached Kinesis instance from the cache | ||
const deleted = kinesisUtils.deleteKinesis('eu-west-1'); | ||
const deleted = kinesisCache.deleteKinesis('eu-west-1'); | ||
``` | ||
@@ -193,2 +195,3 @@ | ||
const stageHandlingSettings2 = { | ||
envStageName: myEnvStageName, | ||
customToStage: myCustomToStageFunction, // or undefined if not needed | ||
@@ -212,2 +215,4 @@ convertAliasToStage: myConvertAliasToStageFunction, // or undefined to knockout using AWS aliases as stages | ||
// ... OR using custom stage handling settings and/or options and configuring dependencies at the same time | ||
stages.configureStageHandlingAndDependencies(context, stageHandlingSettings, stageHandlingOptions, otherSettings, otherOptions, forceConfiguration); | ||
@@ -285,2 +290,15 @@ // To check if stage handling is configured | ||
### 4.0.0 | ||
- Renamed `kinesis-utils` module to `kinesis-cache` to better reflect its actual purpose | ||
- Renamed `dynamodb-doc-clients` module to `dynamodb-doc-client-cache` to better reflect its actual purpose | ||
- Added new `dynamodb-utils` module | ||
- Changes to `stages.js` module: | ||
- Added new `configureStageHandlingAndDependencies` function to enable configuration of stage handling settings and | ||
all stage handling dependencies (currently just logging) at the same time | ||
- Added new `configureDependencies` function, which is used by the new `configureStageHandlingAndDependencies` function | ||
- Added new `envStageName` setting to enable configuration of the name of the `process.env` environment variable to be | ||
checked for a stage value during execution of the `resolveStage` or `configureStage` functions | ||
- Changed `resolveStage` function to first attempt to resolve a stage from a named `process.env` environment variable | ||
(if available), which must be configured using AWS Lambda's new environment support | ||
### 3.0.2 | ||
@@ -287,0 +305,0 @@ - Changes to `stages.js` module: |
114
stages.js
'use strict'; | ||
// Stage handling setting names | ||
const ENV_STAGE_NAME_SETTING = 'envStageName'; | ||
const CUSTOM_TO_STAGE_SETTING = 'customToStage'; | ||
@@ -36,2 +38,3 @@ const CONVERT_ALIAS_TO_STAGE_SETTING = 'convertAliasToStage'; | ||
getStageHandlingFunction: getStageHandlingFunction, | ||
configureStageHandlingAndDependencies: configureStageHandlingAndDependencies, | ||
configureStageHandlingIfNotConfigured: configureStageHandlingIfNotConfigured, | ||
@@ -111,2 +114,4 @@ // Stage resolution | ||
* @typedef {Object} StageHandlingSettings | ||
* @property {string|undefined} [envStageName] - the optional name of a process.env environment variable that holds the | ||
* configured stage (if any) (using AWS Lambda's new environment support), defaults to 'STAGE' if not defined | ||
* @property {Function|undefined} [customToStage] - an optional custom function that accepts: an AWS event; an AWS context; | ||
@@ -142,2 +147,4 @@ * and a context, and somehow extracts a usable stage from the AWS event and/or AWS context. | ||
* @typedef {Object} StageHandlingOptions | ||
* @property {string|undefined} [envStageName] - the optional name of a process.env environment variable that holds the | ||
* configured stage (if any) (using AWS Lambda's new environment support), defaults to 'STAGE' if not defined | ||
* @property {string|undefined} [streamNameStageSeparator] - an optional non-blank separator to use to extract a stage from | ||
@@ -243,2 +250,4 @@ * a stage-qualified stream name or inject a stage into an unqualified stream name | ||
return { | ||
envStageName: select(options, 'envStageName', defaults.envStageName), | ||
customToStage: undefined, | ||
@@ -271,2 +280,3 @@ convertAliasToStage: convertAliasToStage, | ||
return { | ||
envStageName: select(defaultOptions, 'envStageName', 'STAGE'), | ||
streamNameStageSeparator: select(defaultOptions, 'streamNameStageSeparator', '_'), | ||
@@ -283,3 +293,3 @@ resourceNameStageSeparator: select(defaultOptions, 'resourceNameStageSeparator', '_'), | ||
* | ||
* @param {Object} context - the context onto which to configure the given stream processing dependencies | ||
* @param {Object} context - the context onto which to configure the given stage handling dependencies | ||
* @param {Object|undefined} [otherSettings] - optional other configuration settings to use | ||
@@ -290,3 +300,3 @@ * @param {LoggingSettings|undefined} [otherSettings.loggingSettings] - optional logging settings to use to configure logging | ||
* @param {string|undefined} [caller] - optional arbitrary text to identify the caller of this function | ||
* @returns {Object} the context object configured with stream processing dependencies | ||
* @returns {Object} the context object configured with stage handling dependencies | ||
*/ | ||
@@ -365,6 +375,66 @@ function configureDependenciesIfNotConfigured(context, otherSettings, otherOptions, caller) { | ||
/** | ||
* Configures the given context with the given stage handling settings (if any) otherwise with the default stage | ||
* handling settings partially overridden by the given stage handling options (if any), but only if stage handling is | ||
* not already configured on the given context OR if forceConfiguration is true. | ||
* | ||
* @param {Object} context - the context to configure | ||
* @param {StageHandlingSettings|undefined} [settings] - optional stage handling settings to use to configure stage handling | ||
* @param {StageHandlingOptions|undefined} [options] - optional stage handling options to use to override default options | ||
* @param {Object|undefined} [otherSettings] - optional other settings to use to configure dependencies | ||
* @param {LoggingSettings|undefined} [otherSettings.loggingSettings] - optional logging settings to use to configure logging | ||
* @param {Object|undefined} [otherOptions] - optional other options to use to configure dependencies if corresponding settings are not provided | ||
* @param {LoggingOptions|undefined} [otherOptions.loggingOptions] - optional logging options to use to configure logging | ||
* @param {boolean|undefined} [forceConfiguration] - whether or not to force configuration of the given settings, which | ||
* will override any previously configured stage handling settings on the given context | ||
* @returns {Object} the given context | ||
*/ | ||
function configureStageHandlingAndDependencies(context, settings, options, otherSettings, otherOptions, forceConfiguration) { | ||
// First configure all stage handling dependencies | ||
configureDependencies(context, otherSettings, otherOptions, forceConfiguration); | ||
// Check if stage handling was already configured | ||
const stageHandlingWasConfigured = isStageHandlingConfigured(context); | ||
// Determine the stage handling settings to be used | ||
const settingsAvailable = settings && typeof settings === 'object'; | ||
const stageHandlingSettings = settingsAvailable ? settings : getDefaultStageHandlingSettings(options); | ||
// Configure stage handling with the given or derived stage handling settings | ||
configureStageHandling(context, stageHandlingSettings, otherSettings, otherOptions, forceConfiguration); | ||
// Log a warning if no settings and no options were provided and the default settings were applied | ||
if (!settingsAvailable && (!options || typeof options !== 'object') && (forceConfiguration || !stageHandlingWasConfigured)) { | ||
context.warn(`Stage handling was configured without settings or options - used default stage handling configuration (${stringify(stageHandlingSettings)})`); | ||
} | ||
return context; | ||
} | ||
/** | ||
* Configures the given context with the stage handling dependencies (currently only logging) using the given other | ||
* settings and given other options. | ||
* | ||
* @param {Object} context - the context onto which to configure the given stage handling dependencies | ||
* @param {Object|undefined} [otherSettings] - optional other configuration settings to use | ||
* @param {LoggingSettings|undefined} [otherSettings.loggingSettings] - optional logging settings to use to configure logging | ||
* @param {Object|undefined} [otherOptions] - optional other configuration options to use if no corresponding other settings are provided | ||
* @param {LoggingOptions|undefined} [otherOptions.loggingOptions] - optional logging options to use to configure logging | ||
* @param {boolean|undefined} [forceConfiguration] - whether or not to force configuration of the given settings, which | ||
* will override any previously configured dependencies' settings on the given context | ||
* @returns {Object} the context object configured with stage handling dependencies | ||
*/ | ||
function configureDependencies(context, otherSettings, otherOptions, forceConfiguration) { | ||
// Configure logging if not configured yet | ||
logging.configureLoggingWithSettingsOrOptions(context, otherSettings ? otherSettings.loggingSettings : undefined, | ||
otherOptions ? otherOptions.loggingOptions : undefined, undefined, forceConfiguration); | ||
} | ||
/** | ||
* Attempts to resolve the stage from the given AWS event, AWS context and context using the following process: | ||
* | ||
* 1. Uses an explicit (or previously resolved) context.stage (if non-blank). | ||
* | ||
* 2. Uses the given context's stageHandling.customToStage function (if any) to attempt to somehow extract a stage from | ||
* 2. Uses the stage in the process.env[context.envStageName] environment variable (if any), which must be configured | ||
* using AWS Lambda's new environment support. This is now the preferred way of configuring the current stage. | ||
* | ||
* 3. Uses the given context's stageHandling.customToStage function (if any) to attempt to somehow extract a stage from | ||
* the AWS event and/or AWS context. This function has no default implementation and is merely provided as a hook for | ||
@@ -374,7 +444,6 @@ * callers to add their own custom technique for resolving a stage, which will take precedence over all other steps | ||
* | ||
* 3. Uses the AWS event's stage (if non-blank). | ||
* 4. Uses the AWS event's stage (if non-blank). | ||
* NB: event.stage is NOT a standard AWS event property, but it may be set by API Gateway or tools like serverless. | ||
* TODO check whether API Gateway sets the stage on the event and if so confirm WHERE it sets it! | ||
* | ||
* 4. Extracts the alias from the AWS context's invokedFunctionArn (if any) and then uses the given context's | ||
* 5. Extracts the alias from the AWS context's invokedFunctionArn (if any) and then uses the given context's | ||
* stageHandling.convertAliasToStage function (if any) to convert the extracted alias (if any) into a stage. | ||
@@ -386,3 +455,3 @@ * | ||
* | ||
* 5. Extracts the stream names from the AWS event's records' eventSourceARNs (if any) and then uses the given context's | ||
* 6. Extracts the stream names from the AWS event's records' eventSourceARNs (if any) and then uses the given context's | ||
* configured stageHandling.extractStageFromStreamName function (if defined) to extract the stages from these stream | ||
@@ -396,7 +465,7 @@ * names and returns the first non-blank stage (if any and if there are NOT multiple distinct results). | ||
* | ||
* 6. Uses context.stageHandling.defaultStage (if non-blank). | ||
* 7. Uses context.stageHandling.defaultStage (if non-blank). | ||
* | ||
* 7. Uses context.defaultStage (if non-blank). | ||
* 8. Uses context.defaultStage (if non-blank). | ||
* | ||
* 8. Gives up and returns an empty string. | ||
* 9. Gives up and returns an empty string. | ||
* | ||
@@ -444,2 +513,15 @@ * NB: If no stage handling settings have been configured by the time this function is first called, then the given | ||
// Attempt 2 | ||
const envStageName = getStageHandlingSetting(context, ENV_STAGE_NAME_SETTING); | ||
if (isNotBlank(envStageName)) { | ||
// Look up the current stage from the named process.env environment variable | ||
const stage = process.env[envStageName]; | ||
if (isNotBlank(stage)) { | ||
context.debug(`Resolved stage (${stage}) from process.env.${envStageName}`); | ||
return toCase(trim(stage), extractInCase); | ||
} | ||
} | ||
// Attempt 3 | ||
const customToStage = getStageHandlingFunction(context, CUSTOM_TO_STAGE_SETTING); | ||
@@ -456,3 +538,3 @@ | ||
// Attempt 3 | ||
// Attempt 4 | ||
if (event && isNotBlank(event.stage)) { | ||
@@ -463,3 +545,3 @@ context.debug(`Resolved stage (${event.stage}) from event.stage)`); | ||
// Attempt 4 | ||
// Attempt 5 | ||
// Check have all the pieces needed to extract an alias and apply the given convertAliasToStage function to it | ||
@@ -481,3 +563,3 @@ const convertAliasToStage = getStageHandlingFunction(context, CONVERT_ALIAS_TO_STAGE_SETTING); | ||
// Attempt 5 | ||
// Attempt 6 | ||
// Check have all the pieces needed to extract a stream name and apply the given extractStageFromStreamName function to it | ||
@@ -506,3 +588,3 @@ const extractStageFromStreamName = getStageHandlingFunction(context, EXTRACT_STAGE_FROM_STREAM_NAME_SETTING); | ||
// Attempt 6 | ||
// Attempt 7 | ||
const stageHandlingDefaultStage = getStageHandlingSetting(context, 'defaultStage'); | ||
@@ -515,3 +597,3 @@ | ||
// Attempt 7 | ||
// Attempt 8 | ||
const defaultStage = context && context.defaultStage ? context.defaultStage : undefined; | ||
@@ -524,3 +606,3 @@ | ||
// Give up 8 | ||
// Give up 9 | ||
return ''; | ||
@@ -527,0 +609,0 @@ } |
{ | ||
"name": "aws-core-utils-tests", | ||
"description": "Unit tests for aws-core-utils modules", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"author": "Byron du Preez", | ||
@@ -6,0 +6,0 @@ "license": "Apache-2.0", |
@@ -17,2 +17,4 @@ 'use strict'; | ||
const configureDefaultStageHandling = stages.configureDefaultStageHandling; | ||
const getDefaultStageHandlingSettings = stages.getDefaultStageHandlingSettings; | ||
const configureStageHandlingAndDependencies = stages.configureStageHandlingAndDependencies; | ||
const getStageHandlingSetting = stages.getStageHandlingSetting; | ||
@@ -96,11 +98,12 @@ // Stage resolution | ||
function checkConfigureStageHandling(t, context, customToStage, convertAliasToStage, | ||
injectStageIntoStreamName, extractStageFromStreamName, streamNameStageSeparator, | ||
injectStageIntoResourceName, extractStageFromResourceName, resourceNameStageSeparator, | ||
injectInCase, extractInCase, defaultStage, forceConfiguration) { | ||
function checkConfigureStageHandling(t, context, envStageName, customToStage, | ||
convertAliasToStage, injectStageIntoStreamName, extractStageFromStreamName, | ||
streamNameStageSeparator, injectStageIntoResourceName, extractStageFromResourceName, | ||
resourceNameStageSeparator, injectInCase, extractInCase, defaultStage, forceConfiguration) { | ||
const before = context.stageHandling; | ||
const envStageNameBefore = before ? before.envStageName : undefined; | ||
const customToStageBefore = before ? before.customToStage : undefined; | ||
const convertAliasToStageBefore = before ? before.convertAliasToStage : undefined; | ||
@@ -124,2 +127,4 @@ | ||
const settings = { | ||
envStageName: envStageName, | ||
customToStage: customToStage, | ||
@@ -145,4 +150,5 @@ convertAliasToStage: convertAliasToStage, | ||
const envStageNameAfter = after ? after.envStageName : undefined; | ||
const customToStageAfter = after ? after.customToStage : undefined; | ||
const convertAliasToStageAfter = after ? after.convertAliasToStage : undefined; | ||
@@ -166,4 +172,5 @@ | ||
// Set up the right expectations | ||
const envStageNameExpected = mustChange ? envStageName : envStageNameBefore; | ||
const customToStageExpected = mustChange ? customToStage : customToStageBefore; | ||
const convertAliasToStageExpected = mustChange ? convertAliasToStage : convertAliasToStageBefore; | ||
@@ -183,4 +190,5 @@ | ||
t.equal(getStageHandlingSetting(context, 'envStageName'), envStageNameExpected, `envStageName (${stringify(envStageNameAfter)}) must be ${stringify(envStageNameExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'customToStage'), customToStageExpected, `customToStage (${stringify(customToStageAfter)}) must be ${stringify(customToStageExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'convertAliasToStage'), convertAliasToStageExpected, `convertAliasToStage (${stringify(convertAliasToStageAfter)}) must be ${stringify(convertAliasToStageExpected)}`); | ||
@@ -206,8 +214,8 @@ | ||
function checkConfigureDefaultStageHandling(t, context, forceConfiguration) { | ||
const before = context.stageHandling; | ||
const envStageNameBefore = before ? before.envStageName : undefined; | ||
const customToStageBefore = before ? before.customToStage : undefined; | ||
const convertAliasToStageBefore = before ? before.convertAliasToStage : undefined; | ||
@@ -234,4 +242,5 @@ | ||
const envStageNameAfter = after ? after.envStageName : undefined; | ||
const customToStageAfter = after ? after.customToStage : undefined; | ||
const convertAliasToStageAfter = after ? after.convertAliasToStage : undefined; | ||
@@ -254,4 +263,5 @@ | ||
// Expect the defaults to be in place | ||
const envStageNameExpected = mustChange ? 'STAGE' : envStageNameBefore; | ||
const customToStageExpected = mustChange ? undefined : customToStageBefore; | ||
const convertAliasToStageExpected = mustChange ? convertAliasToStage : convertAliasToStageBefore; | ||
@@ -271,4 +281,5 @@ | ||
t.equal(getStageHandlingSetting(context, 'envStageName'), envStageNameExpected, `envStageName (${stringify(envStageNameAfter)}) must be ${stringify(envStageNameExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'customToStage'), customToStageExpected, `customToStage (${stringify(customToStageAfter)}) must be ${stringify(customToStageExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'convertAliasToStage'), convertAliasToStageExpected, `convertAliasToStage (${stringify(convertAliasToStageAfter)}) must be ${stringify(convertAliasToStageExpected)}`); | ||
@@ -293,2 +304,93 @@ | ||
function checkConfigureStageHandlingAndDependencies(t, context, settings, options, otherSettings, otherOptions, forceConfiguration) { | ||
const before = context.stageHandling; | ||
const envStageNameBefore = before ? before.envStageName : undefined; | ||
const customToStageBefore = before ? before.customToStage : undefined; | ||
const convertAliasToStageBefore = before ? before.convertAliasToStage : undefined; | ||
const injectStageIntoStreamNameBefore = before ? before.injectStageIntoStreamName : undefined; | ||
const extractStageFromStreamNameBefore = before ? before.extractStageFromStreamName : undefined; | ||
const streamNameStageSeparatorBefore = before ? before.streamNameStageSeparator : undefined; | ||
const injectStageIntoResourceNameBefore = before ? before.injectStageIntoResourceName : undefined; | ||
const extractStageFromResourceNameBefore = before ? before.extractStageFromResourceName : undefined; | ||
const resourceNameStageSeparatorBefore = before ? before.resourceNameStageSeparator : undefined; | ||
const injectInCaseBefore = before ? before.injectInCase : undefined; | ||
const extractInCaseBefore = before ? before.extractInCase : undefined; | ||
const defaultStageBefore = before ? before.defaultStage : undefined; | ||
const mustChange = forceConfiguration || !before; | ||
configureStageHandlingAndDependencies(context, settings, options, otherSettings, otherOptions, forceConfiguration); | ||
const after = context.stageHandling; | ||
const envStageNameAfter = after ? after.envStageName : undefined; | ||
const customToStageAfter = after ? after.customToStage : undefined; | ||
const convertAliasToStageAfter = after ? after.convertAliasToStage : undefined; | ||
const injectStageIntoStreamNameAfter = after ? after.injectStageIntoStreamName : undefined; | ||
const extractStageFromStreamNameAfter = after ? after.extractStageFromStreamName : undefined; | ||
const streamNameStageSeparatorAfter = after ? after.streamNameStageSeparator : undefined; | ||
const injectStageIntoResourceNameAfter = after ? after.injectStageIntoResourceName : undefined; | ||
const extractStageFromResourceNameAfter = after ? after.extractStageFromResourceName : undefined; | ||
const resourceNameStageSeparatorAfter = after ? after.resourceNameStageSeparator : undefined; | ||
const injectInCaseAfter = after ? after.injectInCase : undefined; | ||
const extractInCaseAfter = after ? after.extractInCase : undefined; | ||
const defaultStageAfter = after ? after.defaultStage : undefined; | ||
t.ok(isStageHandlingConfigured(context), `stage handling settings must be configured now`); | ||
// Get defaults | ||
const defaults = getDefaultStageHandlingSettings(options); | ||
// Set up the right expectations | ||
const envStageNameExpected = mustChange ? settings ? settings.envStageName : defaults.envStageName : envStageNameBefore; | ||
const customToStageExpected = mustChange ? settings ? settings.customToStage : defaults.customToStage : customToStageBefore; | ||
const convertAliasToStageExpected = mustChange ? settings ? settings.convertAliasToStage : defaults.convertAliasToStage : convertAliasToStageBefore; | ||
const injectStageIntoStreamNameExpected = mustChange ? settings ? settings.injectStageIntoStreamName : defaults.injectStageIntoStreamName : injectStageIntoStreamNameBefore; | ||
const extractStageFromStreamNameExpected = mustChange ? settings ? settings.extractStageFromStreamName : defaults.extractStageFromStreamName : extractStageFromStreamNameBefore; | ||
const streamNameStageSeparatorExpected = mustChange ? settings ? settings.streamNameStageSeparator : defaults.streamNameStageSeparator : streamNameStageSeparatorBefore; | ||
const injectStageIntoResourceNameExpected = mustChange ? settings ? settings.injectStageIntoResourceName : defaults.injectStageIntoResourceName : injectStageIntoResourceNameBefore; | ||
const extractStageFromResourceNameExpected = mustChange ? settings ? settings.extractStageFromResourceName : defaults.extractStageFromResourceName : extractStageFromResourceNameBefore; | ||
const resourceNameStageSeparatorExpected = mustChange ? settings ? settings.resourceNameStageSeparator : defaults.resourceNameStageSeparator : resourceNameStageSeparatorBefore; | ||
const injectInCaseExpected = mustChange ? settings ? settings.injectInCase : defaults.injectInCase : injectInCaseBefore; | ||
const extractInCaseExpected = mustChange ? settings ? settings.extractInCase : defaults.extractInCase : extractInCaseBefore; | ||
const defaultStageExpected = mustChange ? settings ? settings.defaultStage : defaults.defaultStage : defaultStageBefore; | ||
t.equal(getStageHandlingSetting(context, 'envStageName'), envStageNameExpected, `envStageName (${stringify(envStageNameAfter)}) must be ${stringify(envStageNameExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'customToStage'), customToStageExpected, `customToStage (${stringify(customToStageAfter)}) must be ${stringify(customToStageExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'convertAliasToStage'), convertAliasToStageExpected, `convertAliasToStage (${stringify(convertAliasToStageAfter)}) must be ${stringify(convertAliasToStageExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'injectStageIntoStreamName'), injectStageIntoStreamNameExpected, `injectStageIntoStreamName (${stringify(injectStageIntoStreamNameAfter)}) must be ${stringify(injectStageIntoStreamNameExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'extractStageFromStreamName'), extractStageFromStreamNameExpected, `extractStageFromStreamName (${stringify(extractStageFromStreamNameAfter)}) must be ${stringify(extractStageFromStreamNameExpected)}`); | ||
t.equal(getStageHandlingSetting(context, 'streamNameStageSeparator'), streamNameStageSeparatorExpected, `streamNameStageSeparator (${stringify(streamNameStageSeparatorAfter)}) must be ${stringify(streamNameStageSeparatorExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'injectStageIntoResourceName'), injectStageIntoResourceNameExpected, `injectStageIntoResourceName (${stringify(injectStageIntoResourceNameAfter)}) must be ${stringify(injectStageIntoResourceNameExpected)}`); | ||
t.deepEqual(getStageHandlingSetting(context, 'extractStageFromResourceName'), extractStageFromResourceNameExpected, `extractStageFromResourceName (${stringify(extractStageFromResourceNameAfter)}) must be ${stringify(extractStageFromResourceNameExpected)}`); | ||
t.equal(getStageHandlingSetting(context, 'resourceNameStageSeparator'), resourceNameStageSeparatorExpected, `resourceNameStageSeparator (${stringify(resourceNameStageSeparatorAfter)}) must be ${stringify(resourceNameStageSeparatorExpected)}`); | ||
t.equal(getStageHandlingSetting(context, 'injectInCase'), injectInCaseExpected, `injectInCase (${stringify(injectInCaseAfter)}) must be ${stringify(injectInCaseExpected)}`); | ||
t.equal(getStageHandlingSetting(context, 'extractInCase'), extractInCaseExpected, `extractInCase (${stringify(extractInCaseAfter)}) must be ${stringify(extractInCaseExpected)}`); | ||
t.equal(getStageHandlingSetting(context, 'defaultStage'), defaultStageExpected, `defaultStage (${stringify(defaultStageAfter)}) must be ${stringify(defaultStageExpected)}`); | ||
// Check whether stage handling works with this configuration | ||
const expected = mustChange ? toCase(trimOrEmpty(defaultStageExpected), extractInCaseExpected) : toCase(trimOrEmpty(defaultStageBefore), extractInCaseBefore); | ||
checkResolveStage(t, '', '', '', '', context, expected); | ||
} | ||
// ===================================================================================================================== | ||
@@ -303,9 +405,9 @@ // Tests for configureStageHandling and isStageHandlingConfigured | ||
// Configure it | ||
checkConfigureStageHandling(t, context, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, false); | ||
checkConfigureStageHandling(t, context, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, false); | ||
// Must NOT be able to reconfigure it with force false | ||
checkConfigureStageHandling(t, context, undefined, 'convertAliasToStage', 'injectStageIntoStreamName', 'extractStageFromStreamName', 'streamNameStageSeparator', 'injectStageIntoResourceName', 'extractStageFromResourceName', 'resourceNameStageSeparator', 'injectInCase', 'extractInCase', 'defaultStage', false); | ||
checkConfigureStageHandling(t, context, 'envStage', 'customToStage', 'convertAliasToStage', 'injectStageIntoStreamName', 'extractStageFromStreamName', 'streamNameStageSeparator', 'injectStageIntoResourceName', 'extractStageFromResourceName', 'resourceNameStageSeparator', 'injectInCase', 'extractInCase', 'defaultStage', false); | ||
// Must be able to reconfigure it with force true | ||
checkConfigureStageHandling(t, context, undefined, 'convertAliasToStage', 'injectStageIntoStreamName', 'extractStageFromStreamName', 'streamNameStageSeparator', 'injectStageIntoResourceName, extractStageFromResourceName, resourceNameStageSeparator, injectInCase', 'extractInCase', 'defaultStage', true); | ||
checkConfigureStageHandling(t, context, 'envStage', 'customToStage', 'convertAliasToStage', 'injectStageIntoStreamName', 'extractStageFromStreamName', 'streamNameStageSeparator', 'injectStageIntoResourceName, extractStageFromResourceName, resourceNameStageSeparator, injectInCase', 'extractInCase', 'defaultStage', true); | ||
@@ -315,8 +417,7 @@ t.end(); | ||
// ===================================================================================================================== | ||
// Tests for configureStageHandlingWithDefaults and isStageHandlingConfigured | ||
// Tests for configureDefaultStageHandling and isStageHandlingConfigured | ||
// ===================================================================================================================== | ||
test('configureStageHandlingWithDefaults with all undefined', t => { | ||
test('configureDefaultStageHandling with all undefined', t => { | ||
const context = {}; | ||
@@ -329,3 +430,3 @@ t.notOk(isStageHandlingConfigured(context), `stage handling settings must not be configured yet`); | ||
// Overwrite it with arbitrary values to be able to check if defaults are NOT re-instated in next step | ||
checkConfigureStageHandling(t, context, undefined, 'convertAliasToStage', 'injectStageIntoStreamName', 'extractStageFromStreamName', 'streamNameStageSeparator', 'injectStageIntoResourceName, extractStageFromResourceName, resourceNameStageSeparator, injectInCase', 'extractInCase', 'defaultStage', true); | ||
checkConfigureStageHandling(t, context, 'envStage', 'customToStage', 'convertAliasToStage', 'injectStageIntoStreamName', 'extractStageFromStreamName', 'streamNameStageSeparator', 'injectStageIntoResourceName, extractStageFromResourceName, resourceNameStageSeparator, injectInCase', 'extractInCase', 'defaultStage', true); | ||
@@ -341,4 +442,130 @@ // Must NOT be able to reconfigure it with force false | ||
// ===================================================================================================================== | ||
// Tests for configureStageHandlingAndDependencies with settings | ||
// ===================================================================================================================== | ||
test('configureStageHandlingAndDependencies with settings', t => { | ||
const context = {}; | ||
t.notOk(isStageHandlingConfigured(context), `stage handling settings must not be configured yet`); | ||
// Configure settings | ||
const settings = { | ||
envStageName: 'envStageName', | ||
customToStage: 'customToStage', | ||
convertAliasToStage: 'convertAliasToStage', | ||
injectStageIntoStreamName: 'injectStageIntoStreamName', | ||
extractStageFromStreamName: 'extractStageFromStreamName', | ||
streamNameStageSeparator: 'streamNameStageSeparator', | ||
injectStageIntoResourceName: 'injectStageIntoResourceName', | ||
extractStageFromResourceName: 'extractStageFromResourceName', | ||
resourceNameStageSeparator: 'resourceNameStageSeparator', | ||
injectInCase: 'injectInCase', | ||
extractInCase: 'extractInCase', | ||
defaultStage: 'defaultStage', | ||
}; | ||
// Configure it | ||
checkConfigureStageHandlingAndDependencies(t, context, undefined, undefined, undefined, undefined, false); | ||
// Must NOT be able to reconfigure it with force false | ||
checkConfigureStageHandlingAndDependencies(t, context, settings, undefined, undefined, undefined, false); | ||
// Must be able to reconfigure it with force true | ||
checkConfigureStageHandlingAndDependencies(t, context, settings, undefined, undefined, undefined, true); | ||
t.end(); | ||
}); | ||
// ===================================================================================================================== | ||
// Tests for configureStageHandlingAndDependencies with options | ||
// ===================================================================================================================== | ||
test('configureStageHandlingAndDependencies with settings', t => { | ||
const context = {}; | ||
t.notOk(isStageHandlingConfigured(context), `stage handling settings must not be configured yet`); | ||
// Configure options | ||
const options = { | ||
envStageName: 'envStageName', | ||
streamNameStageSeparator: 'streamNameStageSeparator', | ||
resourceNameStageSeparator: 'resourceNameStageSeparator', | ||
injectInCase: 'injectInCase', | ||
extractInCase: 'extractInCase', | ||
defaultStage: 'defaultStage', | ||
}; | ||
// Configure it | ||
checkConfigureStageHandlingAndDependencies(t, context, undefined, undefined, undefined, undefined, false); | ||
// Must NOT be able to reconfigure it with force false | ||
checkConfigureStageHandlingAndDependencies(t, context, undefined, options, undefined, undefined, false); | ||
// Must be able to reconfigure it with force true | ||
checkConfigureStageHandlingAndDependencies(t, context, undefined, options, undefined, undefined, true); | ||
t.end(); | ||
}); | ||
// ===================================================================================================================== | ||
// Tests for configureStageHandlingAndDependencies with settings AND options | ||
// ===================================================================================================================== | ||
test('configureStageHandlingAndDependencies with settings AND options', t => { | ||
const context = {}; | ||
t.notOk(isStageHandlingConfigured(context), `stage handling settings must not be configured yet`); | ||
// Configure settings | ||
const settings = { | ||
envStageName: 'envStageName', | ||
customToStage: 'customToStage', | ||
convertAliasToStage: 'convertAliasToStage', | ||
injectStageIntoStreamName: 'injectStageIntoStreamName', | ||
extractStageFromStreamName: 'extractStageFromStreamName', | ||
streamNameStageSeparator: 'streamNameStageSeparator', | ||
injectStageIntoResourceName: 'injectStageIntoResourceName', | ||
extractStageFromResourceName: 'extractStageFromResourceName', | ||
resourceNameStageSeparator: 'resourceNameStageSeparator', | ||
injectInCase: 'injectInCase', | ||
extractInCase: 'extractInCase', | ||
defaultStage: 'defaultStage', | ||
}; | ||
// Configure options | ||
const options = { | ||
envStageName: 'envStageName2', | ||
streamNameStageSeparator: 'streamNameStageSeparator2', | ||
resourceNameStageSeparator: 'resourceNameStageSeparator2', | ||
injectInCase: 'injectInCase2', | ||
extractInCase: 'extractInCase2', | ||
defaultStage: 'defaultStage2', | ||
}; | ||
// Configure it | ||
checkConfigureStageHandlingAndDependencies(t, context, undefined, undefined, undefined, undefined, false); | ||
// Must NOT be able to reconfigure it with force false | ||
checkConfigureStageHandlingAndDependencies(t, context, settings, options, undefined, undefined, false); | ||
// Must be able to reconfigure it with force true | ||
checkConfigureStageHandlingAndDependencies(t, context, settings, options, undefined, undefined, true); | ||
t.end(); | ||
}); | ||
// ===================================================================================================================== | ||
// Tests for convertAliasToStage | ||
@@ -704,3 +931,5 @@ // ===================================================================================================================== | ||
context.stageHandling.extractInCase = 'as_is'; | ||
context.stageHandling.customToStage = () => { return 'CustomStage'; }; | ||
context.stageHandling.customToStage = () => { | ||
return 'CustomStage'; | ||
}; | ||
@@ -735,3 +964,5 @@ // Context stage must override default stage | ||
context.stageHandling.extractInCase = 'as_is'; | ||
context.stageHandling.customToStage = () => { return 'CustomStage'; }; | ||
context.stageHandling.customToStage = () => { | ||
return 'CustomStage'; | ||
}; | ||
@@ -763,1 +994,80 @@ context.stage = 'Cs'; | ||
}); | ||
test('resolveStage with env stage, custom-to-stage, event stage, but no context stage', t => { | ||
try { | ||
process.env.STAGE = 'EnvStage'; | ||
const context = configureDefaultStageHandling({}); | ||
context.stageHandling.defaultStage = 'Ds'; | ||
context.stageHandling.extractInCase = 'as_is'; | ||
context.stageHandling.customToStage = () => { | ||
return 'CustomStage'; | ||
}; | ||
// Context stage must override default stage | ||
checkResolveStage(t, 'Es', '', '', '', context, 'EnvStage'); | ||
// Context stage must override stream without suffix and default | ||
checkResolveStage(t, 'Es', '', '', 'Stream', context, 'EnvStage'); | ||
// Context stage must override stream with suffix and default | ||
checkResolveStage(t, 'Es', '', '', 'Stream_Ss', context, 'EnvStage'); | ||
// Context stage must override event stage and Lambda without alias and default | ||
checkResolveStage(t, 'Es', '1.0.1', '1.01', '', context, 'EnvStage'); | ||
// Context stage must override event stage and Lambda without alias and stream without suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', '1.01', 'Stream', context, 'EnvStage'); | ||
// Context stage must override event stage and Lambda without alias and stream with suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', '1.01', 'Stream_Ss', context, 'EnvStage'); | ||
// Context stage must override event stage and Lambda with alias and default | ||
checkResolveStage(t, 'Es', '1.0.1', 'As', '', context, 'EnvStage'); | ||
// Context stage must override event stage and Lambda with alias and stream without suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', 'As', 'Stream', context, 'EnvStage'); | ||
// Context stage must override event stage and Lambda with alias and stream with suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', 'As', 'Stream_Ss', context, 'EnvStage'); | ||
t.end(); | ||
} finally { | ||
process.env.STAGE = undefined; | ||
} | ||
}); | ||
test('resolveStage with env stage, custom-to-stage and context stage and event stage', t => { | ||
try { | ||
process.env.STAGE = 'EnvStage'; | ||
const context = configureDefaultStageHandling({}); | ||
context.stageHandling.defaultStage = 'Ds'; | ||
context.stageHandling.extractInCase = 'as_is'; | ||
context.stageHandling.customToStage = () => { | ||
return 'CustomStage'; | ||
}; | ||
context.stage = 'Cs'; | ||
// Context stage must override default stage | ||
checkResolveStage(t, 'Es', '', '', '', context, 'Cs'); | ||
// Context stage must override stream without suffix and default | ||
checkResolveStage(t, 'Es', '', '', 'Stream', context, 'Cs'); | ||
// Context stage must override stream with suffix and default | ||
checkResolveStage(t, 'Es', '', '', 'Stream_Ss', context, 'Cs'); | ||
// Context stage must override event stage and Lambda without alias and default | ||
checkResolveStage(t, 'Es', '1.0.1', '1.01', '', context, 'Cs'); | ||
// Context stage must override event stage and Lambda without alias and stream without suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', '1.01', 'Stream', context, 'Cs'); | ||
// Context stage must override event stage and Lambda without alias and stream with suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', '1.01', 'Stream_Ss', context, 'Cs'); | ||
// Context stage must override env stage, custom stage, event stage and Lambda with alias and default | ||
checkResolveStage(t, 'Es', '1.0.1', 'As', '', context, 'Cs'); | ||
// Context stage must override env stage, custom stage, event stage and Lambda with alias and stream without suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', 'As', 'Stream', context, 'Cs'); | ||
// Context stage must override env stage, custom stage, event stage and Lambda with alias and stream with suffix and default | ||
checkResolveStage(t, 'Es', '1.0.1', 'As', 'Stream_Ss', context, 'Cs'); | ||
t.end(); | ||
} finally { | ||
process.env.STAGE = undefined; | ||
} | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
290982
24
4202
400
27
Updatedlogging-utils@^2.0.4