fix: remove open execution record caps and preserve recovery history
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// A page is a view over retained records, never a storage or execution ceiling.
|
||||
const invalid = (message, status = 400) => Object.assign(new Error(message), { status });
|
||||
export function recordPageQuery(url) {
|
||||
const params = url.searchParams;
|
||||
if ([...params.keys()].some(key => !['cursor', 'limit'].includes(key))
|
||||
|| ['cursor', 'limit'].some(key => params.getAll(key).length > 1)) throw invalid('记录分页参数无效。');
|
||||
if (!params.size) return null;
|
||||
return { cursor: params.get('cursor'), limit: params.get('limit') ?? 50 };
|
||||
}
|
||||
export function recordPage(records, { cursor = null, limit = 50 } = {}, scope) {
|
||||
if (!/^\d+$/.test(String(limit)) || !Number.isSafeInteger(Number(limit)) || Number(limit) < 1) throw invalid('记录每页数量需要正整数。');
|
||||
let start = 0;
|
||||
if (cursor !== null) {
|
||||
let value;
|
||||
try {
|
||||
if (typeof cursor !== 'string' || !/^[A-Za-z0-9_-]+$/.test(cursor)) throw new Error();
|
||||
value = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
|
||||
} catch { throw invalid('记录游标无效,请刷新记录。'); }
|
||||
if (value?.scope !== scope || typeof value.id !== 'string') throw invalid('记录游标不属于当前范围。');
|
||||
const index = records.findIndex(record => record.id === value.id);
|
||||
if (index < 0) throw invalid('原记录游标已变化,请刷新记录。', 409);
|
||||
start = index + 1;
|
||||
}
|
||||
const items = records.slice(start, start + Number(limit)), hasMore = start + items.length < records.length;
|
||||
const nextCursor = hasMore ? Buffer.from(JSON.stringify({ scope, id: items.at(-1).id })).toString('base64url') : null;
|
||||
return { items, total: records.length, hasMore, nextCursor };
|
||||
}
|
||||
Reference in New Issue
Block a user