从 util 中将属于 runtime 的迁移到这里

This commit is contained in:
Star 2025-06-25 17:10:17 +08:00
commit 62b91c98ce
5 changed files with 185 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.*
!.gitignore
go.sum
node_modules
package.json
env.yml

27
go.mod Normal file
View File

@ -0,0 +1,27 @@
module apigo.cc/apigo/know
go 1.24
require (
apigo.cc/gojs v0.0.13
apigo.cc/gojs/console v0.0.2
apigo.cc/gojs/file v0.0.3
github.com/ssgo/u v1.7.18
)
require (
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/ssgo/config v1.7.9 // indirect
github.com/ssgo/log v1.7.7 // indirect
github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.28 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.23.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

92
plugin.go Normal file
View File

@ -0,0 +1,92 @@
package plugin
import (
"errors"
"runtime"
"strings"
"time"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/ssgo/u"
)
const pluginName = "runtime"
var _pluginObject = &PluginObject{}
func init() {
tsCode := gojs.MakeTSCode(_pluginObject)
mappedObj := gojs.ToMap(_pluginObject)
gojs.Register("apigo.cc/gojs/"+pluginName, gojs.Module{
ObjectMaker: func(vm *goja.Runtime) gojs.Map {
return mappedObj
},
TsCode: tsCode,
Desc: "runtime api",
})
}
type PluginObject struct {
}
func (obj *PluginObject) Os() string {
return runtime.GOOS
}
func (obj *PluginObject) NumCPU() int {
return runtime.NumCPU()
}
func (obj *PluginObject) Arch() string {
return runtime.GOARCH
}
func (obj *PluginObject) Sleep(timeValue any) error {
var timeV time.Duration
if ms := u.Int64(timeValue); ms > 0 {
timeV = time.Duration(ms) * time.Millisecond
} else {
if v, err := time.ParseDuration(u.String(timeValue)); err == nil {
timeV = v
}
}
if timeV > time.Hour {
return errors.New("sleep time too long, max 1h")
}
if timeV > 0 {
time.Sleep(timeV)
}
return nil
}
func (obj *PluginObject) Shell(cmd string, args ...string) ([]string, error) {
if len(args) == 0 && strings.ContainsRune(cmd, ' ') {
args = u.SplitArgs(cmd)
cmd = args[0]
args = args[1:]
}
if r, err := u.RunCommand(cmd, args...); err == nil {
return r, nil
} else {
return []string{}, err
}
}
// 暂不支持,因为回调是异步的可能会发生冲突
// func (obj *PluginObject) SetTimeout(timeValue any, vm *goja.Runtime) error {
// 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
// }

28
plugin_test.go Normal file
View File

@ -0,0 +1,28 @@
package plugin_test
import (
"fmt"
"strings"
"testing"
"apigo.cc/gojs"
_ "apigo.cc/gojs/console"
_ "apigo.cc/gojs/file"
"github.com/ssgo/u"
)
func TestPlugin(t *testing.T) {
gojs.ExportForDev()
for _, f := range u.ReadDirN(".") {
if strings.HasSuffix(f.Name, "_test.js") {
r, err := gojs.RunFile(f.Name)
if err != nil {
t.Fatal(u.Red(f.Name), u.BRed(err.Error()))
} else if r != true {
t.Fatal(u.Red(f.Name), u.BRed(u.JsonP(r)))
} else {
fmt.Println(u.Green(f.Name), u.BGreen("test succeess"))
}
}
}
}

32
plugin_test.js Normal file
View File

@ -0,0 +1,32 @@
import rt from 'apigo.cc/gojs/runtime'
import co from 'apigo.cc/gojs/console'
if (!rt.os()) {
co.info('OS: ', rt.os())
return 'os not found'
}
if (!rt.arch()) {
co.info('Arch: ', rt.arch())
return 'arch not found'
}
if (!rt.numCPU()) {
co.info('NumCPU: ', rt.numCPU())
return 'numCPU not found'
}
let goEnv = rt.shell('go "env"')
if (goEnv.join('\n').indexOf('GO111MODULE=') === -1) {
co.info('Go Env: ', goEnv)
return 'failed to get go env by shell'
}
let t1 = new Date().getMilliseconds()
rt.sleep(123)
let t2 = new Date().getMilliseconds()
if (t2 - t1 < 120 || t2 - t1 > 125) {
co.info('sleep time: ', t2 - t1)
return 'sleep failed'
}
return true