Skip to content

Commit

Permalink
Merge pull request #120 from dscho/make-getting-webhook-payload-safer
Browse files Browse the repository at this point in the history
get-webhook-event-payload: be more careful about missing `epoch`s
  • Loading branch information
dscho authored Feb 13, 2025
2 parents db2da3a + 0914c83 commit 92e1d90
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions get-webhook-event-payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@
if (answer.oldest.epoch > until) {
let tooNew = answer.oldest
// first find a good starting cursor
while (answer.oldest.epoch > until) {
while (answer.oldest?.epoch && answer.oldest.epoch > until) {
tooNew = answer.oldest

const rate = (answer.newest.id - answer.oldest.id) / (answer.newest.epoch - answer.oldest.epoch)
let cursor = Math.floor(answer.oldest.id - rate * (answer.oldest.epoch - until))
answer = await getAtCursor(cursor)
}

while (answer.newest.epoch < until) {
while (answer.newest?.epoch && answer.newest.epoch < until) {
const tooOldID = answer.newest.id
// we overshot, now the time window does not include `until`, backtrack via bisecting
const rate = (tooNew.id - answer.newest.id) / (tooNew.epoch - answer.newest.epoch)
Expand All @@ -101,14 +101,14 @@
}
}

while (answer.oldest.epoch > until) {
while (answer.oldest?.epoch && answer.oldest.epoch > until) {
// we overshot, maybe again, now even the oldest is too new
answer = await getAtCursor(answer.oldest.id - 1)
}
}

const events = [...answer.events]
while (answer.oldest.epoch > since) {
while (answer.oldest?.epoch && answer.oldest.epoch > since) {
answer = await getAtCursor(answer.oldest.id - 1)
events.push(...answer.events)
}
Expand Down

0 comments on commit 92e1d90

Please sign in to comment.