Skip to content

Commit

Permalink
fix(api): configurable puppeteer headless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashroch committed Aug 22, 2024
1 parent e5fa65e commit 3ad644e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
4 changes: 4 additions & 0 deletions apps/api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { SessionConfig } from './session';
import app from './app';
import cache from './cache';
import filesystem from './filesystem';
import pdf, { type PdfConfig } from './pdf';
import { publisherConfig as publisher, subscriberConfig as subscriber } from './pub-sub';
import queue from './queue';
import rateLimiter from './rate-limiter';
Expand All @@ -28,6 +29,7 @@ export * from './app';
export * from './cache';
export * from './filesystem';
export * from './image-processor';
export * from './pdf';
export * from './queue';
export * from './rate-limiter';
export * from './security';
Expand All @@ -42,6 +44,7 @@ export type Config = {
filesystem: FileSystemConfig;
log: LogConfig;
mail: MailConfig;
pdf: PdfConfig;
queue: QueueConfig;
rateLimiter: RateLimiterConfig;
security: SecurityConfig;
Expand All @@ -60,6 +63,7 @@ const config: Config = {
filesystem,
log,
mail,
pdf,
queue,
rateLimiter,
security,
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/config/pdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod';

export const pdfConfig = z.object({
puppeteer: z.object({
headless: z.union([z.literal('shell'), z.boolean()]).default(true),
}),
});
export type PdfConfig = z.infer<typeof pdfConfig>;
export type PuppeteerOptions = PdfConfig['puppeteer'];

export default pdfConfig.parse({
puppeteer: {
headless: process.env.PUPPETEER_HEADLESS === 'true' ? true : process.env.PUPPETEER_HEADLESS,
},
});
2 changes: 2 additions & 0 deletions apps/api/src/ioc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface IoC extends Jobs {
fsConfig: Config['filesystem'];
logConfig: Config['log'];
mailConfig: Config['mail'];
pdfConfig: Config['pdf'];
queueConfig: Config['queue'];
rateLimiterConfig: Config['rateLimiter'];
publisherConfig: Config['publisher'];
Expand Down Expand Up @@ -228,6 +229,7 @@ function configureContainer() {
fsConfig: asValue(config.filesystem),
logConfig: asValue(config.log),
mailConfig: asValue(config.mail),
pdfConfig: asValue(config.pdf),
queueConfig: asValue(config.queue),
rateLimiterConfig: asValue(config.rateLimiter),
securityConfig: asValue(config.security),
Expand Down
10 changes: 7 additions & 3 deletions apps/api/src/services/feedback/feedback-pdf-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import { Readable } from 'node:stream';
import type { CookieParam } from 'puppeteer';
import puppeteer from 'puppeteer';

import { PuppeteerOptions } from '@intake24/api/config';

export default class FeedbackPdfGenerator {
readonly url;
readonly refreshCookie;
readonly options;

constructor(url: string, cookie: CookieParam) {
constructor(url: string, cookie: CookieParam, options: PuppeteerOptions) {
this.url = url;
this.refreshCookie = cookie;
this.options = options;
}

async loadFeedback() {
const browser = await puppeteer.launch({ headless: true });
const browser = await puppeteer.launch({ headless: this.options.headless });

try {
const page = await browser.newPage();
Expand All @@ -24,7 +28,7 @@ export default class FeedbackPdfGenerator {
return { browser, page };
}
catch (error) {
browser.close();
await browser.close();
throw error;
}
}
Expand Down
7 changes: 4 additions & 3 deletions apps/api/src/services/feedback/feedback.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export type CreateRefreshCookie = {
function feedbackService({
appConfig,
fsConfig,
pdfConfig,
securityConfig,
jwtService,
}: Pick<IoC, 'appConfig' | 'fsConfig' | 'securityConfig' | 'jwtService'>) {
}: Pick<IoC, 'appConfig' | 'fsConfig' | 'pdfConfig' | 'securityConfig' | 'jwtService'>) {
const getNutrientTypes = async (): Promise<NutrientType[]> => {
const nutrients = await FoodsNutrientType.findAll({
include: [
Expand Down Expand Up @@ -113,7 +114,7 @@ function feedbackService({
const { url, filename, slug, userId } = await getFeedbackLinks(surveyId, username, submissions);
const cookie = await createRefreshCookie({ slug, username, userId });

const pdfStream = await new FeedbackPdfGenerator(url, cookie).getPdfStream();
const pdfStream = await new FeedbackPdfGenerator(url, cookie, pdfConfig.puppeteer).getPdfStream();

return { pdfStream, filename, url };
};
Expand All @@ -128,7 +129,7 @@ function feedbackService({

const path = resolve(fsConfig.local.downloads, filename);

await new FeedbackPdfGenerator(url, cookie).getPdfFile(path);
await new FeedbackPdfGenerator(url, cookie, pdfConfig.puppeteer).getPdfFile(path);

return { path, filename, url };
};
Expand Down
12 changes: 12 additions & 0 deletions docs/config/api/pdf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# PDF

PDF generation is implemented using [puppeteer](https://pptr.dev).

## Puppeteer

### Headless

- object-path: `puppeteer.headless`
- dotenv var: `PUPPETEER_HEADLESS`
- type: `boolean | 'shell'`
- default: `true`

0 comments on commit 3ad644e

Please sign in to comment.