From 40aae0e465c3da8c98eeb61cb4f5bc71943d6e01 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Fri, 29 Jun 2018 12:27:55 +0200 Subject: [PATCH] Unify browserless instance --- package.json | 2 +- src/index.js | 31 +++++-------------------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index fe8ee73..b8075e8 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "request" ], "dependencies": { - "browserless": "~3.6.1", + "browserless": "~3.7.0", "got": "~8.3.1", "html-encode": "~2.0.1", "parse-domain": "~2.1.2", diff --git a/src/index.js b/src/index.js index 2f3b5ab..379c8dd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,35 +1,13 @@ 'use strict' +const { create: createBrowserless } = require('browserless') const parseDomain = require('parse-domain') -const browserless = require('browserless') const htmlEncode = require('html-encode') const timeSpan = require('time-span') const got = require('got') const autoDomains = require('./auto-domains') -const PUPPETEER_CONFIG = { - ignoreHTTPSErrors: true, - args: [ - '--disable-notifications', - '--disable-offer-store-unmasked-wallet-cards', - '--disable-offer-upload-credit-cards', - '--disable-setuid-sandbox', - '--enable-async-dns', - '--enable-simple-cache-backend', - '--enable-tcp-fast-open', - '--media-cache-size=33554432', - '--no-default-browser-check', - '--no-pings', - '--no-sandbox', - '--no-zygote', - '--prerender-from-omnibox=disabled', - '--single-process' - ] -} - -const createBrowserless = () => browserless(PUPPETEER_CONFIG) - const fetch = async (url, { toEncode, ...opts }) => { const res = await got(url, { encoding: null, ...opts }) return toEncode(res.body, res.headers['content-type']) @@ -37,9 +15,10 @@ const fetch = async (url, { toEncode, ...opts }) => { const prerender = async ( url, - { getBrowserless = createBrowserless, gotOptions, toEncode, ...opts } + { getBrowserless, gotOptions, toEncode, ...opts } ) => { const fetchData = fetch(url, { toEncode, ...gotOptions }) + const browserless = await createBrowserless() let html try { @@ -63,7 +42,7 @@ const getFetchMode = (url, { prerender }) => { module.exports = async ( url, { - browserless, + getBrowserless = createBrowserless, encoding = 'utf-8', fetchMode = getFetchMode, gotOptions, @@ -76,7 +55,7 @@ module.exports = async ( const opts = mode === 'fetch' ? { toEncode, ...gotOptions } - : { toEncode, browserless, gotOptions, ...puppeteerOpts } + : { toEncode, getBrowserless, gotOptions, ...puppeteerOpts } const time = timeSpan() const html = await FETCH_MODE[mode](url, opts) return { html, stats: { mode, timing: time() } }