app

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

inbox-probe-readback.ts (4142B)


      1 import { finalize, tap, map } from 'rxjs';
      2 import { RelayPool } from 'applesauce-relay/pool';
      3 import { makePublicationFixture } from './inbox-probe-publication.ts';
      4 import { captureInboxProbeReadback } from '../../../src/lib/nostr/private-publisher.ts';
      5 import {
      6   getPrivatePool,
      7   readInboxProbeAttempt,
      8   closePrivatePool,
      9   type PrivatePool,
     10   type InboxProbeReadbackAttemptResult
     11 } from '../../../src/lib/nostr/private-pool.ts';
     12 import { commitInboxProbeReadbackAttempt } from '../../../src/lib/persistence/inbox-probes.ts';
     13 export * from './inbox-probe-publication.ts';
     14 export async function makeReadbackFixture() {
     15   const f = await makePublicationFixture();
     16   let pool: PrivatePool | undefined,
     17     last: InboxProbeReadbackAttemptResult | undefined;
     18   const consumer = Object.freeze({});
     19   function permission(
     20     origin = 'wss://inbox.example.org',
     21     review: unknown = 'reviewed_inbox_probe_readback'
     22   ) {
     23     const ack = f.acknowledgement(),
     24       action = f.action();
     25     return ack && action
     26       ? captureInboxProbeReadback(
     27           f.repository,
     28           f.session,
     29           f.policy,
     30           ack,
     31           action,
     32           origin,
     33           review
     34         )
     35       : undefined;
     36   }
     37   function acquire() {
     38     pool ??= getPrivatePool(
     39       f.session,
     40       f.policy,
     41       f.snapshot()!.record.binding.readTargets,
     42       consumer,
     43       'access'
     44     );
     45     if (!pool) throw Error('actual original access scope');
     46     return pool;
     47   }
     48   return {
     49     ...f,
     50     readPermission: permission,
     51     readPool: acquire,
     52     async read(token = permission(), signal = new AbortController().signal) {
     53       if (!token) return { status: 'stopped' as const };
     54       last = await readInboxProbeAttempt(acquire(), token, signal);
     55       return last;
     56     },
     57     lastRead: () => last,
     58     appendRead: (result = last) =>
     59       result
     60         ? commitInboxProbeReadbackAttempt(f.repository, result)
     61         : Promise.resolve({ status: 'invalid' as const }),
     62     installWrongReadSource() {
     63       const original = Object.getOwnPropertyDescriptor(
     64         RelayPool.prototype,
     65         'req'
     66       )?.value as RelayPool['req'];
     67       RelayPool.prototype.req = function (
     68         ...args: Parameters<RelayPool['req']>
     69       ) {
     70         return original
     71           .apply(this, args)
     72           .pipe(
     73             map((message) =>
     74               message.type === 'EVENT'
     75                 ? { ...message, from: 'wss://foreign.example.org' }
     76                 : message
     77             )
     78           );
     79       };
     80       return () => {
     81         RelayPool.prototype.req = original;
     82       };
     83     },
     84     installReadInstallation(onInstallation: () => void) {
     85       const original = Object.getOwnPropertyDescriptor(
     86         RelayPool.prototype,
     87         'req'
     88       )?.value as RelayPool['req'];
     89       let fired = false;
     90       RelayPool.prototype.req = function (
     91         ...args: Parameters<RelayPool['req']>
     92       ) {
     93         return original.apply(this, args).pipe(
     94           tap((message) => {
     95             if (!fired && message.type === 'OPEN') {
     96               fired = true;
     97               onInstallation();
     98             }
     99           })
    100         );
    101       };
    102       return () => {
    103         RelayPool.prototype.req = original;
    104       };
    105     },
    106     installReadTeardownFailure(onTeardown?: () => void) {
    107       const original = Object.getOwnPropertyDescriptor(
    108         RelayPool.prototype,
    109         'req'
    110       )?.value as RelayPool['req'];
    111       let failing = true,
    112         faults = 0;
    113       RelayPool.prototype.req = function (
    114         ...args: Parameters<RelayPool['req']>
    115       ) {
    116         return original.apply(this, args).pipe(
    117           finalize(() => {
    118             if (failing) {
    119               faults++;
    120               onTeardown?.();
    121               throw Error('actual original REQ teardown failure');
    122             }
    123           })
    124         );
    125       };
    126       return {
    127         faults: () => faults,
    128         restore: () => {
    129           failing = false;
    130           RelayPool.prototype.req = original;
    131         }
    132       };
    133     },
    134     stopRead: () => {
    135       if (pool) closePrivatePool(pool);
    136     },
    137     close() {
    138       if (pool) closePrivatePool(pool);
    139       f.close();
    140     }
    141   };
    142 }