-
Notifications
You must be signed in to change notification settings - Fork 6
/
demo.js
61 lines (52 loc) · 1.69 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Usage: node demo.js [streamName]
//
// Sends a trickle of data into a Kinesis stream using KinesisWritable.
// Designed with some jitter so sometimes you'll hit the highWaterMark and
// sometimes you'll hit the timeout.
//
// If you want prettier logging:
//
// node demo.js hello-world | bunyan --output short
//
// And to verify the stream output:
//
// kinesis-console-consumer hello-world
//
// In local dev, if you get "Cannot find module 'kinesis-streams'", run `npm link`
//
const { Readable } = require('stream')
const AWS = require('aws-sdk')
const bunyan = require('bunyan')
const { KinesisWritable } = require('kinesis-streams')
const WAIT = 500
const logger = bunyan.createLogger({ name: 'demo', level: 'debug' })
class NoiseReadable extends Readable {
constructor (options = {}) {
options.objectMode = true
super(options)
this._alphabet = '0123456789ABCDEF'.split('')
}
_read (size) {
const throbber = this._alphabet.shift()
const data = throbber
// const data = {foo: throbber}
this._alphabet.push(throbber)
setTimeout(() => this.push(data), WAIT * 1.1 * Math.random())
}
}
const client = new AWS.Kinesis()
const stream = new KinesisWritable(client, process.argv[2] || 'demo', { logger, wait: WAIT })
stream.on('kinesis.putRecords', (response) => {
/* eslint-disable no-unused-vars */
const failedCount = response.FailedRecordCount
const successCount = response.Records.length - response.FailedRecordCount
const queueDepth = stream.queue.length
/* eslint-enable */
})
stream.on('error', () => {
/* eslint-disable no-unused-vars */
const errorCount = 1
const queueDepth = stream.queue.length
/* eslint-enable */
})
new NoiseReadable().pipe(stream)