status-go/jail/README.md

100 lines
3.8 KiB
Markdown
Raw Normal View History

# jail [![GoDoc](https://godoc.org/github.com/status-im/status-go/jail?status.png)](https://godoc.org/github.com/status-im/status-go/jail)
2018-02-09 00:25:17 +00:00
jail - jailed environment for executing JS code.
2017-09-13 14:27:19 +00:00
2017-09-15 12:16:43 +00:00
Download:
```shell
go get github.com/status-im/status-go/jail
2017-09-15 12:16:43 +00:00
```
2017-09-13 14:27:19 +00:00
2017-09-15 12:16:43 +00:00
* * *
2018-02-09 00:25:17 +00:00
jail - jailed environment for executing JS code.
2017-09-01 18:18:09 +00:00
2018-02-09 00:25:17 +00:00
Package jail implements "jailed" environment for executing arbitrary
2017-09-15 12:16:43 +00:00
JavaScript code using Otto JS interpreter (https://github.com/robertkrimen/otto).
2017-09-01 18:18:09 +00:00
Jail create multiple Cells, one cell per status client chat. Each cell runs own
Otto virtual machine and lives forever, but that may change in the future.
2017-09-01 18:18:09 +00:00
2017-09-15 12:16:43 +00:00
```
+----------------------------------------------+
| Jail |
+----------------------------------------------+
+---------+ +---------+ +---------+ +---------+
| Cell | | Cell | | Cell | | Cell |
|ChatID 1 | |ChatID 2 | |ChatID 3 | |ChatID N |
|+-------+| |+-------+| |+-------+| |+-------+|
||Otto VM|| ||Otto VM|| ||Otto VM|| ||Otto VM||
|+-------+| |+-------+| |+-------+| |+-------+|
|| Loop || || Loop || || Loop || || Loop ||
++-------++ ++-------++ ++-------++ ++-------++
```
2018-02-09 00:25:17 +00:00
## Cells
Each Cell object embeds *VM from 'jail/vm' for concurrency safe wrapper around
2017-09-13 14:27:19 +00:00
*otto.VM functions. This is important when dealing with setTimeout and Fetch API
functions (see below).
2018-02-09 00:25:17 +00:00
## Get and Set
(*VM).Get/Set functions provide transparent and concurrently safe wrappers for
2017-09-15 12:16:43 +00:00
Otto VM Get and Set functions respectively. See Otto documentation for usage examples:
https://godoc.org/github.com/robertkrimen/otto
2017-09-01 18:18:09 +00:00
2018-02-09 00:25:17 +00:00
## Call and Run
(*VM).Call/Run functions allows executing arbitrary JS in the cell. They're also
2018-02-09 00:25:17 +00:00
wrappers around Otto VM functions of the same name. `Run` accepts raw JS strings for execution while `Call` takes a JS function name (defined in VM) and parameters.
2017-09-01 18:18:09 +00:00
2018-02-09 00:25:17 +00:00
## Timeouts and intervals support
Default Otto VM interpreter doesn't support setTimeout() / setInterval() JS functions,
2017-09-15 12:16:43 +00:00
because they're not part of ECMA-262 spec, but properties of the window object in browser.
2017-09-01 18:18:09 +00:00
We add support for them using own implementation of Event Loop, heavily based on [ottoext package](https://github.com/deoxxa/ottoext). See loop/fetch/promise packages under [jail/internal/](https://github.com/status-im/status-go/tree/develop/jail/internal).
2018-02-09 00:25:17 +00:00
Each cell starts a new loop in a separate goroutine, registers functions for setTimeout / setInterval calls and associate them with this loop. All JS code executed as callback to setTimeout/setInterval will be handled by this loop.
2017-09-01 18:18:09 +00:00
For example, following code:
2017-09-15 12:16:43 +00:00
```
cell.Run(`setTimeout(function(){ value = "42" }, 2000);`)
```
2017-09-01 18:18:09 +00:00
2018-02-09 00:25:17 +00:00
will execute setTimeout and return immediately, but callback function will
2017-09-15 12:16:43 +00:00
be executed after 2 seconds in the loop that was started upon current cell.
2017-09-01 18:18:09 +00:00
In order to capture response one may use following approach:
2017-09-15 12:16:43 +00:00
```
err = cell.Set("__captureResponse", func(val string) otto.Value {
fmt.Println("Captured response from callback:", val)
return otto.UndefinedValue()
})
cell.Run(`setTimeout(function(){ __captureResponse("OK") }, 2000);`)
```
2017-09-01 18:18:09 +00:00
2018-02-09 00:25:17 +00:00
## Fetch support
2017-09-15 12:16:43 +00:00
Fetch API is implemented in a similar way using the same loop. When Cell is created, corresponding handlers are registered within VM and associated event loop.
2017-09-15 12:16:43 +00:00
Due to asynchronous nature of Fetch API, the following code will return immediately:
2017-09-15 12:16:43 +00:00
```
cell.Run(`fetch('http://example.com/').then(function(data) { ... })`)
```
2018-02-09 00:25:17 +00:00
and callback function in a promise will be executed in a event loop in the background. Thus, it's user responsibility to register a corresponding callback function before:
2017-09-15 12:16:43 +00:00
```
cell.Set("__captureSuccess", func(res otto.Value) { ... })
2017-09-15 12:16:43 +00:00
cell.Run(`fetch('http://example.com').then(function(r) {
return r.text()
}).then(function(data) {
// user code
__captureSuccess(data)
}))
```
2017-09-01 18:18:09 +00:00
2017-09-13 14:27:19 +00:00
2017-09-15 12:16:43 +00:00
* * *
2018-02-09 00:25:17 +00:00
Automatically generated by [autoreadme](https://github.com/jimmyfrasche/autoreadme) on 2018.02.08