import fs from 'node:fs/promises'; import http from 'node:http'; import crypto from 'node:crypto'; import ts from 'typescript'; import {fileURLToPath} from 'node:url'; const base=new URL('.',import.meta.url),out=fileURLToPath(new URL('results/',base)); await fs.mkdir(out+'evidence',{recursive:true}); const historical=await fs.readFile(new URL('source-before.ts.txt',base),'utf8'); const current=await fs.readFile(new URL('source-after.ts.txt',base),'utf8'); const meta=JSON.parse(await fs.readFile(new URL('source-manifest.json',base),'utf8')); const hash=s=>crypto.createHash('sha256').update(s).digest('hex'); if(hash(historical)!==meta.historicalVariants.gitBlob || hash(current)!==meta.currentSHA256)throw Error('Source hash mismatch'); const compile=s=>ts.transpileModule(s,{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ES2022}}).outputText; if(!historical.includes("indexedDB.open('layer-studio',1)"))throw Error('Unexpected source'); const before=compile(historical.replace("indexedDB.open('layer-studio',1)","indexedDB.open(globalThis.__EVIDENCE_DB__,1)")),after=compile(current); const html=await fs.readFile(new URL('reproduction-screen.html',base),'utf8'); const web=http.createServer(async(req,res)=>{try{const u=new URL(req.url,'http://localhost'); if(u.pathname==='/result'&&req.method==='POST'){let raw='';for await(const c of req)raw+=c;if(raw.length>20000)throw Error('size');const d=JSON.parse(raw);if(!['before','after'].includes(d.mode))throw Error('mode');await fs.writeFile(out+'evidence/'+d.mode+'-result.json',JSON.stringify(d,null,2));res.end('ok');return;} const routes={'/':['text/html; charset=utf-8',html],'/before.js':['application/javascript',before],'/after.js':['application/javascript',after],'/document.ts':['application/javascript',"export function validateDoc(){throw Error('Domain validation is outside this write-timing test');}"],'/config/profile.ts':['application/javascript',"export const PROFILE={workspaceDatabase:globalThis.__EVIDENCE_DB__,workspaceFormat:'slohero-vector-workspace'};"],'/metadata':['application/json',JSON.stringify(meta)]},p=routes[u.pathname]; if(!p){res.statusCode=404;res.end('not found');return;}res.setHeader('Content-Type',p[0]);res.setHeader('Cache-Control','no-store');res.end(p[1]);}catch(e){res.statusCode=500;res.end(String(e));}}); web.listen(0,'127.0.0.1',async()=>{const url='http://127.0.0.1:'+web.address().port+'/';await fs.writeFile(new URL('results/server.json',base),JSON.stringify({url,out,meta},null,2));console.log(JSON.stringify({url,out,matchesRecorded:meta.matchesRecorded,historicalVariants:variants}));});