mirror of
https://github.com/komari-monitor/komari.git
synced 2026-11-23 08:20:46 +00:00
209 lines
7.6 KiB
Go
209 lines
7.6 KiB
Go
package process
|
|
|
|
import (
|
|
"fmt"
|
|
"math/big"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/dop251/goja"
|
|
"github.com/komari-monitor/komari/pkg/jsruntime/events"
|
|
"github.com/komari-monitor/komari/pkg/jsruntime/fs"
|
|
"github.com/komari-monitor/komari/pkg/jsruntime/internal/bridge"
|
|
"github.com/komari-monitor/komari/pkg/jsruntime/internal/metrics"
|
|
"github.com/komari-monitor/komari/utils"
|
|
)
|
|
|
|
type nodeNextTick struct {
|
|
callback goja.Callable
|
|
arguments []goja.Value
|
|
}
|
|
|
|
type Module struct {
|
|
runtime *bridge.Runtime
|
|
fs *fs.Module
|
|
allowExec bool
|
|
startedAt time.Time
|
|
reportError func(*goja.Runtime, []goja.Value)
|
|
|
|
nextTickMu sync.Mutex
|
|
nextTicks []nodeNextTick
|
|
nextTickScheduled bool
|
|
nextTickDraining bool
|
|
nextTickTurn goja.Callable
|
|
nextTickSchedule goja.Callable
|
|
}
|
|
|
|
func New(runtime *bridge.Runtime, filesystem *fs.Module, allowExec bool, startedAt time.Time, reportError func(*goja.Runtime, []goja.Value)) *Module {
|
|
return &Module{runtime: runtime, fs: filesystem, allowExec: allowExec, startedAt: startedAt, reportError: reportError}
|
|
}
|
|
|
|
func (m *Module) Load(vm *goja.Runtime, module *goja.Object) {
|
|
process := events.NewEmitter(vm)
|
|
environment := make(map[string]string)
|
|
for _, item := range os.Environ() {
|
|
parts := strings.SplitN(item, "=", 2)
|
|
if len(parts) == 2 {
|
|
environment[parts[0]] = parts[1]
|
|
}
|
|
}
|
|
_ = process.Set("env", environment)
|
|
_ = process.Set("argv", append([]string{os.Args[0], "script.js"}, os.Args[1:]...))
|
|
_ = process.Set("execArgv", []string{})
|
|
_ = process.Set("execPath", os.Args[0])
|
|
_ = process.Set("pid", os.Getpid())
|
|
_ = process.Set("ppid", os.Getppid())
|
|
_ = process.Set("platform", metrics.Platform())
|
|
_ = process.Set("arch", metrics.Arch())
|
|
_ = process.Set("version", "v"+strings.TrimPrefix(runtime.Version(), "go"))
|
|
_ = process.Set("versions", map[string]string{"node": "0.0.0-goja", "go": runtime.Version(), "komari": utils.CurrentVersion, "hash": utils.VersionHash})
|
|
_ = process.Set("release", map[string]string{"name": "node", "sourceUrl": "", "headersUrl": ""})
|
|
_ = process.Set("title", "komari-jsruntime")
|
|
_ = process.Set("exitCode", 0)
|
|
_ = process.Set("connected", false)
|
|
_ = process.Set("config", map[string]any{})
|
|
_ = process.Set("cwd", func() string { return m.fs.Cwd() })
|
|
_ = process.Set("chdir", func(path string) {
|
|
if err := m.fs.Chdir(path); err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
})
|
|
_ = process.Set("uptime", func() float64 { return time.Since(m.startedAt).Seconds() })
|
|
memoryUsage := vm.ToValue(func() map[string]uint64 {
|
|
var memory runtime.MemStats
|
|
runtime.ReadMemStats(&memory)
|
|
usage, err := metrics.ReadProcess()
|
|
if err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
return map[string]uint64{"rss": usage.RSS, "heapTotal": memory.HeapSys, "heapUsed": memory.HeapAlloc, "external": memory.OtherSys, "arrayBuffers": 0}
|
|
}).ToObject(vm)
|
|
_ = memoryUsage.Set("rss", func() uint64 {
|
|
usage, err := metrics.ReadProcess()
|
|
if err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
return usage.RSS
|
|
})
|
|
_ = process.Set("memoryUsage", memoryUsage)
|
|
_ = process.Set("cpuUsage", func(call goja.FunctionCall) goja.Value {
|
|
usage, err := metrics.ReadProcess()
|
|
if err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
userMicros, systemMicros := usage.UserCPU.Microseconds(), usage.SystemCPU.Microseconds()
|
|
if previous := call.Argument(0); !goja.IsUndefined(previous) && !goja.IsNull(previous) {
|
|
object := previous.ToObject(vm)
|
|
userMicros -= object.Get("user").ToInteger()
|
|
systemMicros -= object.Get("system").ToInteger()
|
|
}
|
|
return vm.ToValue(map[string]int64{"user": max(userMicros, 0), "system": max(systemMicros, 0)})
|
|
})
|
|
_ = process.Set("resourceUsage", func() map[string]any {
|
|
usage, err := metrics.ReadProcess()
|
|
if err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
return map[string]any{
|
|
"userCPUTime": usage.UserCPU.Microseconds(), "systemCPUTime": usage.SystemCPU.Microseconds(), "maxRSS": usage.MaxRSS,
|
|
"minorPageFault": usage.MinorPageFault, "majorPageFault": usage.MajorPageFault,
|
|
"fsRead": usage.FSRead, "fsWrite": usage.FSWrite,
|
|
"voluntaryContextSwitches": usage.VoluntarySwitches, "involuntaryContextSwitches": usage.InvoluntarySwitches,
|
|
}
|
|
})
|
|
hrtime := vm.ToValue(func(call goja.FunctionCall) goja.Value {
|
|
now := time.Since(m.startedAt)
|
|
seconds := int64(now / time.Second)
|
|
nanoseconds := int64(now % time.Second)
|
|
if previous := call.Argument(0); !goja.IsUndefined(previous) {
|
|
var values []int64
|
|
if err := vm.ExportTo(previous, &values); err == nil && len(values) >= 2 {
|
|
seconds -= values[0]
|
|
nanoseconds -= values[1]
|
|
if nanoseconds < 0 {
|
|
seconds--
|
|
nanoseconds += int64(time.Second)
|
|
}
|
|
}
|
|
}
|
|
return vm.ToValue([]int64{seconds, nanoseconds})
|
|
}).ToObject(vm)
|
|
_ = hrtime.Set("bigint", func() *big.Int { return big.NewInt(time.Since(m.startedAt).Nanoseconds()) })
|
|
_ = process.Set("hrtime", hrtime)
|
|
_ = process.Set("nextTick", func(call goja.FunctionCall) goja.Value {
|
|
callback, ok := goja.AssertFunction(call.Argument(0))
|
|
if !ok {
|
|
panic(vm.NewTypeError("callback must be a function"))
|
|
}
|
|
arguments := append([]goja.Value(nil), call.Arguments[1:]...)
|
|
m.enqueueNextTick(vm, nodeNextTick{callback: callback, arguments: arguments})
|
|
return goja.Undefined()
|
|
})
|
|
_ = process.Set("emitWarning", func(call goja.FunctionCall) goja.Value {
|
|
if m.reportError != nil {
|
|
m.reportError(vm, []goja.Value{vm.ToValue("Warning: " + call.Argument(0).String())})
|
|
}
|
|
return goja.Undefined()
|
|
})
|
|
_ = process.Set("kill", func(pid int, signal string) bool {
|
|
if !m.allowExec {
|
|
panic(vm.NewGoError(fmt.Errorf("process.kill requires AllowExec")))
|
|
}
|
|
process, err := os.FindProcess(pid)
|
|
if err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
if signal == "SIGKILL" {
|
|
err = process.Kill()
|
|
} else {
|
|
err = process.Signal(os.Interrupt)
|
|
}
|
|
if err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
return true
|
|
})
|
|
_ = process.Set("exit", func(call goja.FunctionCall) goja.Value {
|
|
code := call.Argument(0).ToInteger()
|
|
_ = process.Set("exitCode", code)
|
|
panic(vm.NewGoError(fmt.Errorf("process.exit(%d) requested", code)))
|
|
})
|
|
_ = process.Set("abort", func() { panic(vm.NewGoError(fmt.Errorf("process.abort() requested"))) })
|
|
_ = process.Set("stdout", processStream(vm, "stdout", os.Stdout, true))
|
|
_ = process.Set("stderr", processStream(vm, "stderr", os.Stderr, true))
|
|
_ = process.Set("stdin", processStream(vm, "stdin", nil, false))
|
|
if err := m.initNextTickScheduler(vm); err != nil {
|
|
panic(vm.NewGoError(err))
|
|
}
|
|
_ = module.Set("exports", process)
|
|
}
|
|
|
|
func processStream(vm *goja.Runtime, name string, output *os.File, writable bool) *goja.Object {
|
|
stream := events.NewEmitter(vm)
|
|
_ = stream.Set("isTTY", false)
|
|
_ = stream.Set("fd", -1)
|
|
_ = stream.Set("write", func(call goja.FunctionCall) goja.Value {
|
|
if !writable || output == nil {
|
|
panic(vm.NewGoError(fmt.Errorf("process.%s.write is not supported by jsruntime; the stream is not connected", name)))
|
|
}
|
|
_, _ = fmt.Fprint(output, call.Argument(0).String())
|
|
if callback, ok := goja.AssertFunction(call.Argument(2)); ok {
|
|
_, _ = callback(goja.Undefined())
|
|
}
|
|
return vm.ToValue(true)
|
|
})
|
|
_ = stream.Set("setEncoding", func() *goja.Object {
|
|
panic(vm.NewGoError(fmt.Errorf("process.%s.setEncoding is not supported by jsruntime; streams have no encoding state", name)))
|
|
})
|
|
_ = stream.Set("resume", func() *goja.Object {
|
|
panic(vm.NewGoError(fmt.Errorf("process.%s.resume is not supported by jsruntime; streams have no backpressure state", name)))
|
|
})
|
|
_ = stream.Set("pause", func() *goja.Object {
|
|
panic(vm.NewGoError(fmt.Errorf("process.%s.pause is not supported by jsruntime; streams have no backpressure state", name)))
|
|
})
|
|
return stream
|
|
}
|