Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(function): avoid page template if it's not used #593

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
},
"devDependencies": {
"@browserless/test": "^10.6.0",
"acorn": "~8.12.1",
"acorn-walk": "~8.3.4",
"ava": "5",
"browserless": "^10.6.0",
"lodash": "latest"
},
"engines": {
"node": ">= 12"
"node": ">= 20"
},
"files": [
"src"
Expand Down
15 changes: 2 additions & 13 deletions packages/function/src/function.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
'use strict'

const isolatedFunction = require('isolated-function')

const createFn = code => `
async (url, browserWSEndpoint, opts) => {
const puppeteer = require('@cloudflare/puppeteer')
const browser = await puppeteer.connect({ browserWSEndpoint })
const page = (await browser.pages())[1]
try {
return await (${code})({ page, ...opts })
} finally {
await browser.disconnect()
}
}`
const template = require('./template')

module.exports = async ({ url, code, vmOpts, browserWSEndpoint, ...opts }) => {
const [fn, teardown] = isolatedFunction(createFn(code), { ...vmOpts, throwError: false })
const [fn, teardown] = isolatedFunction(template(code), { ...vmOpts, throwError: false })
const result = await fn(url, browserWSEndpoint, opts)
await teardown()
return result
Expand Down
41 changes: 41 additions & 0 deletions packages/function/src/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const walk = require('acorn-walk')
const acorn = require('acorn')

module.exports = code => {
const ast = acorn.parse(code, { ecmaVersion: 2023, sourceType: 'module' })

let isUsingPage = false

walk.simple(ast, {
ObjectPattern (node) {
node.properties.forEach(prop => {
if (prop.type === 'Property' && prop.key.name === 'page') {
isUsingPage = true
}
if (prop.type === 'RestElement' && prop.argument.name === 'page') {
isUsingPage = true
}
})
},
MemberExpression (node) {
if (node.property.name === 'page' || node.property.value === 'page') {
isUsingPage = true
}
}
})

if (!isUsingPage) return `async (url, _, opts) => (${code})(opts)`
return `
async (url, browserWSEndpoint, opts) => {
const puppeteer = require('@cloudflare/puppeteer')
const browser = await puppeteer.connect({ browserWSEndpoint })
const page = (await browser.pages())[1]
try {
return await (${code})({ page, ...opts })
} finally {
await browser.disconnect()
}
}`
}
53 changes: 53 additions & 0 deletions packages/function/test/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

const test = require('ava')

const template = require('../src/template')

test('use a simplified template if page is not used', t => {
{
const code = () => {
function isStrict () {
return !this
}
return isStrict()
}

t.is(template(code.toString()).includes('puppeteer'), false)
}
{
const code = () => ({ page: 'title' })
t.is(template(code.toString()).includes('puppeteer'), false)
}
{
const code = () => 'page'
t.is(template(code.toString()).includes('puppeteer'), false)
}
})

test('require puppeteer if page is used', t => {
{
const code = ({ page }) => page.title()
t.is(template(code.toString()).includes('puppeteer'), true)
}
{
const code = ({ page: p }) => p.title()
t.is(template(code.toString()).includes('puppeteer'), true)
}
{
const code = obj => obj.page.title()
t.is(template(code.toString()).includes('puppeteer'), true)
}
{
const code = obj => (() => obj.page.title())()
t.is(template(code.toString()).includes('puppeteer'), true)
}
{
const code = ({ ...page }) => page.title
t.is(template(code.toString()).includes('puppeteer'), true)
}
{
const code = obj => obj.page.title()
t.is(template(code.toString()).includes('puppeteer'), true)
}
})