ai_old/js/util.go

237 lines
7.6 KiB
Go
Raw Normal View History

2024-09-18 18:29:21 +08:00
package js
import (
"apigo.cc/ai/ai/goja"
2024-09-18 18:29:21 +08:00
"bytes"
"encoding/hex"
"encoding/json"
"github.com/ssgo/u"
"gopkg.in/yaml.v3"
"strings"
2024-09-18 18:29:21 +08:00
"text/template"
)
func RequireUtil() map[string]any {
return map[string]any{
2024-09-23 18:15:02 +08:00
"json": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
if r, err := json.Marshal(args.Arguments[0].Export()); err == nil {
return vm.ToValue(string(r))
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"jsonP": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
if r, err := json.Marshal(args.Arguments[0].Export()); err == nil {
r1 := bytes.Buffer{}
if err2 := json.Indent(&r1, r, "", " "); err2 == nil {
return vm.ToValue(string(r))
} else {
return vm.ToValue(string(r))
}
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"unJson": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
var r any
if err := json.Unmarshal(u.Bytes(args.Arguments[0].Export()), &r); err == nil {
return vm.ToValue(r)
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"yaml": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
if r, err := yaml.Marshal(args.Arguments[0].Export()); err == nil {
return vm.ToValue(string(r))
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"unYaml": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
var r any
if err := yaml.Unmarshal(u.Bytes(args.Arguments[0].Export()), &r); err == nil {
return vm.ToValue(r)
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
"save": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(2)
filename := args.Str(0)
var data []byte
var err error
if strings.HasSuffix(filename, ".yml") || strings.HasSuffix(filename, ".yaml") {
data, err = yaml.Marshal(args.Any(1))
} else {
data, err = json.Marshal(args.Any(1))
2024-09-18 18:29:21 +08:00
}
if err == nil {
if err = u.WriteFileBytes(findPath(vm, filename), data); err != nil {
panic(vm.NewGoError(err))
}
return nil
} else {
panic(vm.NewGoError(err))
}
},
"load": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
filename := args.Str(0)
if data, err := u.ReadFileBytes(findPath(vm, filename)); err == nil {
var r any
if strings.HasSuffix(filename, ".yml") || strings.HasSuffix(filename, ".yaml") {
err = yaml.Unmarshal(data, &r)
} else {
err = json.Unmarshal(data, &r)
}
if err == nil {
return vm.ToValue(r)
} else {
panic(vm.NewGoError(err))
}
} else {
panic(vm.NewGoError(err))
}
},
"base64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
return vm.ToValue(u.Base64(u.Bytes(args.Arguments[0].Export())))
},
2024-09-23 18:15:02 +08:00
"unBase64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-23 18:15:02 +08:00
return vm.ToValue(u.UnBase64String(args.Str(0)))
2024-09-18 18:29:21 +08:00
},
2024-09-23 18:15:02 +08:00
"urlBase64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-23 18:15:02 +08:00
return vm.ToValue(u.UrlBase64String(args.Str(0)))
2024-09-18 18:29:21 +08:00
},
2024-09-23 18:15:02 +08:00
"unUrlBase64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-23 18:15:02 +08:00
return vm.ToValue(u.UnUrlBase64String(args.Str(0)))
2024-09-18 18:29:21 +08:00
},
2024-09-23 18:15:02 +08:00
"hex": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
return vm.ToValue(hex.EncodeToString(u.Bytes(args.Arguments[0].Export())))
},
2024-09-23 18:15:02 +08:00
"unHex": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-23 18:15:02 +08:00
if r, err := hex.DecodeString(args.Str(0)); err == nil {
2024-09-18 18:29:21 +08:00
return vm.ToValue(string(r))
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"aes": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(3)
2024-09-18 18:29:21 +08:00
if r, err := u.EncryptAesBytes(u.Bytes(args.Arguments[0].Export()), u.Bytes(args.Arguments[1].Export()), u.Bytes(args.Arguments[2].Export())); err == nil {
return vm.ToValue(string(r))
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"unAes": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(3)
2024-09-18 18:29:21 +08:00
if r, err := u.DecryptAesBytes(u.Bytes(args.Arguments[0].Export()), u.Bytes(args.Arguments[1].Export()), u.Bytes(args.Arguments[2].Export())); err == nil {
return vm.ToValue(string(r))
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"gzip": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
if r, err := u.Gzip(u.Bytes(args.Arguments[0].Export())); err == nil {
return vm.ToValue(string(r))
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"gunzip": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
if r, err := u.Gunzip(u.Bytes(args.Arguments[0].Export())); err == nil {
return vm.ToValue(string(r))
} else {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
},
2024-09-23 18:15:02 +08:00
"id": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
2024-09-18 18:29:21 +08:00
return vm.ToValue(u.Id12())
},
2024-09-23 18:15:02 +08:00
"uniqueId": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
2024-09-18 18:29:21 +08:00
return vm.ToValue(u.UniqueId())
},
2024-09-23 18:15:02 +08:00
"token": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
2024-09-18 18:29:21 +08:00
size := 20
2024-09-23 18:15:02 +08:00
if len(argsIn.Arguments) > 0 {
size = u.Int(argsIn.Arguments[0].Export())
2024-09-18 18:29:21 +08:00
}
return vm.ToValue(u.MakeToken(size))
},
2024-09-23 18:15:02 +08:00
"md5": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
return vm.ToValue(u.MD5(u.Bytes(args.Arguments[0].Export())))
},
2024-09-23 18:15:02 +08:00
"sha1": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
return vm.ToValue(u.Sha1(u.Bytes(args.Arguments[0].Export())))
},
2024-09-23 18:15:02 +08:00
"sha256": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
return vm.ToValue(u.Sha256(u.Bytes(args.Arguments[0].Export())))
},
2024-09-23 18:15:02 +08:00
"sha512": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-18 18:29:21 +08:00
return vm.ToValue(u.Sha512(u.Bytes(args.Arguments[0].Export())))
},
2024-09-23 18:15:02 +08:00
"tpl": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(2)
2024-09-23 18:15:02 +08:00
var functions = map[string]any{}
if len(argsIn.Arguments) > 2 {
obj := argsIn.Arguments[2].ToObject(vm)
for _, k := range obj.Keys() {
v := obj.Get(k)
if cb, ok := goja.AssertFunction(v); ok {
functions[k] = func(in string) string {
if r, err := cb(argsIn.This, vm.ToValue(in)); err == nil {
return r.String()
}
return ""
}
}
}
}
2024-09-18 18:29:21 +08:00
buf := bytes.NewBuffer(make([]byte, 0))
var err error
2024-09-23 18:15:02 +08:00
tpl := template.New("tpl")
if len(functions) > 0 {
tpl.Funcs(functions)
}
if tpl, err = tpl.Parse(args.Str(0)); err == nil {
2024-09-20 16:50:35 +08:00
err = tpl.Execute(buf, args.Arguments[1].Export())
2024-09-18 18:29:21 +08:00
}
if err != nil {
panic(vm.NewGoError(err))
2024-09-18 18:29:21 +08:00
}
return vm.ToValue(buf.String())
},
2024-09-23 18:15:02 +08:00
"shell": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
2024-09-20 16:50:35 +08:00
a := make([]string, len(args.Arguments)-1)
for i := 1; i < len(args.Arguments); i++ {
2024-09-23 18:15:02 +08:00
a[i-1] = args.Str(i)
2024-09-20 16:50:35 +08:00
}
2024-09-23 18:15:02 +08:00
if r, err := u.RunCommand(args.Str(0), a...); err == nil {
2024-09-20 16:50:35 +08:00
return vm.ToValue(r)
} else {
panic(vm.NewGoError(err))
2024-09-20 16:50:35 +08:00
}
},
2024-09-18 18:29:21 +08:00
}
}