Skip to content

Commit

Permalink
CB-5749 fix: prevent page offset overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Wroud committed Oct 11, 2024
1 parent b182c04 commit d90bd77
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function useOffsetPagination<TResource extends CachedMapResource<any, any
const resource = useService(ctor);
const pageInfo = resource.offsetPagination.getPageInfo(createPageKey(0, 0, targetKey));
const offset = Math.max(
(pageInfo ? getNextPageOffset(pageInfo) : CACHED_RESOURCE_DEFAULT_PAGE_OFFSET) - pageSize,
(pageInfo ? getNextPageOffset(pageInfo) : CACHED_RESOURCE_DEFAULT_PAGE_OFFSET) - (pageInfo?.end ?? pageSize),
CACHED_RESOURCE_DEFAULT_PAGE_OFFSET,
);

Expand All @@ -86,7 +86,7 @@ export function useOffsetPagination<TResource extends CachedMapResource<any, any
},
loadMore() {
if (this.hasNextPage) {
this._key = createPageKey(this._key.options.offset + this._key.options.limit, this._key.options.limit, this._target);
this._key = createPageKey(this.offset + this._key.options.limit, this._key.options.limit, this._target);
}
},
refresh() {
Expand All @@ -102,7 +102,7 @@ export function useOffsetPagination<TResource extends CachedMapResource<any, any
loadMore: action.bound,
refresh: action.bound,
},
{ resource },
{ resource, offset },
);

if (!resource.isIntersect(targetKey, pagination._target)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export class DBObjectResource extends CachedMapResource<string, DBObject> {

this.offsetPagination.setPage(
isPageListKey
? CachedResourceOffsetPageListKey(offset, limit).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId))
: CachedResourceOffsetPageKey(offset, limit).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId)),
? CachedResourceOffsetPageListKey(offset, keys.length).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId))
: CachedResourceOffsetPageKey(offset, keys.length).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId)),
keys,
keys.length === limit,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { describe, expect, test } from '@jest/globals';

import { expandOffsetPageRange } from './CachedResourceOffsetPageKeys.js';
import { expandOffsetPageRange, getNextPageOffset, type ICachedResourceOffsetPage } from './CachedResourceOffsetPageKeys.js';
import type { IResourceOffsetPage } from './OffsetPagination/IResourceOffsetPage.js';
import { ResourceOffsetPage } from './OffsetPagination/ResourceOffsetPage.js';

Expand Down Expand Up @@ -108,6 +108,49 @@ describe('CachedResourceOffsetPageKeys', () => {
expect(pages).toStrictEqual(initialPages);
});
});

describe('getNextPageOffset', () => {
test('should return next page offset', () => {
const randomPage = getRandomPage(0, 100, false);
const pageInfo: ICachedResourceOffsetPage = {
pages: [randomPage],
};
expect(getNextPageOffset(pageInfo)).toBe(100);
});
test('should return next page offset with multiple pages', () => {
const pages = [];
for (let i = 0; i < 10; i++) {
pages.push(getRandomPage(i * 100, 100, false));
}
const pageInfo: ICachedResourceOffsetPage = {
pages,
};
expect(getNextPageOffset(pageInfo)).toBe(1000);
});
test('should return next page offset with multiple pages with gaps', () => {
const pages = [];
for (let i = 0; i < 10; i++) {
pages.push(getRandomPage(i * 100, 100, false));
}
pages.push(getRandomPage(11 * 100, 100, false));
const pageInfo: ICachedResourceOffsetPage = {
pages,
};
expect(getNextPageOffset(pageInfo)).toBe(1000);
});
test('should return next page offset with end', () => {
const pages = [];
for (let i = 0; i < 10; i++) {
pages.push(getRandomPage(i * 100, 100, false));
}
pages.push(getRandomPage(10 * 100, 20, false));
const pageInfo: ICachedResourceOffsetPage = {
end: 1020,
pages,
};
expect(getNextPageOffset(pageInfo)).toBe(1020);
});
});
});

function getRandomPage(offset: number, limit: number, outdate: boolean): IResourceOffsetPage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function getNextPageOffset(info: ICachedResourceOffsetPage): number {
lastPage = page;
}

return lastPage?.to ?? CACHED_RESOURCE_DEFAULT_PAGE_OFFSET;
return Math.min(info.end ?? Number.MAX_SAFE_INTEGER, lastPage?.to ?? CACHED_RESOURCE_DEFAULT_PAGE_OFFSET);
}

export function isOffsetPageOutdated(pages: IResourceOffsetPage[], info: IOffsetPageInfo): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ export class ResourceOffsetPagination<TKey, TMetadata extends ICachedResourceMet

setPage(key: ResourceAlias<TKey, Readonly<ICachedResourceOffsetPageOptions>>, items: any[], hasNextPage: boolean) {
const offset = key.options.offset;
const limit = offset + key.options.limit;
const pageEnd = offset + items.length;

this.metadata.update(key as TKey, metadata => {
let end = metadata.offsetPage?.end;

if (hasNextPage) {
if (end !== undefined && end <= limit) {
if (end !== undefined && end <= pageEnd) {
end = undefined;
}
} else {
end = limit;
end = pageEnd;
}

if (!metadata.offsetPage) {
Expand Down

0 comments on commit d90bd77

Please sign in to comment.