Skip to content

Commit

Permalink
Unify browserless instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jun 29, 2018
1 parent 3f7fb90 commit 40aae0e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 5 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
'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'])
}

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 {
Expand All @@ -63,7 +42,7 @@ const getFetchMode = (url, { prerender }) => {
module.exports = async (
url,
{
browserless,
getBrowserless = createBrowserless,
encoding = 'utf-8',
fetchMode = getFetchMode,
gotOptions,
Expand All @@ -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() } }
Expand Down

0 comments on commit 40aae0e

Please sign in to comment.