An open API service indexing awesome lists of open source software.

https://github.com/kyleross/kinesis-events

AWS Kinesis event parser and handler for Lambda consumers
https://github.com/kyleross/kinesis-events

aws aws-kinesis aws-kinesis-stream events javascript json json-parser kinesis node6 nodejs npm-module parser

Last synced: 5 months ago
JSON representation

AWS Kinesis event parser and handler for Lambda consumers

Awesome Lists containing this project

README

          

# kinesis-events
[![npm](https://img.shields.io/npm/v/kinesis-events.svg?style=for-the-badge)](https://www.npmjs.com/package/kinesis-events) [![npm](https://img.shields.io/npm/dt/kinesis-events.svg?style=for-the-badge)](https://www.npmjs.com/package/kinesis-events) [![David](https://img.shields.io/david/KyleRoss/kinesis-events.svg?style=for-the-badge)](https://david-dm.org/KyleRoss/kinesis-events) [![Travis](https://img.shields.io/travis/KyleRoss/kinesis-events/master.svg?style=for-the-badge)](https://travis-ci.org/KyleRoss/kinesis-events) [![license](https://img.shields.io/github/license/KyleRoss/kinesis-events.svg?style=for-the-badge)](https://github.com/KyleRoss/kinesis-events/blob/master/LICENSE) [![Beerpay](https://img.shields.io/beerpay/KyleRoss/kinesis-events.svg?style=for-the-badge)](https://beerpay.io/KyleRoss/kinesis-events)

AWS Kinesis event parser and handler for Lambda consumers. Ability to parse kinesis events with error handling and JSON support. Supports Node 8.10+ on AWS Lambda.

---

## Install
```bash
npm i --save kinesis-events
```

## Usage
```js
const kinesisEvents = require('kinesis-events');

// Lambda function handler
exports.handler = async event => {
// Parse the records
const result = kinesisEvents.parse(event);

// Check for errors (optional)
if(result.hasErrors) {
console.error('There are errors while parsing, ending process...');
process.exit(1);
}

result.records.forEach(record => {
//... iterate through the parsed records
});
};
```

---

# API Documentation

## kinesis-events

### kinesisEvents : [KinesisEvents](#KinesisEvents) ⏏
Instance of the [KinesisEvents](#kinesisevents) class which is exported when calling `require('kinesis-events')`.
For more advanced usage, you may create a new instance of KinesisEvents (see example below).

**Kind**: Exported KinesisEvents Instance
**Example**
```js
const kinesisEvents = require('kinesis-events');

// Advanced usage
const { KinesisEvents } = require('kinesis-events');
const kinesisEvents = new KinesisEvents({
// options...
});
```

## ParseError ⇐ Error
Custom error that is generated when there is a parsing error.

**Kind**: global class
**Extends**: Error

### parseError.payload : String
The original data that caused the error.

**Kind**: instance property of [ParseError](#ParseError)

## KinesisEvents
**Kind**: global class

* [KinesisEvents](#KinesisEvents)
* [new KinesisEvents([options])](#new_KinesisEvents_new)
* [kinesisEvents.options](#KinesisEvents+options) : Object
* [kinesisEvents.ParseError](#KinesisEvents+ParseError) : [ParseError](#ParseError)
* [kinesisEvents.parse(records, [json])](#KinesisEvents+parse) ⇒ [RecordSet](#RecordSet)

### new KinesisEvents([options])
Constructor for KinesisEvents.

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [options] | Object | {} | Options object to control certain features of KinesisEvents. |
| [options.transform(record, index)] | function | | Optional transform function to call for each record. See [Transform Function](#transform-function). |

### kinesisEvents.options : Object
Options object for KinesisEvents. Allows overridding options after instantiation.

**Kind**: instance property of [KinesisEvents](#KinesisEvents)
**Example**
```js
kinesisEvents.options.transform = function(record, index) {
// transform record...
return record;
};
```

### kinesisEvents.ParseError : [ParseError](#ParseError)
Access to the ParseError class.

**Kind**: instance property of [KinesisEvents](#KinesisEvents)
**Read only**: true

### kinesisEvents.parse(records, [json]) ⇒ [RecordSet](#RecordSet)
Parses records from the incoming Kinesis event.

**Kind**: instance method of [KinesisEvents](#KinesisEvents)
**Returns**: [RecordSet](#RecordSet) - New instance of RecordSet with the parsed records.

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| records | Array | | Event data (records) to parse. |
| [json] | Boolean | true | Enable/disable JSON parsing for each event. |

**Example**
```js
const result = kinesisEvents.parse(event.Records);

result.records.forEach(record => {
// do something with each record...
});
```

## RecordSet
A set of parsed records with additional functionality.

**Kind**: global class

* [RecordSet](#RecordSet)
* [recordSet.records](#RecordSet+records) : Array
* [recordSet.failed](#RecordSet+failed) : [Array.<ParseError>](#ParseError)
* [recordSet.length](#RecordSet+length) : Number
* [recordSet.hasErrors](#RecordSet+hasErrors) : Boolean

### recordSet.records : Array
The records within this record set.

**Kind**: instance property of [RecordSet](#RecordSet)

### recordSet.failed : [Array.<ParseError>](#ParseError)
List of failed records (ParseError).

**Kind**: instance property of [RecordSet](#RecordSet)

### recordSet.length : Number
The total number of parsed records in the record set.

**Kind**: instance property of [RecordSet](#RecordSet)
**Read only**: true

### recordSet.hasErrors : Boolean
Boolean flag if this record set has failed records.

**Kind**: instance property of [RecordSet](#RecordSet)
**Read only**: true
## Transform Function
New in v3.0.0, there is now an option to pass in a transform function that will allow you to transform the record before it is added to the RecordSet. This allows custom functionality or business logic to be implemented at a higher level.

The transform function takes 2 arguments, `record` and `index`. The function must return the transformed record in order for it to be added to the RecordSet. If the record is not returned from the function, it will be ignored.

```js
const { KinesisEvents } = require('kinesis-events');
const kinesisEvents = new KinesisEvents({
transform: (record, index) => {
if(record.firstName && record.lastName) {
// example, remove record if data is missing
return null;
}

record.someCustomProperty = 'some custom value';
return record;
}
});
```

---

## Tests
Tests are written and provided as part of the module. It requires mocha to be installed which is included as a `devDependency`. You may run the tests by calling:

```bash
$ npm run test
```

## License
MIT License. See [License](https://github.com/KyleRoss/kinesis-events/blob/master/LICENSE) in the repository.