edge/pkg/app/server.go

237 lines
4.2 KiB
Go
Raw Normal View History

2023-02-09 12:16:36 +01:00
package app
import (
"context"
2023-02-09 12:16:36 +01:00
"math/rand"
"sync"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
2023-02-09 12:16:36 +01:00
)
var (
ErrFuncDoesNotExist = errors.New("function does not exist")
ErUnknownError = errors.New("unknown error")
)
2023-02-09 12:16:36 +01:00
type Server struct {
loop *eventloop.EventLoop
factories []ServerModuleFactory
modules []ServerModule
2023-02-09 12:16:36 +01:00
}
func (s *Server) Load(name string, src string) error {
var err error
s.loop.RunOnLoop(func(rt *goja.Runtime) {
_, err = rt.RunScript(name, src)
if err != nil {
err = errors.Wrap(err, "could not run js script")
}
})
2023-02-09 12:16:36 +01:00
if err != nil {
return errors.WithStack(err)
2023-02-09 12:16:36 +01:00
}
return nil
}
func (s *Server) ExecFuncByName(ctx context.Context, funcName string, args ...interface{}) (goja.Value, error) {
ctx = logger.With(ctx, logger.F("function", funcName), logger.F("args", args))
ret, err := s.Exec(ctx, funcName, args...)
if err != nil {
return nil, errors.WithStack(err)
2023-02-09 12:16:36 +01:00
}
return ret, nil
2023-02-09 12:16:36 +01:00
}
func (s *Server) Exec(ctx context.Context, callableOrFuncname any, args ...interface{}) (goja.Value, error) {
2023-02-09 12:16:36 +01:00
var (
wg sync.WaitGroup
value goja.Value
err error
)
wg.Add(1)
s.loop.RunOnLoop(func(rt *goja.Runtime) {
var callable goja.Callable
switch typ := callableOrFuncname.(type) {
case goja.Callable:
callable = typ
case string:
call, ok := goja.AssertFunction(rt.Get(typ))
if !ok {
err = errors.WithStack(ErrFuncDoesNotExist)
return
}
callable = call
default:
err = errors.Errorf("callableOrFuncname: expected callable or function name, got '%T'", callableOrFuncname)
return
}
logger.Debug(ctx, "executing callable")
defer wg.Done()
defer func() {
if recovered := recover(); recovered != nil {
revoveredErr, ok := recovered.(error)
if ok {
logger.Error(ctx, "recovered runtime error", logger.E(errors.WithStack(revoveredErr)))
err = errors.WithStack(ErUnknownError)
return
}
panic(recovered)
}
}()
2023-02-09 12:16:36 +01:00
jsArgs := make([]goja.Value, 0, len(args))
for _, a := range args {
jsArgs = append(jsArgs, rt.ToValue(a))
2023-02-09 12:16:36 +01:00
}
value, err = callable(nil, jsArgs...)
if err != nil {
err = errors.WithStack(err)
}
})
wg.Wait()
if err != nil {
return nil, errors.WithStack(err)
}
2023-02-09 12:16:36 +01:00
return value, nil
2023-02-09 12:16:36 +01:00
}
func (s *Server) WaitForPromise(promise *goja.Promise) goja.Value {
var (
wg sync.WaitGroup
value goja.Value
)
wg.Add(1)
// Wait for promise completion
go func() {
for {
var loopWait sync.WaitGroup
loopWait.Add(1)
breakLoop := false
s.loop.RunOnLoop(func(vm *goja.Runtime) {
defer loopWait.Done()
if promise.State() == goja.PromiseStatePending {
return
}
value = promise.Result()
breakLoop = true
})
loopWait.Wait()
if breakLoop {
wg.Done()
return
}
}
}()
wg.Wait()
return value
}
func (s *Server) Start() error {
s.loop.Start()
var err error
2023-02-09 12:16:36 +01:00
s.loop.RunOnLoop(func(rt *goja.Runtime) {
rt.SetFieldNameMapper(goja.TagFieldNameMapper("goja", true))
rt.SetRandSource(createRandomSource())
if err = s.initModules(rt); err != nil {
err = errors.WithStack(err)
2023-02-09 12:16:36 +01:00
}
})
if err != nil {
return errors.WithStack(err)
2023-02-09 12:16:36 +01:00
}
return nil
}
func (s *Server) Stop() {
s.loop.Stop()
}
func (s *Server) initModules(rt *goja.Runtime) error {
modules := make([]ServerModule, 0, len(s.factories))
2023-02-09 12:16:36 +01:00
for _, moduleFactory := range s.factories {
2023-02-09 12:16:36 +01:00
mod := moduleFactory(s)
export := rt.NewObject()
2023-02-09 12:16:36 +01:00
mod.Export(export)
rt.Set(mod.Name(), export)
2023-02-09 12:16:36 +01:00
modules = append(modules, mod)
}
for _, mod := range modules {
initMod, ok := mod.(InitializableModule)
if !ok {
continue
}
logger.Debug(context.Background(), "initializing module", logger.F("module", initMod.Name()))
if err := initMod.OnInit(rt); err != nil {
return errors.WithStack(err)
}
}
2023-02-09 12:16:36 +01:00
s.modules = modules
return nil
2023-02-09 12:16:36 +01:00
}
func NewServer(factories ...ServerModuleFactory) *Server {
server := &Server{
factories: factories,
2023-02-09 12:16:36 +01:00
loop: eventloop.NewEventLoop(
eventloop.EnableConsole(false),
),
}
return server
}
func createRandomSource() goja.RandSource {
rnd := rand.New(&cryptoSource{})
return rnd.Float64
}