init
This commit is contained in:
1 parent
bdbb3216ce
commit
c0775acb8a
25 files changed
+2252
No files matched your search
@@ -0,0 +1,83 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/router"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var pool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return initVm()
|
||||
},
|
||||
}
|
||||
|
||||
func initVm() *goja.Runtime {
|
||||
vm := goja.New()
|
||||
vm.Set("invoke", router.Invoke)
|
||||
return vm
|
||||
}
|
||||
|
||||
type Goja struct{}
|
||||
|
||||
func (*Goja) Compile(script string) (Script, error) {
|
||||
prog, err := goja.Compile("script", script, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &GojaProgram{prog: prog}, nil
|
||||
}
|
||||
|
||||
func (*Goja) Execute(program Script, arg interface{}) (interface{}, error) {
|
||||
value, ok := program.Src().(*goja.Program)
|
||||
if !ok {
|
||||
return nil, errors.New("unknown engine")
|
||||
}
|
||||
|
||||
vm := pool.Get().(*goja.Runtime)
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
awaitTime := time.Duration(cfg.ScriptExecutionTimeoutMs) * time.Millisecond
|
||||
t := time.AfterFunc(awaitTime, func() {
|
||||
vm.Interrupt("execution timeout")
|
||||
})
|
||||
defer func() {
|
||||
t.Stop()
|
||||
pool.Put(vm)
|
||||
}()
|
||||
|
||||
vm.Set("arg", arg)
|
||||
res, err := vm.RunProgram(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res.Export(), nil
|
||||
/*go func() {
|
||||
result, err := vm.RunProgram(value)
|
||||
if err != nil {
|
||||
errorCh <- err
|
||||
return
|
||||
}
|
||||
resultCh <- result.Export()
|
||||
}()
|
||||
select {
|
||||
case result := <-resultCh:
|
||||
return result, nil
|
||||
case err := <-errorCh:
|
||||
return nil, err
|
||||
case <-time.After(awaitTime):
|
||||
vm.Interrupt("time exceeded")
|
||||
return nil, errors.New("time exceeded")
|
||||
}*/
|
||||
}
|
||||
|
||||
type GojaProgram struct {
|
||||
prog *goja.Program
|
||||
}
|
||||
|
||||
func (a *GojaProgram) Src() interface{} {
|
||||
return a.prog
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGoja_AddFunction(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
const SCRIPT = `
|
||||
var response = {};
|
||||
try {
|
||||
response["test"] = f("string", "unknown", "test");
|
||||
} catch (e) {
|
||||
if (!(e instanceof GoError)) {
|
||||
throw(e);
|
||||
}
|
||||
if (e.value.Error() !== "TEST") {
|
||||
throw("Unexpected value: " + e.value.Error());
|
||||
}
|
||||
}
|
||||
return response;
|
||||
`
|
||||
|
||||
f := func(varchar string, integer int, object string) (interface{}, error) {
|
||||
a.Equal("string", varchar)
|
||||
a.Equal(0, integer)
|
||||
a.Equal("test", object)
|
||||
return "test", nil
|
||||
}
|
||||
vm := goja.New()
|
||||
vm.Set("f", f)
|
||||
resp, err := vm.RunString(fmt.Sprintf("(function() { %s })();", SCRIPT))
|
||||
a.NoError(err)
|
||||
a.Equal(resp.Export(), map[string]interface{}{"test": "test"})
|
||||
|
||||
f2 := func(varchar string, integer int, object string) (interface{}, error) {
|
||||
a.Equal("string", varchar)
|
||||
a.Equal(0, integer)
|
||||
a.Equal("test", object)
|
||||
return "test", errors.New("TEST")
|
||||
}
|
||||
vm = goja.New()
|
||||
vm.Set("f", f2)
|
||||
resp, err = vm.RunString(fmt.Sprintf("(function() { %s })();", SCRIPT))
|
||||
a.NoError(err)
|
||||
a.NoError(err)
|
||||
a.Equal(resp.Export(), map[string]interface{}{})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package script
|
||||
|
||||
var scriptEngine = &Goja{}
|
||||
|
||||
func Default() Engine {
|
||||
return scriptEngine
|
||||
}
|
||||
|
||||
type Engine interface {
|
||||
Compile(string) (Script, error)
|
||||
Execute(Script, interface{}) (interface{}, error)
|
||||
}
|
||||
|
||||
type Script interface {
|
||||
Src() interface{}
|
||||
}
|
||||
Reference in new issue
Block a user