presidium 0.15.18 → 0.15.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DynamoStream.js +49 -37
- package/package.json +1 -1
package/DynamoStream.js
CHANGED
|
@@ -58,9 +58,8 @@ const DynamoStream = function (options) {
|
|
|
58
58
|
this.getRecordsLimit = options.getRecordsLimit ?? 1000
|
|
59
59
|
this.getRecordsInterval = options.getRecordsInterval ?? 1000
|
|
60
60
|
this.shardIteratorType = options.shardIteratorType ?? 'LATEST'
|
|
61
|
-
this.shardUpdatePeriod = options.shardUpdatePeriod ??
|
|
61
|
+
this.shardUpdatePeriod = options.shardUpdatePeriod ?? 15000
|
|
62
62
|
this.listStreamsLimit = options.listStreamsLimit ?? 100
|
|
63
|
-
this.debug = options.debug ?? false
|
|
64
63
|
this.client = new DynamoDBStreams({
|
|
65
64
|
apiVersion: '2012-08-10',
|
|
66
65
|
accessKeyId: 'id',
|
|
@@ -90,14 +89,18 @@ DynamoStream.prototype.getStreams = async function* getStreams() {
|
|
|
90
89
|
Limit: this.listStreamsLimit,
|
|
91
90
|
TableName: this.table
|
|
92
91
|
}).promise()
|
|
93
|
-
|
|
92
|
+
if (streams.Streams.length > 0) {
|
|
93
|
+
yield* streams.Streams
|
|
94
|
+
}
|
|
94
95
|
while (!this.closed && streams.LastEvaluatedStreamArn != null) {
|
|
95
96
|
streams = await this.client.listStreams({
|
|
96
97
|
Limit: this.listStreamsLimit,
|
|
97
98
|
TableName: this.table,
|
|
98
99
|
ExclusiveStartStreamArn: streams.LastEvaluatedStreamArn,
|
|
99
100
|
}).promise()
|
|
100
|
-
|
|
101
|
+
if (streams.Streams.length > 0) {
|
|
102
|
+
yield* streams.Streams
|
|
103
|
+
}
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
|
|
@@ -109,16 +112,18 @@ DynamoStream.prototype.getShards = async function* getShards(
|
|
|
109
112
|
StreamArn: Stream.StreamArn,
|
|
110
113
|
Limit: 100,
|
|
111
114
|
}).promise().then(get('StreamDescription'))
|
|
112
|
-
|
|
113
|
-
|
|
115
|
+
if (shards.Shards.length > 0) {
|
|
116
|
+
yield* shards.Shards.map(assign({ Stream: always(Stream) }))
|
|
117
|
+
}
|
|
114
118
|
while (!this.closed && shards.LastEvaluatedShardId != null) {
|
|
115
119
|
shards = await this.client.describeStream({
|
|
116
120
|
StreamArn: Stream.StreamArn,
|
|
117
121
|
Limit: 100,
|
|
118
122
|
ExclusiveStartShardId: shards.LastEvaluatedShardId,
|
|
119
123
|
}).promise().then(get('StreamDescription'))
|
|
120
|
-
|
|
121
|
-
|
|
124
|
+
if (shards.Shards.length > 0) {
|
|
125
|
+
yield* shards.Shards.map(assign({ Stream: always(Stream) }))
|
|
126
|
+
}
|
|
122
127
|
}
|
|
123
128
|
}
|
|
124
129
|
|
|
@@ -144,13 +149,23 @@ DynamoStream.prototype.getRecords = async function* getRecords(
|
|
|
144
149
|
Limit: this.getRecordsLimit
|
|
145
150
|
}).promise()
|
|
146
151
|
|
|
147
|
-
|
|
152
|
+
if (records.Records.length > 0) {
|
|
153
|
+
yield* records.Records.map(assign({
|
|
154
|
+
table: always(this.table),
|
|
155
|
+
shardId: always(Shard.ShardId),
|
|
156
|
+
}))
|
|
157
|
+
}
|
|
148
158
|
while (!this.closed && records.NextShardIterator != null) {
|
|
149
159
|
records = await this.client.getRecords({
|
|
150
160
|
ShardIterator: records.NextShardIterator,
|
|
151
161
|
Limit: this.getRecordsLimit
|
|
152
162
|
}).promise()
|
|
153
|
-
|
|
163
|
+
if (records.Records.length > 0) {
|
|
164
|
+
yield* records.Records.map(assign({
|
|
165
|
+
table: always(this.table),
|
|
166
|
+
shardId: always(Shard.ShardId),
|
|
167
|
+
}))
|
|
168
|
+
}
|
|
154
169
|
await new Promise(resolve => setTimeout(resolve, this.getRecordsInterval))
|
|
155
170
|
}
|
|
156
171
|
}
|
|
@@ -166,21 +181,21 @@ DynamoStream.prototype[Symbol.asyncIterator] = async function* () {
|
|
|
166
181
|
})),
|
|
167
182
|
transform(map(identity), []),
|
|
168
183
|
])()
|
|
169
|
-
let muxAsyncIterator = Mux.race(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
184
|
+
let muxAsyncIterator = Mux.race([
|
|
185
|
+
...shards.map(Shard => this.getRecords(Shard)),
|
|
186
|
+
(async function* UpdateShardsGenerator() {
|
|
187
|
+
while (true) {
|
|
188
|
+
await new Promise(resolve => {
|
|
189
|
+
setTimeout(resolve, this.shardUpdatePeriod)
|
|
190
|
+
})
|
|
191
|
+
yield SymbolUpdateShards
|
|
192
|
+
}
|
|
193
|
+
}).call(this),
|
|
194
|
+
])
|
|
177
195
|
|
|
178
196
|
while (!this.closed) {
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
iterationPromise,
|
|
182
|
-
])
|
|
183
|
-
if (iteration == SymbolUpdateShards) {
|
|
197
|
+
const { value, done } = await muxAsyncIterator.next()
|
|
198
|
+
if (value == SymbolUpdateShards) {
|
|
184
199
|
const latestShards = await pipe([
|
|
185
200
|
always(this.getStreams()),
|
|
186
201
|
flatMap(Stream => this.getShards(Stream)),
|
|
@@ -197,23 +212,20 @@ DynamoStream.prototype[Symbol.asyncIterator] = async function* () {
|
|
|
197
212
|
})),
|
|
198
213
|
])()
|
|
199
214
|
|
|
200
|
-
if (this.debug) {
|
|
201
|
-
console.log('Latest shards:', latestShards.map(get('ShardId')))
|
|
202
|
-
}
|
|
203
|
-
|
|
204
215
|
shards = latestShards
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
216
|
+
if (newShards.length > 0) {
|
|
217
|
+
newShards.forEach(Shard => {
|
|
218
|
+
console.log('New shard:', Shard.ShardId, new Date().toLocaleString())
|
|
219
|
+
})
|
|
220
|
+
muxAsyncIterator = Mux.race([
|
|
221
|
+
...newShards.map(Shard => this.getRecords(Shard)),
|
|
222
|
+
muxAsyncIterator,
|
|
223
|
+
])
|
|
224
|
+
}
|
|
225
|
+
} else if (done) {
|
|
212
226
|
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
213
|
-
iterationPromise = muxAsyncIterator.next()
|
|
214
227
|
} else {
|
|
215
|
-
yield
|
|
216
|
-
iterationPromise = muxAsyncIterator.next()
|
|
228
|
+
yield value
|
|
217
229
|
}
|
|
218
230
|
}
|
|
219
231
|
}
|