Skip to content

Commit

Permalink
feat(relay): handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Sep 11, 2024
1 parent c813129 commit 7771a94
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ linters:
- makezero
- mirror
- misspell
- musttag
# - musttag
- nakedret
- nilerr
- nilnil
Expand Down
8 changes: 6 additions & 2 deletions relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ func (r *Relay) HandleReq(ws *websocket.Conn, m message.Message) {
msg, ok := m.(*message.Req)
if !ok {
_, _ = ws.Write(message.MakeNotice("error: can't parse REQ message")) // TODO::: should we check error?

return
}

subs, ok := r.conns[ws]
if !ok {
_, _ = ws.Write(message.MakeNotice(fmt.Sprintf("error: can't find connection %s", ws.RemoteAddr()))) // TODO::: should we check error?
_, _ = ws.Write(message.MakeNotice(fmt.Sprintf("error: can't find connection %s",
ws.RemoteAddr()))) // TODO::: should we check error?

return
}

Expand Down Expand Up @@ -155,7 +158,8 @@ func (r *Relay) HandleClose(ws *websocket.Conn, m message.Message) {

conn, ok := r.conns[ws]
if !ok {
_, _ = ws.Write(message.MakeNotice(fmt.Sprintf("error: can't find connection %s", ws.RemoteAddr()))) // TODO::: should we check error?
_, _ = ws.Write(message.MakeNotice(fmt.Sprintf("error: can't find connection %s",
ws.RemoteAddr()))) // TODO::: should we check error?

return
}
Expand Down
12 changes: 6 additions & 6 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package types

import "fmt"

// ErrEncode represents an encoding error.
type ErrEncode struct {
// EncodeError represents an encoding error.
type EncodeError struct {
Reason string
}

func (e ErrEncode) Error() string {
func (e EncodeError) Error() string {
return fmt.Sprintf("encoding error: %s", e.Reason)
}

// ErrDecode represents an decoding error.
type ErrDecode struct {
// DecodeError represents an decoding error.
type DecodeError struct {
Reason string
}

func (e ErrDecode) Error() string {
func (e DecodeError) Error() string {
return fmt.Sprintf("decoding error: %s", e.Reason)
}
4 changes: 2 additions & 2 deletions types/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Decode(b []byte) (*Event, error) {
e := new(Event)

if err := easyjson.Unmarshal(b, e); err != nil {
return nil, types.ErrDecode{
return nil, types.DecodeError{
Reason: err.Error(),
}
}
Expand All @@ -38,7 +38,7 @@ func Decode(b []byte) (*Event, error) {
func (e *Event) Encode() ([]byte, error) {
b, err := easyjson.Marshal(e)
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: err.Error(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions types/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Decode(b []byte) (*Filter, error) {
e := new(Filter)

if err := easyjson.Unmarshal(b, e); err != nil {
return nil, types.ErrDecode{
return nil, types.DecodeError{
Reason: err.Error(),
}
}
Expand All @@ -93,7 +93,7 @@ func Decode(b []byte) (*Filter, error) {
func (f *Filter) Encode() ([]byte, error) {
ee, err := easyjson.Marshal(f)
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: err.Error(),
}
}
Expand Down
39 changes: 20 additions & 19 deletions types/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func (em *Event) DecodeFromJSON(data []byte) error {
case len(arr) >= 2:
err := easyjson.Unmarshal([]byte(arr[1].Raw), em.Event)
if err != nil {
return types.ErrDecode{
return types.DecodeError{
Reason: fmt.Sprintf("EVENT message: %s", err.Error()),
}
}

return nil
default:

return types.ErrDecode{
return types.DecodeError{
Reason: "EVENT messag: no event found.",
}
}
Expand All @@ -115,7 +115,7 @@ func (em Event) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("EVENT message: %s", err.Error()),
}
}
Expand All @@ -135,7 +135,7 @@ func (rm *Req) DecodeFromJSON(data []byte) error {
r := gjson.ParseBytes(data)
arr := r.Array()
if len(arr) < 3 {
return types.ErrDecode{
return types.DecodeError{
Reason: "REQ message: missing filters.",
}
}
Expand All @@ -144,7 +144,7 @@ func (rm *Req) DecodeFromJSON(data []byte) error {
f := 0
for i := 2; i < len(arr); i++ {
if err := easyjson.Unmarshal([]byte(arr[i].Raw), &rm.Filters[f]); err != nil {
return types.ErrDecode{
return types.DecodeError{
Reason: fmt.Sprintf("REQ message: %s", err.Error()),
}
}
Expand Down Expand Up @@ -179,14 +179,14 @@ func (cm *Count) DecodeFromJSON(data []byte) error {
r := gjson.ParseBytes(data)
arr := r.Array()
if len(arr) < 3 {
return types.ErrDecode{
return types.DecodeError{
Reason: "COUNT message: missing filters.",
}
}
cm.SubscriptionID = arr[1].Str

if len(arr) < 3 {
return types.ErrDecode{
return types.DecodeError{
Reason: "COUNT message: array must have at least 3 items.",
}
}
Expand All @@ -197,7 +197,7 @@ func (cm *Count) DecodeFromJSON(data []byte) error {
item := []byte(arr[i].Raw)

if err := easyjson.Unmarshal(item, cm.Filters[f]); err != nil {
return types.ErrDecode{
return types.DecodeError{
Reason: fmt.Sprintf("COUNT message: %s", err.Error()),
}
}
Expand All @@ -215,7 +215,7 @@ func (cm Count) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("COUNT message: %s", err.Error()),
}
}
Expand Down Expand Up @@ -257,7 +257,7 @@ func (nm Notice) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("NOTICE message: %s", err.Error()),
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (em EOSE) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("EOSE message: %s", err.Error()),
}
}
Expand All @@ -316,7 +316,7 @@ func (cm *Close) DecodeFromJSON(data []byte) error {
return nil
default:

return types.ErrDecode{
return types.DecodeError{
Reason: "CLOSE message: subscription ID missed.",
}
}
Expand All @@ -330,13 +330,12 @@ func (cm Close) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("CLOSE message: %s", err.Error()),
}
}

return res, nil

}

// Closed reperesents a NIP-01 CLOSED message.
Expand Down Expand Up @@ -384,7 +383,7 @@ func (cm Closed) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("CLOSED message: %s", err.Error()),
}
}
Expand Down Expand Up @@ -443,7 +442,7 @@ func (om OK) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("OK message: %s", err.Error()),
}
}
Expand Down Expand Up @@ -472,18 +471,19 @@ func (am *Auth) DecodeFromJSON(data []byte) error {
r := gjson.ParseBytes(data)
arr := r.Array()
if len(arr) < 2 {
return types.ErrDecode{
return types.DecodeError{
Reason: "AUTH message: missing fields.",
}
}

if arr[1].IsObject() {
err := easyjson.Unmarshal([]byte(arr[1].Raw), am.Event)
if err != nil {
return types.ErrDecode{
return types.DecodeError{
Reason: fmt.Sprintf("AUTH message: %s", err.Error()),
}
}

return nil
}

Expand All @@ -500,9 +500,10 @@ func (am Auth) EncodeToJSON() ([]byte, error) {

res, err := w.BuildBytes()
if err != nil {
return nil, types.ErrEncode{
return nil, types.EncodeError{
Reason: fmt.Sprintf("AUTH message: %s", err.Error()),
}
}

return res, nil
}

0 comments on commit 7771a94

Please sign in to comment.