@browserless/function 10.9.9 → 10.9.11
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/LICENSE.md +0 -0
- package/package.json +8 -7
- package/src/function.js +5 -1
- package/src/index.js +4 -0
- package/src/template.js +13 -6
package/LICENSE.md
CHANGED
|
File without changes
|
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": "10.9.
|
|
5
|
+
"version": "10.9.11",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@browserless/errors": "^10.9.7",
|
|
33
|
-
"isolated-function": "~0.1.
|
|
33
|
+
"isolated-function": "~0.1.46",
|
|
34
34
|
"require-one-of": "~1.0.24"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@browserless/test": "^10.9.
|
|
37
|
+
"@browserless/test": "^10.9.11",
|
|
38
38
|
"acorn": "~8.15.0",
|
|
39
39
|
"acorn-walk": "~8.3.4",
|
|
40
40
|
"ava": "5",
|
|
@@ -46,13 +46,14 @@
|
|
|
46
46
|
"files": [
|
|
47
47
|
"src"
|
|
48
48
|
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "ava"
|
|
51
|
+
},
|
|
49
52
|
"license": "MIT",
|
|
50
53
|
"ava": {
|
|
51
54
|
"serial": true,
|
|
52
55
|
"timeout": "2m",
|
|
53
56
|
"workerThreads": false
|
|
54
57
|
},
|
|
55
|
-
"
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
}
|
|
58
|
+
"gitHead": "01e4b9a320e1f0f7a7005710e1eec841fed917b6"
|
|
59
|
+
}
|
package/src/function.js
CHANGED
|
@@ -3,8 +3,12 @@
|
|
|
3
3
|
const isolatedFunction = require('isolated-function')
|
|
4
4
|
const template = require('./template')
|
|
5
5
|
|
|
6
|
+
const [nodeMajor] = process.version.slice(1).split('.').map(Number)
|
|
7
|
+
|
|
6
8
|
module.exports = async ({ url, code, vmOpts, browserWSEndpoint, ...opts }) => {
|
|
7
|
-
const
|
|
9
|
+
const needsNetwork = template.isUsingPage(code)
|
|
10
|
+
const allow = needsNetwork && nodeMajor >= 25 ? ['net'] : []
|
|
11
|
+
const [fn, teardown] = isolatedFunction(template(code), { ...vmOpts, allow, throwError: false })
|
|
8
12
|
const result = await fn(url, browserWSEndpoint, opts)
|
|
9
13
|
await teardown()
|
|
10
14
|
return result
|
package/src/index.js
CHANGED
|
@@ -24,6 +24,10 @@ module.exports =
|
|
|
24
24
|
|
|
25
25
|
return browserless.withPage((page, goto) => async () => {
|
|
26
26
|
const { device } = await goto(page, { url, timeout, ...gotoOpts })
|
|
27
|
+
|
|
28
|
+
const browserWSEndpoint = (await browserless.browser()).wsEndpoint()
|
|
29
|
+
if (!browserWSEndpoint) throw new Error('Browser WebSocket endpoint not found')
|
|
30
|
+
|
|
27
31
|
const result = await runFunction({
|
|
28
32
|
url,
|
|
29
33
|
code: stringify(fn),
|
package/src/template.js
CHANGED
|
@@ -3,30 +3,34 @@
|
|
|
3
3
|
const walk = require('acorn-walk')
|
|
4
4
|
const acorn = require('acorn')
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
const isUsingPage = code => {
|
|
7
7
|
const ast = acorn.parse(code, { ecmaVersion: 2023, sourceType: 'module' })
|
|
8
8
|
|
|
9
|
-
let
|
|
9
|
+
let result = false
|
|
10
10
|
|
|
11
11
|
walk.simple(ast, {
|
|
12
12
|
ObjectPattern (node) {
|
|
13
13
|
node.properties.forEach(prop => {
|
|
14
14
|
if (prop.type === 'Property' && prop.key.name === 'page') {
|
|
15
|
-
|
|
15
|
+
result = true
|
|
16
16
|
}
|
|
17
17
|
if (prop.type === 'RestElement' && prop.argument.name === 'page') {
|
|
18
|
-
|
|
18
|
+
result = true
|
|
19
19
|
}
|
|
20
20
|
})
|
|
21
21
|
},
|
|
22
22
|
MemberExpression (node) {
|
|
23
23
|
if (node.property.name === 'page' || node.property.value === 'page') {
|
|
24
|
-
|
|
24
|
+
result = true
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
})
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
return result
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const template = code => {
|
|
33
|
+
if (!isUsingPage(code)) return `async (url, _, opts) => (${code})(opts)`
|
|
30
34
|
return `
|
|
31
35
|
async (url, browserWSEndpoint, opts) => {
|
|
32
36
|
const puppeteer = require('@cloudflare/puppeteer')
|
|
@@ -40,3 +44,6 @@ module.exports = code => {
|
|
|
40
44
|
}
|
|
41
45
|
}`
|
|
42
46
|
}
|
|
47
|
+
|
|
48
|
+
module.exports = template
|
|
49
|
+
module.exports.isUsingPage = isUsingPage
|