Skip to content

Commit

Permalink
Merge pull request #409 from marp-team/puppeteer-timeout
Browse files Browse the repository at this point in the history
Allow to set timeout for Puppeteer actions by `PUPPETEER_TIMEOUT` env
  • Loading branch information
yhatt authored Jan 9, 2022
2 parents 5f490b3 + 2f1a4d2 commit 8d37880
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Allow to set timeout for Puppeteer actions by `PUPPETEER_TIMEOUT` env ([#409](https://github.com/marp-team/marp-cli/pull/409))

### Changed

- Upgrade Marpit to [v2.2.1](https://github.com/marp-team/marpit/releases/tag/v2.2.1) ([#408](https://github.com/marp-team/marp-cli/pull/408))
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,20 @@ export class MarpCLIConfig {
return scale
})()

const puppeteerTimeout = (() => {
if (process.env['PUPPETEER_TIMEOUT']) {
const envTimeout = Number.parseInt(process.env['PUPPETEER_TIMEOUT'], 10)
if (!Number.isNaN(envTimeout)) return envTimeout
}
return undefined
})()

return {
imageScale,
inputDir,
output,
preview,
puppeteerTimeout,
server,
template,
templateOption,
Expand Down
20 changes: 17 additions & 3 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface ConverterOption {
pages?: boolean | number[]
pdfNotes?: boolean
preview?: boolean
puppeteerTimeout?: number
jpegQuality?: number
server?: boolean
template: string
Expand Down Expand Up @@ -105,6 +106,10 @@ export class Converter {
return template
}

get puppeteerTimeout(): number {
return this.options.puppeteerTimeout ?? 30000
}

async convert(
markdown: string,
file?: File,
Expand Down Expand Up @@ -259,7 +264,11 @@ export class Converter {

ret.buffer = await this.usePuppeteer(html, async (page, uri) => {
await page.goto(uri, { waitUntil: ['domcontentloaded', 'networkidle0'] })
return await page.pdf({ printBackground: true, preferCSSPageSize: true })
return await page.pdf({
printBackground: true,
preferCSSPageSize: true,
timeout: this.puppeteerTimeout,
})
})

// Apply PDF metadata and annotations
Expand Down Expand Up @@ -482,7 +491,9 @@ export class Converter {
})()

try {
const browser = await Converter.runBrowser()
const browser = await Converter.runBrowser({
timeout: this.puppeteerTimeout,
})

const uri = await (async () => {
if (tmpFile) {
Expand All @@ -496,6 +507,8 @@ export class Converter {
})()

const page = await browser.newPage()
page.setDefaultTimeout(this.puppeteerTimeout)

const { missingFileSet, failedFileSet } =
this.trackFailedLocalFileAccess(page)

Expand Down Expand Up @@ -560,12 +573,13 @@ export class Converter {

private static browser?: puppeteer.Browser

private static async runBrowser() {
private static async runBrowser({ timeout }: { timeout?: number }) {
if (!Converter.browser) {
const baseArgs = generatePuppeteerLaunchArgs()

Converter.browser = await launchPuppeteer({
...baseArgs,
timeout,
userDataDir: await generatePuppeteerDataDirPath('marp-cli-conversion', {
wslHost: isChromeInWSLHost(baseArgs.executablePath),
}),
Expand Down
25 changes: 25 additions & 0 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Options } from '@marp-team/marpit'
import cheerio from 'cheerio'
import { imageSize } from 'image-size'
import { PDFDocument, PDFDict, PDFName, PDFHexString } from 'pdf-lib'
import { TimeoutError } from 'puppeteer-core'
import { Page } from 'puppeteer-core/lib/cjs/puppeteer/common/Page'
import yauzl from 'yauzl'
import { Converter, ConvertType, ConverterOption } from '../src/converter'
Expand Down Expand Up @@ -80,6 +81,17 @@ describe('Converter', () => {
})
})

describe('get #puppeteerTimeout', () => {
it('returns specified timeout', () => {
expect(instance({ puppeteerTimeout: 1000 }).puppeteerTimeout).toBe(1000)
expect(instance({ puppeteerTimeout: 0 }).puppeteerTimeout).toBe(0)
})

it('returns the default timeout 30000ms if not specified', () => {
expect(instance().puppeteerTimeout).toBe(30000)
})
})

describe('#convert', () => {
const md = '# <i>Hello!</i>'
const dummyFile = new File(process.cwd())
Expand Down Expand Up @@ -518,6 +530,19 @@ describe('Converter', () => {
)
})
})

describe('with custom puppeteer timeout', () => {
it('follows setting timeout', async () => {
;(fs as any).__mockWriteFile()

await expect(
pdfInstance({
output: 'test.pdf',
puppeteerTimeout: 1,
}).convertFile(new File(onePath))
).rejects.toThrow(TimeoutError)
})
})
})

describe('when convert type is PPTX', () => {
Expand Down
24 changes: 24 additions & 0 deletions test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,30 @@ describe('Marp CLI', () => {
)
})
})

describe('with PUPPETEER_TIMEOUT env', () => {
beforeEach(() => {
process.env.PUPPETEER_TIMEOUT = '12345'
})

afterEach(() => {
delete process.env.PUPPETEER_TIMEOUT
})

it('follows specified timeout in conversion that is using Puppeteer', async () => {
expect(
(await conversion(onePath, '--pdf')).options.puppeteerTimeout
).toBe(12345)
})

it('does not follows specified timeout if the env value is not valid number', async () => {
process.env.PUPPETEER_TIMEOUT = 'invalid'

expect(
(await conversion(onePath, '--pdf')).options.puppeteerTimeout
).toBeUndefined()
})
})
})

describe('with passing directory', () => {
Expand Down

0 comments on commit 8d37880

Please sign in to comment.