mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-22 08:44:33 +08:00
refactor: js api
This commit is contained in:
120
parsers/js.go
120
parsers/js.go
@@ -4,12 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/blang/semver"
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/parser"
|
"github.com/krau/SaveAny-Bot/pkg/parser"
|
||||||
@@ -101,50 +98,6 @@ func newJSParser(vm *goja.Runtime, canHandleFunc, parseFunc goja.Value, metadata
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerParser(vm *goja.Runtime) func(call goja.FunctionCall) goja.Value {
|
|
||||||
return func(call goja.FunctionCall) goja.Value {
|
|
||||||
jsObj := call.Argument(0)
|
|
||||||
if jsObj == nil || goja.IsUndefined(jsObj) || goja.IsNull(jsObj) {
|
|
||||||
panic("registerParser expects an object { canHandle, parse }")
|
|
||||||
}
|
|
||||||
|
|
||||||
obj := jsObj.ToObject(vm)
|
|
||||||
if obj == nil {
|
|
||||||
panic("registerParser: cannot convert argument to object")
|
|
||||||
}
|
|
||||||
metaValue := obj.Get("metadata")
|
|
||||||
if metaValue == nil || goja.IsUndefined(metaValue) {
|
|
||||||
panic("parser must provide metadata")
|
|
||||||
}
|
|
||||||
var metadata PluginMeta
|
|
||||||
if exported := metaValue.Export(); exported != nil {
|
|
||||||
data, err := json.Marshal(exported)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("failed to marshal metadata to JSON: %v", err))
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(data, &metadata); err != nil {
|
|
||||||
panic(fmt.Sprintf("failed to unmarshal JSON to PluginMeta: %v", err))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic("metadata cannot be null or undefined")
|
|
||||||
}
|
|
||||||
|
|
||||||
pluginV := semver.MustParse(metadata.Version)
|
|
||||||
if pluginV.LT(MinimumParserVersion) || pluginV.GT(LatestParserVersion) {
|
|
||||||
panic(fmt.Sprintf("parser version %s is not supported, must be between %s and %s", metadata.Version, MinimumParserVersion, LatestParserVersion))
|
|
||||||
}
|
|
||||||
|
|
||||||
handleFn := obj.Get("canHandle")
|
|
||||||
parseFn := obj.Get("parse")
|
|
||||||
if parseFn == nil || goja.IsUndefined(parseFn) {
|
|
||||||
panic("parser must provide a parse function")
|
|
||||||
}
|
|
||||||
|
|
||||||
parsers = append(parsers, newJSParser(vm, handleFn, parseFn, metadata))
|
|
||||||
return goja.Undefined()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadPlugins(ctx context.Context, dir string) error {
|
func LoadPlugins(ctx context.Context, dir string) error {
|
||||||
entries, err := os.ReadDir(dir)
|
entries, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -162,79 +115,12 @@ func LoadPlugins(ctx context.Context, dir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vm := goja.New()
|
vm := goja.New()
|
||||||
vm.Set("registerParser", registerParser(vm))
|
vm.Set("registerParser", jsRegisterParser(vm))
|
||||||
// Inject some utils to vm
|
// Inject some utils to vm
|
||||||
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("[plugin|parser]/%s", e.Name()))
|
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("[plugin|parser]/%s", e.Name()))
|
||||||
vm.Set("console", map[string]any{
|
vm.Set("console", jsConsole(logger))
|
||||||
"log": func(args ...any) {
|
|
||||||
if len(args) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(args) > 1 {
|
|
||||||
logger.Info(args[0], args[1:]...)
|
|
||||||
} else {
|
|
||||||
logger.Info(args[0])
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
// http fetch funcs
|
// http fetch funcs
|
||||||
ghttp := vm.NewObject()
|
vm.Set("ghttp", jsGhttp(vm))
|
||||||
ghttp.Set("get", func(call goja.FunctionCall) goja.Value {
|
|
||||||
url := call.Argument(0).String()
|
|
||||||
resp, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"error": fmt.Sprintf("failed to fetch %s: %v", url, err),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"error": fmt.Sprintf("failed to fetch %s: %s", url, resp.Status),
|
|
||||||
"status": resp.StatusCode,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"error": fmt.Errorf("failed to read response body: %w", err).Error(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return vm.ToValue(string(body))
|
|
||||||
})
|
|
||||||
ghttp.Set("getJSON", func(call goja.FunctionCall) goja.Value {
|
|
||||||
url := call.Argument(0).String()
|
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"error": fmt.Sprintf("failed to fetch %s: %v", url, err),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"error": fmt.Sprintf("failed to fetch %s: %s", url, resp.Status),
|
|
||||||
"status": resp.StatusCode,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"error": fmt.Errorf("failed to read response body: %w", err).Error(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
var jsonData map[string]any
|
|
||||||
if err := json.Unmarshal(body, &jsonData); err != nil {
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"error": fmt.Errorf("failed to unmarshal JSON: %w", err).Error(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return vm.ToValue(map[string]any{
|
|
||||||
"data": jsonData,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
vm.Set("ghttp", ghttp)
|
|
||||||
|
|
||||||
if _, err := vm.RunString(string(code)); err != nil {
|
if _, err := vm.RunString(string(code)); err != nil {
|
||||||
return fmt.Errorf("error loading plugin %s: %w", e.Name(), err)
|
return fmt.Errorf("error loading plugin %s: %w", e.Name(), err)
|
||||||
|
|||||||
151
parsers/js_api.go
Normal file
151
parsers/js_api.go
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
package parsers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/blang/semver"
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
|
"github.com/dop251/goja"
|
||||||
|
"github.com/krau/SaveAny-Bot/common/utils/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func jsRegisterParser(vm *goja.Runtime) func(call goja.FunctionCall) goja.Value {
|
||||||
|
return func(call goja.FunctionCall) goja.Value {
|
||||||
|
jsObj := call.Argument(0)
|
||||||
|
if jsObj == nil || goja.IsUndefined(jsObj) || goja.IsNull(jsObj) {
|
||||||
|
panic("registerParser expects an object { canHandle, parse }")
|
||||||
|
}
|
||||||
|
|
||||||
|
obj := jsObj.ToObject(vm)
|
||||||
|
if obj == nil {
|
||||||
|
panic("registerParser: cannot convert argument to object")
|
||||||
|
}
|
||||||
|
metaValue := obj.Get("metadata")
|
||||||
|
if metaValue == nil || goja.IsUndefined(metaValue) {
|
||||||
|
panic("parser must provide metadata")
|
||||||
|
}
|
||||||
|
var metadata PluginMeta
|
||||||
|
if exported := metaValue.Export(); exported != nil {
|
||||||
|
data, err := json.Marshal(exported)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to marshal metadata to JSON: %v", err))
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(data, &metadata); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to unmarshal JSON to PluginMeta: %v", err))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic("metadata cannot be null or undefined")
|
||||||
|
}
|
||||||
|
|
||||||
|
pluginV := semver.MustParse(metadata.Version)
|
||||||
|
if pluginV.LT(MinimumParserVersion) || pluginV.GT(LatestParserVersion) {
|
||||||
|
panic(fmt.Sprintf("parser version %s is not supported, must be between %s and %s", metadata.Version, MinimumParserVersion, LatestParserVersion))
|
||||||
|
}
|
||||||
|
|
||||||
|
handleFn := obj.Get("canHandle")
|
||||||
|
parseFn := obj.Get("parse")
|
||||||
|
if parseFn == nil || goja.IsUndefined(parseFn) {
|
||||||
|
panic("parser must provide a parse function")
|
||||||
|
}
|
||||||
|
|
||||||
|
parsers = append(parsers, newJSParser(vm, handleFn, parseFn, metadata))
|
||||||
|
return goja.Undefined()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsConsole = func(logger *log.Logger) map[string]any {
|
||||||
|
return map[string]any{
|
||||||
|
"log": func(args ...any) {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(args) > 1 {
|
||||||
|
logger.Info(args[0], args[1:]...)
|
||||||
|
} else {
|
||||||
|
logger.Info(args[0])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsGhttp = func(vm *goja.Runtime) *goja.Object {
|
||||||
|
ghttp := vm.NewObject()
|
||||||
|
client := netutil.DefaultParserHTTPClient()
|
||||||
|
ghttp.Set("get", func(call goja.FunctionCall) goja.Value {
|
||||||
|
url := call.Argument(0).String()
|
||||||
|
resp, err := client.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Sprintf("failed to fetch %s: %v", url, err),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Sprintf("failed to fetch %s: %s", url, resp.Status),
|
||||||
|
"status": resp.StatusCode,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Errorf("failed to read response body: %w", err).Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return vm.ToValue(string(body))
|
||||||
|
})
|
||||||
|
ghttp.Set("getJSON", func(call goja.FunctionCall) goja.Value {
|
||||||
|
url := call.Argument(0).String()
|
||||||
|
|
||||||
|
resp, err := client.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Sprintf("failed to fetch %s: %v", url, err),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Sprintf("failed to fetch %s: %s", url, resp.Status),
|
||||||
|
"status": resp.StatusCode,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Errorf("failed to read response body: %w", err).Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
var jsonData map[string]any
|
||||||
|
if err := json.Unmarshal(body, &jsonData); err != nil {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Errorf("failed to unmarshal JSON: %w", err).Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"data": jsonData,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
ghttp.Set("head", func(call goja.FunctionCall) goja.Value {
|
||||||
|
url := call.Argument(0).String()
|
||||||
|
resp, err := client.Head(url)
|
||||||
|
if err != nil {
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"error": fmt.Sprintf("failed to fetch %s: %v", url, err),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
headers := make(map[string]string)
|
||||||
|
for k, v := range resp.Header {
|
||||||
|
headers[k] = v[0]
|
||||||
|
}
|
||||||
|
return vm.ToValue(map[string]any{
|
||||||
|
"status": resp.StatusCode,
|
||||||
|
"headers": headers,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return ghttp
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user