Skip to content

Commit

Permalink
vendor goja
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblakeley committed Nov 5, 2018
1 parent 5bf1a09 commit eeae39d
Show file tree
Hide file tree
Showing 118 changed files with 18,170 additions and 182,436 deletions.
11 changes: 1 addition & 10 deletions jsvm_goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/user"
"github.com/dop251/goja"
_ "github.com/robertkrimen/otto/underscore"
)

type TykJSVM interface {
Expand Down Expand Up @@ -131,12 +130,6 @@ func (j *GojaJSVM) RunJSRequestDynamic(d *DynamicMiddleware, logger *logrus.Entr
ret := make(chan goja.Value, 1)
errRet := make(chan error, 1)
go func() {
defer func() {
// the VM executes the panic func that gets it
// to stop, so we must recover here to not crash
// the whole Go program.
recover()
}()
returnRaw, err := vm.RunString(middlewareClassname + `.DoProcessRequest(` + requestAsJson + `, ` + sessionAsJson + `, ` + specAsJson + `);`)
ret <- returnRaw
errRet <- err
Expand All @@ -154,9 +147,7 @@ func (j *GojaJSVM) RunJSRequestDynamic(d *DynamicMiddleware, logger *logrus.Entr
t.Stop()
logger.Error("JS middleware timed out after ", d.Spec.JSVM.GetTimeout())
interrupt <- func() {
// only way to stop the VM is to send it a func
// that panics.
panic("stop")
vm.Interrupt("HALT")
}
return nil, http.StatusOK, ""
}
Expand Down
12 changes: 5 additions & 7 deletions jsvm_otto.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,27 @@ func (j *OttoJSVM) GetTimeout() time.Duration {
// Init creates the JSVM with the core library and sets up a default
// timeout.
func (j *OttoJSVM) Init(spec *APISpec, logger *logrus.Entry) {
vm := otto.New()
j.VM = otto.New()
logger = logger.WithField("prefix", "jsvm")

// Init TykJS namespace, constructors etc.
if _, err := vm.Run(coreJS); err != nil {
if _, err := j.Run(coreJS); err != nil {
logger.WithError(err).Error("Could not load TykJS")
return
}

// Load user's TykJS on top, if any
if path := config.Global().TykJSPath; path != "" {
f, err := os.Open(path)
f, err := ioutil.ReadFile(path)
if err == nil {
_, err = vm.Run(f)
f.Close()
_, err = j.Run(string(f))

if err != nil {
logger.WithError(err).Error("Could not load user's TykJS")
}
}
}

j.VM = vm
j.Spec = spec

// Add environment API
Expand Down Expand Up @@ -434,7 +432,7 @@ func (j *OttoJSVM) Run(s string) (interface{}, error) {
return j.VM.Run(s)
}

// wraps otto string function to avoid using reflection in tests etc when stringifying results of vm.Run() so it here where its safer
// wraps otto String() function to avoid using reflection in tests etc when stringifying results of vm.Run() so it here where its safer
func (j *OttoJSVM) String(val interface{}) string {
return val.(otto.Value).String()
}
2 changes: 1 addition & 1 deletion utils/ci-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ go get -t
# profile for multiple pkgs
for pkg in $PKGS; do
for opts in "${MATRIX[@]}"; do
show go test -timeout 60s -v -coverprofile=test-$i.cov $opts $pkg \
show go test -timeout 40s -v -coverprofile=test-$i.cov $opts $pkg \
|| fatal "go test errored"
let i++ || true
done
Expand Down
15 changes: 15 additions & 0 deletions vendor/github.com/dop251/goja/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions vendor/github.com/dop251/goja/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eeae39d

Please sign in to comment.