@browserless/function 9.6.7 → 9.7.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@browserless/function",
3
3
  "description": "Run abritrary JavaScript inside a browser sandbox",
4
4
  "homepage": "https://browserless.js.org",
5
- "version": "9.6.7",
5
+ "version": "9.7.0-beta.0",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -29,11 +29,9 @@
29
29
  "serverless"
30
30
  ],
31
31
  "dependencies": {
32
- "@browserless/errors": "^9.6.6",
32
+ "@browserless/errors": "9.6.6",
33
33
  "debug-logfmt": "~1.0.4",
34
34
  "deepmerge": "~4.2.2",
35
- "execa": "~5.1.1",
36
- "p-event": "~4.2.0",
37
35
  "p-reflect": "~2.1.0",
38
36
  "p-retry": "~4.6.1",
39
37
  "p-timeout": "~4.1.0",
@@ -42,9 +40,10 @@
42
40
  "vm2": "~3.9.11"
43
41
  },
44
42
  "devDependencies": {
45
- "@browserless/test": "latest",
43
+ "@browserless/test": "9.6.6",
46
44
  "ava": "3",
47
- "browserless": "latest"
45
+ "browserless": "9.7.0-beta.0",
46
+ "lodash": "latest"
48
47
  },
49
48
  "engines": {
50
49
  "node": ">= 12"
@@ -61,5 +60,5 @@
61
60
  "timeout": "30s",
62
61
  "verbose": true
63
62
  },
64
- "gitHead": "ca9f66c91b35160b92828f41f603bcc7e15274a7"
63
+ "gitHead": "9ad58b35b63036848f22662a04ed913e364e1297"
65
64
  }
package/src/function.js CHANGED
@@ -42,9 +42,9 @@ async ({ url, gotoOpts, browserWSEndpoint, ...opts }) => {
42
42
  }
43
43
  }`
44
44
 
45
- process.on('message', async ({ url, code, vmOpts, gotoOpts, browserWSEndpoint, ...opts }) => {
45
+ module.exports = ({ url, code, vmOpts, gotoOpts, browserWSEndpoint, ...opts }) => {
46
46
  const vm = createVm(vmOpts)
47
47
  const fn = createFn(code)
48
48
  const run = vm(fn, scriptPath)
49
- process.send(await run({ url, gotoOpts, browserWSEndpoint, ...opts }))
50
- })
49
+ return run({ url, gotoOpts, browserWSEndpoint, ...opts })
50
+ }
package/src/index.js CHANGED
@@ -3,16 +3,18 @@
3
3
  const { ensureError, browserTimeout } = require('@browserless/errors')
4
4
  const debug = require('debug-logfmt')('browserless:function')
5
5
  const requireOneOf = require('require-one-of')
6
- const { driver } = require('browserless')
7
6
  const pTimeout = require('p-timeout')
8
7
  const pRetry = require('p-retry')
9
- const pEvent = require('p-event')
10
- const execa = require('execa')
11
- const path = require('path')
12
8
 
13
9
  const { AbortError } = pRetry
14
10
 
15
- const execPath = path.resolve(__dirname, 'function.js')
11
+ const runFunction = require('./function')
12
+
13
+ const stringify = fn =>
14
+ fn
15
+ .toString()
16
+ .trim()
17
+ .replace(/;$/, '')
16
18
 
17
19
  module.exports = (
18
20
  fn,
@@ -21,37 +23,20 @@ module.exports = (
21
23
  return async (url, fnOpts = {}) => {
22
24
  const browserlessPromise = getBrowserless()
23
25
  let isRejected = false
24
- let subprocess
25
26
 
26
27
  async function run () {
27
28
  const browserless = await browserlessPromise
28
29
  const browser = await browserless.browser()
29
30
  const browserWSEndpoint = browser.wsEndpoint()
30
31
 
31
- subprocess = execa.node(execPath, {
32
- detached: process.platform !== 'win32'
33
- })
34
-
35
- if (opts && opts.vmOpts && opts.vmOpts.console === 'inherit') {
36
- subprocess.stderr.pipe(process.stderr)
37
- subprocess.stdout.pipe(process.stdout)
38
- }
39
-
40
- debug('spawn', { pid: subprocess.pid })
41
-
42
- subprocess.send({
32
+ const { value, reason, isFulfilled } = await runFunction({
43
33
  url,
44
- code: fn
45
- .toString()
46
- .trim()
47
- .replace(/;$/, ''),
34
+ code: stringify(fn),
48
35
  browserWSEndpoint,
49
36
  ...opts,
50
37
  ...fnOpts
51
38
  })
52
39
 
53
- const { value, reason, isFulfilled } = await pEvent(subprocess, 'message')
54
- await driver.close(subprocess)
55
40
  if (isFulfilled) return value
56
41
  throw ensureError(reason)
57
42
  }
@@ -71,7 +56,6 @@ module.exports = (
71
56
  // main
72
57
  const result = await pTimeout(task(), timeout, () => {
73
58
  isRejected = true
74
- if (subprocess) subprocess.kill('SIGKILL')
75
59
  throw browserTimeout({ timeout })
76
60
  })
77
61