Skip to content
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

feat: better process management #280

Merged
merged 86 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
d2e274b
feat: add reconnect method
Kikobeats May 9, 2021
f8a05c8
v8.9.0-alpha.0
Kikobeats May 9, 2021
c38cfca
refactor: improve api
Kikobeats May 10, 2021
c7219fb
v9.0.0-alpha.0
Kikobeats May 10, 2021
efbec94
refactor: tweaks
Kikobeats May 10, 2021
48053d5
build(wip): avoid respawn
Kikobeats May 10, 2021
33cb6a4
v9.0.0-alpha.1
Kikobeats May 10, 2021
7294a7e
refactor: better way to respawn browser process
Kikobeats May 10, 2021
a832e0d
build: update respawn interface
Kikobeats May 10, 2021
a4433be
v9.0.0-alpha.2
Kikobeats May 10, 2021
dcfdb80
build: remove duplicate flags
Kikobeats May 10, 2021
f8193f0
build: wait until page is closed
Kikobeats May 10, 2021
99bab57
v9.0.0-alpha.3
Kikobeats May 10, 2021
a32b0e2
build(revert): wait until page is closed
Kikobeats May 10, 2021
70f3cec
v9.0.0-alpha.4
Kikobeats May 10, 2021
acaf27e
fix: throw an error if browser is not connected
Kikobeats May 10, 2021
4cb70a5
build: add debug log
Kikobeats May 10, 2021
a01d865
v9.0.0-alpha.5
Kikobeats May 10, 2021
f3e4f7c
fix: await respawn
Kikobeats May 10, 2021
e2dc993
build: remove debug logs
Kikobeats May 10, 2021
a5ab81f
v9.0.0-alpha.6
Kikobeats May 10, 2021
944db57
fix(revert): await respawn
Kikobeats May 10, 2021
adfb41c
v9.0.0-alpha.7
Kikobeats May 10, 2021
77cfc7c
fix: prevent respawn after respawn
Kikobeats May 10, 2021
63e8977
v9.0.0-alpha.8
Kikobeats May 10, 2021
9835254
build: add debug log
Kikobeats May 10, 2021
0dcdd49
v9.0.0-alpha.9
Kikobeats May 10, 2021
855d565
build: use a different context per session
Kikobeats May 10, 2021
b4d32d6
v9.0.0-alpha.10
Kikobeats May 10, 2021
1c33e0f
build: use browser contexts by default
Kikobeats May 11, 2021
10d3355
v9.0.0-alpha.11
Kikobeats May 11, 2021
51e37d7
build: update link reference
Kikobeats May 13, 2021
d8dde3c
fix(function): connect to browser instance properly
Kikobeats May 13, 2021
ec2d0e7
v9.0.0-alpha.12
Kikobeats May 13, 2021
a899428
test: update tests
Kikobeats May 13, 2021
443cdc2
build: createContext returns a promise
Kikobeats May 13, 2021
0d8f271
v9.0.0-alpha.13
Kikobeats May 13, 2021
201f020
build: add respawn debug flag
Kikobeats May 13, 2021
f67fbe7
v9.0.0-alpha.14
Kikobeats May 13, 2021
254554b
build: add closePage debug log
Kikobeats May 13, 2021
c7f4f73
v9.0.0-alpha.15
Kikobeats May 13, 2021
254012c
fix: avoid finally
Kikobeats May 13, 2021
9636ca7
v9.0.0-alpha.16
Kikobeats May 13, 2021
d95d1b7
build: log error code
Kikobeats May 14, 2021
b739a07
v9.0.0-alpha.17
Kikobeats May 14, 2021
5461af3
build: remove close lock
Kikobeats May 14, 2021
4fb4af6
v9.0.0-alpha.18
Kikobeats May 14, 2021
851e18e
fix: destroy context when possible
Kikobeats May 14, 2021
88e8b3b
v9.0.0-alpha.19
Kikobeats May 14, 2021
61da28d
fix: respawn under EPROTOCOL error
Kikobeats May 14, 2021
a7e9339
v9.0.0-alpha.20
Kikobeats May 14, 2021
abe88e4
refactor: rewrite lock logic
Kikobeats May 14, 2021
c143de8
v9.0.0-alpha.21
Kikobeats May 14, 2021
a52d2c5
build: add cookies debug
Kikobeats May 14, 2021
f86c389
fix: remove respawn under EPROTOCOL
Kikobeats May 14, 2021
932d88d
refactor: clean respawn logic
Kikobeats May 14, 2021
5c25224
v9.0.0-alpha.22
Kikobeats May 14, 2021
3dfb6d7
fix: cookie subdomains
Kikobeats May 14, 2021
513a516
refactor: better cookies parser implementation
Kikobeats May 14, 2021
234b44e
v9.0.0-alpha.23
Kikobeats May 14, 2021
8a5c88d
refactor: improve cookie debug log
Kikobeats May 14, 2021
55306a0
v9.0.0-alpha.24
Kikobeats May 14, 2021
515c22e
test: adjust settings
Kikobeats May 15, 2021
630707f
fix: check if browser is closed
Kikobeats May 15, 2021
9e7b724
v9.0.0-alpha.25
Kikobeats May 15, 2021
6db03aa
fix: createContext returns a promise
Kikobeats May 18, 2021
ade361a
refactor: update cli interface
Kikobeats May 18, 2021
5756317
build: use once listener
Kikobeats May 18, 2021
a4ef23d
build: update dependencies
Kikobeats May 19, 2021
892db93
v9.0.0-alpha.26
Kikobeats May 19, 2021
c597684
build: increment spawn timeout
Kikobeats May 19, 2021
2c9aa78
v9.0.0-alpha.27
Kikobeats May 19, 2021
bc5e1bf
Revert "build: increment spawn timeout"
Kikobeats May 19, 2021
c473aed
v9.0.0-alpha.28
Kikobeats May 19, 2021
2235649
Merge branch 'master' into next
Kikobeats May 31, 2021
cb32a21
build: add proxy support per page
Kikobeats Jun 2, 2021
98d9ab9
build: add retry & proxy per context
Kikobeats Jun 4, 2021
0a908cd
v9.0.0-alpha.29
Kikobeats Jun 4, 2021
8e7b5d7
refactor: pass agent instead of proxy
Kikobeats Jun 6, 2021
97a4776
v9.0.0-alpha.30
Kikobeats Jun 6, 2021
316f111
chore: enable lossyDeviceName by default
Kikobeats Jun 15, 2021
f640859
docs: add v9 documentation
Kikobeats Jun 15, 2021
fec2b45
v9.0.0-alpha.31
Kikobeats Jun 15, 2021
d5bf99f
build: update dependencies
Kikobeats Jun 15, 2021
9272607
build: update dependencies
Kikobeats Jun 22, 2021
238387b
v9.0.0-rc.0
Kikobeats Jun 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
430 changes: 267 additions & 163 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "8.8.17",
"version": "9.0.0-rc.0",
"command": {
"bootstrap": {
"npmClientArgs": [
Expand Down
18 changes: 10 additions & 8 deletions packages/browserless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "browserless",
"description": "The Headless Chrome/Chromium perfomance driver for Node.js",
"homepage": "https://browserless.js.org",
"version": "8.8.17",
"version": "9.0.0-rc.0",
"main": "src/index.js",
"author": {
"email": "josefrancisco.verdu@gmail.com",
Expand Down Expand Up @@ -31,16 +31,17 @@
"text"
],
"dependencies": {
"@browserless/errors": "^8.8.8",
"@browserless/goto": "^8.8.14",
"@browserless/pdf": "^8.8.14",
"@browserless/screenshot": "^8.8.14",
"@browserless/errors": "^9.0.0-alpha.25",
"@browserless/goto": "^9.0.0-rc.0",
"@browserless/pdf": "^9.0.0-rc.0",
"@browserless/screenshot": "^9.0.0-rc.0",
"debug-logfmt": "~1.0.4",
"mutexify": "~1.3.1",
"p-reflect": "~2.1.0",
"p-retry": "~4.5.0",
"p-timeout": "~4.1.0",
"parse-proxy-uri": "~1.0.3",
"pidtree": "~0.5.0",
"puppeteer-proxy": "~2.1.2",
"require-one-of": "~1.0.15"
},
"devDependencies": {
Expand All @@ -55,10 +56,11 @@
"src"
],
"scripts": {
"test": "ava"
"test": "NODE_ENV=test ava"
},
"license": "MIT",
"ava": {
"timeout": "2m"
"timeout": "2m",
"verbose": true
}
}
107 changes: 51 additions & 56 deletions packages/browserless/src/driver.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
'use strict'

const debug = require('debug-logfmt')('browserless')
const requireOneOf = require('require-one-of')
const pReflect = require('p-reflect')
const pidtree = require('pidtree')

// flags explained: https://peter.sh/experiments/chromium-command-line-switches/
// default flags: https://github.com/puppeteer/puppeteer/blob/master/lib/Launcher.js#L269
// AWS Lambda flags: https://github.com/alixaxel/chrome-aws-lambda/blob/10feb8d162626d34aad2ee1e657f20956f53fe11/source/index.js
const args = ({ proxy } = {}) =>
[
// base
'--disable-cloud-import',
'--disable-gesture-typing',
'--disable-infobars',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-offer-upload-credit-cards',
'--disable-print-preview',
'--disable-speech-api',
'--disable-tab-for-desktop-share',
'--disable-translate',
'--disable-voice-input',
'--disable-wake-on-wifi',
'--enable-async-dns',
'--enable-simple-cache-backend',
'--enable-tcp-fast-open',
'--enable-webgl',
'--hide-scrollbars',
'--ignore-gpu-blocklist',
'--mute-audio',
'--no-default-browser-check',
'--no-pings',
'--no-zygote',
'--prerender-from-omnibox=disabled',
'--use-gl=swiftshader',
'--no-sandbox',
// disable navigator.webdriver
/// https://stackoverflow.com/a/60409220
// https://blog.m157q.tw/posts/2020/09/11/bypass-cloudflare-detection-while-using-selenium-with-chromedriver/
'--disable-blink-features=AutomationControlled',
// extra
'--disable-web-security',
'--font-render-hinting=none', // could be 'none', 'medium'
// '--enable-font-antialiasing'
// perf
// '--single-process',
// '--memory-pressure-off',
proxy && `--proxy-server=${proxy.protocol}://${proxy.hostname}:${proxy.port}`
].filter(Boolean)
const defaultArgs = [
// base
'--disable-cloud-import',
'--disable-gesture-typing',
'--disable-infobars',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-offer-upload-credit-cards',
'--disable-print-preview',
'--disable-speech-api',
'--disable-tab-for-desktop-share',
'--disable-translate',
'--disable-voice-input',
'--disable-wake-on-wifi',
'--enable-async-dns',
'--enable-simple-cache-backend',
'--enable-tcp-fast-open',
'--enable-webgl',
'--ignore-gpu-blocklist',
'--no-default-browser-check',
'--no-pings',
'--no-zygote',
'--prerender-from-omnibox=disabled',
'--use-gl=swiftshader',
'--no-sandbox',
// disable navigator.webdriver
/// https://stackoverflow.com/a/60409220
// https://blog.m157q.tw/posts/2020/09/11/bypass-cloudflare-detection-while-using-selenium-with-chromedriver/
'--disable-blink-features=AutomationControlled',
// extra
'--disable-web-security',
'--font-render-hinting=none' // could be 'none', 'medium'
// '--enable-font-antialiasing'
// perf
// '--single-process',
// '--memory-pressure-off',
]

const spawn = (puppeteer, { mode = 'launch', proxy, ...launchOpts }) =>
puppeteer[mode]({
ignoreHTTPSErrors: true,
// flags explained: https://peter.sh/experiments/chromium-command-line-switches/
// default flags: https://github.com/puppeteer/puppeteer/blob/master/lib/Launcher.js#L269
// AWS Lambda flags: https://github.com/alixaxel/chrome-aws-lambda/blob/10feb8d162626d34aad2ee1e657f20956f53fe11/source/index.js
args: args({ proxy }),
...launchOpts
})
// The param `timeout` means here the maximum time in milliseconds
// to wait for the browser instance to start
const spawn = ({
puppeteer = requireOneOf(['puppeteer', 'puppeteer-core', 'puppeteer-firefox']),
mode = 'launch',
...launchOpts
} = {}) => {
const args = launchOpts.args ? undefined : defaultArgs
return puppeteer[mode]({ ignoreHTTPSErrors: true, timeout: 5000, args, ...launchOpts })
}

const getPid = childProcess => {
if (!childProcess) return null
Expand All @@ -77,23 +75,20 @@ const close = async (childProcess, { signal = 'SIGKILL', ...debugOpts } = {}) =>
if (!pid) return

const pids = await getPids(pid)

pids.forEach(pid => {
try {
process.kill(pid, signal)
} catch (error) {
debug('error', { pid, signal, ...debugOpts, message: error.message || error })
}
} catch (_) {}
})

// It's necessary to call `browser.close` for removing temporal files associated
// and remove listeners attached to the main process
// see https://github.com/puppeteer/puppeteer/blob/main/src/node/BrowserRunner.ts#L129
// see https://github.com/puppeteer/puppeteer/blob/778ac92469d66c542c3c12fe0aa23703dd6315c2/src/node/BrowserRunner.ts#L146
if (childProcess.close) await childProcess.close()

debug('close', { pids, signal, ...debugOpts })

return { pids }
}

module.exports = { getPid, spawn, close, args }
module.exports = { spawn, getPid, close, defaultArgs }