Launch puppeteer browser on app start
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-09-14 09:38:48 +02:00
parent edf3f1e96e
commit 279120dd03
2 changed files with 16 additions and 8 deletions

View File

@ -1,11 +1,13 @@
const express = require('express'); const express = require('express');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const puppeteer = require('puppeteer');
const templatesPath = path.resolve(__dirname, './templates'); const templatesPath = path.resolve(__dirname, './templates');
const componentsPath = path.resolve(__dirname, './core/components'); const componentsPath = path.resolve(__dirname, './core/components');
const config = require('./core/config');
module.exports = app => { module.exports = async app => {
global.appPath = __dirname; global.appPath = __dirname;
process.env.OPENSSL_CONF = '/etc/ssl/'; process.env.OPENSSL_CONF = '/etc/ssl/';
@ -51,4 +53,16 @@ module.exports = app => {
app.use(`/api/${templateName}/assets`, express.static(assetsDir)); app.use(`/api/${templateName}/assets`, express.static(assetsDir));
}); });
}); });
// Instantiate Puppeteer browser
async function launchBrowser() {
config.browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
config.browser.on('disconnected', launchBrowser);
}
launchBrowser();
}; };

View File

@ -1,5 +1,4 @@
const fs = require('fs'); const fs = require('fs');
const puppeteer = require('puppeteer');
const path = require('path'); const path = require('path');
const config = require('./config'); const config = require('./config');
const Component = require('./component'); const Component = require('./component');
@ -28,11 +27,7 @@ class Report extends Component {
if (fs.existsSync(fullPath)) if (fs.existsSync(fullPath))
options = require(optionsPath); options = require(optionsPath);
const browser = await puppeteer.launch({ const page = await config.browser.newPage();
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
await page.emulateMedia('screen'); await page.emulateMedia('screen');
await page.setContent(template); await page.setContent(template);
@ -51,7 +46,6 @@ class Report extends Component {
options.footerTemplate = footer; options.footerTemplate = footer;
const buffer = await page.pdf(options); const buffer = await page.pdf(options);
await browser.close();
return buffer; return buffer;
} }