Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch all shards by paginating results #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,32 @@ class DynamoDBStream extends EventEmitter {
StreamArn: this._streamArn
}

const { StreamDescription } = await this._ddbStreams.describeStream(params)

const shards = StreamDescription.Shards
const newShardIds = []
let lastShardId = null

do {
if (lastShardId) {
debug('lastShardId: %s', lastShardId)
params.ExclusiveStartShardId = lastShardId
}
const { StreamDescription } = await this._ddbStreams.describeStream(params)

// collect all the new shards of this stream
for (const newShardEntry of shards) {
const existingShardEntry = this._shards.get(newShardEntry.ShardId)
const shards = StreamDescription.Shards
lastShardId = StreamDescription.LastEvaluatedShardId

if (!existingShardEntry) {
this._shards.set(newShardEntry.ShardId, {
shardId: newShardEntry.ShardId
})
// collect all the new shards of this stream
for (const newShardEntry of shards) {
const existingShardEntry = this._shards.get(newShardEntry.ShardId)

newShardIds.push(newShardEntry.ShardId)
if (!existingShardEntry) {
this._shards.set(newShardEntry.ShardId, {
shardId: newShardEntry.ShardId
})

newShardIds.push(newShardEntry.ShardId)
}
}
}
} while (lastShardId)

if (newShardIds.length > 0) {
debug('Added %d new shards', newShardIds.length)
Expand Down