/** * Renders the provided audit results to well-formatted and valid HTML. * * Do note that the rendered result is not an HTML document, it's rather * just a component with results. */ export async function renderAuditResultsToHTML(results) { const grouped = { total: 0, ok: [], notice: [], warn: [], error: [], }; for (const result of results) { grouped.total++; if (result.status === 'ok') { grouped[result.status].push(result); } else { grouped[result.status].push(result); } } let report = '* This report was auto-generated by graphql-http\n'; report += '\n'; report += '
${result.id}
${result.name}${result.id}
${result.name}\n`;
report += ''; // no "\n" because they count in HTML pre tags
const res = result.response;
const headers = {};
for (const [key, val] of res.headers.entries()) {
// some headers change on each run, dont report it
if (key === 'date') {
headers[key] = '';
}
else if (['cf-ray', 'server-timing'].includes(key)) {
headers[key] = '';
}
else {
headers[key] = val;
}
}
let text = '', json;
try {
text = await res.text();
json = JSON.parse(text);
}
catch (_a) {
// noop
}
const stringified = JSON.stringify({
status: res.status,
statusText: res.statusText,
headers,
body: json || ((text === null || text === void 0 ? void 0 : text.length) > 5120 ? '' : text) || null,
}, (_k, v) => {
if (v != null && typeof v === 'object' && !Array.isArray(v)) {
// sort object fields for stable stringify
const acc = {};
return Object.keys(v)
.sort()
.reverse() // body on bottom
.reduce((acc, k) => {
acc[k] = v[k];
return acc;
}, acc);
}
return v;
}, 2);
report += stringified + '\n';
report += '
\n';
report += '