aws-core-utils
Advanced tools
Comparing version 6.1.0 to 7.0.0
"use strict"; | ||
// noinspection JSUnusedGlobalSymbols | ||
/** | ||
@@ -4,0 +5,0 @@ * Utilities for working with and interpreting AWS errors. |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const stages = require('./stages'); | ||
const lambdas = require('./lambdas'); | ||
const kinesisCache = require('./kinesis-cache'); | ||
@@ -64,3 +65,3 @@ const dynamoDBDocClientCache = require('./dynamodb-doc-client-cache'); | ||
* @return {StandardContext} the given context configured as a standard context | ||
* @throws {Error} an error if the region and/or stage cannot be resolved | ||
* @throws {Error} an error if the stage cannot be resolved | ||
*/ | ||
@@ -72,4 +73,4 @@ function configureStandardContext(context, settings, options, event, awsContext, forceConfiguration) { | ||
// Configure the region after configuring logging (failing fast if process.env.AWS_REGION is blank) | ||
regions.configureRegion(context, true); | ||
// Configure the region after configuring logging | ||
regions.configureRegion(context); | ||
@@ -111,3 +112,3 @@ // Configure the given context with any custom settings and/or custom options | ||
* @param {AWSContext} awsContext - the AWS context, which was passed to your lambda | ||
* @return {EventAWSContextAndStageAware} the given context configured with a stage and the given AWS context | ||
* @return {EventAWSContextAndStageAware} the given context configured with the given AWS event, AWS context & resolved stage | ||
* @throws {Error} if the resolved stage is blank | ||
@@ -124,2 +125,5 @@ */ | ||
context.awsContext = awsContext; | ||
// Resolve the invoked Lambda's function name, version & alias (if possible) | ||
context.invokedLambda = lambdas.getFunctionNameVersionAndAlias(awsContext); | ||
} | ||
@@ -126,0 +130,0 @@ |
@@ -26,4 +26,2 @@ 'use strict'; | ||
//const LATEST_VERSION = '$LATEST'; | ||
const arns = require('./arns'); | ||
@@ -33,17 +31,13 @@ const getArnResources = arns.getArnResources; | ||
const Strings = require('core-functions/strings'); | ||
//const trim = Strings.trim; | ||
const trimOrEmpty = Strings.trimOrEmpty; | ||
//const isBlank = Strings.isBlank; | ||
const isNotBlank = Strings.isNotBlank; | ||
const appErrors = require('core-functions/app-errors'); | ||
//const AppError = appErrors.AppError; | ||
/** | ||
* Returns the function name from the given AWS context | ||
* @param awsContext the AWS context | ||
* @param {AWSContext|undefined} [awsContext] - the AWS context | ||
* @returns {string} the function name | ||
*/ | ||
function getFunctionName(awsContext) { | ||
return awsContext && awsContext.functionName ? trimOrEmpty(awsContext.functionName) : ''; | ||
return process.env.AWS_LAMBDA_FUNCTION_NAME || (awsContext && awsContext.functionName) || ''; | ||
} | ||
@@ -53,7 +47,7 @@ | ||
* Returns the function version from the given AWS context | ||
* @param awsContext the AWS context | ||
* @param {AWSContext|undefined} [awsContext] - the AWS context | ||
* @returns {string} the function version | ||
*/ | ||
function getFunctionVersion(awsContext) { | ||
return awsContext && awsContext.functionVersion ? trimOrEmpty(awsContext.functionVersion) : ''; | ||
return process.env.AWS_LAMBDA_FUNCTION_VERSION || (awsContext && awsContext.functionVersion) || ''; | ||
} | ||
@@ -63,7 +57,7 @@ | ||
* Returns the invokedFunctionArn of the given AWS context, which was passed to your Lambda function. | ||
* @param awsContext the AWS context | ||
* @param {AWSContext} awsContext - the AWS context | ||
* @returns {string} the invoked function ARN | ||
*/ | ||
function getInvokedFunctionArn(awsContext) { | ||
return awsContext && awsContext.invokedFunctionArn ? trimOrEmpty(awsContext.invokedFunctionArn) : ''; | ||
return (awsContext && awsContext.invokedFunctionArn) || ''; | ||
} | ||
@@ -73,7 +67,7 @@ | ||
* Extracts and returns the function name from the given AWS context's invokedFunctionArn. | ||
* @param awsContext the AWS context | ||
* @param {AWSContext} awsContext - the AWS context | ||
* @returns {string} the extracted function name | ||
*/ | ||
function getInvokedFunctionArnFunctionName(awsContext) { | ||
const invokedFunctionArn = getInvokedFunctionArn(awsContext); | ||
const invokedFunctionArn = awsContext && awsContext.invokedFunctionArn; | ||
const resources = getArnResources(invokedFunctionArn); | ||
@@ -92,19 +86,24 @@ return resources.resource; | ||
* | ||
* @param awsContext the AWS context | ||
* @returns {{functionName: string, version: string, alias: string}} | ||
* @param {AWSContext} awsContext - the AWS context | ||
* @returns {LambdaFunctionNameVersionAndAlias} | ||
*/ | ||
function getFunctionNameVersionAndAlias(awsContext) { | ||
const functionName = getFunctionName(awsContext); | ||
const version = getFunctionVersion(awsContext); | ||
const name = process.env.AWS_LAMBDA_FUNCTION_NAME; | ||
const version = process.env.AWS_LAMBDA_FUNCTION_VERSION; | ||
const invokedFunctionArn = getInvokedFunctionArn(awsContext); | ||
const nameFromContext = awsContext && awsContext.functionName; | ||
const versionFromContext = awsContext && awsContext.functionVersion; | ||
const invokedFunctionArn = awsContext && awsContext.invokedFunctionArn; | ||
const resources = getArnResources(invokedFunctionArn); | ||
const functionNameFromArn = resources.resource; | ||
if (functionName !== functionNameFromArn) { | ||
console.error(`Lambda context with function name (${functionName}) has different name (${functionNameFromArn}) in invoked function ARN`); | ||
const nameFromArn = resources.resource; | ||
if (nameFromArn !== nameFromContext) { | ||
console.warn(`Lambda context with function name (${nameFromContext}) has different name (${nameFromArn}) in invoked function ARN`); | ||
} | ||
const aliasOrVersion = resources.aliasOrVersion; | ||
const alias = isNotBlank(aliasOrVersion) && aliasOrVersion !== version ? aliasOrVersion : ''; | ||
const alias = isNotBlank(aliasOrVersion) && aliasOrVersion !== versionFromContext ? //&& aliasOrVersion !== version ? | ||
aliasOrVersion : ''; | ||
return {functionName: functionName, version: version, alias: alias}; | ||
return {functionName: name || nameFromContext || '', version: version || versionFromContext || '', alias: alias}; | ||
} | ||
@@ -120,3 +119,3 @@ | ||
* | ||
* @param awsContext the AWS context | ||
* @param {AWSContext} awsContext - the AWS context | ||
* @returns {string} the alias (if any); otherwise an empty string | ||
@@ -141,3 +140,3 @@ */ | ||
* @param {Error} error - the error with which you need to fail your Lambda | ||
* @param {Object|undefined} [awsContext] - the AWS context passed as the second argument to your Lambda function on invocation | ||
* @param {AWSContext|undefined} [awsContext] - the AWS context passed as the second argument to your Lambda function on invocation | ||
* @param {string|undefined} [awsContext.awsRequestId] - the AWS context's request ID | ||
@@ -144,0 +143,0 @@ * @param {string|undefined} [message] - an optional message; will use error's message if not specified and needed |
{ | ||
"name": "aws-core-utils", | ||
"version": "6.1.0", | ||
"version": "7.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.", | ||
@@ -19,3 +19,3 @@ "author": "Byron du Preez", | ||
"devDependencies": { | ||
"aws-sdk": "2.54.0", | ||
"aws-sdk": "2.92.0", | ||
"aws-core-test-utils": "2.0.11", | ||
@@ -22,0 +22,0 @@ "tape": "^4.7.0", |
@@ -1,2 +0,2 @@ | ||
# aws-core-utils v6.1.0 | ||
# aws-core-utils v7.0.0 | ||
@@ -3,0 +3,0 @@ Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc. |
@@ -16,21 +16,14 @@ 'use strict'; | ||
getDefaultRegion: getDefaultRegion, | ||
//getRegionOrDefault: getRegionOrDefault, | ||
getInvokedFunctionArnRegion: getInvokedFunctionArnRegion, | ||
getEventAwsRegions: getEventAwsRegions, | ||
getEventSourceArnRegions: getEventSourceArnRegions, | ||
/** @deprecated simply use `getRegion` directly instead when the region is required */ | ||
configureRegion: configureRegion, | ||
//resolveRegion: resolveRegion, | ||
getRegionKey: getRegionKey, | ||
ONLY_FOR_TESTING: { | ||
getRegionRaw: getRegionRaw, | ||
getDefaultRegionRaw: getDefaultRegionRaw, | ||
setRegionIfNotSet: setRegionIfNotSet | ||
} | ||
getRegionRaw: getRegionRaw, | ||
getDefaultRegionRaw: getDefaultRegionRaw | ||
}; | ||
const Strings = require('core-functions/strings'); | ||
const isBlank = Strings.isBlank; | ||
const isNotBlank = Strings.isNotBlank; | ||
@@ -45,11 +38,3 @@ const trim = Strings.trim; | ||
* Gets the region in which this function is running from the `AWS_REGION` environment variable and returns it as is if | ||
* it's neither "undefined" nor "null"; otherwise logs a warning and returns undefined if it's "undefined" or "null" (or | ||
* undefined). | ||
* | ||
* The `AWS_REGION` environment variable is the best option to use to get the region within AWS Lambda code (and the | ||
* only option to use at module-level scope), since these environment variables will be set by AWS Lambda. | ||
* | ||
* An optional "hidden" 'failFast' boolean argument, which defaults to false, can be passed as true to raise an error if | ||
* the AWS_REGION env variable is not available or unusable | ||
* | ||
* it's neither "undefined" nor "null"; otherwise returns undefined. | ||
* @returns {string|undefined} the AWS region (if it exists); otherwise undefined. | ||
@@ -59,13 +44,3 @@ */ | ||
const awsRegion = trim(process.env.AWS_REGION); | ||
const region = awsRegion !== "undefined" && awsRegion !== "null" ? awsRegion : undefined; | ||
if (!region) { | ||
const errorMsg = `Failed to get usable region from AWS_REGION env variable (${awsRegion}) - for unit testing call setRegion beforehand`; | ||
const failFast = arguments.length > 0 && arguments[0] === true; | ||
if (failFast) { | ||
console.error(errorMsg); | ||
throw new Error(errorMsg); | ||
} | ||
console.warn(errorMsg); | ||
} | ||
return region; | ||
return awsRegion !== "undefined" && awsRegion !== "null" ? awsRegion : undefined; | ||
} | ||
@@ -85,3 +60,3 @@ | ||
// If the given region is undefined or null, then must delete process.env.AWS_REGION rather than setting it to | ||
// undefined or null, which incorrectly sets it to the strings "undefined" or "null" respectively | ||
// undefined or null, which sets it to the strings "undefined" or "null" respectively | ||
delete process.env.AWS_REGION; | ||
@@ -102,23 +77,2 @@ } else { | ||
/** | ||
* Sets the process.env.AWS_REGION environment variable to the given region, but ONLY if is is not already set! | ||
* NB: This only sets the region temporarily on the current process and should probably only be used for testing purposes. | ||
* @deprecated use `setRegion` instead | ||
* @returns {boolean} true if set; false otherwise. | ||
*/ | ||
function setRegionIfNotSet(awsRegion) { | ||
const newRegion = trim(awsRegion); | ||
// Check if AWS region is already set or not | ||
const region = getRegionRaw(); | ||
if (isBlank(region)) { | ||
setRegion(newRegion); | ||
return true; | ||
} | ||
if (process.env.AWS_REGION !== newRegion) { | ||
console.log(`Ignoring attempt to change ALREADY set AWS_REGION env variable (${process.env.AWS_REGION}) to (${newRegion})`); | ||
} | ||
return false; | ||
} | ||
/** | ||
* Extracts the region from the invokedFunctionArn of the given AWS Lambda context, which was passed to your Lambda | ||
@@ -171,3 +125,3 @@ * function. | ||
function getRegionRaw() { | ||
return trim(process.env.AWS_REGION); | ||
return process.env.AWS_REGION; | ||
} | ||
@@ -185,3 +139,3 @@ | ||
function getDefaultRegionRaw() { | ||
return trim(process.env.AWS_DEFAULT_REGION); | ||
return process.env.AWS_DEFAULT_REGION; | ||
} | ||
@@ -191,17 +145,12 @@ | ||
* Keeps context.region as is if it's already configured on the given context, otherwise gets the current region from | ||
* process.env.AWS_REGION and if it's not blank, sets it on the context as context.region; otherwise either throws an | ||
* error if failFast is explicitly true. | ||
* @deprecated simply use `getRegion` directly instead when the region is required | ||
* process.env.AWS_REGION and sets it on the context as context.region. | ||
* @param {Object|RegionAware} context - a context on which to set the region | ||
* @param {boolean|undefined} [failFast] - an optional flag that is only used when AWS_REGION is needed and blank and | ||
* that determines whether the error will be raised (if failFast is explicitly true) or simply logged as a warning | ||
* @returns {RegionAware} the context with its existing region or the current AWS_REGION env variable value. | ||
* @throws {Error} if failFast is explicitly true and an AWS_REGION env variable is needed and not available | ||
*/ | ||
function configureRegion(context, failFast) { | ||
function configureRegion(context) { | ||
// Resolve the AWS region, if it is not already defined on the context | ||
if (!context.region) { | ||
context.region = getRegion(failFast === true); | ||
context.region = getRegion(); | ||
} | ||
(context.info || console.log)(`Using region (${getRegion()}) & context.region (${context.region})`); | ||
(context.debug || console.log)(`Using region (${process.env.AWS_REGION}) & context.region (${context.region})`); | ||
return context; | ||
@@ -213,8 +162,6 @@ } | ||
* @param {string|undefined} [region] - the name of the region (defaults to current region if not defined) | ||
* @param {boolean|undefined} [failFast] - an optional flag that is only used when AWS_REGION is needed and blank and | ||
* that determines whether the error will be raised (if failFast is explicitly true) or simply logged as a warning | ||
* @returns {{region: string}} a region key object | ||
*/ | ||
function getRegionKey(region, failFast) { | ||
const regionName = region ? region : getRegion(failFast === true); | ||
function getRegionKey(region) { | ||
const regionName = region ? region : getRegion(); | ||
let regionKey = regionKeysByRegion.get(regionName); | ||
@@ -221,0 +168,0 @@ if (!regionKey) { |
## Changes | ||
### 7.0.0 | ||
- Changes to `type-defs` module: | ||
- Defined more properties of `AWSContext` type definition | ||
- Added `LambdaFunctionNameVersionAndAlias` type definition | ||
- Added optional `invokedLambda` property to `EventAWSContextAndStageAware` type definition | ||
- Changes to `regions` module: | ||
- Removed `ONLY_FOR_TESTING.setRegionIfNotSet` function | ||
- Removed fail fast logic from `getRegion`, `getRegionKey` & `configureRegion` functions, since its only applicable for unit testing | ||
- Replaced `ONLY_FOR_TESTING.getRegionRaw` export with direct export of `getRegionRaw` function | ||
- Replaced `ONLY_FOR_TESTING.getDefaultRegionRaw` export with direct export of `getDefaultRegionRaw` function | ||
- Removed `deprecation` from `configureRegion` function | ||
- Changes to `stages` module: | ||
- Removed deprecated `configureStageAndAwsContext` function | ||
- Removed deprecated `configureRegionStageAndAwsContext` function | ||
- Changes to `lambdas` module: | ||
- Changed `getFunctionName` function to also use `process.env.AWS_LAMBDA_FUNCTION_NAME` | ||
- Changed `getFunctionVersion` function to also use `process.env.AWS_LAMBDA_FUNCTION_VERSION` | ||
- Changed `getFunctionNameVersionAndAlias` function to also use `process.env.AWS_LAMBDA_FUNCTION_NAME` & `AWS_LAMBDA_FUNCTION_VERSION` | ||
- Changes to `contexts` module: | ||
- Changed `configureEventAwsContextAndStage` function to also set `context.invokedLambda` when `awsContext` provided & | ||
to NOT fail if region is not defined | ||
- Upgraded `aws-sdk` dependency to 2.92.0 | ||
### 6.1.0 | ||
@@ -4,0 +27,0 @@ - Changes to `type-defs` module: |
@@ -51,6 +51,2 @@ 'use strict'; | ||
configureStage: configureStage, | ||
/** @deprecated Use configureStage instead & configure context.awsContext elsewhere (e.g. contexts.configureEventAwsContextAndStage) */ | ||
configureStageAndAwsContext: configureStageAndAwsContext, | ||
/** @deprecated Use configureStage instead & either regions.getRegion or regions.configureRegion & configure context.awsContext elsewhere (e.g. contexts.configureEventAwsContextAndStage) */ | ||
configureRegionStageAndAwsContext: configureRegionStageAndAwsContext, | ||
@@ -107,3 +103,2 @@ // Stream name qualification | ||
const regions = require('./regions'); | ||
const lambdas = require('./lambdas'); | ||
@@ -913,58 +908,2 @@ | ||
return context; | ||
} | ||
/** | ||
* Configures the given context with the resolved stage and the given AWS event & AWS context. In order to resolve the | ||
* stage, stage handling settings and logging must already be configured on the given context (see {@linkcode | ||
* stages#configureStageHandling} for details). | ||
* @deprecated Use module:./contexts#configureEventAwsContextAndStage instead | ||
* @param {StageHandling|EventAWSContextAndStageAware} context - the context to configure | ||
* @param {Object} event - the AWS event, which was passed to your lambda | ||
* @param {Object} awsContext - the AWS context, which was passed to your lambda | ||
* @return {EventAWSContextAndStageAware} the given context configured with a stage and the given AWS context | ||
* @throws {Error} if the resolved stage is blank | ||
*/ | ||
function configureStageAndAwsContext(context, event, awsContext) { | ||
// Configure context.event with the given AWS event | ||
context.event = event; | ||
// Configure context.awsContext with the given AWS context, if not already configured | ||
if (!context.awsContext) { | ||
context.awsContext = awsContext; | ||
} | ||
// Resolve the current stage (e.g. dev, qa, prod, ...) if possible and configure context.stage with it, if it is not | ||
// already configured | ||
configureStage(context, event, awsContext, true); | ||
return context; | ||
} | ||
/** | ||
* Configures the given context with the current region, the resolved stage and the given AWS context. In order to | ||
* resolve the stage, stage handling settings and logging must already be configured on the given context (see | ||
* {@linkcode stages#configureStageHandling} for details). | ||
* @deprecated Use module:./contexts#configureEventAwsContextAndStage instead & either regions.getRegion or regions.configureRegion | ||
* @param {StageHandling|RegionStageAWSContextAware} context - the context to configure | ||
* @param {Object} event - the AWS event, which was passed to your lambda | ||
* @param {Object} awsContext - the AWS context, which was passed to your lambda | ||
* @return {RegionStageAWSContextAware} the given context configured with a region, stage and the given AWS context | ||
* @throws {Error} if no region is available in the AWS_REGION environment variable or if the resolved stage is blank | ||
*/ | ||
function configureRegionStageAndAwsContext(context, event, awsContext) { | ||
// Configure context.event with the given AWS event | ||
context.event = event; | ||
// Configure context.awsContext with the given AWS context, if not already configured | ||
if (!context.awsContext) { | ||
context.awsContext = awsContext; | ||
} | ||
// Configure context.region to the AWS region, if it is not already configured | ||
regions.configureRegion(context, true); | ||
// Resolve the current stage (e.g. dev, qa, prod, ...) if possible and configure context.stage with it, if it is not | ||
// already configured | ||
configureStage(context, event, awsContext, true); | ||
return context; | ||
} |
@@ -264,3 +264,5 @@ 'use strict'; | ||
t.notOk(context.stage, 'context.stage must not be defined'); | ||
t.notOk(context.event, 'context.event must not be defined'); | ||
t.notOk(context.awsContext, 'context.awsContext must not be defined'); | ||
t.notOk(context.invokedLambda, 'context.invokedLambda must not be defined'); | ||
@@ -295,3 +297,5 @@ t.end(); | ||
t.notOk(context.stage, 'context.stage must not be defined'); | ||
t.notOk(context.event, 'context.event must not be defined'); | ||
t.notOk(context.awsContext, 'context.awsContext must not be defined'); | ||
t.notOk(context.invokedLambda, 'context.invokedLambda must not be defined'); | ||
@@ -329,3 +333,5 @@ t.end(); | ||
t.notOk(context.stage, 'context.stage must not be defined'); | ||
t.notOk(context.event, 'context.event must not be defined'); | ||
t.notOk(context.awsContext, 'context.awsContext must not be defined'); | ||
t.notOk(context.invokedLambda, 'context.invokedLambda must not be defined'); | ||
@@ -363,3 +369,5 @@ t.end(); | ||
t.notOk(context.stage, 'context.stage must not be defined'); | ||
t.notOk(context.event, 'context.event must not be defined'); | ||
t.notOk(context.awsContext, 'context.awsContext must not be defined'); | ||
t.notOk(context.invokedLambda, 'context.invokedLambda must not be defined'); | ||
@@ -384,2 +392,3 @@ t.end(); | ||
const awsContext = sampleAwsContext('1.0.1', 'dev1'); | ||
const invokedLambda = {functionName: 'sampleFunctionName', version: '1.0.1', alias: 'dev1'}; | ||
@@ -405,8 +414,15 @@ contexts.configureStandardContext(context, standardSettings, standardOptions, event, awsContext, false); | ||
t.ok(context.dynamoDBDocClient, 'context.dynamoDBDocClient must be defined'); | ||
t.ok(context.region, 'context.region must be defined'); | ||
t.equal(context.region, 'us-west-1', 'context.region must be us-west-1'); | ||
t.ok(context.event, 'context.event must be defined'); | ||
t.equal(context.event, event, 'context.event must be event'); | ||
t.ok(context.awsContext, 'context.awsContext must be defined'); | ||
t.equal(context.awsContext, awsContext, 'context.awsContext must be awsContext'); | ||
t.ok(context.invokedLambda, 'context.invokedLambda must be defined'); | ||
t.deepEqual(context.invokedLambda, invokedLambda, `context.invokedLambda must be ${JSON.stringify(invokedLambda)}`); | ||
t.ok(context.stage, 'context.stage must be defined'); | ||
t.equal(context.stage, expectedStage, `context.stage must be ${expectedStage}`); | ||
t.ok(context.awsContext, 'context.awsContext must be defined'); | ||
t.equal(context.awsContext, awsContext, 'context.awsContext must be given awsContext'); | ||
@@ -435,4 +451,13 @@ } finally { | ||
// Generate a sample AWS context | ||
const awsContext = sampleAwsContext('1.0.1', 'dev1'); | ||
const functionAlias = 'dev1'; | ||
const awsContext = sampleAwsContext('1.0.1', functionAlias); | ||
const functionName = 'my-test-function'; | ||
process.env.AWS_LAMBDA_FUNCTION_NAME = functionName; | ||
const functionVersion = '1.0.23'; | ||
process.env.AWS_LAMBDA_FUNCTION_VERSION = functionVersion; | ||
const invokedLambda = {functionName: functionName, version: functionVersion, alias: functionAlias}; | ||
// Initial configuration WITHOUT event & AWS context | ||
@@ -443,2 +468,3 @@ contexts.configureStandardContext(context, standardSettings, standardOptions, undefined, undefined, false); | ||
t.notOk(context.awsContext, 'context.awsContext must not be defined'); | ||
t.notOk(context.invokedLambda, 'context.invokedLambda must not be defined'); | ||
t.notOk(context.stage, 'context.stage must not be defined'); | ||
@@ -449,4 +475,9 @@ | ||
t.ok(context.event, 'context.event must be defined'); | ||
t.equal(context.event, event, 'context.event must be event'); | ||
t.ok(context.awsContext, 'context.awsContext must be defined'); | ||
t.equal(context.awsContext, awsContext, 'context.awsContext must be awsContext'); | ||
t.ok(context.invokedLambda, 'context.invokedLambda must be defined'); | ||
t.deepEqual(context.invokedLambda, invokedLambda, `context.invokedLambda must be ${JSON.stringify(invokedLambda)}`); | ||
t.equal(context.stage, expectedStage, `context.stage must be ${expectedStage}`); | ||
@@ -453,0 +484,0 @@ |
@@ -19,6 +19,2 @@ 'use strict'; | ||
const getRegion = regions.getRegion; | ||
// const getRegionRaw = regions.ONLY_FOR_TESTING.getRegionRaw; | ||
// const getDefaultRegion = regions.getDefaultRegion; | ||
// const resolveRegion = regions.resolveRegion; | ||
// const setRegionIfNotSet = regions.ONLY_FOR_TESTING.setRegionIfNotSet; | ||
@@ -30,6 +26,2 @@ const logging = require('logging-utils'); | ||
const stringify = Strings.stringify; | ||
// const isBlank = Strings.isBlank; | ||
// const isNotBlank = Strings.isNotBlank; | ||
// const trim = Strings.trim; | ||
// const trimOrEmpty = Strings.trimOrEmpty; | ||
@@ -36,0 +28,0 @@ // ===================================================================================================================== |
@@ -19,6 +19,2 @@ 'use strict'; | ||
const getRegion = regions.getRegion; | ||
// const getRegionRaw = regions.ONLY_FOR_TESTING.getRegionRaw; | ||
// const getDefaultRegion = regions.getDefaultRegion; | ||
// const resolveRegion = regions.resolveRegion; | ||
// const setRegionIfNotSet = regions.ONLY_FOR_TESTING.setRegionIfNotSet; | ||
@@ -30,6 +26,2 @@ const logging = require('logging-utils'); | ||
const stringify = Strings.stringify; | ||
// const isBlank = Strings.isBlank; | ||
// const isNotBlank = Strings.isNotBlank; | ||
// const trim = Strings.trim; | ||
// const trimOrEmpty = Strings.trimOrEmpty; | ||
@@ -36,0 +28,0 @@ // ===================================================================================================================== |
@@ -19,6 +19,2 @@ 'use strict'; | ||
const getRegion = regions.getRegion; | ||
// const getRegionRaw = regions.ONLY_FOR_TESTING.getRegionRaw; | ||
// const getDefaultRegion = regions.getDefaultRegion; | ||
// const resolveRegion = regions.resolveRegion; | ||
// const setRegionIfNotSet = regions.ONLY_FOR_TESTING.setRegionIfNotSet; | ||
@@ -30,6 +26,2 @@ const logging = require('logging-utils'); | ||
const stringify = Strings.stringify; | ||
// const isBlank = Strings.isBlank; | ||
// const isNotBlank = Strings.isNotBlank; | ||
// const trim = Strings.trim; | ||
// const trimOrEmpty = Strings.trimOrEmpty; | ||
@@ -36,0 +28,0 @@ // ===================================================================================================================== |
@@ -19,6 +19,2 @@ 'use strict'; | ||
const getRegion = regions.getRegion; | ||
// const getRegionRaw = regions.ONLY_FOR_TESTING.getRegionRaw; | ||
// const getDefaultRegion = regions.getDefaultRegion; | ||
// const resolveRegion = regions.resolveRegion; | ||
// const setRegionIfNotSet = regions.ONLY_FOR_TESTING.setRegionIfNotSet; | ||
@@ -30,6 +26,2 @@ const logging = require('logging-utils'); | ||
const stringify = Strings.stringify; | ||
// const isBlank = Strings.isBlank; | ||
// const isNotBlank = Strings.isNotBlank; | ||
// const trim = Strings.trim; | ||
// const trimOrEmpty = Strings.trimOrEmpty; | ||
@@ -36,0 +28,0 @@ // ===================================================================================================================== |
@@ -10,4 +10,2 @@ 'use strict'; | ||
const uuid = require("uuid"); | ||
// The test subject | ||
@@ -22,8 +20,2 @@ const lambdas = require('../lambdas'); | ||
// const Strings = require('core-functions/strings'); | ||
// const isBlank = Strings.isBlank; | ||
// const isNotBlank = Strings.isNotBlank; | ||
// const trim = Strings.trim; | ||
// const trimOrEmpty = Strings.trimOrEmpty; | ||
const samples = require('./samples'); | ||
@@ -81,3 +73,5 @@ //const sampleInvokedFunctionArn = samples.sampleInvokedFunctionArn; | ||
test('getFunctionName', t => { | ||
test('getFunctionName WITHOUT process.env.AWS_LAMBDA_FUNCTION_NAME', t => { | ||
delete process.env.AWS_LAMBDA_FUNCTION_NAME; | ||
t.equal(getFunctionName(undefined), '', `undefined context must give ''`); | ||
@@ -98,2 +92,26 @@ t.equal(getFunctionName(null), '', `null context must give ''`); | ||
test('getFunctionName WITH process.env.AWS_LAMBDA_FUNCTION_NAME', t => { | ||
try { | ||
const expected = 'test999'; | ||
process.env.AWS_LAMBDA_FUNCTION_NAME = expected; | ||
t.equal(getFunctionName(undefined), expected, `undefined context must give '${expected}'`); | ||
t.equal(getFunctionName(null), expected, `null context must give '${expected}'`); | ||
t.equal(getFunctionName({}), expected, `{} context must give '${expected}'`); | ||
checkGetFunctionName('test0', '$LATEST', arn0, expected, t); | ||
checkGetFunctionName('test1', '$LATEST', arn1, expected, t); | ||
checkGetFunctionName('test2', '1', arn2, expected, t); | ||
checkGetFunctionName('test3', '1.0.1', arn3, expected, t); | ||
checkGetFunctionName('test4', '4.0', arn4, expected, t); | ||
checkGetFunctionName('test5', '5.0', arn5, expected, t); | ||
checkGetFunctionName('test6', '6.0', arn6, expected, t); | ||
checkGetFunctionName('test7', '7.0', arn7, expected, t); | ||
t.end(); | ||
} finally { | ||
delete process.env.AWS_LAMBDA_FUNCTION_NAME; | ||
} | ||
}); | ||
// ===================================================================================================================== | ||
@@ -103,3 +121,5 @@ // Tests for getFunctionName | ||
test('getFunctionVersion', t => { | ||
test('getFunctionVersion WITHOUT process.env.AWS_LAMBDA_FUNCTION_VERSION', t => { | ||
delete process.env.AWS_LAMBDA_FUNCTION_VERSION; | ||
t.equal(getFunctionVersion(undefined), '', `undefined context must give ''`); | ||
@@ -120,2 +140,26 @@ t.equal(getFunctionVersion(null), '', `null context must give ''`); | ||
test('getFunctionVersion WITH process.env.AWS_LAMBDA_FUNCTION_VERSION', t => { | ||
try { | ||
const expected = '999.0.123'; | ||
process.env.AWS_LAMBDA_FUNCTION_VERSION = expected; | ||
t.equal(getFunctionVersion(undefined), expected, `undefined context must give '${expected}'`); | ||
t.equal(getFunctionVersion(null), expected, `null context must give '${expected}'`); | ||
t.equal(getFunctionVersion({}), expected, `{} context must give '${expected}'`); | ||
checkGetFunctionVersion('test0', '$LATEST', arn0, expected, t); | ||
checkGetFunctionVersion('test1', '$LATEST', arn1, expected, t); | ||
checkGetFunctionVersion('test2', '1', arn2, expected, t); | ||
checkGetFunctionVersion('test3', '1.0.1', arn3, expected, t); | ||
checkGetFunctionVersion('test4', '4.0', arn4, expected, t); | ||
checkGetFunctionVersion('test5', '5.0', arn5, expected, t); | ||
checkGetFunctionVersion('test6', '6.0', arn6, expected, t); | ||
checkGetFunctionVersion('test7', '7.0', arn7, expected, t); | ||
t.end(); | ||
} finally { | ||
delete process.env.AWS_LAMBDA_FUNCTION_VERSION; | ||
} | ||
}); | ||
// ===================================================================================================================== | ||
@@ -145,3 +189,6 @@ // Tests for getInvokedFunctionArnFunctionName | ||
test('getFunctionNameVersionAndAlias', t => { | ||
test('getFunctionNameVersionAndAlias WITHOUT process.env.AWS_LAMBDA_FUNCTION_NAME/_VERSION', t => { | ||
delete process.env.AWS_LAMBDA_FUNCTION_NAME; | ||
delete process.env.AWS_LAMBDA_FUNCTION_VERSION; | ||
const expected = {functionName: '', version: '', alias: ''}; | ||
@@ -172,2 +219,39 @@ t.deepEqual(getFunctionNameVersionAndAlias(undefined), expected, `undefined context must give ${JSON.stringify(expected)}`); | ||
test('getFunctionNameVersionAndAlias WITH process.env.AWS_LAMBDA_FUNCTION_NAME/_VERSION', t => { | ||
try { | ||
const name = 'test9999'; | ||
const version = '123.456.789'; | ||
process.env.AWS_LAMBDA_FUNCTION_NAME = name; | ||
process.env.AWS_LAMBDA_FUNCTION_VERSION = version; | ||
const expected = {functionName: name, version: version, alias: ''}; | ||
t.deepEqual(getFunctionNameVersionAndAlias(undefined), expected, `undefined context must give ${JSON.stringify(expected)}`); | ||
t.deepEqual(getFunctionNameVersionAndAlias(null), expected, `null context must give ${JSON.stringify(expected)}`); | ||
t.deepEqual(getFunctionNameVersionAndAlias({}), expected, `{} context must give ${JSON.stringify(expected)}`); | ||
const expected0 = {functionName: name, version: version, alias: ''}; | ||
const expected1 = {functionName: name, version: version, alias: ''}; | ||
const expected2 = {functionName: name, version: version, alias: ''}; | ||
const expected3 = {functionName: name, version: version, alias: ''}; | ||
const expected4 = {functionName: name, version: version, alias: 'DEV'}; | ||
const expected5 = {functionName: name, version: version, alias: 'qa'}; | ||
const expected6 = {functionName: name, version: version, alias: 'prod'}; | ||
const expected7 = {functionName: name, version: version, alias: 'BETA'}; | ||
checkGetFunctionNameVersionAndAlias('test0', '$LATEST', arn0, expected0, t); | ||
checkGetFunctionNameVersionAndAlias('test1', '$LATEST', arn1, expected1, t); | ||
checkGetFunctionNameVersionAndAlias('test2', '1', arn2, expected2, t); | ||
checkGetFunctionNameVersionAndAlias('test3', '1.0.1', arn3, expected3, t); | ||
checkGetFunctionNameVersionAndAlias('test4', '4.0', arn4, expected4, t); | ||
checkGetFunctionNameVersionAndAlias('test5', '5.0', arn5, expected5, t); | ||
checkGetFunctionNameVersionAndAlias('test6', '6.0', arn6, expected6, t); | ||
checkGetFunctionNameVersionAndAlias('test7', '7.0', arn7, expected7, t); | ||
t.end(); | ||
} finally { | ||
delete process.env.AWS_LAMBDA_FUNCTION_NAME; | ||
delete process.env.AWS_LAMBDA_FUNCTION_VERSION; | ||
} | ||
}); | ||
// ===================================================================================================================== | ||
@@ -174,0 +258,0 @@ // Tests for getAlias |
@@ -15,4 +15,3 @@ 'use strict'; | ||
const getRegionRaw = regions.ONLY_FOR_TESTING.getRegionRaw; | ||
const setRegionIfNotSet = regions.ONLY_FOR_TESTING.setRegionIfNotSet; | ||
const getRegionRaw = regions.getRegionRaw; | ||
@@ -48,3 +47,2 @@ const Strings = require('core-functions/strings'); | ||
t.equal(getRegion(), undefined, `getRegion() must be '${undefined}'`); | ||
t.throws(() => getRegion(true), undefined, `getRegion(true) must throw'`); | ||
@@ -76,55 +74,2 @@ // check will set, when not set | ||
} | ||
}); | ||
// ===================================================================================================================== | ||
// Tests for getRegion and setRegionIfNotSet | ||
// ===================================================================================================================== | ||
test('getRegion and setRegionIfNotSet', t => { | ||
// Attempt to preserve the original AWS_REGION setting (unfortunately cannot preserve undefined or null) | ||
const origRegion = getRegionRaw(); | ||
// check orig | ||
if (origRegion === undefined) { | ||
t.equal(origRegion, process.env.AWS_REGION, `original raw must be '${process.env.AWS_REGION}'`); | ||
t.equal(getRegion(), undefined, `original must be empty string '${process.env.AWS_REGION}'`); | ||
} else if (isBlank(origRegion)) { | ||
t.equal(origRegion, process.env.AWS_REGION, `original raw must be '${process.env.AWS_REGION}'`); | ||
t.equal(getRegion(), '', `original must be undefined '${process.env.AWS_REGION}'`); | ||
} | ||
// Must use delete to "clear" property.env variable - since setting to undefined & null don't work as intended | ||
try { | ||
// Clear AWS_REGION to undefined (by deleting it) | ||
delete process.env.AWS_REGION; | ||
t.equal(process.env.AWS_REGION, undefined, `process.env.AWS_REGION must be '${undefined}' after delete`); | ||
// check get when not set | ||
t.equal(getRegion(), undefined, `getRegion() must be '${undefined}'`); | ||
t.throws(() => getRegion(true), undefined, `getRegion(true) must throw'`); | ||
// check will set, when not set | ||
const expected = 'TEST_REGION_1'; | ||
t.ok(setRegionIfNotSet(expected), `setRegionIfNotSet('TEST_REGION_1') must set successfully`); | ||
t.equal(getRegion(), expected, `getRegion() must be ${expected}`); | ||
t.equal(process.env.AWS_REGION, expected, `process.env.AWS_REGION must be ${expected}`); | ||
// check was NOT set, when already set set | ||
t.notOk(setRegionIfNotSet('TEST_REGION_3'), `setRegionIfNotSet('TEST_REGION_3') must NOT set successfully`); | ||
t.equal(getRegion(), expected, `getRegion() must still be ${expected}`); | ||
} finally { | ||
// "Restore" original aws region | ||
setRegion(origRegion); | ||
// Check "restore" worked | ||
if (origRegion === undefined) { | ||
t.equal(getRegion(), undefined, `getRegion() must be "restored" to undefined' (orig was ${origRegion})`); | ||
} else if (isBlank(origRegion)) { | ||
t.equal(getRegion(), '', `getRegion() must be "restored" to empty string (orig was '${origRegion}')`); | ||
} else { | ||
t.equal(getRegion(), origRegion, `getRegion() must be restored to ${origRegion}`); | ||
} | ||
t.end(); | ||
} | ||
}); | ||
}); |
@@ -10,2 +10,10 @@ 'use strict'; | ||
* @property {uuid} awsRequestId - a unique identifier assigned to the current invocation of your handler function by AWS Lambda | ||
* @property {boolean} callbackWaitsForEmptyEventLoop | ||
* @property {string} logGroupName | ||
* @property {string} logStreamName | ||
* @property {string} functionName | ||
* @property {string} functionVersion | ||
* @property {string} invokedFunctionArn | ||
* @property {string} memoryLimitInMB | ||
* @property {uuid} invokeid - appears to be same value as awsRequestId | ||
* @property {function(): number} getRemainingTimeInMillis - gets the remaining time to execute in milliseconds | ||
@@ -76,2 +84,9 @@ */ | ||
/** | ||
* @typedef {Object} LambdaFunctionNameVersionAndAlias - the name, version & alias of the invoked Lambda function | ||
* @property {string} functionName - the name of the Lambda function | ||
* @property {string} version - the version of the Lambda function | ||
* @property {string|undefined} [alias] - the alias of the Lambda function | ||
*/ | ||
/** | ||
* @typedef {StageAware} EventAWSContextAndStageAware - an object configured with the AWS event, AWS context and the resolved stage, | ||
@@ -81,2 +96,3 @@ * which implies pre-configured stage handling settings and logging functionality | ||
* @property {AWSContext} awsContext - the AWS context passed to your Lambda function on invocation | ||
* @property {LambdaFunctionNameVersionAndAlias|undefined} [invokedLambda] - the name, version & alias of the invoked Lambda function | ||
*/ | ||
@@ -83,0 +99,0 @@ |
Sorry, the diff of this file is too big to display
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
553240
8116
111