@browserless/function 12.0.0 → 13.0.1

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.
Files changed (2) hide show
  1. package/package.json +6 -5
  2. package/src/index.js +33 -11
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@browserless/function",
3
3
  "description": "Execute arbitrary JavaScript code in a secure browser sandbox with access to page DOM and network.",
4
4
  "homepage": "https://browserless.js.org",
5
- "version": "12.0.0",
5
+ "version": "13.0.1",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -31,16 +31,16 @@
31
31
  "vm"
32
32
  ],
33
33
  "dependencies": {
34
- "@browserless/errors": "^12.0.0",
34
+ "@browserless/errors": "^13.0.0",
35
35
  "acorn": "~8.16.0",
36
36
  "acorn-walk": "~8.3.5",
37
- "isolated-function": "~0.1.55",
37
+ "isolated-function": "~0.1.56",
38
38
  "require-one-of": "~1.0.24"
39
39
  },
40
40
  "devDependencies": {
41
+ "@browserless/test": "13.0.0",
41
42
  "ava": "5",
42
- "lodash": "latest",
43
- "@browserless/test": "12.0.0"
43
+ "lodash": "latest"
44
44
  },
45
45
  "engines": {
46
46
  "node": ">= 20"
@@ -54,6 +54,7 @@
54
54
  "timeout": "2m",
55
55
  "workerThreads": false
56
56
  },
57
+ "gitHead": "b4f677642547f5f9ea0dd35539aeca7dede213f3",
57
58
  "scripts": {
58
59
  "test": "ava"
59
60
  }
package/src/index.js CHANGED
@@ -59,7 +59,7 @@ module.exports = ({ tmpdir } = {}) => {
59
59
  return browserPromise
60
60
  }
61
61
 
62
- return async (url, fnOpts = {}) => {
62
+ const runWithBrowser = async (url, fnOpts) => {
63
63
  const browser = await getBrowser()
64
64
  const browserless = await browser.createContext()
65
65
 
@@ -82,17 +82,15 @@ module.exports = ({ tmpdir } = {}) => {
82
82
  runFunctionOpts.source = source
83
83
  }
84
84
 
85
- if (runFunctionOpts.needsNetwork !== false) {
86
- const browserFromPage = typeof page.browser === 'function' ? page.browser() : undefined
87
- const browserWSEndpoint =
88
- browserFromPage && typeof browserFromPage.wsEndpoint === 'function'
89
- ? browserFromPage.wsEndpoint()
90
- : undefined
85
+ const browserFromPage = typeof page.browser === 'function' ? page.browser() : undefined
86
+ const browserWSEndpoint =
87
+ browserFromPage && typeof browserFromPage.wsEndpoint === 'function'
88
+ ? browserFromPage.wsEndpoint()
89
+ : undefined
91
90
 
92
- if (!browserWSEndpoint) throw new Error('Browser WebSocket endpoint not found')
93
- runFunctionOpts.browserWSEndpoint = browserWSEndpoint
94
- runFunctionOpts.targetId = targetId
95
- }
91
+ if (!browserWSEndpoint) throw new Error('Browser WebSocket endpoint not found')
92
+ runFunctionOpts.browserWSEndpoint = browserWSEndpoint
93
+ runFunctionOpts.targetId = targetId
96
94
 
97
95
  const result = await runFunction(runFunctionOpts)
98
96
 
@@ -102,6 +100,30 @@ module.exports = ({ tmpdir } = {}) => {
102
100
  return result
103
101
  })()
104
102
  }
103
+
104
+ const runWithoutBrowser = async (url, fnOpts) => {
105
+ const runFunctionOpts = {
106
+ url,
107
+ code,
108
+ ...opts,
109
+ ...fnOpts
110
+ }
111
+
112
+ if (runFunctionOpts.code === code) {
113
+ runFunctionOpts.needsNetwork = false
114
+ runFunctionOpts.source = source
115
+ }
116
+
117
+ const result = await runFunction(runFunctionOpts)
118
+
119
+ if (result.isFulfilled) return result
120
+ const error = ensureError(result.value)
121
+ if (isBrowserlessError(error)) throw error
122
+ return result
123
+ }
124
+
125
+ return async (url, fnOpts = {}) =>
126
+ needsNetwork ? runWithBrowser(url, fnOpts) : runWithoutBrowser(url, fnOpts)
105
127
  }
106
128
 
107
129
  createFunction.teardown = () => isolatedFunction.teardown()