-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
251 lines (200 loc) · 5.7 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const debug = require('debug')('DynamoDBStream')
const map = require('@kessler/async-map-limit')
const { EventEmitter } = require('events')
class DynamoDBStream extends EventEmitter {
/**
* @param {object} ddbStreams - an instance of DynamoDBStreams
* @param {string} streamArn - the arn of the stream we're consuming
* @param {function} unmarshall - directly from
* ```js
* const { unmarshall } = require('@aws-sdk/util-dynamodb')
* ```
* if not provided then records will be returned using low level api/shape
*/
constructor(ddbStreams, streamArn, unmarshall) {
super()
if (!typeof ddbStreams === 'object') {
throw new Error('missing or invalid ddbStreams argument')
}
if (!typeof streamArn === 'string') {
throw new Error('missing or invalid streamArn argument')
}
this._ddbStreams = ddbStreams
this._streamArn = streamArn
this._shards = new Map()
this._unmarshall = unmarshall
}
/**
* this will update the stream, shards and records included
*
*/
async fetchStreamState() {
debug('fetchStreamState')
await this.fetchStreamShards()
await this.fetchStreamRecords()
}
/**
* update the shard state of the stream
* this will emit new shards / remove shards events
*/
async fetchStreamShards() {
debug('fetchStreamShards')
this._trimShards()
const params = {
StreamArn: this._streamArn
}
const { StreamDescription } = await this._ddbStreams.describeStream(params)
const shards = StreamDescription.Shards
const newShardIds = []
// collect all the new shards of this stream
for (const newShardEntry of shards) {
const existingShardEntry = this._shards.get(newShardEntry.ShardId)
if (!existingShardEntry) {
this._shards.set(newShardEntry.ShardId, {
shardId: newShardEntry.ShardId
})
newShardIds.push(newShardEntry.ShardId)
}
}
if (newShardIds.length > 0) {
debug('Added %d new shards', newShardIds.length)
this._emitNewShardsEvent(newShardIds)
}
}
/**
* get latest updates from the underlying stream
*
*/
async fetchStreamRecords() {
debug('fetchStreamRecords')
if (this._shards.size === 0) {
debug('no shards found, this is ok')
return
}
await this._getShardIterators()
const records = await this._getRecords()
debug('fetchStreamRecords', records)
this._trimShards()
this._emitRecordEvents(records)
return records
}
/**
* get a COPY of the current/internal shard state.
* this, in conjuction with setShardState is used to
* persist the stream state locally.
*
* @returns {object}
*/
getShardState() {
const state = {}
for (const [shardId, shardData] of this._shards) {
state[shardId] = { ...shardData }
}
return state
}
/**
* @param {object} shards
*/
setShardState(shards) {
this._shards = new Map()
for (const [shardId, shardData] of Object.entries(shards)) {
this._shards.set(shardId, shardData)
}
}
_getShardIterators() {
debug('_getShardIterators')
return map(this._shards.values(), shardData => this._getShardIterator(shardData), 10)
}
async _getShardIterator(shardData) {
debug('_getShardIterator')
debug(shardData)
// no need to get an iterator if this shard already has NextShardIterator
if (shardData.nextShardIterator) {
debug('shard %s already has an iterator, skipping', shardData.shardId)
return
}
const params = {
ShardId: shardData.shardId,
ShardIteratorType: 'LATEST',
StreamArn: this._streamArn
}
const { ShardIterator } = await this._ddbStreams.getShardIterator(params)
shardData.nextShardIterator = ShardIterator
}
async _getRecords() {
debug('_getRecords')
const results = await map(this._shards.values(), shardData => this._getShardRecords(shardData), 10)
return results.flat()
}
async _getShardRecords(shardData) {
debug('_getShardRecords')
const params = { ShardIterator: shardData.nextShardIterator }
try {
const { Records, NextShardIterator } = await this._ddbStreams.getRecords(params)
if (NextShardIterator) {
shardData.nextShardIterator = NextShardIterator
} else {
shardData.nextShardIterator = null
}
return Records
} catch (e) {
if (e.name === 'ExpiredIteratorException') {
debug('_getShardRecords expired iterator', shardData)
shardData.nextShardIterator = null
} else {
throw e
}
}
}
_trimShards() {
debug('_trimShards')
const removedShards = []
for (const [shardId, shardData] of this._shards) {
if (shardData.nextShardIterator === null) {
debug('deleting shard %s', shardId)
this._shards.delete(shardId)
removedShards.push(shardId)
}
}
if (removedShards.length > 0) {
this._emitRemoveShardsEvent(removedShards)
}
}
/**
* you may override in subclasses to change record transformation behavior
* for records emitted during _emitRecordEvents()
*/
_transformRecord(record) {
if (this._unmarshall && record) {
return this._unmarshall(record)
}
}
_emitRecordEvents(events) {
debug('_emitRecordEvents')
for (const event of events) {
const keys = this._transformRecord(event.dynamodb.Keys)
const newRecord = this._transformRecord(event.dynamodb.NewImage)
const oldRecord = this._transformRecord(event.dynamodb.OldImage)
switch (event.eventName) {
case 'INSERT':
this.emit('insert record', newRecord, keys)
break
case 'MODIFY':
this.emit('modify record', newRecord, oldRecord, keys)
break
case 'REMOVE':
this.emit('remove record', oldRecord, keys)
break
default:
throw new Error(`unknown dynamodb event ${event.eventName}`)
}
}
}
_emitRemoveShardsEvent(shardIds) {
this.emit('remove shards', shardIds)
}
_emitNewShardsEvent(shardIds) {
this.emit('new shards', shardIds)
}
}
module.exports = DynamoDBStream