app

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

inbox-probe-publication.ts (5467B)


      1 import { finalize } from 'rxjs';
      2 export { makeFixture as makeOrdinaryPublicationFixture } from './private-publisher.ts';
      3 import { makeProbeFixture } from './inbox-probe.ts';
      4 import { RelayPool } from 'applesauce-relay/pool';
      5 import {
      6   getPrivatePool,
      7   publishInboxProbeArchiveAttempt,
      8   closePrivatePool,
      9   type PrivatePool,
     10   type InboxProbeArchiveAttemptResult
     11 } from '../../../src/lib/nostr/private-pool.ts';
     12 import {
     13   captureInboxProbePublicationAction,
     14   captureInboxProbePublication,
     15   type InboxProbePublicationAction
     16 } from '../../../src/lib/nostr/private-publisher.ts';
     17 import {
     18   inboxProbeAcknowledgementSnapshot,
     19   commitInboxProbeArchiveAttempt
     20 } from '../../../src/lib/persistence/inbox-probes.ts';
     21 export * from './inbox-probe.ts';
     22 export * from '../../../src/lib/nostr/private-publisher.ts';
     23 export * from '../../../src/lib/nostr/private-pool.ts';
     24 export async function makePublicationFixture() {
     25   const f = await makeProbeFixture();
     26   let pool: PrivatePool | undefined,
     27     last: InboxProbeArchiveAttemptResult | undefined;
     28   let action: InboxProbePublicationAction | undefined;
     29   const consumer = Object.freeze({});
     30   function capture() {
     31     const acknowledgement = f.acknowledgement();
     32     if (!acknowledgement) return undefined;
     33     const next = captureInboxProbePublicationAction(
     34       f.repository,
     35       f.session,
     36       f.policy,
     37       acknowledgement,
     38       'reviewed_inbox_probe_network'
     39     );
     40     action ??= next;
     41     return next;
     42   }
     43   function permission(
     44     origin = 'wss://inbox.example.org',
     45     review: unknown = 'reviewed_inbox_probe_publication'
     46   ) {
     47     const acknowledgement = f.acknowledgement(),
     48       original = capture();
     49     return acknowledgement && original
     50       ? captureInboxProbePublication(
     51           f.repository,
     52           f.session,
     53           f.policy,
     54           acknowledgement,
     55           original,
     56           origin,
     57           review
     58         )
     59       : undefined;
     60   }
     61   function acquire() {
     62     pool ??= getPrivatePool(
     63       f.session,
     64       f.policy,
     65       f.acknowledgement()
     66         ? inboxProbeAcknowledgementSnapshot(f.acknowledgement()!)!.record
     67             .binding.archiveTargets
     68         : ['wss://inbox.example.org'],
     69       consumer,
     70       'delivery'
     71     );
     72     if (!pool) throw Error('actual source delivery scope unavailable');
     73     return pool;
     74   }
     75   return {
     76     ...f,
     77     action: capture,
     78     permission,
     79     pool: acquire,
     80     snapshot: () =>
     81       f.acknowledgement() &&
     82       inboxProbeAcknowledgementSnapshot(f.acknowledgement()!),
     83     async publish(token = permission(), signal = new AbortController().signal) {
     84       if (!token) return { status: 'stopped' as const };
     85       last = await publishInboxProbeArchiveAttempt(acquire(), token, signal);
     86       return last;
     87     },
     88     last: () => last,
     89     append: (result = last) =>
     90       result
     91         ? commitInboxProbeArchiveAttempt(f.repository, result)
     92         : Promise.resolve({ status: 'invalid' as const }),
     93     installNetworkClock() {
     94       const original = performance.now.bind(performance);
     95       let now = original(),
     96         observations = 0;
     97       // Only the actual shared publication meter samples this controlled clock.
     98       // Public discovery/head/lease lifetimes and Date.now remain real.
     99       performance.now = () => {
    100         if (/at clock(?:\d+)? \(/.test(new Error().stack ?? '')) {
    101           observations++;
    102           return now;
    103         }
    104         return original();
    105       };
    106       return {
    107         set: (value: number) => {
    108           now = value;
    109         },
    110         advance: (value: number) => {
    111           now += value;
    112         },
    113         observations: () => observations,
    114         restore: () => {
    115           performance.now = original;
    116         }
    117       };
    118     },
    119     installAttemptTeardownFailure(onTeardown?: () => void) {
    120       const original = Object.getOwnPropertyDescriptor(
    121         RelayPool.prototype,
    122         'relay'
    123       )?.value as RelayPool['relay'];
    124       const restorations: (() => void)[] = [];
    125       let failing = true,
    126         faults = 0;
    127       RelayPool.prototype.relay = function (
    128         ...args: Parameters<RelayPool['relay']>
    129       ) {
    130         const relay = original.apply(this, args),
    131           event = relay.event.bind(relay);
    132         relay.event = (...eventArgs: Parameters<typeof relay.event>) =>
    133           event(...eventArgs).pipe(
    134             finalize(() => {
    135               if (failing) {
    136                 onTeardown?.();
    137                 faults++;
    138                 throw Error(
    139                   'controlled original SDK observable teardown failure'
    140                 );
    141               }
    142             })
    143           );
    144         restorations.push(() => {
    145           relay.event = event;
    146         });
    147         return relay;
    148       };
    149       return {
    150         faults: () => faults,
    151         restore: () => {
    152           failing = false;
    153           RelayPool.prototype.relay = original;
    154           for (const restoration of restorations) restoration();
    155         }
    156       };
    157     },
    158     installAcquisitionFailure() {
    159       const original = Object.getOwnPropertyDescriptor(
    160         RelayPool.prototype,
    161         'relay'
    162       )?.value as RelayPool['relay'];
    163       RelayPool.prototype.relay = () => {
    164         throw Error('controlled actual SDK metadata acquisition failure');
    165       };
    166       return () => {
    167         RelayPool.prototype.relay = original;
    168       };
    169     },
    170     stop: () => {
    171       if (pool) closePrivatePool(pool);
    172     },
    173     close() {
    174       if (pool) closePrivatePool(pool);
    175       f.close();
    176     }
    177   };
    178 }