-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(functions): use isolated-function
It drops vm2 which is considered unsecured in favour of isolated-function
- Loading branch information
Showing
6 changed files
with
218 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,35 @@ | ||
'use strict' | ||
|
||
const isolatedFunction = require('isolated-function') | ||
const fs = require('fs/promises') | ||
const path = require('path') | ||
|
||
const createVm = require('./vm') | ||
|
||
const scriptPath = path.resolve(__dirname, 'function.js') | ||
|
||
const createFn = code => ` | ||
async ({ url, gotoOpts, browserWSEndpoint, ...opts }) => { | ||
const { serializeError } = require('serialize-error') | ||
const getBrowserless = require('browserless') | ||
const browserless = await getBrowserless({ mode: 'connect', browserWSEndpoint }).createContext() | ||
const fnWrapper = fn => (page, response) => { | ||
if (!response && opts.response) { | ||
const { status, statusText, headers, html } = opts.response | ||
response = { | ||
ok: () => status === 0 || (status >= 200 && opts.status <= 299), | ||
fromCache: () => false, | ||
fromServiceWorker: () => false, | ||
url: () => url, | ||
text: () => html, | ||
statusText: () => statusText, | ||
json: () => JSON.parse(html), | ||
headers: () => headers, | ||
status: () => status | ||
} | ||
} | ||
return fn({ ...opts, page, response, url }) | ||
} | ||
const browserFn = browserless.evaluate(fnWrapper(${code}), gotoOpts) | ||
async (url, browserWSEndpoint, opts) => { | ||
const puppeteer = require('@cloudflare/puppeteer') | ||
const browser = await puppeteer.connect({ browserWSEndpoint }) | ||
const page = (await browser.pages())[1] | ||
try { | ||
const value = await browserFn(url) | ||
return { isFulfilled: true, isRejected: false, value } | ||
} catch (error) { | ||
return { isFulfilled: false, isRejected: true, reason: serializeError(error) } | ||
return await (${code})({ page, ...opts }) | ||
} finally { | ||
await browserless.destroyContext() | ||
await browserless.browser().then(browser => browser.disconnect()) | ||
await browser.disconnect() | ||
} | ||
}` | ||
|
||
module.exports = ({ url, code, vmOpts, gotoOpts, browserWSEndpoint, ...opts }) => { | ||
const vm = createVm(vmOpts) | ||
const fn = createFn(code) | ||
const run = vm(fn, scriptPath) | ||
return run({ url, gotoOpts, browserWSEndpoint, ...opts }) | ||
module.exports = async ({ url, code, vmOpts, browserWSEndpoint, ...opts }) => { | ||
const [fn, teardown] = isolatedFunction(createFn(code), { | ||
...vmOpts, | ||
tmpdir: async () => { | ||
const basepath = path.resolve(__dirname, '../node_modules/.cache') | ||
const tmp = await fs.mkdtemp(path.join(basepath, 'compile-')) | ||
return tmp | ||
}, | ||
throwError: false | ||
}) | ||
|
||
const result = await fn(url, browserWSEndpoint, opts) | ||
|
||
await teardown() | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.