Embedded javascript plugins (#1393)

This commit is contained in:
WithoutPants
2021-05-26 14:17:53 +10:00
committed by GitHub
parent cc5ec650ae
commit 9b57fbbf50
108 changed files with 30179 additions and 91 deletions

107
pkg/plugin/js/gql.go Normal file
View File

@@ -0,0 +1,107 @@
package js
import (
"bytes"
"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(vm *otto.Otto, gqlHandler http.HandlerFunc) 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.NewRequest("POST", "/graphql", &body)
if err != nil {
throw(vm, "could not make request")
}
r.Header.Set("Content-Type", "application/json")
w := &responseWriter{
header: make(http.Header),
}
gqlHandler(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["error"]
if hasErr {
throw(vm, fmt.Sprintf("graphql error: %v", retErr))
}
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(vm *otto.Otto, gqlHandler http.HandlerFunc) {
gql, _ := vm.Object("({})")
gql.Set("Do", gqlRequestFunc(vm, gqlHandler))
vm.Set("gql", gql)
}

75
pkg/plugin/js/log.go Normal file
View File

@@ -0,0 +1,75 @@
package js
import (
"encoding/json"
"math"
"github.com/robertkrimen/otto"
"github.com/stashapp/stash/pkg/logger"
)
func argToString(call otto.FunctionCall) string {
arg := call.Argument(0)
if arg.IsObject() {
o, _ := arg.Export()
data, _ := json.Marshal(o)
return string(data)
}
return arg.String()
}
func logTrace(call otto.FunctionCall) otto.Value {
logger.Trace(argToString(call))
return otto.UndefinedValue()
}
func logDebug(call otto.FunctionCall) otto.Value {
logger.Debug(argToString(call))
return otto.UndefinedValue()
}
func logInfo(call otto.FunctionCall) otto.Value {
logger.Info(argToString(call))
return otto.UndefinedValue()
}
func logWarn(call otto.FunctionCall) otto.Value {
logger.Warn(argToString(call))
return otto.UndefinedValue()
}
func logError(call otto.FunctionCall) otto.Value {
logger.Error(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) {
log, _ := vm.Object("({})")
log.Set("Trace", logTrace)
log.Set("Debug", logDebug)
log.Set("Info", logInfo)
log.Set("Warn", logWarn)
log.Set("Error", logError)
log.Set("Progress", logProgressFunc(progress))
vm.Set("log", log)
}

22
pkg/plugin/js/util.go Normal file
View File

@@ -0,0 +1,22 @@
package js
import (
"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) {
util, _ := vm.Object("({})")
util.Set("Sleep", sleepFunc)
vm.Set("util", util)
}