Skip to content

Commit

Permalink
Feature http headers (#276)
Browse files Browse the repository at this point in the history
Implement feature flag to pass headers to Puppeteer via "headers" option.

Also, add feature flag to set headless mode via "headless" option.

Update headless behavior to sync with puppeteer API change. See: https://developer.chrome.com/articles/new-headless/#new-headless-in-puppeteer
  • Loading branch information
ddemoss222 committed May 22, 2023
1 parent 382b7e4 commit 21a8d26
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion decktape.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ parser.script('decktape').options({
transform : parseRange,
help : 'Range of slides to be exported, a combination of slide indexes and ranges (e.g. \'1-3,5,8\')',
},
headless : {
default : 'new', // false to enable headed mode and true to enable old puppeteer headless. See: https://developer.chrome.com/articles/new-headless/#new-headless-in-puppeteer
help : 'Control headless mode puppeteer.',
},
headers : {
type : 'string',
callback : parseHeaders,
transform : parseHeaders,
help : 'Add headers to puppetter page instance. Comma deliminated list of strings. <header>,<value>. E.g. -headers "Authorization,\'Bearer ASDJASLKJALKSJDL\'"',
},
// Chrome options
chromePath : {
full : 'chrome-path',
Expand Down Expand Up @@ -137,6 +147,18 @@ parser.script('decktape').options({
},
});

function parseHeaders(headerString) {
const h = headerString.split(",")
if ((h.length % 2) != 0) {
return 'header flag must be a comma delimited key value pairing and should always have an even number of kv pairs'
}
let headers = {}
for (let i = 0; i < h.length; i += 2) {
headers[h[i]] = h[i+1]
}
return headers
}

function parseSize(size) {
// we may want to support height and width labeled with units
// /^(\d+(?:px)?|\d+(?:\.\d+)?(?:in|cm|mm)?)\s?x\s?(\d+(?:px)?|\d+(?:\.\d+)?(?:in|cm|mm)?)$/
Expand Down Expand Up @@ -230,13 +252,15 @@ process.on('unhandledRejection', error => {
const options = parser.parse(process.argv.slice(2));

const browser = await puppeteer.launch({
headless : 'new',
headless : options.headless,
// TODO: add a verbose option
// dumpio : true,
executablePath : options.chromePath,
args : options.chromeArgs,
});
const page = await browser.newPage();
if (options.headers)
page.setExtraHTTPHeaders(options.headers)
await page.emulateMediaType('screen');
const pdf = await PDFDocument.create();
pdf.setCreator('Decktape');
Expand Down

0 comments on commit 21a8d26

Please sign in to comment.