presidium 0.15.16 → 0.15.20

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 CHANGED
@@ -58,7 +58,7 @@ 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 ?? 30000
61
+ this.shardUpdatePeriod = options.shardUpdatePeriod ?? 15000
62
62
  this.listStreamsLimit = options.listStreamsLimit ?? 100
63
63
  this.client = new DynamoDBStreams({
64
64
  apiVersion: '2012-08-10',
@@ -89,14 +89,18 @@ DynamoStream.prototype.getStreams = async function* getStreams() {
89
89
  Limit: this.listStreamsLimit,
90
90
  TableName: this.table
91
91
  }).promise()
92
- yield* streams.Streams
92
+ if (streams.Streams.length > 0) {
93
+ yield* streams.Streams
94
+ }
93
95
  while (!this.closed && streams.LastEvaluatedStreamArn != null) {
94
96
  streams = await this.client.listStreams({
95
97
  Limit: this.listStreamsLimit,
96
98
  TableName: this.table,
97
99
  ExclusiveStartStreamArn: streams.LastEvaluatedStreamArn,
98
100
  }).promise()
99
- yield* streams.Streams
101
+ if (streams.Streams.length > 0) {
102
+ yield* streams.Streams
103
+ }
100
104
  }
101
105
  }
102
106
 
@@ -108,16 +112,18 @@ DynamoStream.prototype.getShards = async function* getShards(
108
112
  StreamArn: Stream.StreamArn,
109
113
  Limit: 100,
110
114
  }).promise().then(get('StreamDescription'))
111
- yield* shards.Shards.map(
112
- (Shard, ShardNumber) => ({ ...Shard, Stream, ShardNumber }))
115
+ if (shards.Shards.length > 0) {
116
+ yield* shards.Shards.map(assign({ Stream: always(Stream) }))
117
+ }
113
118
  while (!this.closed && shards.LastEvaluatedShardId != null) {
114
119
  shards = await this.client.describeStream({
115
120
  StreamArn: Stream.StreamArn,
116
121
  Limit: 100,
117
122
  ExclusiveStartShardId: shards.LastEvaluatedShardId,
118
123
  }).promise().then(get('StreamDescription'))
119
- yield* shards.Shards.map(
120
- (Shard, ShardNumber) => ({ ...Shard, Stream, ShardNumber }))
124
+ if (shards.Shards.length > 0) {
125
+ yield* shards.Shards.map(assign({ Stream: always(Stream) }))
126
+ }
121
127
  }
122
128
  }
123
129
 
@@ -129,25 +135,37 @@ DynamoStream.prototype.getRecords = async function* getRecords(
129
135
  ShardId: Shard.ShardId,
130
136
  StreamArn: Shard.Stream.StreamArn,
131
137
  ShardIteratorType: Shard.ShardIteratorType,
138
+
139
+ /*
132
140
  ...(
133
- this.shardIteratorType == 'AFTER_SEQUENCE_NUMBER'
134
- || this.shardIteratorType == 'AT_SEQUENCE_NUMBER'
135
- ) ? {
136
- SequenceNumber: Shard.SequenceNumberRange.StartingSequenceNumber,
137
- } : {},
141
+ Shard.ShardIteratorType == 'AFTER_SEQUENCE_NUMBER'
142
+ || Shard.ShardIteratorType == 'AT_SEQUENCE_NUMBER'
143
+ ) ? { SequenceNumber: Shard.SequenceNumber } : {},
144
+ */
145
+
138
146
  }).promise().then(get('ShardIterator'))
139
147
  let records = await this.client.getRecords({
140
148
  ShardIterator: startingShardIterator,
141
149
  Limit: this.getRecordsLimit
142
150
  }).promise()
143
151
 
144
- yield* records.Records
152
+ if (records.Records.length > 0) {
153
+ yield* records.Records.map(assign({
154
+ table: always(this.table),
155
+ shardId: always(Shard.ShardId),
156
+ }))
157
+ }
145
158
  while (!this.closed && records.NextShardIterator != null) {
146
159
  records = await this.client.getRecords({
147
160
  ShardIterator: records.NextShardIterator,
148
161
  Limit: this.getRecordsLimit
149
162
  }).promise()
150
- yield* records.Records
163
+ if (records.Records.length > 0) {
164
+ yield* records.Records.map(assign({
165
+ table: always(this.table),
166
+ shardId: always(Shard.ShardId),
167
+ }))
168
+ }
151
169
  await new Promise(resolve => setTimeout(resolve, this.getRecordsInterval))
152
170
  }
153
171
  }
@@ -156,46 +174,55 @@ const SymbolUpdateShards = Symbol('UpdateShards')
156
174
 
157
175
  DynamoStream.prototype[Symbol.asyncIterator] = async function* () {
158
176
  let shards = await pipe([
159
- transform(map(identity), []),
177
+ always(this.getStreams()),
160
178
  flatMap(Stream => this.getShards(Stream)),
161
- map(assign({ ShardIteratorType: always(this.shardIteratorType) })),
162
- ])(this.getStreams())
163
- let muxAsyncIterator = Mux.race(shards.map(Shard => this.getRecords(Shard)))
164
- let iterationPromise = muxAsyncIterator.next()
165
- let shardUpdatePromise = new Promise(resolve => setTimeout(
166
- thunkify(resolve, SymbolUpdateShards), this.shardUpdatePeriod))
179
+ map(assign({
180
+ ShardIteratorType: always(this.shardIteratorType),
181
+ })),
182
+ transform(map(identity), []),
183
+ ])()
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
+ ])
167
195
 
168
196
  while (!this.closed) {
169
- const iteration = await Promise.race([
170
- shardUpdatePromise,
171
- iterationPromise,
172
- ])
173
- if (iteration == SymbolUpdateShards) {
197
+ const { value, done } = await muxAsyncIterator.next()
198
+ if (value == SymbolUpdateShards) {
174
199
  const latestShards = await pipe([
175
- transform(map(identity), []),
200
+ always(this.getStreams()),
176
201
  flatMap(Stream => this.getShards(Stream)),
177
- ])(this.getStreams())
202
+ transform(map(identity), []),
203
+ ])()
178
204
  const newShards = pipe([
205
+ always(shards),
179
206
  differenceWith(
180
207
  (ShardA, ShardB) => ShardA.ShardId == ShardB.ShardId,
181
208
  latestShards,
182
209
  ),
183
- map(assign({ ShardIteratorType: always('TRIM_HORIZON') })),
184
- ])(shards)
210
+ map(assign({
211
+ ShardIteratorType: always('TRIM_HORIZON'),
212
+ })),
213
+ ])()
185
214
 
186
215
  shards = latestShards
187
- muxAsyncIterator = newShards.length == 0 ? muxAsyncIterator : Mux.race([
188
- ...newShards.map(Shard => this.getRecords(Shard)),
189
- muxAsyncIterator,
190
- ])
191
- shardUpdatePromise = new Promise(resolve => setTimeout(
192
- thunkify(resolve, SymbolUpdateShards), this.shardUpdatePeriod))
193
- } else if (iteration.done) {
216
+ if (newShards.length > 0) {
217
+ muxAsyncIterator = Mux.race([
218
+ ...newShards.map(Shard => this.getRecords(Shard)),
219
+ muxAsyncIterator,
220
+ ])
221
+ }
222
+ } else if (done) {
194
223
  await new Promise(resolve => setTimeout(resolve, 1000))
195
- iterationPromise = muxAsyncIterator.next()
196
224
  } else {
197
- yield iteration.value
198
- iterationPromise = muxAsyncIterator.next()
225
+ yield value
199
226
  }
200
227
  }
201
228
  }
@@ -223,52 +223,6 @@ const test = Test('DynamoStream', DynamoStream)
223
223
  myStream.close()
224
224
  })
225
225
 
226
- .case({
227
- table: 'my-table',
228
- endpoint: 'http://localhost:8000',
229
- listStreamsLimit: 1,
230
- shardIteratorType: 'AFTER_SEQUENCE_NUMBER',
231
- debug: true,
232
- }, async function (myStream) {
233
- await myStream.ready
234
-
235
- const table = this.table
236
- await table.putItem({
237
- id: '1',
238
- status: 'waitlist',
239
- createTime: 1000,
240
- name: 'George',
241
- })
242
- await table.putItem({
243
- id: '2',
244
- status: 'waitlist',
245
- createTime: 1001,
246
- name: 'geo',
247
- })
248
- await table.putItem({
249
- id: '3',
250
- status: 'waitlist',
251
- createTime: 1002,
252
- name: 'john',
253
- })
254
- await table.putItem({
255
- id: '4',
256
- status: 'approved',
257
- createTime: 1003,
258
- name: 'sally',
259
- })
260
- await table.putItem({
261
- id: '5',
262
- status: 'approved',
263
- createTime: 1004,
264
- name: 'sally',
265
- })
266
-
267
- const first5 = await asyncIterableTake(5)(myStream)
268
- assert.strictEqual(first5.length, 5)
269
- myStream.close()
270
- })
271
-
272
226
  .case({
273
227
  table: 'my-table',
274
228
  endpoint: 'http://localhost:8000',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "presidium",
3
- "version": "0.15.16",
3
+ "version": "0.15.20",
4
4
  "description": "A library for creating web services",
5
5
  "author": "Richard Tong",
6
6
  "license": "MIT",