@browserless/function 9.2.3
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 +21 -0
- package/package.json +65 -0
- package/src/function.js +33 -0
- package/src/index.js +69 -0
- package/src/vm.js +31 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2019 Microlink <hello@microlink.io> (microlink.io)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@browserless/function",
|
|
3
|
+
"description": "Run abritrary JavaScript inside a browser sandbox",
|
|
4
|
+
"homepage": "https://browserless.js.org",
|
|
5
|
+
"version": "9.2.3",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"author": {
|
|
8
|
+
"email": "hello@microlink.io",
|
|
9
|
+
"name": "microlink.io",
|
|
10
|
+
"url": "https://microlink.io"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"directory": "packages/function",
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/microlinkhq/browserless.git#master"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/microlinkhq/browserless/issues"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"browser",
|
|
22
|
+
"browserless",
|
|
23
|
+
"chrome",
|
|
24
|
+
"chromeless",
|
|
25
|
+
"core",
|
|
26
|
+
"function",
|
|
27
|
+
"headless",
|
|
28
|
+
"puppeteer",
|
|
29
|
+
"serverless"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@browserless/errors": "^9.1.6",
|
|
33
|
+
"debug-logfmt": "~1.0.4",
|
|
34
|
+
"deepmerge": "~4.2.2",
|
|
35
|
+
"execa": "~5.1.1",
|
|
36
|
+
"p-event": "~4.2.0",
|
|
37
|
+
"p-reflect": "~2.1.0",
|
|
38
|
+
"p-retry": "~4.6.0",
|
|
39
|
+
"p-timeout": "~4.1.0",
|
|
40
|
+
"require-one-of": "~1.0.15",
|
|
41
|
+
"serialize-error": "~8.1.0",
|
|
42
|
+
"vm2": "~3.9.3"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"ava": "latest",
|
|
46
|
+
"browserless": "latest",
|
|
47
|
+
"nyc": "latest",
|
|
48
|
+
"puppeteer": "latest"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">= 12"
|
|
52
|
+
},
|
|
53
|
+
"files": [
|
|
54
|
+
"src"
|
|
55
|
+
],
|
|
56
|
+
"scripts": {
|
|
57
|
+
"test": "NODE_ENV=test nyc ava"
|
|
58
|
+
},
|
|
59
|
+
"license": "MIT",
|
|
60
|
+
"ava": {
|
|
61
|
+
"timeout": "2m",
|
|
62
|
+
"verbose": true
|
|
63
|
+
},
|
|
64
|
+
"gitHead": "dac162639d969ba1862516bfb3273158af205f5c"
|
|
65
|
+
}
|
package/src/function.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const path = require('path')
|
|
4
|
+
|
|
5
|
+
const createVm = require('./vm')
|
|
6
|
+
|
|
7
|
+
const scriptPath = path.resolve(__dirname, 'function.js')
|
|
8
|
+
|
|
9
|
+
const createFn = code => `
|
|
10
|
+
async ({ url, query, gotoOpts, browserWSEndpoint }) => {
|
|
11
|
+
const { serializeError } = require('serialize-error')
|
|
12
|
+
|
|
13
|
+
const getBrowserless = require('browserless')
|
|
14
|
+
const browserless = await getBrowserless({ mode: 'connect', browserWSEndpoint }).createContext()
|
|
15
|
+
const fnWrapper = fn => (page, response) => fn({ page, response, query })
|
|
16
|
+
const browserFn = browserless.evaluate(fnWrapper(${code}), gotoOpts)
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const value = await browserFn(url)
|
|
20
|
+
await browserless.destroyContext()
|
|
21
|
+
return { isFulfilled: true, isRejected: false, value }
|
|
22
|
+
} catch(error) {
|
|
23
|
+
await browserless.destroyContext()
|
|
24
|
+
return { isFulfilled: false, isRejected: true, reason: serializeError(error) }
|
|
25
|
+
}
|
|
26
|
+
}`
|
|
27
|
+
|
|
28
|
+
process.on('message', async ({ url, code, query, vmOpts, gotoOpts, browserWSEndpoint }) => {
|
|
29
|
+
const vm = createVm(vmOpts)
|
|
30
|
+
const fn = createFn(code.endsWith(';') ? code.slice(0, -1) : code)
|
|
31
|
+
const run = vm(fn, scriptPath)
|
|
32
|
+
process.send(await run({ url, query, gotoOpts, browserWSEndpoint }))
|
|
33
|
+
})
|
package/src/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { ensureError, browserTimeout } = require('@browserless/errors')
|
|
4
|
+
const debug = require('debug-logfmt')('browserless:function')
|
|
5
|
+
const requireOneOf = require('require-one-of')
|
|
6
|
+
const { driver } = require('browserless')
|
|
7
|
+
const pTimeout = require('p-timeout')
|
|
8
|
+
const pRetry = require('p-retry')
|
|
9
|
+
const pEvent = require('p-event')
|
|
10
|
+
const execa = require('execa')
|
|
11
|
+
const path = require('path')
|
|
12
|
+
|
|
13
|
+
const { AbortError } = pRetry
|
|
14
|
+
|
|
15
|
+
const execPath = path.resolve(__dirname, 'function.js')
|
|
16
|
+
|
|
17
|
+
module.exports = (
|
|
18
|
+
fn,
|
|
19
|
+
{ getBrowserless = requireOneOf(['browserless']), retry = 2, timeout = 30000, ...opts }
|
|
20
|
+
) => {
|
|
21
|
+
return async (url, query = {}) => {
|
|
22
|
+
const browserlessPromise = getBrowserless()
|
|
23
|
+
let isRejected = false
|
|
24
|
+
|
|
25
|
+
async function run () {
|
|
26
|
+
const browserless = await browserlessPromise
|
|
27
|
+
const browser = await browserless.browser()
|
|
28
|
+
const browserWSEndpoint = browser.wsEndpoint()
|
|
29
|
+
|
|
30
|
+
const subprocess = execa.node(execPath, { killSignal: 'SIGKILL', timeout })
|
|
31
|
+
subprocess.stderr.pipe(process.stderr)
|
|
32
|
+
|
|
33
|
+
debug('spawn', { pid: subprocess.pid })
|
|
34
|
+
|
|
35
|
+
subprocess.send({
|
|
36
|
+
url,
|
|
37
|
+
code: fn.toString(),
|
|
38
|
+
query,
|
|
39
|
+
browserWSEndpoint,
|
|
40
|
+
...opts
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const { value, reason, isFulfilled } = await pEvent(subprocess, 'message')
|
|
44
|
+
await driver.close(subprocess)
|
|
45
|
+
if (isFulfilled) return value
|
|
46
|
+
throw ensureError(reason)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const task = () =>
|
|
50
|
+
pRetry(run, {
|
|
51
|
+
retries: retry,
|
|
52
|
+
onFailedAttempt: async error => {
|
|
53
|
+
if (error.name === 'AbortError') throw error
|
|
54
|
+
if (isRejected) throw new AbortError()
|
|
55
|
+
await (await browserlessPromise).respawn()
|
|
56
|
+
const { message, attemptNumber, retriesLeft } = error
|
|
57
|
+
debug('retry', { attemptNumber, retriesLeft, message })
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// main
|
|
62
|
+
const result = await pTimeout(task(), timeout, () => {
|
|
63
|
+
isRejected = true
|
|
64
|
+
throw browserTimeout({ timeout })
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return result
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/vm.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const pReflect = require('p-reflect')
|
|
4
|
+
const { NodeVM } = require('vm2')
|
|
5
|
+
const merge = require('deepmerge')
|
|
6
|
+
|
|
7
|
+
const DEFAULT_VM_OPTS = {
|
|
8
|
+
console: 'off',
|
|
9
|
+
sandbox: {},
|
|
10
|
+
require: {
|
|
11
|
+
external: {
|
|
12
|
+
modules: ['serialize-error', 'browserless']
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const createVm = (opts = {}) => new NodeVM(merge(DEFAULT_VM_OPTS, opts))
|
|
18
|
+
|
|
19
|
+
const compile = (vm, fn, scriptPath) => {
|
|
20
|
+
const code = `'use strict'; module.exports = ${fn.toString()}`
|
|
21
|
+
return vm.run(code, scriptPath)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = opts => {
|
|
25
|
+
const vm = createVm(opts)
|
|
26
|
+
|
|
27
|
+
return (fn, scriptPath) => {
|
|
28
|
+
const run = compile(vm, fn, scriptPath)
|
|
29
|
+
return (...args) => pReflect(run(...args))
|
|
30
|
+
}
|
|
31
|
+
}
|