| 1 | import http from "node:http"; |
| 2 | import { writeFile } from "node:fs/promises"; |
| 3 | import path from "node:path"; |
| 4 | |
| 5 | |
| 6 | |
| 7 | const output = process.argv[2]; |
| 8 | if (!output || !path.isAbsolute(output)) throw new Error("An absolute evidence directory is required."); |
| 9 | const reports = []; |
| 10 | const copiedText = "Cage native copy 中文🙂 " + path.basename(output); |
| 11 | const html = `<!doctype html><meta charset="utf-8"><title>Cage native browser acceptance</title> |
| 12 | <style>body{margin:0;background:#eef3f8;color:#17233b;font:18px Arial}header{height:42px;padding:12px;background:#1e4363;color:white}main{padding:20px}textarea,#images{display:block;box-sizing:border-box;width:420px;max-width:90vw;height:90px;margin:12px 0;padding:8px;border:2px solid #345;border-radius:4px;background:white}button{height:42px;margin:0 8px 12px 0}#images{height:100px}#images img{max-height:64px}#drop{height:64px;border:2px dashed #678;width:400px;max-width:90vw}input{margin:10px 0}p{margin:8px}</style> |
| 13 | <header>Cage Windows native browser acceptance</header><button id="another" style="position:fixed;right:20px;top:18px">Open fixture tab</button><main> |
| 14 | <textarea id="text" aria-label="Acceptance text"></textarea> |
| 15 | <button id="copy">Copy fixture text</button><button id="permission">Request fixture permission</button> |
| 16 | <div id="images" contenteditable="true" tabindex="0" aria-label="Acceptance images">Paste image here</div> |
| 17 | <p>Find this unique phrase: cage-native-find-needle</p><a id="download" href="/download">Download fixture</a> |
| 18 | <input id="upload" type="file" multiple><div id="drop" tabindex="0">Drop fixture files here</div></main> |
| 19 | <script> |
| 20 | const report = value => fetch('/report', {method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({page:location.pathname,...value})}); |
| 21 | text.addEventListener('input',()=>report({type:'text',value:text.value})); |
| 22 | document.addEventListener('pointerdown',e=>report({type:'pointer',x:e.clientX,y:e.clientY,target:e.target.id,width:innerWidth,height:innerHeight,outerWidth,outerHeight,dpr:devicePixelRatio,visualScale:visualViewport.scale})); |
| 23 | document.addEventListener('keydown',e=>report({type:'key',key:e.key,target:e.target.id,ctrl:e.ctrlKey,width:innerWidth,height:innerHeight})); |
| 24 | document.addEventListener('scroll',()=>report({type:'scroll',y:scrollY}),{passive:true}); |
| 25 | copy.onclick=async()=>{await navigator.clipboard.writeText(${JSON.stringify(copiedText)});report({type:'copied'})}; |
| 26 | permission.onclick=async()=>report({type:'permission',value:await Notification.requestPermission()}); |
| 27 | another.onclick=()=>window.open('/private-two','_blank'); |
| 28 | images.addEventListener('paste',async e=>{const files=[...e.clipboardData.files];if(!files.length)return; e.preventDefault();for(const f of files){const b=new Uint8Array(await f.arrayBuffer());const image=new Image();image.src=URL.createObjectURL(f);await image.decode();images.replaceChildren(image);report({type:'png',mime:f.type,width:image.naturalWidth,height:image.naturalHeight,bytes:b.length})}}); |
| 29 | const selected=async(files,type)=>report({type,files:await Promise.all([...files].map(async f=>({name:f.name,bytes:[...new Uint8Array(await f.arrayBuffer())]})))}); |
| 30 | upload.onchange=()=>selected(upload.files,'upload');drop.ondragover=e=>e.preventDefault();drop.ondrop=e=>{e.preventDefault();selected(e.dataTransfer.files,'drop')}; |
| 31 | </script>`; |
| 32 | const server = http.createServer(async (request, response) => { |
| 33 | if (request.url === "/state") { response.setHeader("Content-Type", "application/json"); response.end(JSON.stringify(reports)); return; } |
| 34 | if (request.url === "/report" && request.method === "POST") { |
| 35 | const chunks = []; let length = 0; |
| 36 | for await (const chunk of request) { length += chunk.length; if (length > 1 << 20) { response.writeHead(413); response.end(); return; } chunks.push(chunk); } |
| 37 | try { reports.push(JSON.parse(Buffer.concat(chunks).toString("utf8"))); await writeFile(path.join(output, "fixture-state.json"), JSON.stringify(reports, null, 2)); } |
| 38 | catch { response.writeHead(400); response.end(); return; } |
| 39 | response.end("ok"); return; |
| 40 | } |
| 41 | if (request.url === "/download") { response.setHeader("Content-Disposition", 'attachment; filename="cage-native-fixture.txt"'); response.end("Cage native download 中文🙂"); return; } |
| 42 | response.setHeader("Content-Type", "text/html; charset=utf-8"); |
| 43 | const rows = request.url === "/perf" ? Array.from({length:60},(_,i)=>`<section style="height:180px;background:hsl(${i*23%360} 45% 85%);padding:20px">Native scroll fixture row ${i}</section>`).join("") : ""; |
| 44 | response.end(rows ? html.replace("</main>", "</main>"+rows) : html); |
| 45 | }); |
| 46 | server.listen(0, "127.0.0.1", async () => { |
| 47 | await writeFile(path.join(output, "fixture-port"), String(server.address().port)); |
| 48 | process.stdout.write("CAGE_NATIVE_FIXTURE_READY\n"); |
| 49 | }); |
| 50 | for (const signal of ["SIGINT", "SIGTERM"]) process.on(signal, () => server.close(() => process.exit(0))); |