refactor: extract shared PDF route handler to eliminate convert route duplication
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m19s

- New src/utils/pdf-handler.ts with handlePdfRoute() helper
- Handles: content-type validation, PDF option validation, slot acquire/release, error mapping, response headers
- Refactored convert.ts from 388 to 233 lines (40% reduction)
- 10 TDD tests for the new helper (RED→GREEN verified)
- All 618 tests passing, zero tsc --noEmit errors
This commit is contained in:
DocFast CEO 2026-03-09 20:07:27 +01:00
parent 54316d45cf
commit 76b2179be9
3 changed files with 265 additions and 179 deletions

View file

@ -3,33 +3,12 @@ import { renderPdf, renderUrlPdf } from "../services/browser.js";
import { markdownToHtml, wrapHtml } from "../services/markdown.js";
import dns from "node:dns/promises";
import logger from "../services/logger.js";
import { errorMessage } from "../utils/errors.js";
import { isPrivateIP } from "../utils/network.js";
import { sanitizeFilename } from "../utils/sanitize.js";
import { validatePdfOptions } from "../utils/pdf-options.js";
import { handlePdfRoute } from "../utils/pdf-handler.js";
export const convertRouter = Router();
interface ConvertBody {
html?: string;
markdown?: string;
css?: string;
format?: string;
landscape?: boolean;
margin?: { top?: string; right?: string; bottom?: string; left?: string };
printBackground?: boolean;
filename?: string;
headerTemplate?: string;
footerTemplate?: string;
displayHeaderFooter?: boolean;
scale?: number;
pageRanges?: string;
preferCSSPageSize?: boolean;
width?: string;
height?: string;
}
/**
* @openapi
* /v1/convert/html:
@ -80,66 +59,18 @@ interface ConvertBody {
* description: PDF generation failed
*/
convertRouter.post("/html", async (req: Request, res: Response) => {
let slotAcquired = false;
try {
// Reject non-JSON content types
const ct = req.headers["content-type"] || "";
if (!ct.includes("application/json")) {
res.status(415).json({ error: "Unsupported Content-Type. Use application/json." });
return;
}
const body: ConvertBody =
typeof req.body === "string" ? { html: req.body } : req.body;
await handlePdfRoute(req, res, async (sanitizedOptions) => {
const body = typeof req.body === "string" ? { html: req.body } : req.body;
if (!body.html) {
res.status(400).json({ error: "Missing 'html' field" });
return;
return null;
}
// Validate PDF options
const validation = validatePdfOptions(body);
if (!validation.valid) {
res.status(400).json({ error: validation.error });
return;
}
// Acquire concurrency slot
if (req.acquirePdfSlot) {
await req.acquirePdfSlot();
slotAcquired = true;
}
// Wrap bare HTML fragments
const fullHtml = body.html.includes("<html")
? body.html
: wrapHtml(body.html, body.css);
const { pdf, durationMs } = await renderPdf(fullHtml, {
...validation.sanitized,
});
const filename = sanitizeFilename(body.filename || "document.pdf");
res.setHeader("Content-Type", "application/pdf");
res.setHeader("Content-Disposition", `inline; filename="${filename}"`);
res.setHeader("X-Render-Time", String(durationMs));
res.send(pdf);
} catch (err: unknown) {
logger.error({ err }, "Convert HTML error");
const msg = errorMessage(err);
if (msg === "QUEUE_FULL") {
res.status(503).json({ error: "Server busy — too many concurrent PDF generations. Please try again in a few seconds." });
return;
}
if (msg === "PDF_TIMEOUT") {
res.status(504).json({ error: "PDF generation timed out." });
return;
}
res.status(500).json({ error: "PDF generation failed." });
} finally {
if (slotAcquired && req.releasePdfSlot) {
req.releasePdfSlot();
}
}
const { pdf, durationMs } = await renderPdf(fullHtml, { ...sanitizedOptions });
return { pdf, durationMs, filename: sanitizeFilename(body.filename || "document.pdf") };
});
});
/**
@ -191,62 +122,16 @@ convertRouter.post("/html", async (req: Request, res: Response) => {
* description: PDF generation failed
*/
convertRouter.post("/markdown", async (req: Request, res: Response) => {
let slotAcquired = false;
try {
// Reject non-JSON content types
const ct = req.headers["content-type"] || "";
if (!ct.includes("application/json")) {
res.status(415).json({ error: "Unsupported Content-Type. Use application/json." });
return;
}
const body: ConvertBody =
typeof req.body === "string" ? { markdown: req.body } : req.body;
await handlePdfRoute(req, res, async (sanitizedOptions) => {
const body = typeof req.body === "string" ? { markdown: req.body } : req.body;
if (!body.markdown) {
res.status(400).json({ error: "Missing 'markdown' field" });
return;
return null;
}
// Validate PDF options
const validation = validatePdfOptions(body);
if (!validation.valid) {
res.status(400).json({ error: validation.error });
return;
}
// Acquire concurrency slot
if (req.acquirePdfSlot) {
await req.acquirePdfSlot();
slotAcquired = true;
}
const html = markdownToHtml(body.markdown, body.css);
const { pdf, durationMs } = await renderPdf(html, {
...validation.sanitized,
});
const filename = sanitizeFilename(body.filename || "document.pdf");
res.setHeader("Content-Type", "application/pdf");
res.setHeader("Content-Disposition", `inline; filename="${filename}"`);
res.setHeader("X-Render-Time", String(durationMs));
res.send(pdf);
} catch (err: unknown) {
logger.error({ err }, "Convert MD error");
const msg = errorMessage(err);
if (msg === "QUEUE_FULL") {
res.status(503).json({ error: "Server busy — too many concurrent PDF generations. Please try again in a few seconds." });
return;
}
if (msg === "PDF_TIMEOUT") {
res.status(504).json({ error: "PDF generation timed out." });
return;
}
res.status(500).json({ error: "PDF generation failed." });
} finally {
if (slotAcquired && req.releasePdfSlot) {
req.releasePdfSlot();
}
}
const { pdf, durationMs } = await renderPdf(html, { ...sanitizedOptions });
return { pdf, durationMs, filename: sanitizeFilename(body.filename || "document.pdf") };
});
});
/**
@ -303,19 +188,12 @@ convertRouter.post("/markdown", async (req: Request, res: Response) => {
* description: PDF generation failed
*/
convertRouter.post("/url", async (req: Request, res: Response) => {
let slotAcquired = false;
try {
// Reject non-JSON content types
const ct = req.headers["content-type"] || "";
if (!ct.includes("application/json")) {
res.status(415).json({ error: "Unsupported Content-Type. Use application/json." });
return;
}
const body = req.body as { url?: string; format?: string; landscape?: boolean; margin?: string | { top?: string; right?: string; bottom?: string; left?: string }; printBackground?: boolean; waitUntil?: string; filename?: string; headerTemplate?: string; footerTemplate?: string; displayHeaderFooter?: boolean; scale?: number; pageRanges?: string; preferCSSPageSize?: boolean; width?: string; height?: string };
await handlePdfRoute(req, res, async (sanitizedOptions) => {
const body = req.body as { url?: string; waitUntil?: string; filename?: string };
if (!body.url) {
res.status(400).json({ error: "Missing 'url' field" });
return;
return null;
}
// URL validation + SSRF protection
@ -324,65 +202,32 @@ convertRouter.post("/url", async (req: Request, res: Response) => {
parsed = new URL(body.url);
if (!["http:", "https:"].includes(parsed.protocol)) {
res.status(400).json({ error: "Only http/https URLs are supported" });
return;
return null;
}
} catch {
res.status(400).json({ error: "Invalid URL" });
return;
return null;
}
// DNS lookup to block private/reserved IPs + pin resolution to prevent DNS rebinding
// DNS lookup to block private/reserved IPs + pin resolution
let resolvedAddress: string;
try {
const { address } = await dns.lookup(parsed.hostname);
if (isPrivateIP(address)) {
res.status(400).json({ error: "URL resolves to a private/internal IP address" });
return;
return null;
}
resolvedAddress = address;
} catch {
res.status(400).json({ error: "DNS lookup failed for URL hostname" });
return;
}
// Validate PDF options
const validation = validatePdfOptions(body);
if (!validation.valid) {
res.status(400).json({ error: validation.error });
return;
}
// Acquire concurrency slot
if (req.acquirePdfSlot) {
await req.acquirePdfSlot();
slotAcquired = true;
return null;
}
const { pdf, durationMs } = await renderUrlPdf(body.url, {
...validation.sanitized,
...sanitizedOptions,
hostResolverRules: `MAP ${parsed.hostname} ${resolvedAddress}`,
});
const filename = sanitizeFilename(body.filename || "page.pdf");
res.setHeader("Content-Type", "application/pdf");
res.setHeader("Content-Disposition", `inline; filename="${filename}"`);
res.setHeader("X-Render-Time", String(durationMs));
res.send(pdf);
} catch (err: unknown) {
logger.error({ err }, "Convert URL error");
const msg = errorMessage(err);
if (msg === "QUEUE_FULL") {
res.status(503).json({ error: "Server busy — too many concurrent PDF generations. Please try again in a few seconds." });
return;
}
if (msg === "PDF_TIMEOUT") {
res.status(504).json({ error: "PDF generation timed out." });
return;
}
res.status(500).json({ error: "PDF generation failed." });
} finally {
if (slotAcquired && req.releasePdfSlot) {
req.releasePdfSlot();
}
}
return { pdf, durationMs, filename: sanitizeFilename(body.filename || "page.pdf") };
});
});