diff --git a/cmds/liter-server/handler.go b/cmds/liter-server/handler.go index 2d723e3..2bf571d 100644 --- a/cmds/liter-server/handler.go +++ b/cmds/liter-server/handler.go @@ -29,7 +29,7 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){ } cid := s.conns.Put(c0) wc.OnClose = func(){ - wc.Emit(&script.Event{ Name: "close" }) + wc.Emit(&script.Event{ Name: "close", Data: Map{ "conn": wc.Exports() } }) s.conns.Free(cid) } @@ -167,7 +167,7 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){ } }() wconn.OnClose = func(){ - wconn.Emit(&script.Event{ Name: "close" }) + wconn.Emit(&script.Event{ Name: "close", Data: Map{ "conn": wconn.Exports() } }) } ploger.Debugf("Target %q connected", svr.Id) @@ -223,7 +223,7 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){ ploger.Errorf("Error at client connection: %v", err) if <-wconn.Emit(script.NewEvent("before_close", Map{ "conn": wconn.Exports(), - "error": err, + "error": err.Error(), })) { ploger.Infof("Server connection default close action prevented") preventSvrSideClose = true @@ -232,7 +232,7 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){ ploger.Errorf("Error at server connection: %v", err) if <-wc.Emit(script.NewEvent("before_close", Map{ "conn": wc.Exports(), - "error": err, + "error": err.Error(), })) { ploger.Infof("Client connection default close action prevented") preventCliSideClose = true @@ -395,8 +395,7 @@ func parseAndForward(dst, src *script.WrappedConn)(<-chan error){ src.Emit(&script.Event{ Name: "error", Data: Map{ - "src": src.Exports(), - "dst": dst.Exports(), + "conn": src.Exports(), "error": err.Error(), }, }) @@ -404,8 +403,7 @@ func parseAndForward(dst, src *script.WrappedConn)(<-chan error){ return } if !<-src.Emit(script.NewEvent("packet", Map{ - "src": src.Exports(), - "dst": dst.Exports(), + "conn": src.Exports(), "packet": r, })) { if err = dst.Send(pkt.Reset(r.Protocol(), r.Id()).ByteArray(r.Bytes())); err != nil { @@ -413,8 +411,7 @@ func parseAndForward(dst, src *script.WrappedConn)(<-chan error){ dst.Emit(&script.Event{ Name: "error", Data: Map{ - "src": src.Exports(), - "dst": dst.Exports(), + "conn": dst.Exports(), "error": err.Error(), }, }) diff --git a/script/conn.go b/script/conn.go index e779e1d..5a34e3d 100644 --- a/script/conn.go +++ b/script/conn.go @@ -48,7 +48,7 @@ func WrapConn(conn *liter.Conn, vm *goja.Runtime, loop *eventloop.EventLoop)(c * panic(vm.NewTypeError("arg#0 is not a integer of packet id")) } id := (liter.VarInt)(call.Argument(0).ToInteger()) - return WrapPacketBuilder(liter.NewPacket(conn.Protocol(), id), conn, vm) + return WrapPacketBuilder(liter.NewPacket(conn.Protocol(), id), conn, vm, loop) }) c.emitter.ExportTo(o) c.exports = o diff --git a/script/emitter.go b/script/emitter.go index 5d38eed..e7c8ae0 100644 --- a/script/emitter.go +++ b/script/emitter.go @@ -26,10 +26,10 @@ func NewEventEmitter(vm *goja.Runtime, loop *eventloop.EventLoop)(e *EventEmitte } func (e *EventEmitter)ExportTo(o *goja.Object){ - o.Set("on", e.On) - o.Set("addListener", e.On) - o.Set("off", e.Off) - o.Set("removeListener", e.Off) + o.Set("on", e.on) + o.Set("addListener", e.on) + o.Set("off", e.off) + o.Set("removeListener", e.off) o.Set("emit", func(call goja.FunctionCall, vm *goja.Runtime)(goja.Value){ event := &Event{ Name: call.Arguments[0].String(), @@ -46,25 +46,25 @@ func (e *EventEmitter)ExportTo(o *goja.Object){ event.Cancelable = cancelable } } - return vm.ToValue(e.Emit(event)) + return vm.ToValue(e.emit(event)) }) - o.Set("removeAllListeners", e.RemoveAllListeners) - o.Set("eventNames", e.EventNames) - o.Set("listeners", e.Listeners) + o.Set("removeAllListeners", e.removeAllListeners) + o.Set("eventNames", e.eventNames) + o.Set("listeners", e.getListeners) } -func (e *EventEmitter)On(name string, listener goja.Callable)(*EventEmitter){ +func (e *EventEmitter)on(name string, listener goja.Callable)(*EventEmitter){ e.listeners[name] = append(e.listeners[name], listener) return e } func (e *EventEmitter)OnAsync(name string, listener goja.Callable){ e.loop.RunOnLoop(func(vm *goja.Runtime){ - e.On(name, listener) + e.on(name, listener) }) } -func (e *EventEmitter)Off(name string, listener goja.Callable){ +func (e *EventEmitter)off(name string, listener goja.Callable){ if listeners, ok := e.listeners[name]; ok { hp := reflect.ValueOf(listener).Pointer() for i := len(listeners) - 1; i >= 0; i-- { @@ -79,15 +79,15 @@ func (e *EventEmitter)Off(name string, listener goja.Callable){ func (e *EventEmitter)OffAsync(name string, listener goja.Callable){ e.loop.RunOnLoop(func(vm *goja.Runtime){ - e.Off(name, listener) + e.off(name, listener) }) } -func (e *EventEmitter)RemoveAllListeners(name string){ +func (e *EventEmitter)removeAllListeners(name string){ delete(e.listeners, name) } -func (e *EventEmitter)Emit(event *Event)(cancelled bool){ +func (e *EventEmitter)emit(event *Event)(cancelled bool){ if event.Cancelled() { panic(e.vm.ToValue("Error: Trying to emit a cancelled event")) } @@ -112,12 +112,12 @@ func (e *EventEmitter)EmitAsync(event *Event)(done <-chan bool){ exit := make(chan bool, 1) e.loop.RunOnLoop(func(vm *goja.Runtime){ defer close(exit) - exit <- e.Emit(event) + exit <- e.emit(event) }) return exit } -func (e *EventEmitter)EventNames()(names []string){ +func (e *EventEmitter)eventNames()(names []string){ names = make([]string, 0, len(e.listeners)) for n, _ := range e.listeners { names = append(names, n) @@ -125,7 +125,7 @@ func (e *EventEmitter)EventNames()(names []string){ return } -func (e *EventEmitter)Listeners(name string)(handlers []goja.Callable){ +func (e *EventEmitter)getListeners(name string)(handlers []goja.Callable){ listeners := e.listeners[name] handlers = make([]goja.Callable, 0, len(listeners)) for _, c := range listeners { diff --git a/script/packets.go b/script/packets.go index ea36715..9ad73a0 100644 --- a/script/packets.go +++ b/script/packets.go @@ -6,10 +6,11 @@ import ( "github.com/google/uuid" "github.com/dop251/goja" + "github.com/dop251/goja_nodejs/eventloop" "github.com/kmcsr/go-liter" ) -func WrapPacketBuilder(p *liter.PacketBuilder, conn *liter.Conn, vm *goja.Runtime)(o *goja.Object){ +func WrapPacketBuilder(p *liter.PacketBuilder, conn *liter.Conn, vm *goja.Runtime, loop *eventloop.EventLoop)(o *goja.Object){ o = vm.NewObject() sent := false o.DefineAccessorProperty("protocol", vm.ToValue(func(goja.FunctionCall)(goja.Value){ @@ -26,9 +27,13 @@ func WrapPacketBuilder(p *liter.PacketBuilder, conn *liter.Conn, vm *goja.Runtim pm, r, e := vm.NewPromise() go func(){ if err := conn.Send(p); err != nil { - e(err) + loop.RunOnLoop(func(*goja.Runtime){ + e(err) + }) }else{ - r(goja.Undefined()) + loop.RunOnLoop(func(*goja.Runtime){ + r(goja.Undefined()) + }) } }() return vm.ToValue(pm) @@ -130,7 +135,12 @@ func WrapPacketReader(p *liter.PacketReader, vm *goja.Runtime)(*WrappedPacketRea if !p.ByteArray(buf) { panic(EOF) } - return vm.ToValue(vm.NewArrayBuffer(buf)) + jsUint8Array, _ := goja.AssertConstructor(vm.Get("Uint8Array")) + res, err := jsUint8Array(vm.NewObject(), vm.ToValue(vm.NewArrayBuffer(buf))) + if err != nil { + panic(err) + } + return res }) o.Set("bool", func(call goja.FunctionCall)(goja.Value){ v, ok := p.Bool()