report-helper.js (1562B)
1 function wait(ms) { 2 return new Promise(resolve => step_timeout(resolve, ms)); 3 } 4 5 async function pollReports(endpoint, id, min_count) { 6 const res = await fetch(`${endpoint}?reportID=${id}${min_count ? `&min_count=${min_count}` : ''}`, { cache: 'no-store' }); 7 const reports = []; 8 if (res.status === 200) { 9 for (const report of await res.json()) { 10 reports.push(report); 11 } 12 } 13 return reports; 14 } 15 16 async function pollCookies(endpoint, id) { 17 const res = await fetch(`${endpoint}?reportID=${id}&op=retrieve_cookies`, { cache: 'no-store' }); 18 const dict = await res.json(); 19 if (dict.reportCookies == 'None') 20 return {}; 21 return dict.reportCookies; 22 } 23 24 async function pollNumResults(endpoint, id) { 25 const res = await fetch(`${endpoint}?reportID=${id}&op=retrieve_count`, { cache: 'no-store' }); 26 const dict = await res.json(); 27 if (dict.report_count == 'None') 28 return 0; 29 return dict.report_count; 30 } 31 32 function checkReportExists(reports, type, url) { 33 for (const report of reports) { 34 if (report.type !== type) continue; 35 if (report.body.documentURL === url || report.body.sourceFile === url) return true; 36 } 37 assert_unreached(`A report of ${type} from ${url} is not found.`); 38 } 39 40 function getReport(reports, type, document_url, subresource_url) { 41 for (const report of reports) { 42 if (report.type !== type) continue; 43 if (report.body.documentURL === document_url 44 && (report.body.subresourceURL == subresource_url 45 || report.body.blockedURL == subresource_url)) return report; 46 } 47 return null; 48 }