diff --git a/packages/function/package.json b/packages/function/package.json index 44b04e8be..943ada497 100644 --- a/packages/function/package.json +++ b/packages/function/package.json @@ -29,11 +29,8 @@ "serverless" ], "dependencies": { - "@browserless/errors": "^10.5.2", - "debug-logfmt": "~1.2.1", "isolated-function": "~0.1.4", "p-retry": "~4.6.1", - "p-timeout": "~4.1.0", "require-one-of": "~1.0.19" }, "devDependencies": { diff --git a/packages/function/src/function.js b/packages/function/src/function.js index fd7bee74e..1d0c03ddb 100644 --- a/packages/function/src/function.js +++ b/packages/function/src/function.js @@ -15,14 +15,8 @@ async (url, browserWSEndpoint, opts) => { }` module.exports = async ({ url, code, vmOpts, browserWSEndpoint, ...opts }) => { - const [fn, teardown] = isolatedFunction(createFn(code), { - ...vmOpts, - throwError: false - }) - + const [fn, teardown] = isolatedFunction(createFn(code), { ...vmOpts, throwError: false }) const result = await fn(url, browserWSEndpoint, opts) - await teardown() - return result } diff --git a/packages/function/src/index.js b/packages/function/src/index.js index 56c063dbb..7b4c6f7e6 100644 --- a/packages/function/src/index.js +++ b/packages/function/src/index.js @@ -1,13 +1,7 @@ 'use strict' -const { isBrowserlessError, ensureError, browserTimeout } = require('@browserless/errors') -const debug = require('debug-logfmt')('browserless:function') +const { isBrowserlessError, ensureError } = require('@browserless/errors') const requireOneOf = require('require-one-of') -const pTimeout = require('p-timeout') -const pRetry = require('p-retry') - -const { AbortError } = pRetry - const runFunction = require('./function') const stringify = fn => fn.toString().trim().replace(/;$/, '') @@ -25,48 +19,23 @@ module.exports = ) => async (url, fnOpts = {}) => { const browserlessPromise = getBrowserless() - let isRejected = false - - async function run () { - const browser = await browserlessPromise - const browserless = await browser.createContext() - - const withVM = browserless.withPage((page, goto) => async () => { - const { device } = await goto(page, { url, ...gotoOpts }) - return runFunction({ - url, - code: stringify(fn), - browserWSEndpoint: (await browserless.browser()).wsEndpoint(), - device, - ...opts, - ...fnOpts - }) + const browser = await browserlessPromise + const browserless = await browser.createContext() + + return browserless.withPage((page, goto) => async () => { + const { device } = await goto(page, { url, ...gotoOpts }) + const result = await runFunction({ + url, + code: stringify(fn), + browserWSEndpoint: (await browserless.browser()).wsEndpoint(), + device, + ...opts, + ...fnOpts }) - const result = await withVM() if (result.isFulfilled) return result const error = ensureError(result.value) - if (isBrowserlessError(error)) throw ensureError(error) + if (isBrowserlessError(error)) throw error return result - } - - const task = () => - pRetry(run, { - retries: retry, - onFailedAttempt: async error => { - if (error.name === 'AbortError') throw error - if (isRejected) throw new AbortError() - await (await browserlessPromise).respawn() - const { message, attemptNumber, retriesLeft } = error - debug('retry', { attemptNumber, retriesLeft, message }) - } - }) - - // main - const result = await pTimeout(task(), timeout, () => { - isRejected = true - throw browserTimeout({ timeout }) - }) - - return result + })() }