chronological-query.ts (1403B)
1 import { PUBLIC_QUERY_BUDGETS } from '../config/budgets.ts'; 2 import type { PublicFilter } from './exports.ts'; 3 4 // NIP-01 until is inclusive. Never invent a cursor by subtracting a second. 5 export function chronologicalSearchQuery( 6 until?: number 7 ): readonly PublicFilter[] { 8 if (until !== undefined && (!Number.isSafeInteger(until) || until < 0)) 9 throw new Error('chronological_boundary_invalid'); 10 return [ 11 { 12 kinds: [30402], 13 limit: PUBLIC_QUERY_BUDGETS.requestedPerRelay, 14 ...(until === undefined ? {} : { until }) 15 } 16 ]; 17 } 18 // The coordinator accepts only its own observed boundary, never caller offsets 19 // or a keyword-constrained chronological request. Return an independent copy. 20 export function requireChronologicalSearchQuery( 21 filters: readonly PublicFilter[], 22 until?: number 23 ): readonly PublicFilter[] { 24 const expected = chronologicalSearchQuery(until), 25 filter = filters[0]; 26 if ( 27 !Array.isArray(filters) || 28 filters.length !== 1 || 29 !filter || 30 'search' in filter || 31 'q' in filter || 32 'since' in filter || 33 'authors' in filter || 34 'ids' in filter || 35 !Array.isArray(filter.kinds) || 36 filter.kinds.length !== 1 || 37 filter.kinds[0] !== 30402 || 38 filter.limit !== PUBLIC_QUERY_BUDGETS.requestedPerRelay || 39 filter.until !== until 40 ) 41 throw new Error('food_search_chronological_filter'); 42 return expected; 43 }