feat: add PDF render timing to convert and demo routes
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Failing after 1m42s
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Failing after 1m42s
- renderPdf() and renderUrlPdf() now return { pdf, durationMs }
- Timing wraps the actual render with Date.now()
- Log render duration via logger.info
- Add X-Render-Time response header in convert and demo routes
- Update all callers in convert, demo, templates routes
- Add TDD tests in render-timing.test.ts
- Update existing test mocks for new return shape
This commit is contained in:
parent
0283e9dae8
commit
f9caef82e6
13 changed files with 165 additions and 23 deletions
|
|
@ -239,10 +239,11 @@ export interface PdfRenderOptions {
|
|||
export async function renderPdf(
|
||||
html: string,
|
||||
options: PdfRenderOptions = {}
|
||||
): Promise<Buffer> {
|
||||
): Promise<{ pdf: Buffer; durationMs: number }> {
|
||||
const { page, instance } = await acquirePage();
|
||||
try {
|
||||
await page.setJavaScriptEnabled(false);
|
||||
const startTime = Date.now();
|
||||
const result = await Promise.race([
|
||||
(async () => {
|
||||
await page.setContent(html, { waitUntil: "domcontentloaded", timeout: 15_000 });
|
||||
|
|
@ -267,7 +268,9 @@ export async function renderPdf(
|
|||
setTimeout(() => reject(new Error("PDF_TIMEOUT")), 30_000)
|
||||
),
|
||||
]);
|
||||
return result;
|
||||
const durationMs = Date.now() - startTime;
|
||||
logger.info(`PDF rendered in ${durationMs}ms (html, ${result.length} bytes)`);
|
||||
return { pdf: result, durationMs };
|
||||
} finally {
|
||||
releasePage(page, instance);
|
||||
}
|
||||
|
|
@ -279,7 +282,7 @@ export async function renderUrlPdf(
|
|||
waitUntil?: string;
|
||||
hostResolverRules?: string;
|
||||
} = {}
|
||||
): Promise<Buffer> {
|
||||
): Promise<{ pdf: Buffer; durationMs: number }> {
|
||||
const { page, instance } = await acquirePage();
|
||||
try {
|
||||
await page.setJavaScriptEnabled(false);
|
||||
|
|
@ -316,6 +319,7 @@ export async function renderUrlPdf(
|
|||
});
|
||||
}
|
||||
}
|
||||
const startTime = Date.now();
|
||||
const result = await Promise.race([
|
||||
(async () => {
|
||||
await page.goto(url, {
|
||||
|
|
@ -342,7 +346,9 @@ export async function renderUrlPdf(
|
|||
setTimeout(() => reject(new Error("PDF_TIMEOUT")), 30_000)
|
||||
),
|
||||
]);
|
||||
return result;
|
||||
const durationMs = Date.now() - startTime;
|
||||
logger.info(`PDF rendered in ${durationMs}ms (url, ${result.length} bytes)`);
|
||||
return { pdf: result, durationMs };
|
||||
} finally {
|
||||
releasePage(page, instance);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue