Skip to content
Merged
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
25 changes: 25 additions & 0 deletions templates/lib/utils/paginator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const lodashGet = require("lodash.get");
const crypto = require('crypto');

module.exports.createPaginator = function createPaginator(config) {
if (config.strategy.type === "cursor") {
Expand All @@ -14,9 +15,17 @@ module.exports.createPaginator = function createPaginator(config) {
class CursorPaginator {
constructor(config) {
this.config = config;
this.lastPageHash = "";
}

hasNextPage({ headers, body }) {
const hashedBody = crypto.createHash('sha256').update(JSON.stringify(body), 'utf8').digest('hex');
if (hashedBody === this.lastPageHash) {
console.warn("Received same data twice in a row, aborting pagination...")
return false;
}
this.lastPageHash = hashedBody;

return !!this.getNextPageToken({ headers, body });
}

Expand All @@ -31,9 +40,17 @@ class PageIncrementPaginator {

constructor(config) {
this.config = config;
this.lastPageHash = "";
}

hasNextPage({ body }) {
const hashedBody = crypto.createHash('sha256').update(JSON.stringify(body), 'utf8').digest('hex');
if (hashedBody === this.lastPageHash) {
console.warn("Received same data twice in a row, aborting pagination...")
return false;
}
this.lastPageHash = hashedBody;

const resultsPath = [];
if (this.config.strategy.resultsPath) {
resultsPath.push(...this.config.strategy.resultsPath.split('.'));
Expand All @@ -53,9 +70,17 @@ class OffsetIncrementPaginator {

constructor(config) {
this.config = config;
this.lastPageHash = "";
}

hasNextPage({ body }) {
const hashedBody = crypto.createHash('sha256').update(JSON.stringify(body), 'utf8').digest('hex');
if (hashedBody === this.lastPageHash) {
console.warn("Received same data twice in a row, aborting pagination...")
return false;
}
this.lastPageHash = hashedBody;

const resultsPath = [];
if (this.config.strategy.resultsPath) {
resultsPath.push(...this.config.strategy.resultsPath.split('.'));
Expand Down
101 changes: 100 additions & 1 deletion templates/lib/utils/paginator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ describe("Paginators", () => {
expect(paginator.hasNextPage(response)).toBeTruthy();
expect(paginator.getNextPageToken(response)).toBe("next-page-token");
});

it("should abort when receiving the same data twice in a row", () => {
const paginator = new CursorPaginator({
pageTokenOption: {
fieldName: "after",
},
strategy: {
type: "cursor",
tokenIn: "body",
nextCursorPath: "meta.next.token",
},
});

const response = {
headers: {},
body: {
data: ['abc', 'def'],
meta: {
next: {
token: "next-page-token",
},
},
},
};

expect(paginator.hasNextPage(response)).toBeTruthy();
expect(paginator.getNextPageToken(response)).toBe("next-page-token");

expect(paginator.hasNextPage(response)).toBeFalsy();
});
});

describe("PageIncrementPaginator", () => {
Expand Down Expand Up @@ -181,6 +211,40 @@ describe("Paginators", () => {
expect(paginator.getNextPageToken(response)).toBe(2);
expect(paginator.getNextPageToken(response)).toBe(3);
});

it("should abort when receiving the same data twice in a row", () => {
const paginator = createPaginator({
pageSizeOption: {
fieldName: "per_page",
},
pageTokenOption: {
fieldName: "page",
},
strategy: {
type: "page_increment",
pageSize: 2,
resultsPath: "results",
},
});

const response1 = {
body: {
results: [{ id: 1 }, { id: 2 }],
},
headers: {},
};

const response2 = {
body: {
results: [{ id: 3 }, { id: 4 }],
},
headers: {},
};

expect(paginator.hasNextPage(response1)).toBeTruthy();
expect(paginator.hasNextPage(response2)).toBeTruthy();
expect(paginator.hasNextPage(response2)).toBeFalsy();
});
});

describe("OffsetIncrementPaginator", () => {
Expand Down Expand Up @@ -229,7 +293,7 @@ describe("Paginators", () => {
const response = {
body: {
d: {
results: [{id: 1}, {id: 2}]
results: [{ id: 1 }, { id: 2 }]
},
},
headers: {},
Expand Down Expand Up @@ -288,6 +352,41 @@ describe("Paginators", () => {
expect(paginator.getNextPageToken(response)).toBe(2);
expect(paginator.getNextPageToken(response)).toBe(4);
});

it("should when receiving the same data twice in a row", () => {
const paginator = createPaginator({
pageSizeOption: {
fieldName: "per_page",
},
pageTokenOption: {
fieldName: "page",
},
strategy: {
type: "offset_increment",
pageSize: 2,
resultsPath: "results",
},
});

const response1 = {
body: {
results: [{ id: 1 }, { id: 2 }],
},
headers: {},
};

const response2 = {
body: {
results: [{ id: 3 }, { id: 4 }],
},
headers: {},
};

expect(paginator.hasNextPage(response1)).toBeTruthy();
expect(paginator.hasNextPage(response2)).toBeTruthy();
expect(paginator.hasNextPage(response2)).toBeFalsy();
});

});

describe("NoPagingPaginator", () => {
Expand Down