Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
handle new puppeteer parameters, not available to user
  • Loading branch information
dangowans committed Aug 14, 2024
1 parent 1403d34 commit 3809cfe
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 267 deletions.
1 change: 0 additions & 1 deletion .deepsource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ name = "javascript"

[analyzers.meta]
environment = [
"mocha",
"nodejs"
]
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.nyc_output/
bin/daemon/
coverage/
node_modules/

Expand Down
2 changes: 1 addition & 1 deletion defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const defaultPdfPuppeteerOptions = {
};
export const defaultPuppeteerOptions = {
timeout: 30_000,
product: 'chrome'
browser: 'chrome'
};
export const htmlNavigationTimeoutMillis = 60_000;
export const urlNavigationTimeoutMillis = htmlNavigationTimeoutMillis * 2;
2 changes: 1 addition & 1 deletion defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const defaultPdfPuppeteerOptions: PDFPuppeteerOptions = {

export const defaultPuppeteerOptions: puppeteer.LaunchOptions = {
timeout: 30_000,
product: 'chrome'
browser: 'chrome'
}

export const htmlNavigationTimeoutMillis = 60_000
Expand Down
3 changes: 1 addition & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="node" />
import { type puppeteer } from '@cityssm/puppeteer-launch';
import { type PDFPuppeteerOptions } from './defaultOptions.js';
export declare function convertHTMLToPDF(html: string, instancePdfOptions?: puppeteer.PDFOptions, instancePdfPuppeteerOptions?: Partial<PDFPuppeteerOptions>): Promise<Buffer>;
export declare function convertHTMLToPDF(html: string, instancePdfOptions?: puppeteer.PDFOptions, instancePdfPuppeteerOptions?: Partial<PDFPuppeteerOptions>): Promise<Uint8Array>;
export default convertHTMLToPDF;
export declare function closeCachedBrowser(): Promise<void>;
export declare function hasCachedBrowser(): boolean;
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ export async function convertHTMLToPDF(html, instancePdfOptions = {}, instancePd
}
catch (error) {
if (isRunningPdfGeneration &&
defaultPuppeteerOptions.product === 'chrome') {
defaultPuppeteerOptions.browser === 'chrome') {
if (!doCloseBrowser) {
await closeCachedBrowser();
}
defaultPuppeteerOptions.product = 'firefox';
defaultPuppeteerOptions.browser = 'firefox';
debug('Trying again with Firefox.');
return await convertHTMLToPDF(html, instancePdfOptions, instancePdfPuppeteerOptions);
}
Expand Down
15 changes: 8 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ let cachedBrowser: puppeteer.Browser | undefined

/**
* Converts HTML or a webpage into HTML using Puppeteer.
* @param {string} html - An HTML string, or a URL.
* @param {puppeteer.PDFOptions} instancePdfOptions - PDF options for Puppeteer.
* @param {Partial<PDFPuppeteerOptions>} instancePdfPuppeteerOptions - pdf-puppeteer options.
* @returns {Promise<Buffer>} - A Buffer of PDF data.
* @param html - An HTML string, or a URL.
* @param instancePdfOptions - PDF options for Puppeteer.
* @param instancePdfPuppeteerOptions - pdf-puppeteer options.
* @returns - A Buffer of PDF data.
*/
export async function convertHTMLToPDF(
html: string,
instancePdfOptions: puppeteer.PDFOptions = {},
instancePdfPuppeteerOptions: Partial<PDFPuppeteerOptions> = {}
): Promise<Buffer> {
): Promise<Uint8Array> {
if (typeof html !== 'string') {
throw new TypeError(
'Invalid Argument: HTML expected as type of string and received a value of a different type. Check your request body and request headers.'
Expand Down Expand Up @@ -131,13 +131,13 @@ export async function convertHTMLToPDF(
} catch (error) {
if (
isRunningPdfGeneration &&
defaultPuppeteerOptions.product === 'chrome'
defaultPuppeteerOptions.browser === 'chrome'
) {
if (!doCloseBrowser) {
await closeCachedBrowser()
}

defaultPuppeteerOptions.product = 'firefox'
defaultPuppeteerOptions.browser = 'firefox'

debug('Trying again with Firefox.')

Expand All @@ -147,6 +147,7 @@ export async function convertHTMLToPDF(
instancePdfPuppeteerOptions
)
} else {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw error
}
} finally {
Expand Down
Loading

0 comments on commit 3809cfe

Please sign in to comment.