publishers.ts (5970B)
1 import { 2 PUBLIC_QUERY_BUDGETS, 3 PUBLIC_SEARCH_BUDGETS 4 } from '../config/budgets.ts'; 5 import { canonicalPublicKey } from '../contracts/public-key.ts'; 6 import { 7 publisherProfileQuery, 8 assertedProfileName 9 } from '../nostr/profile-query.ts'; 10 import type { PublicRun, PublicRequest } from '../nostr/request-scope.ts'; 11 import { 12 subscribePublicView, 13 publicViewProjectionAvailable, 14 publicViewRunCurrent, 15 publicViewKnownEvidence, 16 type PublicView 17 } from '../runtime/public-runtime.ts'; 18 import { 19 createPublicHeadCandidate, 20 publicHeadEnvelope, 21 publicHeadSnapshot, 22 selectPublicHead, 23 type PublicHead 24 } from './heads.ts'; 25 import { evaluatePublicHeadDeletion } from './deletions.ts'; 26 import { assessFutureTimestamp, type WallClock } from './clock-policy.ts'; 27 declare const publisherBrand: unique symbol; 28 export type PublisherViews = Readonly<{ readonly [publisherBrand]: true }>; 29 export type PublisherView = Readonly<{ 30 pubkey: string; 31 label: string; 32 assertedName: boolean; 33 }>; 34 interface Owner { 35 readonly view: PublicView; 36 readonly clock: WallClock; 37 readonly heads: () => readonly PublicHead[]; 38 readonly remember: (head: PublicHead) => void; 39 readonly reserve: ( 40 run: PublicRun, 41 authors: readonly string[], 42 page: number 43 ) => readonly string[]; 44 } 45 const owners = new WeakMap<PublisherViews, Owner>(); 46 function ownerOf(token: PublisherViews): Owner { 47 const owner = owners.get(token); 48 if (!owner) throw new Error('publisher_owner_invalid'); 49 return owner; 50 } 51 export function createPublisherViews( 52 view: PublicView, 53 clock: WallClock 54 ): PublisherViews { 55 publicViewProjectionAvailable(view); 56 if (typeof clock.nowSeconds !== 'function') 57 throw new Error('publisher_clock_invalid'); 58 const heads = new Map<string, PublicHead>(), 59 attempted = new WeakMap< 60 PublicRun, 61 Readonly<{ 62 seen: Map<string, true>; 63 pages: Map<number, Map<string, true>>; 64 }> 65 >(); 66 const token = Object.freeze({}) as PublisherViews; 67 owners.set(token, { 68 view, 69 clock, 70 heads: () => Array.from(heads.values()), 71 remember(head) { 72 const key = publicHeadSnapshot(head).pubkey; 73 if ( 74 !heads.has(key) && 75 heads.size >= PUBLIC_QUERY_BUDGETS.coordinatesPerRun 76 ) 77 return; 78 heads.set(key, selectPublicHead(heads.get(key), head).head); 79 }, 80 reserve(run, authors, page) { 81 let state = attempted.get(run); 82 if (!state) { 83 state = { seen: new Map(), pages: new Map() }; 84 attempted.set(run, state); 85 } 86 const seen = state.seen; 87 let pageAuthors = state.pages.get(page); 88 if (!pageAuthors) { 89 pageAuthors = new Map(); 90 state.pages.set(page, pageAuthors); 91 } 92 const fresh = new Map<string, true>(); 93 for (const key of authors) 94 if (!seen.has(key)) { 95 if (pageAuthors.size >= PUBLIC_SEARCH_BUDGETS.publisherAuthorsPerPage) 96 continue; 97 if (seen.size >= PUBLIC_QUERY_BUDGETS.coordinatesPerRun) 98 throw new Error('publisher_run_limit'); 99 pageAuthors.set(key, true); 100 seen.set(key, true); 101 fresh.set(key, true); 102 } 103 return Array.from(fresh.keys()); 104 } 105 }); 106 return token; 107 } 108 // Caller selects one current local page, never every discovered publisher. 109 // Page reservations are cumulative across reordered rows. Admission precedes 110 // effects and is not refunded on failure. New explicit 111 // generations can refresh metadata; duplicate page reads cannot retry blindly. 112 export function readPublisherPage( 113 token: PublisherViews, 114 run: PublicRun, 115 authors: readonly string[], 116 page = 1 117 ): readonly PublicRequest[] { 118 if ( 119 !Number.isSafeInteger(page) || 120 page < 1 || 121 page > 122 Math.ceil( 123 PUBLIC_QUERY_BUDGETS.coordinatesPerRun / PUBLIC_SEARCH_BUDGETS.pageRows 124 ) 125 ) 126 throw new Error('publisher_page_invalid'); 127 const filters = publisherProfileQuery(authors), 128 owner = ownerOf(token); 129 if ( 130 !publicViewProjectionAvailable(owner.view) || 131 !publicViewRunCurrent(owner.view, run) 132 ) 133 throw new Error('publisher_run_inactive'); 134 const fresh = owner.reserve(run, filters[0]?.authors ?? [], page); 135 if (!fresh.length) return []; 136 const selected = new Map(fresh.map((key) => [key, true])); 137 const request = subscribePublicView( 138 owner.view, 139 run, 140 'profile', 141 publisherProfileQuery(fresh), 142 (proof) => { 143 const head = createPublicHeadCandidate(proof); 144 if (!head) return; 145 const value = publicHeadSnapshot(head); 146 if (value.kind === 0 && selected.has(value.pubkey)) owner.remember(head); 147 } 148 ); 149 return [request]; 150 } 151 export function publisherSnapshot( 152 token: PublisherViews, 153 pubkey: string 154 ): PublisherView { 155 if (canonicalPublicKey(pubkey) === undefined) 156 throw new Error('publisher_key_invalid'); 157 const owner = ownerOf(token), 158 fallback = { pubkey, label: pubkey, assertedName: false }; 159 if (!publicViewProjectionAvailable(owner.view)) return fallback; 160 const retained = owner 161 .heads() 162 .find((head) => publicHeadSnapshot(head).pubkey === pubkey); 163 if (!retained) return fallback; 164 const known = publicViewKnownEvidence(owner.view, retained), 165 head = known.head ?? retained; 166 owner.remember(head); 167 let now: number; 168 try { 169 now = owner.clock.nowSeconds(); 170 } catch { 171 return fallback; 172 } 173 const settled = publicViewKnownEvidence(owner.view, head); 174 if ( 175 settled.head && 176 publicHeadSnapshot(settled.head).id !== publicHeadSnapshot(head).id 177 ) 178 return fallback; 179 if ( 180 !publicViewProjectionAvailable(owner.view) || 181 assessFutureTimestamp(publicHeadSnapshot(head).created_at, now) !== 182 'within_policy' || 183 evaluatePublicHeadDeletion(head, settled.requests).outcome === 'suppressed' 184 ) 185 return fallback; 186 const name = assertedProfileName(publicHeadEnvelope(head)); 187 return name === undefined 188 ? fallback 189 : { pubkey, label: name, assertedName: true }; 190 }