Skip to content

Commit

Permalink
perf(function): avoid page template if it's not used (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 14, 2024
1 parent 44f9a1f commit d45585f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 14 deletions.
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)
}
})

0 comments on commit d45585f

Please sign in to comment.