app

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

inbox-state.ts (3239B)


      1 import {
      2   LOCAL_PERSISTENCE_BUDGETS,
      3   PRIVATE_TRANSPORT_BUDGETS,
      4   PRIVATE_DECRYPTION_BUDGETS
      5 } from '../config/budgets.ts';
      6 export type OuterFact = Readonly<{ outerId: string; outerTime: number }>;
      7 export type OuterCursor = Readonly<{
      8   source: string;
      9   until: number | undefined;
     10   seen: readonly string[];
     11   state: string;
     12   partial: boolean;
     13   historyComplete: false;
     14 }>;
     15 function facts(rows: readonly OuterFact[]) {
     16   for (const row of rows)
     17     if (
     18       !/^[0-9a-f]{64}$/.test(row.outerId) ||
     19       !Number.isSafeInteger(row.outerTime) ||
     20       row.outerTime < 0
     21     )
     22       throw Error('history_outer_metadata_invalid');
     23 }
     24 export function initialOuterCursor(
     25   source: string,
     26   rows: readonly OuterFact[]
     27 ): OuterCursor {
     28   facts(rows);
     29   const seen = rows
     30     .map((row) => row.outerId)
     31     .filter((id, index, all) => all.indexOf(id) === index);
     32   if (seen.length > LOCAL_PERSISTENCE_BUDGETS.receivedEnvelopes)
     33     throw Error('history_metadata_capacity');
     34   return {
     35     source,
     36     until: rows.length
     37       ? Math.min(...rows.map((row) => row.outerTime))
     38       : undefined,
     39     seen,
     40     state: 'older_available',
     41     partial: false,
     42     historyComplete: false
     43   };
     44 }
     45 export function advanceOuterPage(
     46   cursor: OuterCursor,
     47   rows: readonly OuterFact[],
     48   deliveries: number,
     49   reason: string
     50 ): OuterCursor {
     51   facts(rows);
     52   if (!Number.isSafeInteger(deliveries) || deliveries < 0)
     53     throw Error('history_delivery_count_invalid');
     54   let seen = cursor.seen.slice();
     55   let until = cursor.until,
     56     newIds = 0,
     57     outside = false;
     58   for (const row of rows) {
     59     if (cursor.until !== undefined && row.outerTime > cursor.until) {
     60       outside = true;
     61       continue;
     62     }
     63     if (!seen.includes(row.outerId)) {
     64       if (seen.length >= LOCAL_PERSISTENCE_BUDGETS.receivedEnvelopes)
     65         throw Error('history_metadata_capacity');
     66       seen = seen.concat(row.outerId);
     67       newIds++;
     68     }
     69     until =
     70       until === undefined ? row.outerTime : Math.min(until, row.outerTime);
     71   }
     72   const capped = deliveries >= PRIVATE_TRANSPORT_BUDGETS.requestedPerRelay;
     73   const partial = reason !== 'complete' || outside || rows.length < deliveries;
     74   const state =
     75     reason === 'budget'
     76       ? 'capped'
     77       : reason !== 'complete' || outside
     78         ? 'partial'
     79         : capped
     80           ? newIds === 0 && until === cursor.until
     81             ? 'saturated'
     82             : 'capped'
     83           : newIds > 0
     84             ? 'older_available'
     85             : 'window_exhausted';
     86   return {
     87     source: cursor.source,
     88     until,
     89     seen: seen.slice(),
     90     state,
     91     partial,
     92     historyComplete: false
     93   };
     94 }
     95 
     96 // A work plan is metadata, never an owner, unlock or SDK permission.
     97 export function planDecryptBatch(ids: readonly string[]) {
     98   if (ids.length > LOCAL_PERSISTENCE_BUDGETS.receivedEnvelopes)
     99     throw Error('decryption_queue_capacity');
    100   for (const id of ids)
    101     if (typeof id !== 'string' || !/^[0-9a-f]{64}$/.test(id))
    102       throw Error('decryption_queue_identity_invalid');
    103   const unique = ids.filter((id, index, all) => all.indexOf(id) === index);
    104   const selected = unique.slice(
    105     0,
    106     PRIVATE_DECRYPTION_BUDGETS.envelopesPerAction
    107   );
    108   return { selected, remaining: unique.length - selected.length };
    109 }