@browserless/function 13.6.10 → 13.7.0

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 +4 -5
  2. package/src/index.js +46 -37
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": "13.6.10",
5
+ "version": "13.7.0",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -32,14 +32,13 @@
32
32
  ],
33
33
  "dependencies": {
34
34
  "@browserless/errors": "13.6.10",
35
- "@cloudflare/puppeteer": "~1.1.0",
36
- "acorn": "~8.17.0",
35
+ "@cloudflare/puppeteer": "~1.2.0",
36
+ "acorn": "~8.18.0",
37
37
  "acorn-walk": "~8.3.5",
38
38
  "isolated-function": "~0.2.0",
39
39
  "require-one-of": "~1.0.24"
40
40
  },
41
41
  "devDependencies": {
42
- "@browserless/test": "13.6.10",
43
42
  "ava": "5",
44
43
  "lodash": "latest"
45
44
  },
@@ -58,5 +57,5 @@
58
57
  "timeout": "2m",
59
58
  "workerThreads": false
60
59
  },
61
- "gitHead": "0bdae87d4aff5b26de301b2947b70d8ae6c0dcfd"
60
+ "gitHead": "518aa3f6d07c2794d0f58edd9805bbe08f06d6a3"
62
61
  }
package/src/index.js CHANGED
@@ -6,8 +6,15 @@ const requireOneOf = require('require-one-of')
6
6
  const createRunFunction = require('./function')
7
7
  const path = require('path')
8
8
 
9
+ // `isolated-function` reads these to know which dependencies a snippet requires
10
+ // are already on disk, so it can skip installing them. Walking up from a
11
+ // dependency only lands on the consumer's `node_modules` under a flat install:
12
+ // with pnpm it lands inside the isolated store, which holds that dependency's
13
+ // own dependencies and nothing else, so every snippet paid a fresh install.
14
+ // `module.paths` is the resolution chain Node itself would use from here, so it
15
+ // reaches the consumer under either layout.
9
16
  const cloudflareDir = path.dirname(require.resolve('@cloudflare/puppeteer/package.json'))
10
- const nodePaths = [path.resolve(cloudflareDir, '..', '..')]
17
+ const nodePaths = [...new Set([path.resolve(cloudflareDir, '..', '..'), ...module.paths])]
11
18
 
12
19
  const stringify = fn => fn.toString().trim().replace(/;$/, '')
13
20
 
@@ -69,42 +76,44 @@ module.exports = ({ tmpdir } = {}) => {
69
76
  const browser = await getBrowser()
70
77
  const browserless = await browser.createContext()
71
78
 
72
- return browserless.withPage((page, goto) => async () => {
73
- const { device, response } = await goto(page, { url, timeout, ...gotoOpts })
74
-
75
- const targetId = await getTargetId(page)
76
-
77
- const runFunctionOpts = {
78
- url,
79
- code,
80
- device,
81
- ...opts,
82
- ...fnOpts,
83
- ...(isHttpResponse(response) && { _response: serializeResponse(response) })
84
- }
85
-
86
- if (runFunctionOpts.code === code) {
87
- runFunctionOpts.needsNetwork = needsNetwork
88
- runFunctionOpts.source = source
89
- }
90
-
91
- const browserFromPage = typeof page.browser === 'function' ? page.browser() : undefined
92
- const browserWSEndpoint =
93
- browserFromPage && typeof browserFromPage.wsEndpoint === 'function'
94
- ? browserFromPage.wsEndpoint()
95
- : undefined
96
-
97
- if (!browserWSEndpoint) throw new Error('Browser WebSocket endpoint not found')
98
- runFunctionOpts.browserWSEndpoint = browserWSEndpoint
99
- runFunctionOpts.targetId = targetId
100
-
101
- const result = await runFunction(runFunctionOpts)
102
-
103
- if (result.isFulfilled) return result
104
- const error = ensureError(result.value)
105
- if (isBrowserlessError(error)) throw error
106
- return result
107
- })()
79
+ return browserless
80
+ .withPage((page, goto) => async () => {
81
+ const { device, response } = await goto(page, { url, timeout, ...gotoOpts })
82
+
83
+ const targetId = await getTargetId(page)
84
+
85
+ const runFunctionOpts = {
86
+ url,
87
+ code,
88
+ device,
89
+ ...opts,
90
+ ...fnOpts,
91
+ ...(isHttpResponse(response) && { _response: serializeResponse(response) })
92
+ }
93
+
94
+ if (runFunctionOpts.code === code) {
95
+ runFunctionOpts.needsNetwork = needsNetwork
96
+ runFunctionOpts.source = source
97
+ }
98
+
99
+ const browserFromPage = typeof page.browser === 'function' ? page.browser() : undefined
100
+ const browserWSEndpoint =
101
+ browserFromPage && typeof browserFromPage.wsEndpoint === 'function'
102
+ ? browserFromPage.wsEndpoint()
103
+ : undefined
104
+
105
+ if (!browserWSEndpoint) throw new Error('Browser WebSocket endpoint not found')
106
+ runFunctionOpts.browserWSEndpoint = browserWSEndpoint
107
+ runFunctionOpts.targetId = targetId
108
+
109
+ const result = await runFunction(runFunctionOpts)
110
+
111
+ if (result.isFulfilled) return result
112
+ const error = ensureError(result.value)
113
+ if (isBrowserlessError(error)) throw error
114
+ return result
115
+ })()
116
+ .finally(() => browserless.destroyContext())
108
117
  }
109
118
 
110
119
  const runWithoutBrowser = async (url, fnOpts) => {