2023-02-09 12:16:36 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2023-03-01 13:04:40 +01:00
|
|
|
"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"
|
2023-03-01 13:04:40 +01:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2023-02-09 12:16:36 +01:00
|
|
|
)
|
|
|
|
|
2023-03-01 13:04:40 +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 {
|
2023-04-24 12:16:30 +02:00
|
|
|
loop *eventloop.EventLoop
|
|
|
|
factories []ServerModuleFactory
|
|
|
|
modules []ServerModule
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Load(name string, src string) error {
|
2023-04-24 12:16:30 +02:00
|
|
|
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 {
|
2023-04-24 12:16:30 +02:00
|
|
|
return errors.WithStack(err)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-01 13:04:40 +01:00
|
|
|
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))
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
ret, err := s.Exec(ctx, funcName, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
return ret, nil
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 12:16:30 +02: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)
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-01 13:04:40 +01:00
|
|
|
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 {
|
2023-04-24 12:16:30 +02:00
|
|
|
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()
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-24 12:16:30 +02: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()
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
var err error
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-24 12:16:30 +02: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
|
|
|
}
|
2023-04-24 12:16:30 +02: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()
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
func (s *Server) initModules(rt *goja.Runtime) error {
|
|
|
|
modules := make([]ServerModule, 0, len(s.factories))
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
for _, moduleFactory := range s.factories {
|
2023-02-09 12:16:36 +01:00
|
|
|
mod := moduleFactory(s)
|
2023-04-24 12:16:30 +02:00
|
|
|
|
|
|
|
export := rt.NewObject()
|
2023-02-09 12:16:36 +01:00
|
|
|
mod.Export(export)
|
2023-04-24 12:16:30 +02:00
|
|
|
|
|
|
|
rt.Set(mod.Name(), export)
|
2023-02-09 12:16:36 +01:00
|
|
|
|
|
|
|
modules = append(modules, mod)
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:16:30 +02:00
|
|
|
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
|
2023-04-24 12:16:30 +02:00
|
|
|
|
|
|
|
return nil
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(factories ...ServerModuleFactory) *Server {
|
|
|
|
server := &Server{
|
2023-04-24 12:16:30 +02:00
|
|
|
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
|
|
|
|
}
|