Skip to content

FNDSIT - Update dependencies #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
394 changes: 304 additions & 90 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"homepage": "https://github.com/alvarcarto/url-to-pdf-api#readme",
"dependencies": {
"bluebird": "^3.5.0",
"body-parser": "^1.18.2",
"compression": "^1.7.1",
"cors": "^2.8.4",
Expand All @@ -32,8 +31,7 @@
"lodash": "^4.17.15",
"morgan": "^1.9.1",
"normalize-url": "^5.0.0",
"pdf-parse": "^1.1.1",
"puppeteer": "^2.0.0",
"puppeteer": "^18.0.0",
"server-destroy": "^1.0.1",
"winston": "^2.3.1"
},
Expand All @@ -45,6 +43,7 @@
"eslint-plugin-import": "^2.7.0",
"mocha": "^4.0.1",
"nodemon": "^1.12.1",
"pdf-parse": "^1.1.1",
"supertest": "^3.0.0"
}
}
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const config = {
BROWSER_EXECUTABLE_PATH: process.env.BROWSER_EXECUTABLE_PATH,
API_TOKENS: [],
ALLOW_URLS: [],
TEST: false,
};

if (process.env.API_TOKENS) {
Expand Down
45 changes: 36 additions & 9 deletions src/core/render-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ const _ = require('lodash');
const config = require('../config');
const logger = require('../util/logger')(__filename);

let browserInstance = null;
let browserCalls = 0;

async function createBrowser(opts) {
if (browserInstance) {
logger.info('Reusing browser');
return browserInstance;
}

logger.info('Creating new browser');

const browserOpts = {
ignoreHTTPSErrors: opts.ignoreHttpsErrors,
sloMo: config.DEBUG_MODE ? 250 : undefined,
Expand All @@ -21,7 +30,16 @@ async function createBrowser(opts) {
if (!opts.enableGPU || navigator.userAgent.indexOf('Win') !== -1) {
browserOpts.args.push('--disable-gpu');
}
return puppeteer.launch(browserOpts);
browserInstance = newBrowserInstance(browserOpts);

return browserInstance;
}

async function newBrowserInstance(browserOpts) {
const browserInst = await puppeteer.launch(browserOpts);
browserInst.on('disconnected', async () => { await newBrowserInstance(browserOpts) });

return browserInst;
}

async function getFullPageHeight(page) {
Expand Down Expand Up @@ -81,6 +99,7 @@ async function render(_opts = {}) {
logger.error(`Error event emitted: ${err}`);
logger.error(err.stack);
browser.close();
browserInstance = null;
});


Expand Down Expand Up @@ -108,7 +127,7 @@ async function render(_opts = {}) {
await page.setViewport(opts.viewport);
if (opts.emulateScreenMedia) {
logger.info('Emulate @media screen..');
await page.emulateMedia('screen');
await page.emulateMediaType('screen');
}

if (opts.cookies && opts.cookies.length > 0) {
Expand All @@ -128,9 +147,14 @@ async function render(_opts = {}) {
await page.goto(opts.url, opts.goto);
}

if (_.isNumber(opts.waitFor) || _.isString(opts.waitFor)) {
if (_.isNumber(opts.waitFor)) {
logger.info(`Wait for ${opts.waitFor} ..`);
await page.waitFor(opts.waitFor);
await page.waitForTimeout(opts.waitFor);
}

if (_.isString(opts.waitFor)) {
logger.info(`Wait for selector ${opts.waitFor} ..`);
await page.waitForSelector(opts.waitFor);
}

if (opts.scrollPage) {
Expand Down Expand Up @@ -169,8 +193,7 @@ async function render(_opts = {}) {

if (opts.output === 'pdf') {
if (opts.pdf.fullPage) {
const height = await getFullPageHeight(page);
opts.pdf.height = height;
opts.pdf.height = await getFullPageHeight(page);
}
data = await page.pdf(opts.pdf);
} else if (opts.output === 'html') {
Expand All @@ -197,9 +220,13 @@ async function render(_opts = {}) {
logger.error(err.stack);
throw err;
} finally {
logger.info('Closing browser..');
if (!config.DEBUG_MODE) {
browserCalls += 1;
logger.info(`Current browser calls: ${browserCalls}`);

if (browserCalls > 50 || config.TEST) {
logger.info('Restarting browser!');
await browser.close();
browserInstance = null;
}
}

Expand All @@ -214,7 +241,7 @@ async function scrollPage(page) {
const bottomThreshold = 400;

function bottomPos() {
return window.pageYOffset + window.innerHeight;
return window.scrollY + window.innerHeight;
}

return new Promise((resolve, reject) => {
Expand Down
6 changes: 0 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const createApp = require('./app');
const enableDestroy = require('server-destroy');
const BPromise = require('bluebird');
const logger = require('./util/logger')(__filename);
const config = require('./config');

BPromise.config({
warnings: config.NODE_ENV !== 'production',
longStackTraces: true,
});

const app = createApp();
const server = app.listen(config.PORT, () => {
logger.info(
Expand Down
3 changes: 1 addition & 2 deletions src/util/express.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require('lodash');
const BPromise = require('bluebird');

// Route which assumes that the Promise `func` returns, will be resolved
// with data which will be sent as json response.
Expand Down Expand Up @@ -28,7 +27,7 @@ function createRoute(func, responseHandler) {
let valuePromise = callback();
if (!_.isFunction(_.get(valuePromise, 'then'))) {
// It was a not a Promise, so wrap it as a Promise
valuePromise = BPromise.resolve(valuePromise);
valuePromise = Promise.resolve(valuePromise);
}

if (_.isFunction(responseHandler)) {
Expand Down
12 changes: 6 additions & 6 deletions src/util/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const sharedQuerySchema = Joi.object({
enableGPU: Joi.boolean(),
ignoreHttpsErrors: Joi.boolean(),
waitFor: Joi.alternatives([
Joi.number().min(1).max(60000),
Joi.string().min(1).max(2000),
]),
Joi.number().min(0).max(60000),
Joi.string()
]).allow(null),
cookies: Joi.array().items(cookieSchema),
output: Joi.string().valid(['pdf', 'screenshot', 'html']),
'viewport.width': Joi.number().min(1).max(30000),
Expand Down Expand Up @@ -87,9 +87,9 @@ const renderBodyObject = Joi.object({
isLandscape: Joi.boolean(),
}),
waitFor: Joi.alternatives([
Joi.number().min(1).max(60000),
Joi.string().min(1).max(2000),
]),
Joi.number().min(0).max(60000),
Joi.string()
]).allow(null),
goto: Joi.object({
timeout: Joi.number().min(0).max(60000),
waitUntil: Joi.string().min(1).max(2000),
Expand Down
7 changes: 2 additions & 5 deletions test/test-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
const chai = require('chai');
const fs = require('fs');
const request = require('supertest');
const BPromise = require('bluebird');
const { getResource } = require('./util');
const pdf = require('pdf-parse');
const createApp = require('../src/app');
const config = require('../src/config');

const DEBUG = false;

BPromise.config({
longStackTraces: true,
});
config.TEST = true;

const app = createApp();

Expand Down