MCPcopy
hub / github.com/steel-dev/steel-browser / handlePDF

Function handlePDF

api/src/modules/actions/actions.controller.ts:533–610  ·  view source on GitHub ↗
(
  sessionService: SessionService,
  browserService: CDPService,
  request: PDFRequest,
  reply: FastifyReply,
)

Source from the content-addressed store, hash-verified

531};
532
533export const handlePDF = async (
534 sessionService: SessionService,
535 browserService: CDPService,
536 request: PDFRequest,
537 reply: FastifyReply,
538) => {
539 const startTime = Date.now();
540 let times: Record<string, number> = {};
541 const { url, logUrl, proxyUrl, delay } = request.body;
542
543 let proxy: IProxyServer | null = null;
544 let context: BrowserContext | null = null;
545
546 if (!browserService.isRunning()) {
547 await browserService.launch();
548 }
549
550 try {
551 if (proxyUrl) {
552 proxy = await sessionService.proxyFactory(proxyUrl);
553 await proxy.listen();
554 }
555
556 times.proxyTime = Date.now() - startTime;
557
558 let page: Page;
559
560 if (proxy) {
561 context = await browserService.createBrowserContext(proxy.url);
562 page = await context.newPage();
563 times.proxyPageTime = Date.now() - startTime - times.proxyTime;
564 } else {
565 page = await browserService.getPrimaryPage();
566 times.pageTime = Date.now() - startTime;
567 }
568
569 if (url) {
570 const normalizedUrl = normalizeUrl(url);
571 if (!normalizedUrl) {
572 throw new Error(`Invalid URL: ${url}`);
573 }
574 await page.goto(normalizedUrl, { timeout: 30000, waitUntil: "domcontentloaded" });
575 times.pageLoadTime = Date.now() - times.pageTime - times.proxyTime - startTime;
576 }
577
578 if (delay) {
579 await new Promise((resolve) => setTimeout(resolve, delay));
580 }
581
582 const pdf = await page.pdf();
583 times.pdfTime = Date.now() - times.pageLoadTime - times.pageTime - times.proxyTime - startTime;
584
585 if (logUrl) {
586 await updateLog(logUrl, { times });
587 }
588
589 return reply.send(pdf);
590 } catch (e: unknown) {

Callers 2

routesFunction · 0.85
routesFunction · 0.85

Calls 11

normalizeUrlFunction · 0.85
updateLogFunction · 0.85
getErrorsFunction · 0.85
isRunningMethod · 0.80
createBrowserContextMethod · 0.80
getPrimaryPageMethod · 0.80
sendMethod · 0.80
refreshPrimaryPageMethod · 0.80
listenMethod · 0.65
closeMethod · 0.65
launchMethod · 0.45

Tested by

no test coverage detected