app

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

inbox-access-controller.ts (4644B)


      1 import { makeAuthFixture } from './inbox-auth.ts';
      2 import { validateRelayPolicy } from '../../../src/lib/config/relays.ts';
      3 import {
      4   createPublicRuntimeContext,
      5   mountPublicRuntime,
      6   closePublicRuntime,
      7   createPublicView,
      8   beginPublicViewRun,
      9   disposePublicView
     10 } from '../../../src/lib/runtime/public-runtime.ts';
     11 import {
     12   resolveInboxPreference,
     13   settleInboxResolver,
     14   inboxResolverCurrentAfterSample,
     15   type InboxResolver
     16 } from '../../../src/lib/messaging/resolve-inbox.ts';
     17 import {
     18   createInboxAccessController,
     19   closeInboxAccessController
     20 } from '../../../src/lib/messaging/inbox-access-controller.ts';
     21 import {
     22   getPrivatePool,
     23   subscribePrivatePage,
     24   subscribePrivateLive,
     25   closePrivatePool,
     26   type PrivatePool
     27 } from '../../../src/lib/nostr/private-pool.ts';
     28 export * from '../../../src/lib/messaging/inbox-access-controller.ts';
     29 export { installCloseFailure } from './inbox-auth.ts';
     30 import {
     31   createPrivateSession,
     32   closePrivateSession,
     33   type PrivateSession
     34 } from '../../../src/lib/runtime/private-session.ts';
     35 export const origins = [
     36   'wss://inbox.example.org',
     37   'wss://peer.example.org',
     38   'wss://archive.example.org'
     39 ];
     40 // Genuine actors/SDK discovery/AUTH with controlled loopback/provided identity;
     41 // no installed extension, access probe, operator or full qualification claim.
     42 export async function makeControllerFixture(actualCrypto = false) {
     43   const capturedPolicy = validateRelayPolicy(
     44     JSON.stringify({
     45       schemaVersion: 1,
     46       public: [
     47         {
     48           origin: 'wss://discovery.example.org',
     49           read: true,
     50           write: true,
     51           nip50: false
     52         }
     53       ],
     54       inbox: origins.map((origin) => ({ origin, read: true, write: true })),
     55       postingEnabled: true,
     56       messagingEnabled: true,
     57       operatorDenylist: []
     58     })
     59   );
     60   if (!capturedPolicy) throw Error('fixture policy');
     61   const policy = capturedPolicy;
     62   const fixture = await makeAuthFixture(policy, actualCrypto);
     63   const context = createPublicRuntimeContext(),
     64     runtime = mountPublicRuntime(context, policy);
     65   if (!runtime) throw Error('actual public runtime');
     66   const view = createPublicView(runtime);
     67   let controller: ReturnType<typeof createInboxAccessController>,
     68     own: InboxResolver | undefined,
     69     successor: PrivateSession | undefined;
     70   const pools = new Map<PrivatePool, true>();
     71   function scope(
     72     selected: string[],
     73     role: 'receive' | 'delivery',
     74     bindHead = false
     75   ) {
     76     const pool = getPrivatePool(
     77       fixture.session,
     78       policy,
     79       selected,
     80       Object.freeze({}),
     81       role,
     82       bindHead
     83         ? () => !!own && inboxResolverCurrentAfterSample(own)
     84         : () => true
     85     );
     86     if (!pool) throw Error('actual scoped private pool');
     87     pools.set(pool, true);
     88     return pool;
     89   }
     90   return {
     91     ...fixture,
     92     async capture() {
     93       const run = beginPublicViewRun(view);
     94       own = resolveInboxPreference(view, run, fixture.owner);
     95       await settleInboxResolver(own);
     96       controller = createInboxAccessController(fixture.session, policy, own);
     97       if (!controller) throw Error('actual access controller');
     98       return controller;
     99     },
    100     churnHead() {
    101       beginPublicViewRun(view);
    102     },
    103     scope,
    104     page(pool: PrivatePool, ended: (reason: string) => void = () => {}) {
    105       return subscribePrivatePage(pool, 1, (message) => {
    106         if (message.type === 'end') ended(message.reason);
    107       });
    108     },
    109     live(
    110       pool: PrivatePool,
    111       onOpen: () => void,
    112       candidate: () => void = () => {}
    113     ) {
    114       return subscribePrivateLive(pool, (message) => {
    115         if (message.type === 'open') onOpen();
    116         if (message.type === 'candidate') candidate();
    117       });
    118     },
    119     closeSession() {
    120       return closePrivateSession(fixture.session);
    121     },
    122     async successorScope() {
    123       successor ??= await createPrivateSession(
    124         fixture.identity,
    125         'reviewed_private_session'
    126       );
    127       if (!successor) throw Error('genuine successor session unavailable');
    128       const pool = getPrivatePool(
    129         successor,
    130         policy,
    131         origins,
    132         Object.freeze({}),
    133         'receive'
    134       );
    135       if (!pool) throw Error('genuine successor pool unavailable');
    136       pools.set(pool, true);
    137       return pool;
    138     },
    139     stopScope(pool: PrivatePool) {
    140       closePrivatePool(pool);
    141     },
    142     close() {
    143       if (controller) closeInboxAccessController(controller);
    144       for (const pool of pools.keys()) closePrivatePool(pool);
    145       if (successor) closePrivateSession(successor);
    146       disposePublicView(view);
    147       closePublicRuntime(context);
    148       fixture.close();
    149     }
    150   };
    151 }