Skip to content

Commit 2552f90

Browse files
authored
fix(function): enable network permission for page interaction (#639)
* fix(function): enable network permission for page interaction * chore(function): ensure wsEndpoint is available
1 parent 3c79cd1 commit 2552f90

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

packages/function/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
],
3131
"dependencies": {
3232
"@browserless/errors": "^10.9.7",
33-
"isolated-function": "~0.1.39",
33+
"isolated-function": "~0.1.46",
3434
"require-one-of": "~1.0.24"
3535
},
3636
"devDependencies": {

packages/function/src/function.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
const isolatedFunction = require('isolated-function')
44
const template = require('./template')
55

6+
const [nodeMajor] = process.version.slice(1).split('.').map(Number)
7+
68
module.exports = async ({ url, code, vmOpts, browserWSEndpoint, ...opts }) => {
7-
const [fn, teardown] = isolatedFunction(template(code), { ...vmOpts, throwError: false })
9+
const needsNetwork = template.isUsingPage(code)
10+
const allow = needsNetwork && nodeMajor >= 25 ? ['net'] : []
11+
const [fn, teardown] = isolatedFunction(template(code), { ...vmOpts, allow, throwError: false })
812
const result = await fn(url, browserWSEndpoint, opts)
913
await teardown()
1014
return result

packages/function/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ module.exports =
2424

2525
return browserless.withPage((page, goto) => async () => {
2626
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+
2731
const result = await runFunction({
2832
url,
2933
code: stringify(fn),

packages/function/src/template.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@
33
const walk = require('acorn-walk')
44
const acorn = require('acorn')
55

6-
module.exports = code => {
6+
const isUsingPage = code => {
77
const ast = acorn.parse(code, { ecmaVersion: 2023, sourceType: 'module' })
88

9-
let isUsingPage = false
9+
let result = false
1010

1111
walk.simple(ast, {
1212
ObjectPattern (node) {
1313
node.properties.forEach(prop => {
1414
if (prop.type === 'Property' && prop.key.name === 'page') {
15-
isUsingPage = true
15+
result = true
1616
}
1717
if (prop.type === 'RestElement' && prop.argument.name === 'page') {
18-
isUsingPage = true
18+
result = true
1919
}
2020
})
2121
},
2222
MemberExpression (node) {
2323
if (node.property.name === 'page' || node.property.value === 'page') {
24-
isUsingPage = true
24+
result = true
2525
}
2626
}
2727
})
2828

29-
if (!isUsingPage) return `async (url, _, opts) => (${code})(opts)`
29+
return result
30+
}
31+
32+
const template = code => {
33+
if (!isUsingPage(code)) return `async (url, _, opts) => (${code})(opts)`
3034
return `
3135
async (url, browserWSEndpoint, opts) => {
3236
const puppeteer = require('@cloudflare/puppeteer')
@@ -40,3 +44,6 @@ module.exports = code => {
4044
}
4145
}`
4246
}
47+
48+
module.exports = template
49+
module.exports.isUsingPage = isUsingPage

packages/function/test/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,17 @@ test('interact with npm modules', async t => {
267267
t.true(!!profiling)
268268
t.true(!!logging)
269269
})
270+
271+
test('throws error when browser is launched with pipe mode', async t => {
272+
const createTestUtil = require('@browserless/test/util/create')
273+
const { getBrowser } = createTestUtil({ pipe: true })
274+
275+
const code = ({ page }) => page.title()
276+
277+
const myFn = browserlessFunction(code, {
278+
getBrowserless: () => getBrowser()
279+
})
280+
281+
const error = await t.throwsAsync(myFn(fileUrl))
282+
t.is(error.message, 'Browser WebSocket endpoint not found')
283+
})

0 commit comments

Comments
 (0)