Merge branch 'migrate_to_isp-lib_scripts' into 'master'
Migrate to isp-lib scripts See merge request integration-system/isp-script-service!1
This commit is contained in:
commit
df4fb851aa
9 files changed
+53
-191
No files matched your search
@@ -1,3 +1,5 @@
|
||||
### v2.5.0
|
||||
* migrate from inbuilt wrapping of goja to new isp-lib/scripts package
|
||||
### v2.4.1
|
||||
* updated isp-lib
|
||||
* updated isp-event-lib
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/scripts"
|
||||
"isp-script-service/adapter"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/controller"
|
||||
"isp-script-service/domain"
|
||||
"isp-script-service/script"
|
||||
"isp-script-service/service"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
@@ -91,8 +91,9 @@ func BenchmarkJsCustomData(b *testing.B) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
scriptEngine := scripts.NewEngine()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = script.Default().Execute(scr, request.Arg)
|
||||
_, _ = scriptEngine.Execute(scr, request.Arg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,8 +106,9 @@ func BenchmarkInlineJson(b *testing.B) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
scriptEngine := scripts.NewEngine()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := script.Default().Execute(c, string(data))
|
||||
_, err := scriptEngine.Execute(c, string(data))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -123,12 +125,13 @@ func BenchmarkJsoniterJson(b *testing.B) {
|
||||
panic(err)
|
||||
}
|
||||
json := jsoniter.ConfigFastest
|
||||
scriptEngine := scripts.NewEngine()
|
||||
for i := 0; i < b.N; i++ {
|
||||
m := make(map[string]interface{})
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
res, err := script.Default().Execute(c, m)
|
||||
res, err := scriptEngine.Execute(c, m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -3,12 +3,10 @@ module isp-script-service
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/dlclark/regexp2 v1.2.0 // indirect
|
||||
github.com/dop251/goja v0.0.0-20200526165454-f1752421c432
|
||||
github.com/dop251/goja v0.0.0-20201221183957-6b6d5e2b5d80
|
||||
github.com/go-openapi/spec v0.19.9 // indirect
|
||||
github.com/go-openapi/swag v0.19.9 // indirect
|
||||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
|
||||
github.com/integration-system/isp-lib/v2 v2.7.0
|
||||
github.com/integration-system/isp-lib/v2 v2.8.1
|
||||
github.com/integration-system/isp-log v1.1.6
|
||||
github.com/jmoiron/jsonq v0.0.0-20150511023944-e874b168d07e
|
||||
github.com/json-iterator/go v1.1.10
|
||||
|
||||
@@ -47,8 +47,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
|
||||
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
|
||||
github.com/buaazp/fasthttprouter v0.1.1 h1:4oAnN0C3xZjylvZJdP35cxfclyn4TYkW6Y+DSvS+h8Q=
|
||||
github.com/buaazp/fasthttprouter v0.1.1/go.mod h1:h/Ap5oRVLeItGKTVBb+heQPks+HdIUtGmI4H5WCYijM=
|
||||
github.com/cenkalti/backoff/v4 v4.0.2 h1:JIufpQLbh4DkbQoii76ItQIUFzevQSqOLZca4eamEDs=
|
||||
github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
|
||||
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
|
||||
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
@@ -78,10 +78,10 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
|
||||
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
|
||||
github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk=
|
||||
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
|
||||
github.com/dop251/goja v0.0.0-20200526165454-f1752421c432 h1:EIY1hqp9O08saJ41t7aQy0o1hhq3ByOy61AACthST5M=
|
||||
github.com/dop251/goja v0.0.0-20200526165454-f1752421c432/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
|
||||
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
|
||||
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
|
||||
github.com/dop251/goja v0.0.0-20201221183957-6b6d5e2b5d80 h1:KJXPPsVVe0PC50I+a/dI8IYPvy+3iaXqnjiF19iuLxQ=
|
||||
github.com/dop251/goja v0.0.0-20201221183957-6b6d5e2b5d80/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA=
|
||||
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
@@ -147,7 +147,7 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
|
||||
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
|
||||
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||
github.com/go-redis/redis/v8 v8.2.3/go.mod h1:ysgGY09J/QeDYbu3HikWEIPCwaeOkuNoTgKayTEaEOw=
|
||||
github.com/go-redis/redis/v8 v8.4.4/go.mod h1:nA0bQuF0i5JFx4Ta9RZxGKXFrQ8cRWntra97f0196iY=
|
||||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
|
||||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
|
||||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
@@ -193,8 +193,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
|
||||
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
|
||||
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
|
||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
@@ -253,8 +253,8 @@ github.com/integration-system/go-cmp v0.0.0-20190131081942-ac5582987a2f/go.mod h
|
||||
github.com/integration-system/gowsdl v1.0.0/go.mod h1:ZwTCE+Av5aQLnPj2/3PIop6mzCs1FY7jXrcWDvSjrBA=
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.2 h1:K3mZUm71VuaaBAriMxOp2G/Wql+pgaryI1MZzr81H1U=
|
||||
github.com/integration-system/isp-etp-go/v2 v2.1.2/go.mod h1:fSkS09/engepaDDqaDAEeXUpzmtcMpSLjilZ35BfNcc=
|
||||
github.com/integration-system/isp-lib/v2 v2.7.0 h1:ELGGkNdFhdq+GlNiX8zcFaIkCW8Zr0B/fEIDDEOqbLM=
|
||||
github.com/integration-system/isp-lib/v2 v2.7.0/go.mod h1:SIKj2NRxLHVlV3nMVRrUPjGs554kA5CfVvyzq2dKm2g=
|
||||
github.com/integration-system/isp-lib/v2 v2.8.1 h1:bqL6SYdePr0zQWmmqmgoangypYTk5T6DQEdNV7nkh7A=
|
||||
github.com/integration-system/isp-lib/v2 v2.8.1/go.mod h1:sOiJv5aOy7OonD+BtbarTkuXDMJ4D686whUFxe7NSFs=
|
||||
github.com/integration-system/isp-log v1.1.6 h1:a0X0nT0AsM72Js1aT/EJAItaQJsVqlOMvESIA719cdY=
|
||||
github.com/integration-system/isp-log v1.1.6/go.mod h1:TpUZ/Tx84sRipAT/YXZ8V7Z21C4iZ8o3wXQs3Cee0Pk=
|
||||
github.com/integration-system/jsonschema v1.0.0 h1:Lci4sHGhOX27qCgZo5rvJ37Bjcfm+qCwU0yvw6ImEX4=
|
||||
@@ -349,11 +349,11 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
|
||||
github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
|
||||
github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
|
||||
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
||||
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||
github.com/onsi/gomega v1.10.4/go.mod h1:g/HbgYopi++010VEqkFgJHKC09uJiW9UkXvMUuKHUCQ=
|
||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
|
||||
@@ -470,8 +470,8 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
|
||||
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.17.0 h1:P8/koH4aSnJ4xbd0cUUFEGQs3jQqIxoDDyRQrUiAkqg=
|
||||
github.com/valyala/fasthttp v1.17.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
|
||||
github.com/valyala/fasthttp v1.18.0 h1:IV0DdMlatq9QO1Cr6wGJPVW1sV1Q8HvZXAIcjorylyM=
|
||||
github.com/valyala/fasthttp v1.18.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
|
||||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
|
||||
github.com/vmihailenco/bufpool v0.1.5/go.mod h1:fL9i/PRTuS7AELqAHwSU1Zf1c70xhkhGe/cD5ud9pJk=
|
||||
github.com/vmihailenco/msgpack/v4 v4.3.5/go.mod h1:DuaveEe48abshDmz5UBKyZ+yDugvaeFk5ayfrewUOaw=
|
||||
@@ -487,7 +487,7 @@ go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.opentelemetry.io/otel v0.11.0/go.mod h1:G8UCk+KooF2HLkgo8RHX9epABH/aRGYET7gQOqBVdB0=
|
||||
go.opentelemetry.io/otel v0.15.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA=
|
||||
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
|
||||
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||
go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
|
||||
@@ -569,8 +569,8 @@ golang.org/x/net v0.0.0-20200222033325-078779b8f2d8/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
|
||||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U=
|
||||
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/router"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
)
|
||||
|
||||
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 {
|
||||
//nolint:goerr113
|
||||
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()
|
||||
vm.ClearInterrupt()
|
||||
pool.Put(vm)
|
||||
}()
|
||||
|
||||
vm.Set("arg", arg)
|
||||
res, err := vm.RunProgram(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res.Export(), nil
|
||||
}
|
||||
|
||||
type GojaProgram struct {
|
||||
prog *goja.Program
|
||||
}
|
||||
|
||||
func (a *GojaProgram) Src() interface{} {
|
||||
return a.prog
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package script_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
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)
|
||||
//nolint:goerr113
|
||||
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{}{})
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
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{}
|
||||
}
|
||||
+23
-18
@@ -1,14 +1,14 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/scripts"
|
||||
log_code "isp-script-service/codes"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/domain"
|
||||
"isp-script-service/script"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
log "github.com/integration-system/isp-log"
|
||||
@@ -17,14 +17,17 @@ import (
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
var Script = &scriptService{}
|
||||
var Script = &scriptService{
|
||||
scriptEngine: scripts.NewEngine(),
|
||||
}
|
||||
|
||||
type scriptService struct {
|
||||
store atomic.Value
|
||||
store atomic.Value
|
||||
scriptEngine *scripts.Engine
|
||||
}
|
||||
|
||||
func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition) {
|
||||
newStore := make(map[string]script.Script)
|
||||
newStore := make(map[string]scripts.Script)
|
||||
for i, value := range scriptDef {
|
||||
scr, err := s.Create(value.Script)
|
||||
if err != nil {
|
||||
@@ -47,8 +50,8 @@ func (s *scriptService) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
||||
}
|
||||
|
||||
func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) {
|
||||
scr := s.store.Load().(map[string]script.Script)[req.Id]
|
||||
if scr == nil {
|
||||
scr, ok := s.store.Load().(map[string]scripts.Script)[req.Id]
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id)
|
||||
}
|
||||
|
||||
@@ -57,16 +60,16 @@ func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.Scri
|
||||
|
||||
func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp {
|
||||
wg := sync.WaitGroup{}
|
||||
store := s.store.Load().(map[string]script.Script)
|
||||
store := s.store.Load().(map[string]scripts.Script)
|
||||
response := make([]domain.ScriptResp, len(req))
|
||||
for i := range req {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
if store[req[i].Id] == nil {
|
||||
if scr, ok := store[req[i].Id]; !ok {
|
||||
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req[i].Id), domain.ErrorCompile)
|
||||
} else {
|
||||
response[i] = *s.executeScript(store[req[i].Id], req[i].Arg)
|
||||
response[i] = *s.executeScript(scr, req[i].Arg)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
@@ -77,16 +80,16 @@ func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.S
|
||||
|
||||
func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp {
|
||||
wg := sync.WaitGroup{}
|
||||
store := s.store.Load().(map[string]script.Script)
|
||||
store := s.store.Load().(map[string]scripts.Script)
|
||||
response := make([]domain.ScriptResp, len(req.Ids))
|
||||
for i := range req.Ids {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
if store[req.Ids[i]] == nil {
|
||||
if scr, ok := store[req.Ids[i]]; !ok {
|
||||
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req.Ids[i]), domain.ErrorCompile)
|
||||
} else {
|
||||
response[i] = *s.executeScript(store[req.Ids[i]], req.Arg)
|
||||
response[i] = *s.executeScript(scr, req.Arg)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
@@ -95,17 +98,19 @@ func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []
|
||||
return response
|
||||
}
|
||||
|
||||
func (*scriptService) Create(scr string) (script.Script, error) {
|
||||
func (*scriptService) Create(scr string) (scripts.Script, error) {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
scr = fmt.Sprintf("%s%s%s%s", cfg.SharedScript, "(function() {", scr, "})();")
|
||||
|
||||
return script.Default().Compile(scr)
|
||||
return scripts.NewScript([]byte(cfg.SharedScript),
|
||||
[]byte("(function() {\n"), []byte(scr), []byte("\n})();"))
|
||||
}
|
||||
|
||||
var errEmpty = errors.New("empty answer, maybe lost return")
|
||||
|
||||
func (s *scriptService) executeScript(scr script.Script, arg interface{}) *domain.ScriptResp {
|
||||
response, err := script.Default().Execute(scr, arg)
|
||||
func (s *scriptService) executeScript(scr scripts.Script, arg interface{}) *domain.ScriptResp {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
response, err := s.scriptEngine.Execute(scr, arg,
|
||||
scripts.WithScriptTimeout(time.Duration(cfg.ScriptExecutionTimeoutMs)*time.Millisecond))
|
||||
if err != nil {
|
||||
return s.respError(err, domain.ErrorRunTime)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user