Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,13 @@ export class ProoflineClient extends Operations {
const maxItems = integerLimit(options.maxItems ?? 10_000, "maxItems");
const initialQuery = { ...parameters.query };
const seen = new Set<string>();
let count = 0;
let cursor = initialQuery.cursor;

if (typeof cursor === "string") {
seen.add(cursor);
}
let count = 0;

for (let index = 0; index < maxPages; index += 1) {
const query = {
...initialQuery,
Expand All @@ -187,31 +192,35 @@ export class ProoflineClient extends Operations {
typeof page.next_cursor !== "string")
)
throw protocol("the review page has no typed collection metadata");

for (const item of page.items) {
if (count >= maxItems)
throw paginationLimit("the review collection exceeds maxItems");
count += 1;
yield item;
}

if (!page.truncated) {
if (page.next_cursor)
throw protocol(
"the review page has an unexpected continuation cursor",
);
return;
}

if (
!page.next_cursor ||
page.next_cursor === cursor ||
seen.has(page.next_cursor)
)
throw protocol("the review collection has no advancing cursor");

seen.add(page.next_cursor);
cursor = page.next_cursor;
}

throw paginationLimit("the review collection exceeds maxPages");
}

async waitForReview(
orgId: string,
reviewId: string,
Expand Down
29 changes: 28 additions & 1 deletion tests/http.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,16 @@ test("pagination keeps filters and refuses cycles, incomplete metadata and resou
return json(response, 200, {
items: [{ id: query.get("repository") === repoId ? id : "other-repo" }],
truncated: more,
next_cursor: mode === "missing" ? null : more ? "next" : null,
next_cursor:
mode === "missing"
? null
: mode === "start-cycle"
? cursor === "resume"
? "next"
: "resume"
: more
? "next"
: null,
filters: { repositories: [], environments: [], authors: [] },
});
});
Expand Down Expand Up @@ -1596,6 +1605,24 @@ test("pagination keeps filters and refuses cycles, incomplete metadata and resou
Array.fromAsync(client.iterReviews(parameters)),
(error) => error.type === "urn:proofline:sdk:invalid-response",
);
mode = "start-cycle";
const callsBeforeStartCycle = fixture.calls.length;

await assert.rejects(
Array.fromAsync(
client.iterReviews({
...parameters,
query: { ...parameters.query, cursor: "resume" },
}),
),
(error) => error.type === "urn:proofline:sdk:invalid-response",
);

assert.equal(
fixture.calls.length - callsBeforeStartCycle,
2,
"should reject a cursor that returns to the starting cursor immediately",
);
mode = "missing";
await assert.rejects(
Array.fromAsync(client.iterReviews(parameters)),
Expand Down