Skip to content

Commit

Permalink
feat: Add timeout flag (#46)
Browse files Browse the repository at this point in the history
* feat: Add timeout flag

Fixes #43

* Ensure thrown error exists Puppeteer
  • Loading branch information
nzakas authored Jan 13, 2023
1 parent 3ddf36c commit c5a114e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function setupDebugMessages(printer) {
}
}

const argv = yargs(process.argv.slice(2))
const cargv = yargs(process.argv.slice(2))
.scriptName("print-ready")
.version(false)
.options({
Expand All @@ -44,10 +44,9 @@ const argv = yargs(process.argv.slice(2))
type: "boolean",
describe: "Turn on debugging messages."
},
help: {
alias: "h",
type: "boolean",
describe: "Show the help screen."
timeout: {
type: "number",
describe: "Set the rendering timeout in milliseconds."
},
version: {
alias: "v",
Expand All @@ -56,21 +55,22 @@ const argv = yargs(process.argv.slice(2))
}
})
.usage("$0 [options] filename")
.argv;
.help();

//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------

(async() => {
const printer = new Printer();
const argv = cargv.argv;
const printer = new Printer({ timeout: argv.timeout });

if (argv.debug) {
setupDebugMessages(printer);
}

if (!argv._.length) {
argv.showHelp();
cargv.showHelp();
process.exit(1);
}

Expand Down
30 changes: 27 additions & 3 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function createPuppeteerOptions() {
* PDF options.
*/
function createPdfOptions(options = {}) {
return {
const pdfOptions = {
printBackground: true,
displayHeaderFooter: false,
preferCSSPageSize: options.width ? false : true,
Expand All @@ -85,6 +85,12 @@ function createPdfOptions(options = {}) {
left: 0
}
};

if (typeof options.timeout === "number") {
pdfOptions.timeout = options.timeout;
}

return pdfOptions;
}

/**
Expand Down Expand Up @@ -162,7 +168,8 @@ async function setPdfMeta(page, pdf) {
export class Printer extends EventEmitter {

constructor({
cwd = process.cwd()
cwd = process.cwd(),
timeout
} = {}) {

super();
Expand All @@ -172,6 +179,12 @@ export class Printer extends EventEmitter {
* @type {string}
*/
this.cwd = cwd;

/**
* The timeout for the printer rendering.
* @type {number|undefined}
*/
this.timeout = timeout;
}

get supportedEvents() {
Expand Down Expand Up @@ -320,7 +333,18 @@ export class Printer extends EventEmitter {

// generate the PDF
this.emit("pdfstart", { url });
const blob = await page.pdf(createPdfOptions());
let blob;

try {
blob = await page.pdf(createPdfOptions({
timeout: this.timeout
}));
} catch (error) {
await page.close();
await browser.close();
throw error;
}

this.emit("pdfend", { url });

const pdf = await PDFDocument.load(blob);
Expand Down
33 changes: 33 additions & 0 deletions tests/printer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ const FIXTURES_DIR = path.resolve(__dirname, "fixtures");

describe("Printer", () => {

describe("new Printer()", () => {
it("should have default cwd", () => {
const printer = new Printer();
expect(printer.cwd).to.equal(process.cwd());
});

it("should save cwd", () => {
const printer = new Printer({ cwd: "foo" });
expect(printer.cwd).to.equal("foo");
});

it("should save timeout", () => {
const printer = new Printer({ timeout: 500 });
expect(printer.timeout).to.equal(500);
});
});

describe("printFileToPdf()", () => {

Expand Down Expand Up @@ -71,7 +87,24 @@ describe("Printer", () => {

});

describe("setting timeout", () => {

const filePath = path.resolve(FIXTURES_DIR, "one-page.html");

it("should throw a timeout error", () => {

const printer = new Printer({ timeout: 1 });

return printer.printFileToPdf(filePath)
.then(() => {
expect.fail("Promise should be rejected");
})
.catch(error => {
expect(error.message).to.match(/timeout 1ms exceeded/);
});
});

});

});
});

0 comments on commit c5a114e

Please sign in to comment.