Skip to content
Merged
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
21 changes: 8 additions & 13 deletions packages/browserless/src/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,15 @@ const spawn = ({
...launchOpts
} = {}) => puppeteer[mode]({ ignoreHTTPSErrors: true, args, ...launchOpts })

const getProcess = subprocess => {
if (!subprocess) return
if ('process' in subprocess) return subprocess.process()
if ('pid' in subprocess) return subprocess
const pid = subprocess => {
if ('pid' in subprocess) return subprocess.pid
const browserProcess = 'process' in subprocess ? subprocess.process() : undefined
if (browserProcess === undefined || browserProcess === null) return
return 'pid' in browserProcess ? browserProcess.pid : undefined
}

const getPid = input => {
const subprocess = getProcess(input)
return subprocess ? subprocess.pid : undefined
}

const close = async (input, { signal = 'SIGKILL', ...debugOpts } = {}) => {
const subprocess = getProcess(input)
if (!subprocess) return
const close = async (subprocess, { signal = 'SIGKILL', ...debugOpts } = {}) => {
if (pid(subprocess) === undefined) return

// It's necessary to call `browser.close` for removing temporal files associated
// and remove listeners attached to the main process; check
Expand All @@ -74,4 +69,4 @@ const close = async (input, { signal = 'SIGKILL', ...debugOpts } = {}) => {
return { pid: subprocess.pid }
}

module.exports = { spawn, getPid, getProcess, close, defaultArgs }
module.exports = { spawn, pid, close, defaultArgs }
10 changes: 4 additions & 6 deletions packages/browserless/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ module.exports = ({ timeout: globalTimeout = 30000, ...launchOpts } = {}) => {

promise.then(async browser => {
browser.once('disconnected', getBrowser)
const pid = driver.getPid(browser)

debug('spawn', {
respawn: isRespawn,
pid: pid || launchOpts.mode,
pid: driver.pid(browser) || launchOpts.mode,
version: await browser.version()
})
})
Expand Down Expand Up @@ -99,7 +97,7 @@ module.exports = ({ timeout: globalTimeout = 30000, ...launchOpts } = {}) => {
getBrowserContext()
])
const page = await browserContext.newPage()
debug('createPage', { pid: driver.getPid(browserProcess), id: browserContext._id })
debug('createPage', { pid: driver.pid(browserProcess), id: browserContext._id })
return page
}

Expand All @@ -110,7 +108,7 @@ module.exports = ({ timeout: globalTimeout = 30000, ...launchOpts } = {}) => {
getBrowserContext(),
pReflect(page.close())
])
debug('closePage', { pid: driver.getPid(browserProcess), id: browserContext._id })
debug('closePage', { pid: driver.pid(browserProcess), id: browserContext._id })
}
}

Expand Down Expand Up @@ -170,7 +168,7 @@ module.exports = ({ timeout: globalTimeout = 30000, ...launchOpts } = {}) => {
const id = browserContext._id
await pReflect(browserContext.close())

debug('destroyContext', { pid: driver.getPid(browserProcess), id })
debug('destroyContext', { pid: driver.pid(browserProcess), id })
}

return {
Expand Down