run javascript or typescript code in v8
TypeScript support is type removal only — types are stripped before execution, not checked. Invalid types will be silently removed, not reported as errors.
params:
- code: the javascript or typescript code to run
- heap_memory_max_mb (optional): maximum V8 heap memory in megabytes (4–64, default: 8). Override the server default for this execution.
- execution_timeout_secs (optional): maximum execution time in seconds (1–300, default: 30). Override the server default for this execution.
returns:
- output: the output of the javascript code
## Limitations
- **`async`/`await` and Promises**: Fully supported. If your code returns a Promise, the runtime resolves it automatically.
- **No `fetch` or network access by default**: When the server is started with `--opa-url`, a `fetch(url, opts?)` function becomes available. `fetch()` follows the web standard Fetch API — it returns a Promise that resolves to a Response object. Use `await` to get the response: `const resp = await fetch(url)`. The response object has `.ok`, `.status`, `.statusText`, `.url`, `.headers.get(name)`, `.text()`, and `.json()` methods (`.text()` and `.json()` also return Promises). Each request is checked against an OPA policy before execution. Without `--opa-url`, there is no network access.
- **No `console.log` or standard output**: Output from `console.log` or similar functions will not appear. To return results, ensure the value you want is the last line of your code.
- **No file system access**: The runtime does not provide access to the local file system or environment variables.
- **No `npm install` or external packages**: You cannot install or import npm packages. Only standard JavaScript (ECMAScript) built-ins are available.
- **No timers**: Functions like `setTimeout` and `setInterval` are not available.
- **No DOM or browser APIs**: This is not a browser environment; there is no access to `window`, `document`, or other browser-specific objects.
The way the runtime works, is that there is no console.log. If you want the results of an execution, you must return it in the last line of code.
eg:
```js
const result = 1 + 1;
result;
```
would return:
```
2
```
you must also jsonify an object, and return it as a string to see its content.
eg:
```js
const obj = {
a: 1,
b: 2,
};
JSON.stringify(obj);
```
would return:
```
{"a":1,"b":2}
```
async/await is supported. The runtime resolves top-level Promises automatically:
eg:
```js
async function fetchData() {
const resp = await fetch("https://api.example.com/data");
const data = await resp.json();
return JSON.stringify(data);
}
fetchData();
```
would return the JSON response as a string.
Each execution starts with a fresh V8 isolate — no state is carried between calls.