Replace javascript module otto with goja (#4631)

* Move plugin javascript to own package with goja
* Use javascript package in scraper

Remove otto
This commit is contained in:
WithoutPants
2024-03-14 11:03:40 +11:00
committed by GitHub
parent 49cd214c9d
commit 9ceea952b6
12 changed files with 381 additions and 288 deletions

106
pkg/javascript/gql.go Normal file
View File

@@ -0,0 +1,106 @@
package javascript
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/dop251/goja"
)
type responseWriter struct {
r strings.Builder
header http.Header
statusCode int
}
func (w *responseWriter) Header() http.Header {
return w.header
}
func (w *responseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
}
func (w *responseWriter) Write(b []byte) (int, error) {
return w.r.Write(b)
}
type GQL struct {
Context context.Context
Cookie *http.Cookie
GQLHandler http.Handler
}
func (g *GQL) gqlRequestFunc(vm *VM) func(query string, variables map[string]interface{}) (goja.Value, error) {
return func(query string, variables map[string]interface{}) (goja.Value, error) {
in := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}{
Query: query,
Variables: variables,
}
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(in)
if err != nil {
return nil, err
}
r, err := http.NewRequestWithContext(g.Context, "POST", "/graphql", &body)
if err != nil {
return nil, fmt.Errorf("could not make request")
}
r.Header.Set("Content-Type", "application/json")
if g.Cookie != nil {
r.AddCookie(g.Cookie)
}
w := &responseWriter{
header: make(http.Header),
}
g.GQLHandler.ServeHTTP(w, r)
if w.statusCode != http.StatusOK && w.statusCode != 0 {
vm.Throw(fmt.Errorf("graphQL query failed: %d - %s. Query: %s. Variables: %v", w.statusCode, w.r.String(), in.Query, in.Variables))
}
output := w.r.String()
// convert to JSON
var obj map[string]interface{}
if err = json.Unmarshal([]byte(output), &obj); err != nil {
vm.Throw(fmt.Errorf("could not unmarshal object %s: %s", output, err.Error()))
}
retErr, hasErr := obj["errors"]
if hasErr {
errOut, _ := json.Marshal(retErr)
vm.Throw(fmt.Errorf("graphql error: %s", string(errOut)))
}
v := vm.ToValue(obj["data"])
return v, nil
}
}
func (g *GQL) AddToVM(globalName string, vm *VM) error {
gql := vm.NewObject()
if err := gql.Set("Do", g.gqlRequestFunc(vm)); err != nil {
return fmt.Errorf("unable to set GraphQL Do function: %w", err)
}
if err := vm.Set(globalName, gql); err != nil {
return fmt.Errorf("unable to set gql: %w", err)
}
return nil
}

86
pkg/javascript/log.go Normal file
View File

@@ -0,0 +1,86 @@
package javascript
import (
"encoding/json"
"fmt"
"math"
"reflect"
"github.com/dop251/goja"
"github.com/stashapp/stash/pkg/logger"
)
const pluginPrefix = "[Plugin] "
type Log struct {
Progress chan float64
}
func (l *Log) argToString(call goja.FunctionCall) string {
arg := call.Argument(0)
var o map[string]interface{}
if arg.ExportType() == reflect.TypeOf(o) {
ii := arg.Export()
o = ii.(map[string]interface{})
data, err := json.Marshal(o)
if err != nil {
logger.Warnf("Couldn't json encode object")
}
return string(data)
}
return arg.String()
}
func (l *Log) logTrace(call goja.FunctionCall) goja.Value {
logger.Trace(pluginPrefix + l.argToString(call))
return nil
}
func (l *Log) logDebug(call goja.FunctionCall) goja.Value {
logger.Debug(pluginPrefix + l.argToString(call))
return nil
}
func (l *Log) logInfo(call goja.FunctionCall) goja.Value {
logger.Info(pluginPrefix + l.argToString(call))
return nil
}
func (l *Log) logWarn(call goja.FunctionCall) goja.Value {
logger.Warn(pluginPrefix + l.argToString(call))
return nil
}
func (l *Log) logError(call goja.FunctionCall) goja.Value {
logger.Error(pluginPrefix + l.argToString(call))
return nil
}
// Progress logs the current progress value. The progress value should be
// between 0 and 1.0 inclusively, with 1 representing that the task is
// complete. Values outside of this range will be clamp to be within it.
func (l *Log) logProgress(value float64) {
value = math.Min(math.Max(0, value), 1)
l.Progress <- value
}
func (l *Log) AddToVM(globalName string, vm *VM) error {
log := vm.NewObject()
if err := SetAll(log,
ObjectValueDef{"Trace", l.logTrace},
ObjectValueDef{"Debug", l.logDebug},
ObjectValueDef{"Info", l.logInfo},
ObjectValueDef{"Warn", l.logWarn},
ObjectValueDef{"Error", l.logError},
ObjectValueDef{"Progress", l.logProgress},
); err != nil {
return err
}
if err := vm.Set(globalName, log); err != nil {
return fmt.Errorf("unable to set log: %w", err)
}
return nil
}

25
pkg/javascript/util.go Normal file
View File

@@ -0,0 +1,25 @@
package javascript
import (
"fmt"
"time"
)
type Util struct{}
func (u *Util) sleepFunc(ms int64) {
time.Sleep(time.Millisecond * time.Duration(ms))
}
func (u *Util) AddToVM(globalName string, vm *VM) error {
util := vm.NewObject()
if err := util.Set("Sleep", u.sleepFunc); err != nil {
return fmt.Errorf("unable to set sleep func: %w", err)
}
if err := vm.Set(globalName, util); err != nil {
return fmt.Errorf("unable to set util: %w", err)
}
return nil
}

64
pkg/javascript/vm.go Normal file
View File

@@ -0,0 +1,64 @@
package javascript
import (
"fmt"
"net/http"
"os"
"github.com/dop251/goja"
)
type VM struct {
*goja.Runtime
Progress chan float64
SessionCookie *http.Cookie
GQLHandler http.Handler
}
func NewVM() *VM {
return &VM{Runtime: goja.New()}
}
type APIAdder interface {
AddToVM(globalName string, vm *VM) error
}
type ObjectValueDef struct {
Name string
Value interface{}
}
type setter interface {
Set(name string, value interface{}) error
}
func Compile(path string) (*goja.Program, error) {
js, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
return goja.Compile(path, string(js), true)
}
func CompileScript(name, script string) (*goja.Program, error) {
return goja.Compile(name, string(script), true)
}
func SetAll(s setter, defs ...ObjectValueDef) error {
for _, def := range defs {
if err := s.Set(def.Name, def.Value); err != nil {
return fmt.Errorf("failed to set %s: %w", def.Name, err)
}
}
return nil
}
func (v *VM) Throw(err error) {
e, newErr := v.New(v.Get("Error"), v.ToValue(err))
if newErr != nil {
panic(newErr)
}
panic(e)
}

View File

@@ -77,6 +77,14 @@ func (m ArgsMap) Float(key string) float64 {
return ret
}
func (m ArgsMap) ToMap() map[string]interface{} {
ret := make(map[string]interface{})
for k, v := range m {
ret[k] = v
}
return ret
}
// PluginInput is the data structure that is sent to plugin instances when they
// are spawned.
type PluginInput struct {

View File

@@ -7,9 +7,9 @@ import (
"path/filepath"
"sync"
"github.com/robertkrimen/otto"
"github.com/dop251/goja"
"github.com/stashapp/stash/pkg/javascript"
"github.com/stashapp/stash/pkg/plugin/common"
"github.com/stashapp/stash/pkg/plugin/js"
)
var errStop = errors.New("stop")
@@ -27,7 +27,7 @@ type jsPluginTask struct {
started bool
waitGroup sync.WaitGroup
vm *otto.Otto
vm *javascript.VM
}
func (t *jsPluginTask) onError(err error) {
@@ -37,24 +37,67 @@ func (t *jsPluginTask) onError(err error) {
}
}
func (t *jsPluginTask) makeOutput(o otto.Value) {
func (t *jsPluginTask) makeOutput(o goja.Value) {
t.result = &common.PluginOutput{}
asObj := o.Object()
asObj := o.ToObject(t.vm.Runtime)
if asObj == nil {
return
}
output, _ := asObj.Get("Output")
t.result.Output, _ = output.Export()
err, _ := asObj.Get("Error")
if !err.IsUndefined() {
t.result.Output = asObj.Get("Output")
err := asObj.Get("Error")
if !goja.IsNull(err) && !goja.IsUndefined(err) {
errStr := err.String()
t.result.Error = &errStr
}
}
func (t *jsPluginTask) initVM() error {
// converting the Args field to map[string]interface{} is required, otherwise
// it gets converted to an empty object
type pluginInput struct {
// Server details to connect to the stash server.
ServerConnection common.StashServerConnection
// Arguments to the plugin operation.
Args map[string]interface{}
}
input := pluginInput{
ServerConnection: t.input.ServerConnection,
Args: t.input.Args.ToMap(),
}
if err := t.vm.Set("input", input); err != nil {
return fmt.Errorf("error setting input: %w", err)
}
log := &javascript.Log{
Progress: t.progress,
}
if err := log.AddToVM("log", t.vm); err != nil {
return fmt.Errorf("error adding log API: %w", err)
}
util := &javascript.Util{}
if err := util.AddToVM("util", t.vm); err != nil {
return fmt.Errorf("error adding util API: %w", err)
}
gql := &javascript.GQL{
Context: context.TODO(),
Cookie: t.input.ServerConnection.SessionCookie,
GQLHandler: t.gqlHandler,
}
if err := gql.AddToVM("gql", t.vm); err != nil {
return fmt.Errorf("error adding GraphQL API: %w", err)
}
return nil
}
func (t *jsPluginTask) Start() error {
if t.started {
return errors.New("task already started")
@@ -68,31 +111,17 @@ func (t *jsPluginTask) Start() error {
scriptFile := t.plugin.Exec[0]
t.vm = otto.New()
t.vm = javascript.NewVM()
pluginPath := t.plugin.getConfigPath()
script, err := t.vm.Compile(filepath.Join(pluginPath, scriptFile), nil)
script, err := javascript.Compile(filepath.Join(pluginPath, scriptFile))
if err != nil {
return err
}
if err := t.vm.Set("input", t.input); err != nil {
return fmt.Errorf("error setting input: %w", err)
if err := t.initVM(); err != nil {
return err
}
if err := js.AddLogAPI(t.vm, t.progress); err != nil {
return fmt.Errorf("error adding log API: %w", err)
}
if err := js.AddUtilAPI(t.vm); err != nil {
return fmt.Errorf("error adding util API: %w", err)
}
if err := js.AddGQLAPI(context.TODO(), t.vm, t.input.ServerConnection.SessionCookie, t.gqlHandler); err != nil {
return fmt.Errorf("error adding GraphQL API: %w", err)
}
t.vm.Interrupt = make(chan func(), 1)
t.waitGroup.Add(1)
go func() {
@@ -107,7 +136,7 @@ func (t *jsPluginTask) Start() error {
}
}()
output, err := t.vm.Run(script)
output, err := t.vm.RunProgram(script)
if err != nil {
t.onError(err)
@@ -124,9 +153,6 @@ func (t *jsPluginTask) Wait() {
}
func (t *jsPluginTask) Stop() error {
// TODO - need another way of doing this that doesn't require panic
t.vm.Interrupt <- func() {
panic(errStop)
}
t.vm.Interrupt(errStop)
return nil
}

View File

@@ -1,119 +0,0 @@
package js
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/robertkrimen/otto"
)
type responseWriter struct {
r strings.Builder
header http.Header
statusCode int
}
func (w *responseWriter) Header() http.Header {
return w.header
}
func (w *responseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
}
func (w *responseWriter) Write(b []byte) (int, error) {
return w.r.Write(b)
}
func throw(vm *otto.Otto, str string) {
value, _ := vm.Call("new Error", nil, str)
panic(value)
}
func gqlRequestFunc(ctx context.Context, vm *otto.Otto, cookie *http.Cookie, gqlHandler http.Handler) func(call otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) == 0 {
throw(vm, "missing argument")
}
query := call.Argument(0)
vars := call.Argument(1)
var variables map[string]interface{}
if !vars.IsUndefined() {
exported, _ := vars.Export()
variables, _ = exported.(map[string]interface{})
}
in := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}{
Query: query.String(),
Variables: variables,
}
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(in)
if err != nil {
throw(vm, err.Error())
}
r, err := http.NewRequestWithContext(ctx, "POST", "/graphql", &body)
if err != nil {
throw(vm, "could not make request")
}
r.Header.Set("Content-Type", "application/json")
if cookie != nil {
r.AddCookie(cookie)
}
w := &responseWriter{
header: make(http.Header),
}
gqlHandler.ServeHTTP(w, r)
if w.statusCode != http.StatusOK && w.statusCode != 0 {
throw(vm, fmt.Sprintf("graphQL query failed: %d - %s. Query: %s. Variables: %v", w.statusCode, w.r.String(), in.Query, in.Variables))
}
output := w.r.String()
// convert to JSON
var obj map[string]interface{}
if err = json.Unmarshal([]byte(output), &obj); err != nil {
throw(vm, fmt.Sprintf("could not unmarshal object %s: %s", output, err.Error()))
}
retErr, hasErr := obj["errors"]
if hasErr {
errOut, _ := json.Marshal(retErr)
throw(vm, fmt.Sprintf("graphql error: %s", string(errOut)))
}
v, err := vm.ToValue(obj["data"])
if err != nil {
throw(vm, fmt.Sprintf("could not create return value: %s", err.Error()))
}
return v
}
}
func AddGQLAPI(ctx context.Context, vm *otto.Otto, cookie *http.Cookie, gqlHandler http.Handler) error {
gql, _ := vm.Object("({})")
if err := gql.Set("Do", gqlRequestFunc(ctx, vm, cookie, gqlHandler)); err != nil {
return fmt.Errorf("unable to set GraphQL Do function: %w", err)
}
if err := vm.Set("gql", gql); err != nil {
return fmt.Errorf("unable to set gql: %w", err)
}
return nil
}

View File

@@ -1,96 +0,0 @@
package js
import (
"encoding/json"
"fmt"
"math"
"github.com/robertkrimen/otto"
"github.com/stashapp/stash/pkg/logger"
)
const pluginPrefix = "[Plugin] "
func argToString(call otto.FunctionCall) string {
arg := call.Argument(0)
if arg.IsObject() {
o, _ := arg.Export()
data, err := json.Marshal(o)
if err != nil {
logger.Warnf("Couldn't json encode object")
}
return string(data)
}
return arg.String()
}
func logTrace(call otto.FunctionCall) otto.Value {
logger.Trace(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logDebug(call otto.FunctionCall) otto.Value {
logger.Debug(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logInfo(call otto.FunctionCall) otto.Value {
logger.Info(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logWarn(call otto.FunctionCall) otto.Value {
logger.Warn(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logError(call otto.FunctionCall) otto.Value {
logger.Error(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
// Progress logs the current progress value. The progress value should be
// between 0 and 1.0 inclusively, with 1 representing that the task is
// complete. Values outside of this range will be clamp to be within it.
func logProgressFunc(c chan float64) func(call otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
arg := call.Argument(0)
if !arg.IsNumber() {
return otto.UndefinedValue()
}
progress, _ := arg.ToFloat()
progress = math.Min(math.Max(0, progress), 1)
c <- progress
return otto.UndefinedValue()
}
}
func AddLogAPI(vm *otto.Otto, progress chan float64) error {
log, _ := vm.Object("({})")
if err := log.Set("Trace", logTrace); err != nil {
return fmt.Errorf("error setting Trace: %w", err)
}
if err := log.Set("Debug", logDebug); err != nil {
return fmt.Errorf("error setting Debug: %w", err)
}
if err := log.Set("Info", logInfo); err != nil {
return fmt.Errorf("error setting Info: %w", err)
}
if err := log.Set("Warn", logWarn); err != nil {
return fmt.Errorf("error setting Warn: %w", err)
}
if err := log.Set("Error", logError); err != nil {
return fmt.Errorf("error setting Error: %w", err)
}
if err := log.Set("Progress", logProgressFunc(progress)); err != nil {
return fmt.Errorf("error setting Progress: %v", err)
}
if err := vm.Set("log", log); err != nil {
return fmt.Errorf("unable to set log: %w", err)
}
return nil
}

View File

@@ -1,29 +0,0 @@
package js
import (
"fmt"
"time"
"github.com/robertkrimen/otto"
)
func sleepFunc(call otto.FunctionCall) otto.Value {
arg := call.Argument(0)
ms, _ := arg.ToInteger()
time.Sleep(time.Millisecond * time.Duration(ms))
return otto.UndefinedValue()
}
func AddUtilAPI(vm *otto.Otto) error {
util, _ := vm.Object("({})")
if err := util.Set("Sleep", sleepFunc); err != nil {
return fmt.Errorf("unable to set sleep func: %w", err)
}
if err := vm.Set("util", util); err != nil {
return fmt.Errorf("unable to set util: %w", err)
}
return nil
}

View File

@@ -11,9 +11,9 @@ import (
"strings"
"time"
"github.com/robertkrimen/otto"
"gopkg.in/yaml.v2"
"github.com/stashapp/stash/pkg/javascript"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/sliceutil"
@@ -528,19 +528,19 @@ func (p *postProcessLbToKg) Apply(ctx context.Context, value string, q mappedQue
type postProcessJavascript string
func (p *postProcessJavascript) Apply(ctx context.Context, value string, q mappedQuery) string {
vm := otto.New()
vm := javascript.NewVM()
if err := vm.Set("value", value); err != nil {
logger.Warnf("javascript failed to set value: %v", err)
return value
}
script, err := vm.Compile("", "(function() { "+string(*p)+"})()")
script, err := javascript.CompileScript("", "(function() { "+string(*p)+"})()")
if err != nil {
logger.Warnf("javascript failed to compile: %v", err)
return value
}
output, err := vm.Run(script)
output, err := vm.RunProgram(script)
if err != nil {
logger.Warnf("javascript failed to run: %v", err)
return value