将部分函数移动到gojs/runtime,增强了时间处理能力
This commit is contained in:
parent
108a6ca666
commit
ce923e2bbe
27
go.mod
27
go.mod
@ -1,26 +1,31 @@
|
|||||||
module apigo.cc/gojs/util
|
module apigo.cc/gojs/util
|
||||||
|
|
||||||
go 1.18
|
go 1.23.0
|
||||||
|
|
||||||
|
toolchain go1.24.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
apigo.cc/gojs v0.0.12
|
apigo.cc/gojs v0.0.14
|
||||||
|
apigo.cc/gojs/console v0.0.2
|
||||||
github.com/ZZMarquis/gm v1.3.2
|
github.com/ZZMarquis/gm v1.3.2
|
||||||
github.com/emmansun/gmsm v0.29.6
|
github.com/emmansun/gmsm v0.30.1
|
||||||
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
|
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
|
||||||
github.com/ssgo/u v1.7.13
|
github.com/ssgo/u v1.7.19
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dlclark/regexp2 v1.11.4 // indirect
|
github.com/chzyer/readline v1.5.1 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
github.com/dlclark/regexp2 v1.11.5 // indirect
|
||||||
|
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
||||||
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
|
github.com/google/pprof v0.0.0-20250607225305-033d6d78b36a // indirect
|
||||||
|
github.com/ianlancetaylor/demangle v0.0.0-20250417193237-f615e6bd150b // indirect
|
||||||
github.com/ssgo/config v1.7.9 // indirect
|
github.com/ssgo/config v1.7.9 // indirect
|
||||||
github.com/ssgo/log v1.7.7 // indirect
|
github.com/ssgo/log v1.7.7 // indirect
|
||||||
github.com/ssgo/standard v1.7.7 // indirect
|
github.com/ssgo/standard v1.7.7 // indirect
|
||||||
github.com/ssgo/tool v0.4.28 // indirect
|
github.com/ssgo/tool v0.4.29 // indirect
|
||||||
golang.org/x/crypto v0.31.0 // indirect
|
golang.org/x/crypto v0.39.0 // indirect
|
||||||
golang.org/x/sys v0.28.0 // indirect
|
golang.org/x/sys v0.33.0 // indirect
|
||||||
golang.org/x/text v0.21.0 // indirect
|
golang.org/x/text v0.26.0 // indirect
|
||||||
)
|
)
|
||||||
|
277
util.go
277
util.go
@ -18,15 +18,10 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
|
||||||
|
|
||||||
"apigo.cc/gojs"
|
"apigo.cc/gojs"
|
||||||
"apigo.cc/gojs/goja"
|
"apigo.cc/gojs/goja"
|
||||||
@ -46,17 +41,6 @@ var utilTS string
|
|||||||
//go:embed README.md
|
//go:embed README.md
|
||||||
var utilMD string
|
var utilMD string
|
||||||
|
|
||||||
var envConfigs map[string]map[string]any
|
|
||||||
var confAes = u.NewAes([]byte("?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV"), []byte("VFs7@sK61cj^f?HZ"))
|
|
||||||
var keysIsSet = false
|
|
||||||
|
|
||||||
func SetSSKey(key, iv []byte) {
|
|
||||||
if !keysIsSet {
|
|
||||||
confAes = u.NewAes(key, iv)
|
|
||||||
keysIsSet = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
obj := map[string]any{
|
obj := map[string]any{
|
||||||
"json": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"json": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
@ -106,44 +90,6 @@ func init() {
|
|||||||
panic(vm.NewGoError(err))
|
panic(vm.NewGoError(err))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"save": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
args := gojs.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))
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
if err = u.WriteFileBytes(gojs.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 := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
filename := args.Str(0)
|
|
||||||
if data, err := u.ReadFileBytes(gojs.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 {
|
"base64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
return vm.ToValue(u.Base64(u.Bytes(args.Arguments[0].Export())))
|
return vm.ToValue(u.Base64(u.Bytes(args.Arguments[0].Export())))
|
||||||
@ -264,14 +210,14 @@ func init() {
|
|||||||
if strings.Contains(keyText, "-----BEGIN PRIVATE KEY-----") {
|
if strings.Contains(keyText, "-----BEGIN PRIVATE KEY-----") {
|
||||||
keyType = "private"
|
keyType = "private"
|
||||||
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
||||||
panic(vm.NewGoError(errors.New("bad private pem key")))
|
panic(vm.NewGoError(u.Error("bad private pem key")))
|
||||||
} else {
|
} else {
|
||||||
keyBytes = block.Bytes
|
keyBytes = block.Bytes
|
||||||
}
|
}
|
||||||
} else if strings.Contains(keyText, "-----BEGIN PUBLIC KEY-----") {
|
} else if strings.Contains(keyText, "-----BEGIN PUBLIC KEY-----") {
|
||||||
keyType = "public"
|
keyType = "public"
|
||||||
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
||||||
panic(vm.NewGoError(errors.New("bad public pem key")))
|
panic(vm.NewGoError(u.Error("bad public pem key")))
|
||||||
} else {
|
} else {
|
||||||
keyBytes = block.Bytes
|
keyBytes = block.Bytes
|
||||||
}
|
}
|
||||||
@ -287,7 +233,7 @@ func init() {
|
|||||||
panic(vm.NewGoError(err))
|
panic(vm.NewGoError(err))
|
||||||
} else {
|
} else {
|
||||||
if ecdsaPriKey, ok := priKey.(*ecdsa.PrivateKey); !ok {
|
if ecdsaPriKey, ok := priKey.(*ecdsa.PrivateKey); !ok {
|
||||||
panic(vm.NewGoError(errors.New("bad private key")))
|
panic(vm.NewGoError(u.Error("bad private key")))
|
||||||
} else {
|
} else {
|
||||||
return vm.ToValue(ecdsaPriKey.D.Bytes())
|
return vm.ToValue(ecdsaPriKey.D.Bytes())
|
||||||
}
|
}
|
||||||
@ -297,7 +243,7 @@ func init() {
|
|||||||
panic(vm.NewGoError(err))
|
panic(vm.NewGoError(err))
|
||||||
} else {
|
} else {
|
||||||
if ecdsaPubKey, ok := pubKey.(*ecdsa.PublicKey); !ok {
|
if ecdsaPubKey, ok := pubKey.(*ecdsa.PublicKey); !ok {
|
||||||
panic(vm.NewGoError(errors.New("bad public key")))
|
panic(vm.NewGoError(u.Error("bad public key")))
|
||||||
} else {
|
} else {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
buf.WriteByte(byte(len(ecdsaPubKey.X.Bytes())))
|
buf.WriteByte(byte(len(ecdsaPubKey.X.Bytes())))
|
||||||
@ -401,14 +347,14 @@ func init() {
|
|||||||
if strings.Contains(keyText, "-----BEGIN PRIVATE KEY-----") {
|
if strings.Contains(keyText, "-----BEGIN PRIVATE KEY-----") {
|
||||||
keyType = "private"
|
keyType = "private"
|
||||||
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
||||||
panic(vm.NewGoError(errors.New("bad private pem key")))
|
panic(vm.NewGoError(u.Error("bad private pem key")))
|
||||||
} else {
|
} else {
|
||||||
keyBytes = block.Bytes
|
keyBytes = block.Bytes
|
||||||
}
|
}
|
||||||
} else if strings.Contains(keyText, "-----BEGIN PUBLIC KEY-----") {
|
} else if strings.Contains(keyText, "-----BEGIN PUBLIC KEY-----") {
|
||||||
keyType = "public"
|
keyType = "public"
|
||||||
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
||||||
panic(vm.NewGoError(errors.New("bad public pem key")))
|
panic(vm.NewGoError(u.Error("bad public pem key")))
|
||||||
} else {
|
} else {
|
||||||
keyBytes = block.Bytes
|
keyBytes = block.Bytes
|
||||||
}
|
}
|
||||||
@ -430,7 +376,7 @@ func init() {
|
|||||||
panic(vm.NewGoError(err))
|
panic(vm.NewGoError(err))
|
||||||
} else {
|
} else {
|
||||||
if sm2PubKey, ok := pubKey.(*ecdsa.PublicKey); !ok {
|
if sm2PubKey, ok := pubKey.(*ecdsa.PublicKey); !ok {
|
||||||
panic(vm.NewGoError(errors.New("bad public key")))
|
panic(vm.NewGoError(u.Error("bad public key")))
|
||||||
} else {
|
} else {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
buf.WriteByte(byte(len(sm2PubKey.X.Bytes())))
|
buf.WriteByte(byte(len(sm2PubKey.X.Bytes())))
|
||||||
@ -530,14 +476,14 @@ func init() {
|
|||||||
if strings.Contains(keyText, "-----BEGIN RSA PRIVATE KEY-----") {
|
if strings.Contains(keyText, "-----BEGIN RSA PRIVATE KEY-----") {
|
||||||
keyType = "private"
|
keyType = "private"
|
||||||
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
||||||
panic(vm.NewGoError(errors.New("bad private pem key")))
|
panic(vm.NewGoError(u.Error("bad private pem key")))
|
||||||
} else {
|
} else {
|
||||||
keyBytes = block.Bytes
|
keyBytes = block.Bytes
|
||||||
}
|
}
|
||||||
} else if strings.Contains(keyText, "-----BEGIN RSA PUBLIC KEY-----") {
|
} else if strings.Contains(keyText, "-----BEGIN RSA PUBLIC KEY-----") {
|
||||||
keyType = "public"
|
keyType = "public"
|
||||||
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
if block, _ := pem.Decode([]byte(keyText)); block == nil {
|
||||||
panic(vm.NewGoError(errors.New("bad public pem key")))
|
panic(vm.NewGoError(u.Error("bad public pem key")))
|
||||||
} else {
|
} else {
|
||||||
keyBytes = block.Bytes
|
keyBytes = block.Bytes
|
||||||
}
|
}
|
||||||
@ -801,119 +747,38 @@ func init() {
|
|||||||
}
|
}
|
||||||
return vm.ToValue(buf.String())
|
return vm.ToValue(buf.String())
|
||||||
},
|
},
|
||||||
"sleep": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
time.Sleep(time.Duration(args.Int64(0)) * time.Millisecond)
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
"setTimeout": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
callback := args.Func(0)
|
|
||||||
timeout := time.Duration(args.Int64(1)) * time.Millisecond
|
|
||||||
if callback != nil {
|
|
||||||
go func() {
|
|
||||||
if timeout > 0 {
|
|
||||||
time.Sleep(timeout)
|
|
||||||
}
|
|
||||||
if _, err := callback(args.This, args.Arguments[2:]...); err != nil {
|
|
||||||
args.Logger.Error(err.Error())
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
"shell": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
a := make([]string, len(args.Arguments)-1)
|
|
||||||
for i := 1; i < len(args.Arguments); i++ {
|
|
||||||
a[i-1] = args.Str(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
if r, err := u.RunCommand(args.Str(0), a...); err == nil {
|
|
||||||
return vm.ToValue(r)
|
|
||||||
} else {
|
|
||||||
panic(vm.NewGoError(err))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"formatDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"formatDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
format := args.Str(0)
|
format := args.Str(0)
|
||||||
var tm time.Time
|
v := args.Any(1)
|
||||||
if timestamp := args.Int64(1); timestamp > 0 {
|
return vm.ToValue(u.FormatTime(format, v))
|
||||||
if timestamp < 10000000000 {
|
},
|
||||||
timestamp *= 1000
|
"addDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
}
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
tm = time.UnixMilli(timestamp)
|
addStr := args.Str(0)
|
||||||
} else {
|
v := args.Any(1)
|
||||||
tm = time.Now()
|
return vm.ToValue(u.AddTime(addStr, v))
|
||||||
}
|
|
||||||
return vm.ToValue(tm.Format(format))
|
|
||||||
},
|
},
|
||||||
"timestampMS": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"timestampMS": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
return vm.ToValue(time.Now().UnixMilli())
|
args := gojs.MakeArgs(&argsIn, vm).Check(0)
|
||||||
|
tm := u.ParseTime(args.Any(0))
|
||||||
|
return vm.ToValue(tm.UnixMilli())
|
||||||
},
|
},
|
||||||
"timestamp": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"timestamp": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
return vm.ToValue(time.Now().Unix())
|
args := gojs.MakeArgs(&argsIn, vm).Check(0)
|
||||||
|
tm := u.ParseTime(args.Any(0))
|
||||||
|
return vm.ToValue(tm.Unix())
|
||||||
},
|
},
|
||||||
"toDatetime": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"toDatetime": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm)
|
args := gojs.MakeArgs(&argsIn, vm)
|
||||||
var tm time.Time
|
tm := u.ParseTime(args.Any(0))
|
||||||
if timestamp := args.Int64(0); timestamp > 0 {
|
return vm.ToValue(tm.Format("2006-01-02 15:04:05.000000"))
|
||||||
if timestamp < 10000000000 {
|
|
||||||
timestamp *= 1000
|
|
||||||
}
|
|
||||||
tm = time.UnixMilli(timestamp)
|
|
||||||
} else {
|
|
||||||
tm = time.Now()
|
|
||||||
}
|
|
||||||
return vm.ToValue(tm.Format("2006-01-02 15:04:05"))
|
|
||||||
},
|
|
||||||
"fromDatetime": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
timeStr := args.Str(0)
|
|
||||||
if len(timeStr) > 19 {
|
|
||||||
timeStr = timeStr[0:19]
|
|
||||||
}
|
|
||||||
if strings.ContainsRune(timeStr, 'T') {
|
|
||||||
timeStr = strings.ReplaceAll(timeStr, "T", " ")
|
|
||||||
}
|
|
||||||
if tm, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local); err == nil {
|
|
||||||
return vm.ToValue(tm.UnixMilli())
|
|
||||||
} else {
|
|
||||||
panic(vm.NewGoError(err))
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"toDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"toDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm)
|
args := gojs.MakeArgs(&argsIn, vm)
|
||||||
var tm time.Time
|
tm := u.ParseTime(args.Any(0))
|
||||||
if timestamp := args.Int64(0); timestamp > 0 {
|
|
||||||
if timestamp < 10000000000 {
|
|
||||||
timestamp *= 1000
|
|
||||||
}
|
|
||||||
tm = time.UnixMilli(timestamp)
|
|
||||||
} else {
|
|
||||||
tm = time.Now()
|
|
||||||
}
|
|
||||||
return vm.ToValue(tm.Format("2006-01-02"))
|
return vm.ToValue(tm.Format("2006-01-02"))
|
||||||
},
|
},
|
||||||
"fromDate": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
timeStr := args.Str(0)
|
|
||||||
if len(timeStr) > 10 {
|
|
||||||
timeStr = timeStr[0:10]
|
|
||||||
}
|
|
||||||
if tm, err := time.ParseInLocation("2006-01-02", timeStr, time.Local); err == nil {
|
|
||||||
return vm.ToValue(tm.UnixMilli())
|
|
||||||
} else {
|
|
||||||
panic(vm.NewGoError(err))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"os": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
return vm.ToValue(runtime.GOOS)
|
|
||||||
},
|
|
||||||
"arch": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
return vm.ToValue(runtime.GOARCH)
|
|
||||||
},
|
|
||||||
"joinPath": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
"joinPath": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
return vm.ToValue(filepath.Join(args.StrArray(0)...))
|
return vm.ToValue(filepath.Join(args.StrArray(0)...))
|
||||||
@ -966,67 +831,12 @@ func init() {
|
|||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
||||||
return vm.ToValue(u.Bool(args.Any(0)))
|
return vm.ToValue(u.Bool(args.Any(0)))
|
||||||
},
|
},
|
||||||
"searchFile": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
filename := args.Str(0)
|
|
||||||
searchPath := args.Str(1)
|
|
||||||
findFile := ""
|
|
||||||
if searchPath != "" {
|
|
||||||
findFile = searchFile(searchPath, filename)
|
|
||||||
} else {
|
|
||||||
findFile = searchFileFromCurrent(vm, filename)
|
|
||||||
}
|
|
||||||
return vm.ToValue(findFile)
|
|
||||||
},
|
|
||||||
"loadConfig": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
||||||
if envConfigs == nil {
|
|
||||||
envConfigs = map[string]map[string]any{}
|
|
||||||
envFile := searchFileFromCurrent(vm, "env.yml")
|
|
||||||
if envFile == "" {
|
|
||||||
envFile = searchFileFromCurrent(vm, "env.json")
|
|
||||||
}
|
|
||||||
if envFile != "" {
|
|
||||||
u.LoadX(envFile, &envConfigs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
||||||
confName := args.Str(0)
|
|
||||||
conf := map[string]any{}
|
|
||||||
confFile := searchFileFromCurrent(vm, confName+".yml")
|
|
||||||
if confFile == "" {
|
|
||||||
confFile = searchFileFromCurrent(vm, confName+".json")
|
|
||||||
}
|
|
||||||
if confFile != "" {
|
|
||||||
u.LoadX(confFile, &conf)
|
|
||||||
}
|
|
||||||
if envConf, ok := envConfigs[confName]; ok {
|
|
||||||
u.Convert(envConf, &conf)
|
|
||||||
}
|
|
||||||
|
|
||||||
decTimes := 0
|
|
||||||
confStr := encryptdMatcher.ReplaceAllStringFunc(u.Json(conf), func(str string) string {
|
|
||||||
if buf, err := base64.URLEncoding.DecodeString(str[1 : len(str)-1]); err == nil {
|
|
||||||
if buf1, err1 := confAes.DecryptBytes(buf); err1 == nil && len(buf1) > 0 {
|
|
||||||
decTimes++
|
|
||||||
return "\"" + string(buf1) + "\""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return str
|
|
||||||
})
|
|
||||||
if decTimes > 0 {
|
|
||||||
u.UnJson(confStr, &conf)
|
|
||||||
}
|
|
||||||
|
|
||||||
return vm.ToValue(conf)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gojs.Register("apigo.cc/gojs/util", gojs.Module{
|
gojs.Register("apigo.cc/gojs/util", gojs.Module{
|
||||||
Object: obj,
|
Object: obj,
|
||||||
TsCode: utilTS,
|
TsCode: utilTS,
|
||||||
Example: utilMD,
|
Example: utilMD,
|
||||||
SetSSKey: SetSSKey,
|
|
||||||
JsCode: `
|
JsCode: `
|
||||||
$MOD$.keysBy = function (obj, ...fieldAndValues) {
|
$MOD$.keysBy = function (obj, ...fieldAndValues) {
|
||||||
let keys = []
|
let keys = []
|
||||||
@ -1320,8 +1130,6 @@ $MOD$.excludeByKey = function (obj, ...keys) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var encryptdMatcher = regexp.MustCompile(`"([\w-=]+)"`)
|
|
||||||
|
|
||||||
func makeECDSAPriKey(priKey []byte) *ecdsa.PrivateKey {
|
func makeECDSAPriKey(priKey []byte) *ecdsa.PrivateKey {
|
||||||
if len(priKey) >= 60 {
|
if len(priKey) >= 60 {
|
||||||
return makePriKey(priKey, elliptic.P521())
|
return makePriKey(priKey, elliptic.P521())
|
||||||
@ -1378,36 +1186,3 @@ func makeRSAPubKey(keyBytes []byte) *rsa.PublicKey {
|
|||||||
}
|
}
|
||||||
return pubKey
|
return pubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func searchFile(searchPath, name string) string {
|
|
||||||
searched := map[string]bool{}
|
|
||||||
return _searchFile(searchPath, name, &searched)
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchFileFromCurrent(vm *goja.Runtime, name string) string {
|
|
||||||
searched := map[string]bool{}
|
|
||||||
startPath := u.String(vm.GoData["startPath"])
|
|
||||||
filename := _searchFile(startPath, name, &searched)
|
|
||||||
if filename == "" {
|
|
||||||
currentPath, _ := os.Getwd()
|
|
||||||
filename = _searchFile(currentPath, name, &searched)
|
|
||||||
}
|
|
||||||
return filename
|
|
||||||
}
|
|
||||||
|
|
||||||
func _searchFile(checkPath, name string, searched *map[string]bool) string {
|
|
||||||
for {
|
|
||||||
if !(*searched)[checkPath] {
|
|
||||||
(*searched)[checkPath] = true
|
|
||||||
filename := filepath.Join(checkPath, name)
|
|
||||||
if u.FileExists(filename) {
|
|
||||||
return filename
|
|
||||||
}
|
|
||||||
}
|
|
||||||
oldPath := checkPath
|
|
||||||
checkPath = filepath.Dir(oldPath)
|
|
||||||
if oldPath == checkPath {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
18
util.ts
18
util.ts
@ -6,8 +6,6 @@ export default {
|
|||||||
unJson,
|
unJson,
|
||||||
yaml,
|
yaml,
|
||||||
unYaml,
|
unYaml,
|
||||||
load,
|
|
||||||
save,
|
|
||||||
base64,
|
base64,
|
||||||
unBase64,
|
unBase64,
|
||||||
urlBase64,
|
urlBase64,
|
||||||
@ -60,9 +58,6 @@ export default {
|
|||||||
hmacSHA512,
|
hmacSHA512,
|
||||||
hmacSM3,
|
hmacSM3,
|
||||||
tpl,
|
tpl,
|
||||||
sleep,
|
|
||||||
setTimeout,
|
|
||||||
shell,
|
|
||||||
formatDate,
|
formatDate,
|
||||||
timestampMS,
|
timestampMS,
|
||||||
timestamp,
|
timestamp,
|
||||||
@ -70,8 +65,6 @@ export default {
|
|||||||
fromDatetime,
|
fromDatetime,
|
||||||
toDate,
|
toDate,
|
||||||
fromDate,
|
fromDate,
|
||||||
os,
|
|
||||||
arch,
|
|
||||||
joinPath,
|
joinPath,
|
||||||
getPathDir,
|
getPathDir,
|
||||||
getPathBase,
|
getPathBase,
|
||||||
@ -84,8 +77,6 @@ export default {
|
|||||||
float,
|
float,
|
||||||
bytes,
|
bytes,
|
||||||
bool,
|
bool,
|
||||||
searchFile,
|
|
||||||
loadConfig,
|
|
||||||
keysBy,
|
keysBy,
|
||||||
countBy,
|
countBy,
|
||||||
listBy,
|
listBy,
|
||||||
@ -120,8 +111,6 @@ function jsonP(data: any): string { return '' }
|
|||||||
function unJson(data: string): any { return null }
|
function unJson(data: string): any { return null }
|
||||||
function yaml(data: any): string { return '' }
|
function yaml(data: any): string { return '' }
|
||||||
function unYaml(data: string): any { return null }
|
function unYaml(data: string): any { return null }
|
||||||
function load(filename: string): any { return null }
|
|
||||||
function save(filename: string, data: any) { }
|
|
||||||
function base64(data: any): string { return '' }
|
function base64(data: any): string { return '' }
|
||||||
function unBase64(data: string): any { return null }
|
function unBase64(data: string): any { return null }
|
||||||
function urlBase64(data: any): string { return '' }
|
function urlBase64(data: any): string { return '' }
|
||||||
@ -174,9 +163,6 @@ function hmacSHA256(key: any, ...data: any[]): any { return '' }
|
|||||||
function hmacSHA512(key: any, ...data: any[]): any { return '' }
|
function hmacSHA512(key: any, ...data: any[]): any { return '' }
|
||||||
function hmacSM3(key: any, ...data: any[]): any { return '' }
|
function hmacSM3(key: any, ...data: any[]): any { return '' }
|
||||||
function tpl(text: string, data: any, functions?: Object): string { return '' }
|
function tpl(text: string, data: any, functions?: Object): string { return '' }
|
||||||
function sleep(ms: number): void { }
|
|
||||||
function setTimeout(callback: () => void, ms?: number, ...args: any): void { }
|
|
||||||
function shell(cmd: string, ...args: string[]): string[] { return [] }
|
|
||||||
function formatDate(format: string, timestamp?: number): string { return '' }
|
function formatDate(format: string, timestamp?: number): string { return '' }
|
||||||
function timestampMS(): number { return 0 }
|
function timestampMS(): number { return 0 }
|
||||||
function timestamp(): number { return 0 }
|
function timestamp(): number { return 0 }
|
||||||
@ -184,8 +170,6 @@ function toDatetime(timestamp?: number): string { return '' }
|
|||||||
function fromDatetime(datetimeStr: string): number { return 0 }
|
function fromDatetime(datetimeStr: string): number { return 0 }
|
||||||
function toDate(timestamp?: number): string { return '' }
|
function toDate(timestamp?: number): string { return '' }
|
||||||
function fromDate(dateStr: string): number { return 0 }
|
function fromDate(dateStr: string): number { return 0 }
|
||||||
function os(): string { return '' }
|
|
||||||
function arch(): string { return '' }
|
|
||||||
function joinPath(...paths: string[]): string { return '' }
|
function joinPath(...paths: string[]): string { return '' }
|
||||||
function getPathDir(path: string): string { return '' }
|
function getPathDir(path: string): string { return '' }
|
||||||
function getPathBase(path: string): string { return '' }
|
function getPathBase(path: string): string { return '' }
|
||||||
@ -198,8 +182,6 @@ function int(value: any): number { return 0 }
|
|||||||
function float(value: any): number { return 0 }
|
function float(value: any): number { return 0 }
|
||||||
function bytes(value: any): any { return null }
|
function bytes(value: any): any { return null }
|
||||||
function bool(value: any): boolean { return false }
|
function bool(value: any): boolean { return false }
|
||||||
function searchFile(filename: string, searchPath?: string): string { return '' }
|
|
||||||
function loadConfig(name: string): any { return null }
|
|
||||||
|
|
||||||
// 返回符合条件的key列表,支持数组和Object对象
|
// 返回符合条件的key列表,支持数组和Object对象
|
||||||
function keysBy(obj: Array<Object>, ...fieldAndValues: any): any[] { return [] }
|
function keysBy(obj: Array<Object>, ...fieldAndValues: any): any[] { return [] }
|
||||||
|
25
util_test.go
25
util_test.go
@ -9,6 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"apigo.cc/gojs"
|
"apigo.cc/gojs"
|
||||||
|
_ "apigo.cc/gojs/console"
|
||||||
_ "apigo.cc/gojs/util"
|
_ "apigo.cc/gojs/util"
|
||||||
"github.com/ZZMarquis/gm/sm3"
|
"github.com/ZZMarquis/gm/sm3"
|
||||||
"github.com/ssgo/u"
|
"github.com/ssgo/u"
|
||||||
@ -16,7 +17,7 @@ import (
|
|||||||
|
|
||||||
func TestHash(t *testing.T) {
|
func TestHash(t *testing.T) {
|
||||||
vm := gojs.New()
|
vm := gojs.New()
|
||||||
vm.RunCode("import util from 'apigo.cc/gojs/util'")
|
vm.RunCode("import util from 'apigo.cc/gojs/util'\nimport co from 'apigo.cc/gojs/console'")
|
||||||
testIsSame(vm, t, "util.md5('hello 123')", u.MD5([]byte("hello 123")))
|
testIsSame(vm, t, "util.md5('hello 123')", u.MD5([]byte("hello 123")))
|
||||||
testIsSame(vm, t, "util.base64(util.md5('hello 123'))", u.MD5Base64("hello 123"))
|
testIsSame(vm, t, "util.base64(util.md5('hello 123'))", u.MD5Base64("hello 123"))
|
||||||
testIsSame(vm, t, "util.hex(util.md5('hello 123'))", u.MD5String("hello 123"))
|
testIsSame(vm, t, "util.hex(util.md5('hello 123'))", u.MD5String("hello 123"))
|
||||||
@ -37,7 +38,9 @@ func TestHash(t *testing.T) {
|
|||||||
testIsSame(vm, t, "util.unSM4('"+u.String(sm4R)+"','12345678901234567','12345678901234567')", "hello 123")
|
testIsSame(vm, t, "util.unSM4('"+u.String(sm4R)+"','12345678901234567','12345678901234567')", "hello 123")
|
||||||
testIsSame(vm, t, "util.gzip('hello 123')", u.GzipN([]byte("hello 123")))
|
testIsSame(vm, t, "util.gzip('hello 123')", u.GzipN([]byte("hello 123")))
|
||||||
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", "2024-01-01 00:00:00", time.Local)
|
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", "2024-01-01 00:00:00", time.Local)
|
||||||
testIsSame(vm, t, "util.fromDatetime('2024-01-01 00:00:00')", tm.UnixMilli())
|
testIsSame(vm, t, "util.timestampMS('2024-01-01 00:00:00')", tm.UnixMilli())
|
||||||
|
testIsSame(vm, t, "util.timestampMS('Mon Jan 01 2024 00:00:00 GMT+0800 (中国标准时间)')", tm.UnixMilli())
|
||||||
|
testIsSame(vm, t, "util.timestampMS(new Date("+u.String(tm.UnixMilli())+"))", tm.UnixMilli())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestECDSA(t *testing.T) {
|
func TestECDSA(t *testing.T) {
|
||||||
@ -124,24 +127,6 @@ func TestRSA(t *testing.T) {
|
|||||||
fmt.Println(u.Green("ecdsa test passed"))
|
fmt.Println(u.Green("ecdsa test passed"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfig(t *testing.T) {
|
|
||||||
r, err := gojs.Run(`
|
|
||||||
import util from 'apigo.cc/gojs/util'
|
|
||||||
let t = util.loadConfig('test')
|
|
||||||
if (t.aaa != 111) return 'failed to load config, aaa != 111'
|
|
||||||
if (t.bbb.length !== 2 || t.bbb[0] !== 1 || t.bbb[1] !== 2) return 'failed to load config, bbb != [1,2]'
|
|
||||||
if (t.ccc.c1 !== 111 || t.ccc.c2 !== "222") return 'failed to load config, ccc != [111,"222"]'
|
|
||||||
return true
|
|
||||||
`, "")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if r != true {
|
|
||||||
t.Fatal(r)
|
|
||||||
}
|
|
||||||
fmt.Println(u.Green("ecdsa test passed"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func testIsSame(vm *gojs.Runtime, t *testing.T, code string, checkValue any) {
|
func testIsSame(vm *gojs.Runtime, t *testing.T, code string, checkValue any) {
|
||||||
r, err := vm.RunCode(code)
|
r, err := vm.RunCode(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user