diff --git a/src/index.ts b/src/index.ts index 0374a0e..1ba7b36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); - 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, @@ -187,12 +192,14 @@ 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( @@ -200,18 +207,20 @@ export class ProoflineClient extends Operations { ); 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, diff --git a/tests/http.test.mjs b/tests/http.test.mjs index 0a189be..8a845a6 100644 --- a/tests/http.test.mjs +++ b/tests/http.test.mjs @@ -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: [] }, }); }); @@ -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)),