npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kinesis-events

v3.0.0

Published

AWS Kinesis event parser and handler for Lambda consumers

Downloads

1,517

Readme

kinesis-events

npm npm David Travis license Beerpay

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

npm i --save kinesis-events

Usage

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

Instance of the 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

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

KinesisEvents

Kind: global class

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. |

kinesisEvents.options : Object

Options object for KinesisEvents. Allows overridding options after instantiation.

Kind: instance property of KinesisEvents
Example

kinesisEvents.options.transform = function(record, index) {
    // transform record...
    return record;
};

kinesisEvents.ParseError : ParseError

Access to the ParseError class.

Kind: instance property of KinesisEvents
Read only: true

kinesisEvents.parse(records, [json]) ⇒ RecordSet

Parses records from the incoming Kinesis event.

Kind: instance method of KinesisEvents
Returns: 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

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.records : Array

The records within this record set.

Kind: instance property of RecordSet

recordSet.failed : Array.<ParseError>

List of failed records (ParseError).

Kind: instance property of RecordSet

recordSet.length : Number

The total number of parsed records in the record set.

Kind: instance property of RecordSet
Read only: true

recordSet.hasErrors : Boolean

Boolean flag if this record set has failed records.

Kind: instance property of 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.

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:

$ npm run test

License

MIT License. See License in the repository.