(req, res, arg, tpl = EMPTY)
| 35 | * @returns {string} The rendered HTML string |
| 36 | */ |
| 37 | export function html (req, res, arg, tpl = EMPTY) { |
| 38 | if (tpl.length === INT_0) { |
| 39 | return EMPTY; |
| 40 | } |
| 41 | |
| 42 | const protocol = X_FORWARDED_PROTO in req.headers ? req.headers[X_FORWARDED_PROTO] + COLON : req.parsed.protocol, |
| 43 | headers = res.getHeaders(); |
| 44 | |
| 45 | // Build all replacement values once |
| 46 | const replacements = new Map([ |
| 47 | [new RegExp(TEMPLATE_TITLE, G), req.server.title], |
| 48 | [TEMPLATE_URL, req.parsed.href.replace(req.parsed.protocol, protocol)], |
| 49 | [TEMPLATE_HEADERS, Object.keys(headers).sort().map(i => `<tr><td>${i}</td><td>${sanitize(headers[i])}</td></tr>`).join(NL)], |
| 50 | [TEMPLATE_FORMATS, `<option value=''></option>${Array.from(renderers.keys()).filter(i => i.indexOf(HTML) === INT_NEG_1).map(i => `<option value='${i.trim()}'>${i.replace(/^.*\//, EMPTY).toUpperCase()}</option>`).join(NL)}`], |
| 51 | [TEMPLATE_BODY, sanitize(JSON.stringify(arg, null, INT_2))], |
| 52 | [TEMPLATE_YEAR, new Date().getFullYear()], |
| 53 | [TEMPLATE_VERSION, req.server.version], |
| 54 | [TEMPLATE_ALLOW, headers.allow], |
| 55 | [TEMPLATE_METHODS, explode((headers?.allow ?? EMPTY).replace(HEADER_ALLOW_GET, EMPTY)).filter(i => i !== EMPTY).map(i => `<option value='${i.trim()}'>${i.trim()}</option>`).join(NL)], |
| 56 | [TEMPLATE_CSRF, headers?.[X_CSRF_TOKEN] ?? EMPTY], |
| 57 | ["class=\"headers", req.server.renderHeaders === false ? "class=\"headers dr-hidden" : "class=\"headers"] |
| 58 | ]); |
| 59 | |
| 60 | // Apply all replacements in a single pass |
| 61 | let result = tpl; |
| 62 | for (const [pattern, replacement] of replacements) { |
| 63 | result = result.replace(pattern, replacement); |
| 64 | } |
| 65 | |
| 66 | return result; |
| 67 | } |
no test coverage detected