app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

product-queries.ts (1640B)


      1 import { PUBLIC_QUERY_BUDGETS } from '../config/budgets.ts';
      2 import {
      3   publicHeadKey,
      4   publicHeadSnapshot,
      5   type PublicHead
      6 } from '../catalog/heads.ts';
      7 import type { PublicFilter } from './exports.ts';
      8 
      9 export function headResolutionQueries(heads: readonly PublicHead[]): Readonly<{
     10   head: readonly PublicFilter[];
     11   deletion: readonly PublicFilter[];
     12 }> {
     13   if (heads.length > PUBLIC_QUERY_BUDGETS.coordinatesPerRun)
     14     throw new Error('head_query_input_limit');
     15   const coordinates = new Map<string, PublicHead>(),
     16     authors = new Map<string, true>();
     17   for (const head of heads) {
     18     const row = publicHeadSnapshot(head);
     19     if (row.kind !== 30402) throw new Error('head_query_kind_invalid');
     20     coordinates.set(publicHeadKey(head), head);
     21     authors.set(row.pubkey, true);
     22   }
     23   const limit = PUBLIC_QUERY_BUDGETS.requestedPerRelay;
     24   return {
     25     head: Array.from(coordinates.values(), (head) => {
     26       const row = publicHeadSnapshot(head);
     27       // A missing/malformed first d has the generic empty coordinate. An
     28       // author-only bounded query discovers these, with exact reduction local.
     29       return row.identifier === ''
     30         ? { kinds: [30402], authors: [row.pubkey], limit }
     31         : {
     32             kinds: [30402],
     33             authors: [row.pubkey],
     34             '#d': [row.identifier!],
     35             limit
     36           };
     37     }),
     38     // Author lookups also cover e-only requests for a newer, initially unknown
     39     // head ID. Advisory k and request time cannot restrict deletion authority.
     40     deletion: Array.from(authors, ([author]) => ({
     41       kinds: [5],
     42       authors: [author],
     43       limit
     44     }))
     45   };
     46 }