// @requirements REQ-PRODUCT-RECORDING-INSPECTION // @requirements REQ-PRODUCT-WORKSPACE-OVERVIEW // @requirements REQ-PRODUCT-CARTRIDGES REQ-PRODUCT-CF-ADMIN REQ-PRODUCT-A11Y REQ-PRODUCT-MOBILE import {normalizeAdminPresentation} from './presentation.js'; import {escapeHtml as e,renderWorkspaceShell,renderRecordCards} from './render.js'; import {boundedText} from './records.js'; import {readRecordPage,recordQueryParams} from './export.js'; import type {AdminPresentation,AdminWorker,ExecutionContextLike,RecordStore,RecordQuery,RecordPage} from './types.js'; /** These stores must already be scoped by the host. Neither paths nor display IDs select storage. */ export interface WorkspaceArea {id:string;title:string;description:string;href:string;store:RecordStore;primary?:boolean} export interface WorkspaceOverviewGrant {areas:readonly WorkspaceArea[];scopeLabel?:string;allowSetup?:boolean;allowRecordingInspection?:boolean} export interface WorkspaceOverviewOptions extends AdminPresentation { authorize(request:Request,env:Env,context?:ExecutionContextLike):WorkspaceOverviewGrant|null|Promise; basePath?:string; /** An optional destination, displayed only with this request's allowSetup grant. */ setupPath?:string;recordingPath?:string; } const route=(value:unknown):value is string=>typeof value==='string'&&value.length<=250&&/^\/[a-zA-Z0-9_-]+(?:\/[a-zA-Z0-9_-]+)*$/u.test(value); function normalizeGrant(input:WorkspaceOverviewGrant|null):WorkspaceOverviewGrant { if(!input||!Array.isArray(input.areas)||input.areas.length>10||input.scopeLabel!==undefined&&!boundedText(input.scopeLabel,200)||input.allowSetup!==undefined&&typeof input.allowSetup!=='boolean'||input.allowRecordingInspection!==undefined&&typeof input.allowRecordingInspection!=='boolean')throw Error('Invalid workspace grant.'); const ids=new Set(),paths=new Set();let primary=0; const areas=input.areas.map(a=>{ if(!a||typeof a.id!=='string'||!/^[a-z][a-z0-9-]{0,39}$/u.test(a.id)||ids.has(a.id)||!boundedText(a.title,80)||!a.title.trim()||!boundedText(a.description,300)||!route(a.href)||paths.has(a.href)||a.primary!==undefined&&typeof a.primary!=='boolean'||!a.store||typeof a.store.list!=='function'||typeof a.store.get!=='function')throw Error('Invalid workspace area.'); ids.add(a.id);paths.add(a.href);if(a.primary&&++primary>3)throw Error('Use at most three primary jobs.'); return {id:a.id,title:a.title,description:a.description,href:a.href,store:a.store,primary:a.primary===true}; }); return {areas,...(input.scopeLabel===undefined?{}:{scopeLabel:input.scopeLabel}),allowSetup:input.allowSetup===true,allowRecordingInspection:input.allowRecordingInspection===true}; } export function createWorkspaceOverviewWorker(options:WorkspaceOverviewOptions):AdminWorker { if(!options||typeof options.authorize!=='function')throw Error('Workspace overview needs host authorization.'); const basePath=options.basePath??'/admin/notabenda',setupPath=options.setupPath,recordingPath=options.recordingPath; if(!route(basePath)||setupPath!==undefined&&!route(setupPath)||recordingPath!==undefined&&!route(recordingPath))throw Error('Use clean workspace paths.'); const authorize=options.authorize,presentation=normalizeAdminPresentation(options); return {async fetch(request,env,context){ const headers=new Headers({'Content-Type':'text/plain; charset=utf-8','Cache-Control':'private, no-store','Pragma':'no-cache','Vary':'Cookie, Authorization','Referrer-Policy':'no-referrer','X-Content-Type-Options':'nosniff','X-Frame-Options':'DENY','X-Robots-Tag':'noindex, nofollow','Content-Security-Policy':"default-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'; object-src 'none'"}); const reply=(body:string,status:number)=>new Response(request.method==='HEAD'?null:body,{status,headers}); let grant:WorkspaceOverviewGrant;try{grant=normalizeGrant(await authorize(request,env,context));}catch{return reply('Access denied.',403);} if(!['GET','HEAD'].includes(request.method)){headers.set('Allow','GET, HEAD');return reply('Method not allowed.',405);} if(request.url.length>4096)return reply('Request is too long.',414); const url=new URL(request.url); if(![basePath,basePath+'/'].includes(url.pathname))return reply('Not found.',404); if([...url.searchParams.keys()].some(key=>key!=='q')||url.searchParams.getAll('q').length>1)return reply('Invalid search.',400); const query=url.searchParams.get('q')??'';if(!boundedText(query,256))return reply('Invalid search.',400); const searching=query.trim().length>0,selection:RecordQuery={query,kind:'',keyPrefix:'',from:'',to:'',limit:3}; // A normal overview reads no records. Search reads exactly one validated segment per granted area. const results:{area:WorkspaceArea;page:RecordPage|undefined}[]=[]; if(searching)for(const area of grant.areas){try{results.push({area,page:await readRecordPage(area.store,selection)});}catch{results.push({area,page:undefined});}} const primary=grant.areas.filter(a=>a.primary),more=grant.areas.filter(a=>!a.primary); const job=(a:WorkspaceArea,index:number)=>`

${e(a.title)}

${e(a.description)}

Open records
`; const moreJobs=more.length?`
More records ${more.length} areas
${more.map(a=>`${e(a.title)} ${e(a.description)}`).join('')}
`:''; const searchResult=searching?`

Record finder

Matching records

Clear search

Showing up to three records per area. Open an area’s search to inspect more.

${results.length&&results.every(r=>r.page&&r.page.records.length===0&&r.page.nextAfter===undefined)?'

No matching records in the available areas.

':''}${results.filter(({page})=>!page||page.records.length||page.nextAfter!==undefined).map(({area,page})=>`

${e(area.title)}

Search this area
${page===undefined?'

Records could not be read. Open this area to try again.

':page.records.length?`
${renderRecordCards(page.records,area.href)}
`:`

${page.nextAfter===undefined?'No matches in this area.':'No matches in the examined segment. More records remain.'}

`}${page?.nextAfter===undefined?'':`Continue search in ${e(area.title)} `}
`).join('')}${results.some(r=>r.page&&r.page.records.length===0&&r.page.nextAfter===undefined)?`
Areas without matches
    ${results.filter(r=>r.page&&r.page.records.length===0&&r.page.nextAfter===undefined).map(r=>`
  • ${e(r.area.title)}
  • `).join('')}
`:''}${grant.areas.length?'':'

No record areas are available for this request.

'}
How this search works

Each area supplies one bounded segment, in record-key order. A segment can be empty while more records remain. These are not totals or a snapshot; stored history does not establish current permission.

`:''; const body=`

${e(grant.scopeLabel??'Your workspace')}

${searching?'Find records.':'Your records, by job.'}

${searching?'':'

Find what someone was shown, what they chose, and the version it belongs to.

'}
${searchResult}${searching?'':`

Start here

Choose the record you need to review.

${primary.map(job).join('')}
${moreJobs}${grant.areas.length?'':'

No record areas are available for this request.

'}
`}${setupPath&&grant.allowSetup?``:''}${recordingPath&&grant.allowRecordingInspection?``:''}

Stored history helps you inspect events. It does not establish current permission, installation status or legal compliance.

`; const nonce=crypto.randomUUID().replaceAll('-','');headers.set('Content-Type','text/html; charset=utf-8');headers.set('Content-Security-Policy',headers.get('Content-Security-Policy')!.replace("default-src 'none';",`default-src 'none'; style-src 'nonce-${nonce}';`)); return reply(renderWorkspaceShell(body,{...presentation,basePath,title:searching?'Find records':'Workspace overview',scopeLabel:grant.scopeLabel??'Host-authorized records',nonce},'','workspace-overview-page'),200); }}; }