2
0

🔒 Use isolated-vm

This commit is contained in:
Baptiste Arnaud
2024-05-22 11:42:31 +02:00
parent 15b2901f8a
commit 8d66b52a39
14 changed files with 310 additions and 114 deletions

View File

@ -47,6 +47,11 @@ As you can see, the code block expects the body of the Javascript function. You
If you'd like to set variables directly in this code block, you can use the [`setVariable` function](../logic/script#setvariable-function).
<Warning>
A function is executed on the server so it comes with [some limitations listed
here](../logic/script#limitations-on-scripts-executed-on-server).
</Warning>
## Ask assistant
This action allows you to talk with your [OpenAI assistant](https://platform.openai.com/assistants). All you have to do is to provide its ID.

View File

@ -22,7 +22,7 @@ You need to write `console.log({{My variable}})` instead of `console.log("{{My v
If you want to set a variable value with Javascript, the [Set variable block](./set-variable) is more appropriate for most cases.
However, if you'd like to set variables with the script blocks, you can use the `setVariable` function in your script:
However, if you'd like to set variables in a Script block, you can use the `setVariable` function in your script:
```js
if({{My variable}} === 'foo') {
@ -34,6 +34,37 @@ if({{My variable}} === 'foo') {
The `setVariable` function is only available in script executed on the server, so it won't work if the `Execute on client?` is checked.
## Limitations on scripts executed on server
Because the script is executed on a isolated and secured environment, there are some limitations.
- Global functions like `console.log`, `setTimeout`, `setInterval`, etc. are not available
- The `fetch` function behavior is slightly different from the native `fetch` function. You just have to skip the `await response.text()` or `await response.json()` part.
```js
// ❌ This throws an error
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
const data = await response.text()
// ✅ This works
const data = await fetch('https://jsonplaceholder.typicode.com/todos/1')
```
`response` will always be a `string` even if the the request returns a JSON object. If you know that the response is a JSON object, you can parse it using `JSON.parse(response)`.
```js
// ❌ This throws an error
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
const data = await response.json()
// ✅ This works
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
const data = JSON.parse(response)
```
- You can't use `import` or `require` to import external libraries
- You don't have access to browser APIs like `window`, `document`, `localStorage`, etc. If you need to use browser APIs, you should check the `Execute on client?` option so that the script is executed on the user's browser.
## Examples
### Reload page