inbox-ingress.ts (4151B)
1 import { makeFixture as makeRecoveryFixture } from './resume-preparation.ts'; 2 import { 3 validateRelayPolicy, 4 readRelayPolicy 5 } from '../../../src/lib/config/relays.ts'; 6 import { 7 createPrivateSession, 8 closePrivateSession, 9 privateSessionOwnership 10 } from '../../../src/lib/runtime/private-session.ts'; 11 import { 12 captureInboxIngress, 13 startInboxIngress, 14 stopInboxIngress, 15 inboxIngressSnapshot, 16 type InboxIngress 17 } from '../../../src/lib/nostr/inbox-ingress.ts'; 18 import { loadPrivateRecord } from '../../../src/lib/persistence/private-storage.ts'; 19 import { privateRecordSnapshot } from '../../../src/lib/persistence/private-records.ts'; 20 import { 21 getPublicStore, 22 publicStoreEnvelope 23 } from '../../../src/lib/nostr/public-store.ts'; 24 export async function makeFixture() { 25 const f = await makeRecoveryFixture(); 26 const policy = validateRelayPolicy( 27 JSON.stringify({ 28 ...readRelayPolicy(f.context.policy), 29 messagingEnabled: true 30 }) 31 ); 32 if (!policy) throw Error('fixture policy unavailable'); 33 const session = await createPrivateSession( 34 f.identity, 35 'reviewed_private_session' 36 ); 37 if (!session) throw Error('missing genuine session'); 38 const capture = privateSessionOwnership(session)!; 39 const publicStore = getPublicStore()!; 40 const self = (await f.row())!.record.self; 41 let ingress: InboxIngress | undefined; 42 let authorized = true; 43 let restoreFault = () => {}; 44 return { 45 ...f, 46 outer: self.wire, 47 outerId: self.eventId, 48 capture(access = true, review: unknown = 'reviewed_private_inbox_receive') { 49 ingress = captureInboxIngress( 50 f.repository, 51 session, 52 policy, 53 f.context.own, 54 access 55 ? (context) => ({ 56 ...context, 57 receive: 'qualified_exercised', 58 archive: 'qualified_exercised', 59 current: () => authorized && capture.current() 60 }) 61 : undefined, 62 review 63 ); 64 return !!ingress; 65 }, 66 start(review: unknown = 'reviewed_private_inbox_receive') { 67 if (!ingress) return false; 68 return startInboxIngress(ingress, review); 69 }, 70 stop() { 71 if (ingress) stopInboxIngress(ingress); 72 }, 73 snapshot: () => ingress && inboxIngressSnapshot(ingress), 74 async stored(id = self.eventId) { 75 const row = await loadPrivateRecord( 76 f.repository, 77 'received_envelopes', 78 id 79 ); 80 return row.ok ? privateRecordSnapshot(row.value, f.owner, id) : undefined; 81 }, 82 publicContains: (id = self.eventId) => 83 !!publicStoreEnvelope(publicStore, id), 84 countsValue: () => f.counts(), 85 revokeAccess() { 86 authorized = false; 87 }, 88 logout() { 89 closePrivateSession(session); 90 }, 91 restoreFault() { 92 restoreFault(); 93 }, 94 faultRetention(kind: 'abort' | 'readback') { 95 const put = Reflect.get<IDBObjectStore, 'put'>( 96 IDBObjectStore.prototype, 97 'put' 98 ); 99 const cursor = Reflect.get<IDBIndex, 'openCursor'>( 100 IDBIndex.prototype, 101 'openCursor' 102 ); 103 let written = false; 104 IDBObjectStore.prototype.put = function ( 105 ...args: Parameters<IDBObjectStore['put']> 106 ) { 107 const request = put.apply(this, args); 108 if (this.name === 'received_envelopes') { 109 written = true; 110 if (kind === 'abort') this.transaction.abort(); 111 } 112 return request; 113 }; 114 IDBIndex.prototype.openCursor = function ( 115 ...args: Parameters<IDBIndex['openCursor']> 116 ) { 117 if ( 118 kind === 'readback' && 119 written && 120 this.objectStore.name === 'received_envelopes' && 121 this.objectStore.transaction.mode === 'readonly' 122 ) 123 throw Error('actual post-put inbox readback failure'); 124 return cursor.apply(this, args); 125 }; 126 restoreFault = () => { 127 IDBObjectStore.prototype.put = put; 128 IDBIndex.prototype.openCursor = cursor; 129 }; 130 }, 131 close() { 132 restoreFault(); 133 if (ingress) stopInboxIngress(ingress); 134 closePrivateSession(session); 135 f.close(); 136 } 137 }; 138 }