add searchFile and loadConfig
This commit is contained in:
parent
9376e6d61a
commit
c855c57dba
6
env.yml
Normal file
6
env.yml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
test:
|
||||||
|
aaa: 111
|
||||||
|
bbb:
|
||||||
|
- 2
|
||||||
|
ccc:
|
||||||
|
c2: "A-8ubk1BEEeytkVuyO9F6w=="
|
2
go.mod
2
go.mod
@ -7,7 +7,7 @@ require (
|
|||||||
github.com/ZZMarquis/gm v1.3.2
|
github.com/ZZMarquis/gm v1.3.2
|
||||||
github.com/emmansun/gmsm v0.29.3
|
github.com/emmansun/gmsm v0.29.3
|
||||||
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
|
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
|
||||||
github.com/ssgo/u v1.7.11
|
github.com/ssgo/u v1.7.12
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
109
util.go
109
util.go
@ -20,7 +20,9 @@ import (
|
|||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -44,6 +46,17 @@ 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 {
|
||||||
@ -953,15 +966,72 @@ 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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
@ -1018,3 +1088,36 @@ 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 ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
4
util.ts
4
util.ts
@ -84,6 +84,8 @@ export default {
|
|||||||
float,
|
float,
|
||||||
bytes,
|
bytes,
|
||||||
bool,
|
bool,
|
||||||
|
searchFile,
|
||||||
|
loadConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
function json(data: any): string { return '' }
|
function json(data: any): string { return '' }
|
||||||
@ -169,3 +171,5 @@ 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 }
|
||||||
|
18
util_test.go
18
util_test.go
@ -124,6 +124,24 @@ 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