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

aws-core-utils

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aws-core-utils - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

kinesis-utils.js

8

config.json
{
"stages": {
"defaultStreamNameStageSeparator" : "_",
"defaultResourceNameStageSeparator" : "_",
"defaultExtractInCase" : "lower",
"defaultInjectInCase" : "upper"
"defaultStreamNameStageSeparator": "_",
"defaultResourceNameStageSeparator": "_",
"defaultExtractInCase": "lower",
"defaultInjectInCase": "upper"
}
}
{
"name": "aws-core-utils",
"version": "2.0.0",
"version": "2.0.1",
"description": "Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, etc.",

@@ -14,6 +14,9 @@ "author": "Byron du Preez",

"dependencies": {
"core-functions": "^1.2.0",
"logging-utils": "^1.0.2"
"core-functions": "^2.0.2",
"logging-utils": "^1.0.5"
},
"devDependencies": {
"aws-sdk": "2.4.9"
},
"repository": "https://github.com/byron-dupreez/aws-core-utils"
}

@@ -1,4 +0,4 @@

# aws-core-utils v2.0.0
# aws-core-utils v2.0.1
Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, etc.
Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Kinesis, Lambdas, AWS errors, stream events, etc.

@@ -11,2 +11,4 @@

- Utilities for working with AWS errors.
- kinesis-utils.js
- Utilities for working with AWS.Kinesis and a module-scope cache for a single AWS.Kinesis instance for Lambda.
- lambdas.js

@@ -37,19 +39,117 @@ - Utilities for working with AWS Lambda, which enable extraction of function names, versions and, most importantly,

In Node.js:
* To use the AWS ARN utilities
```js
// To use the ARN utilities
const arns = require('aws-core-utils/arns');
// To resolve the AWS region
const arnComponent = arns.getArnComponent(arn, index);
const arnPartition = arns.getArnPartition(arn);
const arnService = arns.getArnService(arn);
const arnRegion = arns.getArnRegion(arn);
const arnAccountId = arns.getArnAccountId(arn);
const arnResources = arns.getArnResources(arn);
```
* To use the AWS errors utilities
```js
const awsErrors = require('aws-core-utils/aws-errors');
```
* To get the current AWS region & configure it on a context
```js
const regions = require('aws-core-utils/regions');
// To derive stages from AWS events
const stages = require('aws-core-utils/stages');
// To get the current AWS region
const region = regions.getRegion();
// To use the L from AWS events
// To configure a context with the current AWS region
regions.configureRegion(context, failFast)
```
* To use the Kinesis utilities
```js
const kinesisUtils = require('aws-core-utils/kinesis-utils');
// To configure a new AWS.Kinesis instance (or re-use a cached instance) on a context
// Currently only creates a new AWS.Kinesis instance with the current AWS region & given maxRetries
kinesisUtils.configureKinesis(context, maxRetries);
```
* To use the Lambda utilities
```js
const lambdas = require('aws-core-utils/lambdas');
// To use the AWS errors utilities
const awsErros = require('aws-core-utils/aws-errors');
// Fail a Lambda's callback with a standard error and preserve HTTP status codes (for non-API Gateway Lambdas)
// See core-functions/app-errors.js for standard errors to use
lambdas.failCallback(lambdaCallback, error, awsContext, message, code);
// Fail an API Gateway-exposed Lambda's callback with a standard error and preserve HTTP status codes
lambdas.failCallbackForApiGateway(lambdaCallback, error, awsContext, message, code, allowedHttpStatusCodes);
// To resolve the Lambda alias from an AWS Lambda context
const alias = lambdas.getAlias(awsContext);
// To extract other details from an AWS Lambda context
const functionName = lambdas.getFunctionName(awsContext);
const functionVersion = lambdas.getFunctionVersion(awsContext);
const functionNameVersionAndAlias = lambdas.getFunctionNameVersionAndAlias(awsContext);
const invokedFunctionArn = lambdas.getInvokedFunctionArn(awsContext);
const invokedFunctionArnFunctionName = lambdas.getInvokedFunctionArnFunctionName(awsContext);
```
* To use the stage utilities
```js
const stages = require('aws-core-utils/stages');
// To configure default stage handling, which sets the default behaviour of the next 4 functions
stages.configureDefaultStageHandling(context, forceConfiguration);
// 1. To resolve / derive a stage from an AWS event
const context = {};
const stage = stages.resolveStage(event, awsContext, context);
// 2. To configure a context with a resolved stage
stages.configureStage(context, event, awsContext, failFast)
// 3. To qualify an unqualified stream name with a stage
const unqualifiedStreamName = 'TestStream';
const stageQualifiedStreamName = stages.toStageQualifiedStreamName(unqualifiedStreamName, stage, context);
// 4. To extract a stage from a qualified stream name
const qualifiedStreamName = 'TestStream_PROD';
const stage2 = stages.extractStageFromQualifiedStreamName(qualifiedStreamName, context);
// To configure completely customised stage handling of the above 4 functions
stages.configureStageHandling(context, customToStage, convertAliasToStage,
injectStageIntoStreamName, extractStageFromStreamName, streamNameStageSeparator,
injectStageIntoResourceName, extractStageFromResourceName, resourceNameStageSeparator,
injectInCase, extractInCase, defaultStage, forceConfiguration);
// To check if stage handling is configured
stages.isStageHandlingConfigured(context);
// To look up stage handling settings and functions
const setting = stages.getStageHandlingSetting(context, settingName);
const fn = stages.getStageHandlingFunction(context, settingName);
```
* To use the stream event utilities
```js
const streamEvents = require('aws-core-utils/stream-events');
// To extract stream names form AWS event source ARNs
const eventSourceARNs = streamEvents.getEventSourceARNs(event);
const eventSourceStreamNames = streamEvents.getEventSourceStreamNames(event);
const eventSourceStreamName = streamEvents.getEventSourceStreamName(record);
// Simple checks to validate existance of some of parameters of Kinesis & DynamoDB stream event records
try {
streamEvents.validateStreamEventRecord(record);
streamEvents.validateKinesisStreamEventRecord(record);
streamEvents.validateDynamoDBStreamEventRecord(record);
} catch (err) {
// ...
}
```
## Unit tests

@@ -75,11 +175,13 @@ This module's unit tests were developed with and must be run with [tape](https://www.npmjs.com/package/tape). The unit tests have been tested on [Node.js v4.3.2](https://nodejs.org/en/blog/release/v4.3.2/).

## Changes
### 0.9.0
- Initial commit
### 1.0.0
- Completed changes needed to release 1.0.0
- Added unit tests for stages.js
- Simplified regions.js API down to relevant methods
- Fixed defects attempting to source awsRegion and eventSourceARN from event instead of Kinesis records within event.
- Patched repository in package.json
### 2.0.1
- Added new `kinesis-utils` module to provide basic configuration and caching of an AWS.Kinesis instance for Lambda
- Changes to `stream-events` module:
- Added `validateStreamEventRecord` function to check if a record is either a valid Kinesis or DynamoDB stream event record
- Added `validateKinesisStreamEventRecord` function to check if a record is a valid Kinesis stream event record
- Added `validateDynamoDBStreamEventRecord` function to check if a record is a valid DynamoDB stream event record
- Added unit tests for new functions
- Minor change to `setRegionIfNotSet` to eliminate unnecessary logging when regions are the same
- Updated `core-functions` dependency to version 2.0.2
- Updated `logging-utils` dependency to version 1.0.5

@@ -108,2 +210,15 @@ ### 2.0.0

- Updated `core-functions` dependency to version 1.2.0.
- Added `logging-utils` 1.0.2 dependency.
- Added `logging-utils` 1.0.2 dependency.
### 1.0.0
- Completed changes needed to release 1.0.0
- Added unit tests for stages.js
- Simplified regions.js API down to relevant methods
- Fixed defects attempting to source awsRegion and eventSourceARN from event instead of Kinesis records within event.
- Patched repository in package.json
### 0.9.0
- Initial commit

@@ -92,3 +92,5 @@ 'use strict';

} else {
console.log(`Ignoring attempt to change ALREADY set AWS_REGION env variable (${process.env.AWS_REGION}) to (${newRegion})`);
if (process.env.AWS_REGION !== newRegion) {
console.log(`Ignoring attempt to change ALREADY set AWS_REGION env variable (${process.env.AWS_REGION}) to (${newRegion})`);
}
}

@@ -95,0 +97,0 @@ return false;

@@ -6,2 +6,3 @@ 'use strict';

const isNotBlank = Strings.isNotBlank;
const stringify = Strings.stringify;

@@ -19,3 +20,13 @@ /**

/** Extracts and returns the stream name from the given stream event record's eventSourceARN */
getEventSourceStreamName: getEventSourceStreamName
getEventSourceStreamName: getEventSourceStreamName,
/** Validates the given stream event record and raises an error if the record is invalid or not a Kinesis or DynamoDB stream event record */
validateStreamEventRecord: validateStreamEventRecord,
/** Validates the given Kinesis stream event record and raises an error if the record is invalid or not a Kinesis stream event record */
validateKinesisStreamEventRecord: validateKinesisStreamEventRecord,
/** Validates the given DynamoDB stream event record and raises an error if the record is invalid or not a DynamoDB stream event record */
validateDynamoDBStreamEventRecord: validateDynamoDBStreamEventRecord
// /* * Returns a truncated version of the partition key and sequence number of the given stream event record for logging purposes */
// toStreamEventRecordTruncatedKeyInfo: toStreamEventRecordTruncatedKeyInfo,
};

@@ -51,1 +62,153 @@

}
/**
* Validates the given stream event record and raises an error if the record fails to meet any of the following criteria:
* 1. It must be defined;
* 2. It must contain a defined eventSource;
* 3. It must be either a Kinesis or DynamoDB stream event record; and
* 4. It must contain the required properties expected of its type (based on its eventSource).
*
* @param {Object} record - a Kinesis or DynamoDB stream event record
* @throws {Error} if the record is invalid
*/
function validateStreamEventRecord(record) {
if (!record) {
throw new Error(`Missing entire stream event record (${record})`);
}
if (!record.eventSource) {
throw new Error(`Missing eventSource property for stream event record (${stringify(record)})`);
}
switch (record.eventSource) {
case "aws:kinesis":
// Kinesis stream event record
_validateKinesisStreamEventRecord(record);
break;
case "aws:dynamodb":
// DynamoDB stream event record
_validateDynamoDBStreamEventRecord(record);
break;
default:
// Only support Kinesis and DynamoDB stream event records for now
throw new Error(`Unexpected eventSource (${record.eventSource}) on stream event record (${stringify(record)})`);
}
}
/**
* Validates the given Kinesis stream event record and raises an error if the record fails to meet any of the following criteria:
* 1. It must be defined;
* 2. It must be a Kinesis stream event record (i.e. must contain an eventSource of "aws:kinesis"); and
* 3. It must contain kinesis and kinesis.data properties.
*
* @param {Object} record - a Kinesis stream event record
* @param {string} [record.eventSource] - a stream event record's eventSource
* @param {Object} [record.kinesis] - a Kinesis stream event record's kinesis object
* @param {string} [record.kinesis.data] - a Kinesis stream event record's kinesis data
* @throws {Error} if the record is invalid
*/
function validateKinesisStreamEventRecord(record) {
if (!record) {
throw new Error(`Missing entire Kinesis stream event record (${record})`);
}
if (!record.eventSource) {
throw new Error(`Missing eventSource property for Kinesis stream event record (${stringify(record)})`);
}
if (record.eventSource !== "aws:kinesis") {
throw new Error(`Unexpected eventSource (${record.eventSource}) on Kinesis stream event record (${stringify(record)})`)
}
_validateKinesisStreamEventRecord(record);
}
function _validateKinesisStreamEventRecord(record) {
if (!record.kinesis) {
throw new Error(`Missing kinesis property for Kinesis stream event record (${stringify(record)})`);
}
if (!record.kinesis.data) {
throw new Error(`Missing data property for Kinesis stream event record (${stringify(record)})`);
}
}
/**
* Validates the given DynamoDB stream event record and raises an error if the record fails to meet any of the following criteria:
* 1. It must be defined;
* 2. It must be a DynamoDB stream event record (i.e. must contain an eventSource of "aws:dynamodb");
* 3. It must contain dynamodb, dynamodb.Keys and dynamodb.StreamViewType properties; and
* 4. It should contain dynamodb.NewImage and/or dynamodb.OldImage properties according to its StreamViewType.
*
* @param {Object} record - a DynamoDB stream event record
* @param {string} [record.eventSource] - a stream event record's eventSource
* @param {Object} [record.dynamodb] - a DynamoDB stream event record's dynamodb object
* @param {Object} [record.dynamodb.Keys] - a DynamoDB stream event record's Keys object
* @param {string} [record.dynamodb.StreamViewType] - a DynamoDB stream event record's stream view type
* @param {Object} [record.dynamodb.NewImage] - a DynamoDB stream event record's new image object
* @param {Object} [record.dynamodb.OldImage] - a DynamoDB stream event record's old image object
* @throws {Error} if the record is invalid
*/
function validateDynamoDBStreamEventRecord(record) {
if (!record) {
throw new Error(`Missing entire DynamoDB stream event record (${record})`);
}
if (!record.eventSource) {
throw new Error(`Missing eventSource property for DynamoDB stream event record (${stringify(record)})`);
}
if (record.eventSource !== "aws:dynamodb") {
throw new Error(`Unexpected eventSource (${record.eventSource}) on DynamoDB stream event record (${stringify(record)})`)
}
_validateDynamoDBStreamEventRecord(record);
}
function _validateDynamoDBStreamEventRecord(record) {
if (!record.dynamodb) {
throw new Error(`Missing dynamodb property for DynamoDB stream event record (${stringify(record)})`);
}
if (!record.dynamodb.Keys) {
throw new Error(`Missing Keys property for DynamoDB stream event record (${stringify(record)})`);
}
if (!record.dynamodb.StreamViewType) {
throw new Error(`Missing StreamViewType property for DynamoDB stream event record (${stringify(record)})`);
}
switch (record.dynamodb.StreamViewType) {
case 'KEYS_ONLY':
break;
case 'NEW_IMAGE':
if (!record.dynamodb.NewImage) {
throw new Error(`Missing NewImage property for DynamoDB stream event record (${stringify(record)})`);
}
break;
case 'OLD_IMAGE':
if (!record.dynamodb.OldImage) {
throw new Error(`Missing OldImage property for DynamoDB stream event record (${stringify(record)})`);
}
break;
case 'NEW_AND_OLD_IMAGES':
if (!record.dynamodb.NewImage && !record.dynamodb.OldImage) {
throw new Error(`Missing both NewImage and OldImage properties for DynamoDB stream event record (${stringify(record)})`);
}
break;
default:
throw new Error(`Unexpected StreamViewType (${record.dynamodb.StreamViewType}) on DynamoDB stream event record (${stringify(record)})`);
}
}
// /**
// * Returns short descriptive key information for the given Kinesis or DynamoDB stream record, which includes the
// * record's partition key and the first & last 5 characters of its sequence number, for logging purposes.
// * @param {Object} record - a Kinesis or DynamoDB stream record
// * @return {string} short descriptive key information for the given record
// */
// function toStreamEventRecordTruncatedKeyInfo(record) {
// if (!record) return stringify(record);
// if (record.kinesis) {
// let seqNo = record.kinesis.sequenceNumber;
// let seqNoFragment = seqNo ? seqNo.substring(0, 5) + "..." + seqNo.substring(seqNo.length - 5) : '';
// return `${record.kinesis.partitionKey} ${seqNoFragment}`;
// } else if (record.dynamodb) {
// // TO DO
// }
// }
{
"name": "aws-core-utils-tests",
"description": "Unit tests for aws-core-utils modules",
"version": "2.0.0",
"version": "2.0.1",
"author": "Byron du Preez",

@@ -6,0 +6,0 @@ "license": "Apache-2.0",

@@ -44,3 +44,8 @@ 'use strict';

sampleKinesisEventWithRecord: sampleKinesisEventWithRecord,
sampleKinesisEventWithRecords: sampleKinesisEventWithRecords
sampleKinesisEventWithRecords: sampleKinesisEventWithRecords,
awsKinesisStreamsSampleEvent: awsKinesisStreamsSampleEvent,
// For DynamoDB stream events
awsDynamoDBUpdateSampleEvent: awsDynamoDBUpdateSampleEvent
};

@@ -151,1 +156,117 @@

}
function awsKinesisStreamsSampleEvent(identityArn, eventSourceArn) {
return {
"Records": [
{
"eventID": "shardId-000000000000:49545115243490985018280067714973144582180062593244200961",
"eventVersion": "1.0",
"kinesis": {
"partitionKey": "partitionKey-3",
"data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0IDEyMy4=",
"kinesisSchemaVersion": "1.0",
"sequenceNumber": "49545115243490985018280067714973144582180062593244200961"
},
"invokeIdentityArn": identityArn,
"eventName": "aws:kinesis:record",
"eventSourceARN": eventSourceArn,
"eventSource": "aws:kinesis",
"awsRegion": "us-east-1"
}
]
};
}
function awsDynamoDBUpdateSampleEvent(eventSourceArn) {
return {
"Records": [
{
"eventID": "1",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"NewImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
"SequenceNumber": "111",
"SizeBytes": 26
},
"awsRegion": "us-west-2",
"eventName": "INSERT",
"eventSourceARN": eventSourceArn,
"eventSource": "aws:dynamodb"
},
{
"eventID": "2",
"eventVersion": "1.0",
"dynamodb": {
"OldImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"SequenceNumber": "222",
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 59,
"NewImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "us-west-2",
"eventName": "MODIFY",
"eventSourceARN": eventSourceArn,
"eventSource": "aws:dynamodb"
},
{
"eventID": "3",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 38,
"SequenceNumber": "333",
"OldImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "us-west-2",
"eventName": "REMOVE",
"eventSourceARN": eventSourceArn,
"eventSource": "aws:dynamodb"
}
]
};
}

@@ -16,2 +16,6 @@ 'use strict';

const validateStreamEventRecord = streamEvents.validateStreamEventRecord;
const validateKinesisStreamEventRecord = streamEvents.validateKinesisStreamEventRecord;
const validateDynamoDBStreamEventRecord = streamEvents.validateDynamoDBStreamEventRecord;
const Strings = require('core-functions/strings');

@@ -25,2 +29,6 @@ const trim = Strings.trim;

// =====================================================================================================================
// getEventSourceARNs
// =====================================================================================================================
test('getEventSourceARNs', t => {

@@ -63,2 +71,6 @@ function check(streamNames) {

// =====================================================================================================================
// getEventSourceStreamNames
// =====================================================================================================================
test('getEventSourceStreamNames', t => {

@@ -103,2 +115,6 @@ function check(streamNames) {

// =====================================================================================================================
// getEventSourceStreamName
// =====================================================================================================================
test('getEventSourceStreamName', t => {

@@ -133,1 +149,215 @@ function check(streamName) {

});
// =====================================================================================================================
// validateStreamEventRecord
// =====================================================================================================================
test('validateStreamEventRecord', t => {
function check(record, mustPass) {
const prefix = `validateStreamEventRecord(${stringify(record)})`;
try {
validateStreamEventRecord(record);
if (mustPass) {
t.pass(`${prefix} should have passed`);
} else {
t.fail(`${prefix} should NOT have passed`);
}
} catch (err) {
if (mustPass) {
t.fail(`${prefix} should NOT have failed (${err})`);
} else {
t.pass(`${prefix} should have failed (${err.message})`);
}
}
}
// no record
check(undefined, false);
check(null, false);
// invalid records
check(123, false);
check("ABC", false);
check(true, false);
check({}, false);
check({eventSource: 'aws:other'}, false);
// invalid Kinesis records
check({eventSource: 'aws:kinesis'}, false);
check({eventSource: 'aws:kinesis', kinesis: {}}, false);
// "valid" Kinesis records
check({eventSource: 'aws:kinesis', kinesis: {data: "dummy_data"}}, true);
// valid Kinesis records
const sampleRecord = samples.sampleKinesisRecord(undefined, undefined, samples.sampleEventSourceArn('eventSourceArnRegion', trim("streamName")), 'eventAwsRegion');
check(sampleRecord, true);
const awsKinesisStreamsSampleEvent = samples.awsKinesisStreamsSampleEvent("identityArn", "eventSourceArn");
awsKinesisStreamsSampleEvent.Records.forEach(record => {
check(record, true);
});
// invalid DynamoDB stream event records
check({eventSource: 'aws:dynamodb'}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OTHER"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {StreamViewType: "KEYS_ONLY"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OLD_IMAGE"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OLD_IMAGE", NewImage: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_IMAGE"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_IMAGE", OldImage: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_AND_OLD_IMAGES"}}, false);
// "valid" DynamoDB stream event records
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "KEYS_ONLY"}}, true);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OLD_IMAGE", OldImage: {}}}, true);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_IMAGE", NewImage: {}}}, true);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_AND_OLD_IMAGES", OldImage: {}, NewImage: {}}}, true);
// valid DynamoDB stream event records
const awsDynamoDBUpdateSampleEvent = samples.awsDynamoDBUpdateSampleEvent("identityArn", "eventSourceArn");
awsDynamoDBUpdateSampleEvent.Records.forEach(record => {
check(record, true);
});
t.end();
});
// =====================================================================================================================
// validateKinesisStreamEventRecord
// =====================================================================================================================
test('validateKinesisStreamEventRecord', t => {
function check(record, mustPass) {
const prefix = `validateKinesisStreamEventRecord(${stringify(record)})`;
try {
validateKinesisStreamEventRecord(record);
if (mustPass) {
t.pass(`${prefix} should have passed`);
} else {
t.fail(`${prefix} should NOT have passed`);
}
} catch (err) {
if (mustPass) {
t.fail(`${prefix} should NOT have failed (${err})`);
} else {
t.pass(`${prefix} should have failed (${err.message})`);
}
}
}
// no record
check(undefined, false);
check(null, false);
// invalid records
check({}, false);
check({eventSource: 'aws:other'}, false);
// invalid Kinesis records
check({eventSource: 'aws:kinesis'}, false);
check({eventSource: 'aws:kinesis', kinesis: {}}, false);
// "valid" Kinesis records
check({eventSource: 'aws:kinesis', kinesis: {data: "dummy_data"}}, true);
// valid Kinesis records
const sampleRecord = samples.sampleKinesisRecord(undefined, undefined, samples.sampleEventSourceArn('eventSourceArnRegion', trim("streamName")), 'eventAwsRegion');
check(sampleRecord, true);
const awsKinesisStreamsSampleEvent = samples.awsKinesisStreamsSampleEvent("identityArn", "eventSourceArn");
awsKinesisStreamsSampleEvent.Records.forEach(record => {
check(record, true);
});
// invalid - since DynamoDB stream event records
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "KEYS_ONLY"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OLD_IMAGE", OldImage: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_IMAGE", NewImage: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_AND_OLD_IMAGES", OldImage: {}, NewImage: {}}}, false);
// invalid - since DynamoDB stream event records
const awsDynamoDBUpdateSampleEvent = samples.awsDynamoDBUpdateSampleEvent("identityArn", "eventSourceArn");
awsDynamoDBUpdateSampleEvent.Records.forEach(record => {
check(record, false);
});
t.end();
});
// =====================================================================================================================
// validateDynamoDBStreamEventRecord
// =====================================================================================================================
test('validateDynamoDBStreamEventRecord', t => {
function check(record, mustPass) {
const prefix = `validateDynamoDBStreamEventRecord(${stringify(record)})`;
try {
validateDynamoDBStreamEventRecord(record);
if (mustPass) {
t.pass(`${prefix} should have passed`);
} else {
t.fail(`${prefix} should NOT have passed`);
}
} catch (err) {
if (mustPass) {
t.fail(`${prefix} should NOT have failed (${err})`);
} else {
t.pass(`${prefix} should have failed (${err.message})`);
}
}
}
// no record
check(undefined, false);
check(null, false);
// invalid records
check({}, false);
check({eventSource: 'aws:other'}, false);
// invalid DynamoDB stream event records
check({eventSource: 'aws:dynamodb'}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OTHER"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {StreamViewType: "KEYS_ONLY"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OLD_IMAGE"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OLD_IMAGE", NewImage: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_IMAGE"}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_IMAGE", OldImage: {}}}, false);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_AND_OLD_IMAGES"}}, false);
// "valid" DynamoDB stream event records
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "KEYS_ONLY"}}, true);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "OLD_IMAGE", OldImage: {}}}, true);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_IMAGE", NewImage: {}}}, true);
check({eventSource: 'aws:dynamodb', dynamodb: {Keys: {}, StreamViewType: "NEW_AND_OLD_IMAGES", OldImage: {}, NewImage: {}}}, true);
// valid DynamoDB stream event records
const awsDynamoDBUpdateSampleEvent = samples.awsDynamoDBUpdateSampleEvent("identityArn", "eventSourceArn");
awsDynamoDBUpdateSampleEvent.Records.forEach(record => {
check(record, true);
});
// invalid - since Kinesis records
check({eventSource: 'aws:kinesis', kinesis: {data: "dummy_data"}}, false);
// invalid - since Kinesis records
const sampleRecord = samples.sampleKinesisRecord(undefined, undefined, samples.sampleEventSourceArn('eventSourceArnRegion', trim("streamName")), 'eventAwsRegion');
check(sampleRecord, false);
const awsKinesisStreamsSampleEvent = samples.awsKinesisStreamsSampleEvent("identityArn", "eventSourceArn");
awsKinesisStreamsSampleEvent.Records.forEach(record => {
check(record, false);
});
t.end();
});
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