app

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

clock-policy.ts (1039B)


      1 // Explicit prototype display policy; protocol order and signed timestamps stay
      2 // unchanged. A caller supplies wall-clock seconds, independently of the
      3 // monotonic clock used for bounded request deadlines.
      4 export const FUTURE_QUARANTINE_SECONDS = 300;
      5 export type WallClock = Readonly<{ nowSeconds: () => number }>;
      6 export type FutureAssessment =
      7   'within_policy' | 'future_quarantined' | 'clock_unavailable';
      8 export function wallClockAvailable(value: number): boolean {
      9   return (
     10     Number.isFinite(value) && value >= 0 && value <= Number.MAX_SAFE_INTEGER
     11   );
     12 }
     13 export function assessFutureTimestamp(
     14   createdAt: number,
     15   nowSeconds: number
     16 ): FutureAssessment {
     17   if (!wallClockAvailable(nowSeconds)) return 'clock_unavailable';
     18   if (!Number.isSafeInteger(createdAt) || createdAt < 0)
     19     throw new Error('food_head_timestamp_invalid');
     20   // Subtract rather than adding skew to a potentially maximum-safe timestamp.
     21   return createdAt - nowSeconds > FUTURE_QUARANTINE_SECONDS
     22     ? 'future_quarantined'
     23     : 'within_policy';
     24 }