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

v0.12.0

Published

Readable and writable streams for AWS Kinesis

Downloads

9,262

Readme

Kinesis Streams

Build Status npm version Test Coverage

There once was a Kinesis readable stream without a home, and a Kinesis writable stream without a home, so now they're roommates.

NOTE: Kinesis was a bad idea, and we're switching to Kafka. So I won't be using my own library for much longer.

Installing

npm install kinesis-streams

Writeable stream

const AWS = require('aws-sdk')
const { KinesisWritable } = require('kinesis-streams')
const client = new AWS.Kinesis()
client.config.update({ maxRetries: 10 })
const writable = new KinesisWritable(client, 'streamName', options)
inputStream.pipe(writable)

Options

  • options.logger (optional) bunyan, winston, or logger with debug, error and info
  • options.highWaterMark (default: 16) Buffer this many records before writing to Kinesis. Equivalent to CollectionMaxCount
  • options.wait (default: 500) How many milliseconds it should periodically flush. Equivalent to RecordMaxBufferedTime

Some of these options have equivalents in the official KPL.

Custom events

These events are emitted:

  • kinesis.putRecords Fires after records are put and the response is processed. You'll get the original response from AWS. See demo.js for an example of how to interpret it

      reader.on('kinesis.putRecords', (response: {FailedRecordCount: number, Records: Record[]}) => {})

Setting the partition key

By default, the partition key is to a dummy value, '0'. If you have multiple shards, you need to set a partition key in a way that makes sense for your data. Here are two ways to do this:

  1. Set the getPartitionKey method of the writable stream instance:
const AWS = require('aws-sdk')
const { KinesisWritable } = require('kinesis-streams')
const client = new AWS.Kinesis()
const writable = new KinesisWritable(client, 'streamName', options)
writable.getPartitionKey = (data) => data.foo.substr(5)
inputStream.pipe(writable)
  1. Subclass KinesisWritable and provide your own getPartitionKey. See the source for reference.

Readable stream

const AWS = require('aws-sdk')
const { KinesisReadable } = require('kinesis-streams')
const client = new AWS.Kinesis()
const reader = new KinesisReadable(client, streamName, options)
reader.pipe(yourDestinationHere)

Options

  • options.logger (optional) bunyan, winston, or logger with debug, error and info

  • options.interval: number (default: 2000) Milliseconds between each Kinesis read. The AWS limit is 5 reads / second / shard

  • options.parser: Function If this is set, this function is applied to the data. Example:

      const reader = new KinesisReadable(client, streamName, {parser: JSON.parse})
      reader.on('data', console.log(data.id))
  • options.restartOnClose: boolean (default: false) Rediscover new shards once all current shards have been closed

  • And any getShardIterator parameter

Custom events

These events are emitted:

  • checkpoint This fires when data is received so you can keep track of the last successful sequence read:

      reader.on('checkpoint', (sequenceNumber: string) => {})

Loggers

KinesisWritable and KinesisReadable both take an optional logger option. If this is omitted, the debug logger will be used instead. To see output, set DEBUG=kinesis-streams:* in your environment.

Prior art

The writable stream is based on the interface of kinesis-write-stream. The checkpoint event in readable stream is based on kinesis-readable. The readable stream was originally written as a proof of concept in kinesis-console-consumer.

kinesis-write-stream was forked because at the time, it didn't support periodic flushes. Since then the configuration of the readable and writable streams have been rewritten to be consistent, and both emit lots of events now that consumers can use for instrumentation.

License

This package is licensed under Apache License 2.0, but the tests/writable.spec.js and test/fixture/* are originally from kinesis-write-stream MIT licensed from Espen Volden.