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)
}