Skip to content

Commit

Permalink
fix: prevent infinite loop when fetching a list of results
Browse files Browse the repository at this point in the history
When fetching a list of results from cloudflare APIs (e.g. when fetching a list of keys in a kv namespace), the api returns a `cursor` that a consumer should use to get the next 'page' of results. It appears this cursor can also be a blank string (while we'd only account for it to be `undefined`). By only accounting for it to be `undefined`, we were infinitely looping through the same page of results and never terminating. This PR fixes it by letting it be a blank string (and `null`, for good measure)
  • Loading branch information
threepointone committed Jan 29, 2022
1 parent 9c10098 commit 59c2a92
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/brown-chefs-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: prevent infinite loop when fetching a list of results

When fetching a list of results from cloudflare APIs (e.g. when fetching a list of keys in a kv namespace), the api returns a `cursor` that a consumer should use to get the next 'page' of results. It appears this cursor can also be a blank string (while we'd only account for it to be `undefined`). By only accounting for it to be `undefined`, we were infinitely looping through the same page of results and never terminating. This PR fixes it by letting it be a blank string (and `null`, for good measure)
3 changes: 2 additions & 1 deletion packages/wrangler/src/cfetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,6 @@ function throwFetchError(
}

function hasCursor(result_info: unknown): result_info is { cursor: string } {
return (result_info as { cursor: string } | undefined)?.cursor !== undefined;
const cursor = (result_info as { cursor: string } | undefined)?.cursor;
return cursor !== undefined && cursor !== null && cursor !== "";
}

0 comments on commit 59c2a92

Please sign in to comment.