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
8 changes: 2 additions & 6 deletions packages/sqlite-shared/src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ export const decodeCursor = (cursor: string): DecodedCursor => {
throw new StackQueryError(`Invalid cursor: malformed "${cursor}"`);
}
const parts = decoded.split('|');
// Three parts: field|value|id. Two parts: value|id, implying createdAt.
const [field, value, id] =
parts.length === 3
? (parts as [string, string, string])
: (['createdAt', ...parts] as [string, string, string]);
if (field === undefined || value === undefined || id === undefined) {
if (parts.length !== 3) {
throw new StackQueryError(`Invalid cursor: malformed "${cursor}"`);
}
const [field, value, id] = parts as [string, string, string];
if (!SORT_FIELDS.includes(field as SortField)) {
throw new StackQueryError(`Invalid cursor: unknown sort field "${field}"`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sqlite-shared/tests/cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ describe('encodeCursor / decodeCursor', () => {
expect(decodeCursor(cursor)).toEqual({ field: 'createdAt', value: 12345, id: 'rec01' });
});

test('accepts the legacy 2-part (value|id) format as createdAt', () => {
test('throws StackQueryError for a 2-part (value|id) cursor', () => {
const legacy = btoa('12345|rec01');
expect(decodeCursor(legacy)).toEqual({ field: 'createdAt', value: 12345, id: 'rec01' });
expect(() => decodeCursor(legacy)).toThrow(StackQueryError);
});

test('throws StackQueryError for non-base64 garbage', () => {
Expand Down
Loading