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
+8
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 {
+59 -33
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
}
-119
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
}
-96
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
}
-29
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
}