From f6567a6c6ebb4ab397f061aa1d7150dccd541196 Mon Sep 17 00:00:00 2001 From: mleku Date: Tue, 9 Jan 2024 21:14:59 +0000 Subject: [PATCH] migrated parsing to new code tests pass but algia isn't getting anything --- cmd/replicatrd/replicatr/alias.go | 9 ---- cmd/replicatrd/replicatr/listener.go | 2 + pkg/byter/byter.go | 5 ++ pkg/labeler/labeler.go | 5 ++ pkg/nostr/envelopes/OK/okenvelope.go | 26 ++-------- pkg/nostr/envelopes/auth/challenge.go | 44 ++++------------ pkg/nostr/envelopes/auth/response.go | 52 +++++-------------- pkg/nostr/envelopes/closed/closedenvelope.go | 35 ++++--------- pkg/nostr/envelopes/closer/closeenvelope.go | 28 ++++------ .../countrequest/countrequestenvelope.go | 21 +++----- .../envelopes/countresponse/countresponse.go | 28 ++++------ .../{ => envelopes}/enveloper/enveloper.go | 20 +++---- .../enveloper/enveloper_test.go | 6 +-- pkg/nostr/envelopes/eose/eoseenvelope.go | 31 +++-------- pkg/nostr/envelopes/event/eventenvelope.go | 29 +++-------- pkg/nostr/envelopes/notice/noticeenvelope.go | 27 +++------- pkg/nostr/envelopes/process.go | 8 +-- pkg/nostr/envelopes/req/reqenvelope.go | 20 ++----- pkg/nostr/envelopes/sentinel/read.go | 2 +- pkg/nostr/relay/relay.go | 4 +- pkg/relay/handlers.go | 4 +- pkg/wire/array/array.go | 17 +++--- pkg/wire/text/mangle.go | 4 ++ 23 files changed, 131 insertions(+), 296 deletions(-) delete mode 100644 cmd/replicatrd/replicatr/alias.go create mode 100644 pkg/byter/byter.go create mode 100644 pkg/labeler/labeler.go rename pkg/nostr/{ => envelopes}/enveloper/enveloper.go (74%) rename pkg/nostr/{ => envelopes}/enveloper/enveloper_test.go (92%) diff --git a/cmd/replicatrd/replicatr/alias.go b/cmd/replicatrd/replicatr/alias.go deleted file mode 100644 index 568e8a0a..00000000 --- a/cmd/replicatrd/replicatr/alias.go +++ /dev/null @@ -1,9 +0,0 @@ -package replicatr - -import ( - "github.com/puzpuzpuz/xsync/v2" -) - -type ( - ListenerMap = *xsync.MapOf[string, *Listener] -) diff --git a/cmd/replicatrd/replicatr/listener.go b/cmd/replicatrd/replicatr/listener.go index 19cf3acb..50c49b4e 100644 --- a/cmd/replicatrd/replicatr/listener.go +++ b/cmd/replicatrd/replicatr/listener.go @@ -15,6 +15,8 @@ type Listener struct { cancel context.C } +type ListenerMap = *xsync.MapOf[string, *Listener] + var listeners = xsync.NewTypedMapOf[*WebSocket, ListenerMap](pointerHasher[WebSocket]) diff --git a/pkg/byter/byter.go b/pkg/byter/byter.go new file mode 100644 index 00000000..1a9a1bb1 --- /dev/null +++ b/pkg/byter/byter.go @@ -0,0 +1,5 @@ +package byter + +type I interface { + Bytes() []byte +} diff --git a/pkg/labeler/labeler.go b/pkg/labeler/labeler.go new file mode 100644 index 00000000..2a4586aa --- /dev/null +++ b/pkg/labeler/labeler.go @@ -0,0 +1,5 @@ +package labeler + +type I interface{ + Label() string +} diff --git a/pkg/nostr/envelopes/OK/okenvelope.go b/pkg/nostr/envelopes/OK/okenvelope.go index 68f8cc7b..af42c695 100644 --- a/pkg/nostr/envelopes/OK/okenvelope.go +++ b/pkg/nostr/envelopes/OK/okenvelope.go @@ -6,7 +6,7 @@ import ( "strings" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/eventid" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" @@ -39,11 +39,6 @@ type Envelope struct { Reason string } -func (env *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} - func NewOKEnvelope(eventID eventid.EventID, ok bool, reason string) (o *Envelope, e error) { var ei eventid.EventID @@ -58,31 +53,18 @@ func NewOKEnvelope(eventID eventid.EventID, ok bool, reason string) (o *Envelope return } -// Label returns the label enum/type of the envelope. The relevant bytes could -// be retrieved using nip1.List[T] func (env *Envelope) Label() (l string) { return labels.OK } -// ToArray converts an Envelope to a form that has a JSON formatted String -// and Bytes function (array.T). To get the encoded form, invoke either of these -// methods on the returned value. func (env *Envelope) ToArray() (a array.T) { - // log.D.F("'%s' %v '%s' ", env.EventID, env.OK, env.Reason) return array.T{labels.OK, env.EventID, env.OK, env.Reason} } -func (env *Envelope) String() (s string) { - return env.ToArray().String() -} +func (env *Envelope) String() (s string) { return env.ToArray().String() } -func (env *Envelope) Bytes() (s []byte) { - return env.ToArray().Bytes() -} +func (env *Envelope) Bytes() (s []byte) { return env.ToArray().Bytes() } // MarshalJSON returns the JSON encoded form of the envelope. -func (env *Envelope) MarshalJSON() (bytes []byte, e error) { - // log.D.F("ok envelope marshal") - return env.ToArray().Bytes(), nil -} +func (env *Envelope) MarshalJSON() ([]byte, error) { return env.Bytes(), nil } const ( Btrue = "true" diff --git a/pkg/nostr/envelopes/auth/challenge.go b/pkg/nostr/envelopes/auth/challenge.go index dae53583..2d29cd29 100644 --- a/pkg/nostr/envelopes/auth/challenge.go +++ b/pkg/nostr/envelopes/auth/challenge.go @@ -3,8 +3,8 @@ package auth import ( "fmt" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" ) @@ -19,39 +19,15 @@ func NewChallenge(c string) (a *Challenge) { return &Challenge{Challenge: c} } -func (a *Challenge) Label() string { return labels.AUTH } -func (a *Challenge) String() (s string) { return a.ToArray().String() } -func (a *Challenge) Bytes() (s []byte) { return a.ToArray().Bytes() } -func (a *Challenge) ToArray() array.T { - return array.T{labels.AUTH, - a.Challenge} -} -func (a *Challenge) MarshalJSON() (bytes []byte, e error) { - return a.ToArray().Bytes(), nil -} +func (a *Challenge) Label() string { return l.AUTH } -func (a *Challenge) UnmarshalJSON(b []byte) (e error) { - // if a == nil { - // return fmt.Errorf("cannot unmarshal to nil pointer") - // } - // var l labels.T - // var buf *text.Buffer - // if l, buf, e = sentinel.Identify(b); log.Fail(e) { - // return - // } - // if l != labels.LAuth { - // e = fmt.Errorf("expected '%s' envelope, got '%s'", - // labels.AUTH, labels.List[l]) - // log.D.Ln(e) - // return - // } - // var c enveloper.I - // if c, e = sentinel.Read(buf, l); log.Fail(e) { - // return - // } - // *a = *c.(*Challenge) - return -} +func (a *Challenge) String() string { return a.ToArray().String() } + +func (a *Challenge) Bytes() []byte { return a.ToArray().Bytes() } + +func (a *Challenge) ToArray() array.T { return array.T{l.AUTH, a.Challenge} } + +func (a *Challenge) MarshalJSON() ([]byte, error) { return a.Bytes(), nil } func (a *Challenge) Unmarshal(buf *text.Buffer) (e error) { log.D.Ln("ok envelope unmarshal", string(buf.Buf)) diff --git a/pkg/nostr/envelopes/auth/response.go b/pkg/nostr/envelopes/auth/response.go index 587d0907..693bca3d 100644 --- a/pkg/nostr/envelopes/auth/response.go +++ b/pkg/nostr/envelopes/auth/response.go @@ -5,8 +5,8 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/event" "github.com/Hubmakerlabs/replicatr/pkg/nostr/kind" "github.com/Hubmakerlabs/replicatr/pkg/nostr/tags" @@ -22,57 +22,29 @@ type Response struct { var _ enveloper.I = &Response{} -// New creates an Response response from an Challenge. +// NewResponse creates an Response response from an Challenge. // // The caller must sign the embedded event before sending it back to // authenticate. -func New(ac *Challenge, rl string) (ae *Response) { +func NewResponse(ac *Challenge, rl string) (ae *Response) { ae = &Response{ &event.T{ Kind: kind.ClientAuthentication, - Tags: tags.T{ - {"relay", rl}, - {"challenge", ac.Challenge}, - }, + Tags: tags.T{{"relay", rl}, {"challenge", ac.Challenge}}, }, } return } -func (a *Response) Label() string { return labels.AUTH } -func (a *Response) ToArray() array.T { - return array.T{labels.AUTH, - a.T.ToObject()} -} -func (a *Response) String() (s string) { return a.ToArray().String() } -func (a *Response) Bytes() (s []byte) { return a.ToArray().Bytes() } +func (a *Response) Label() string { return l.AUTH } -func (a *Response) MarshalJSON() (b []byte, e error) { - return a.ToArray().Bytes(), nil -} +func (a *Response) ToArray() array.T { return array.T{l.AUTH, a.T.ToObject()} } -func (a *Response) UnmarshalJSON(b []byte) (e error) { - // if a == nil { - // return fmt.Errorf("cannot unmarshal to nil pointer") - // } - // var l labels.T - // var buf *text.Buffer - // if l, buf, e = sentinel.Identify(b); log.Fail(e) { - // return - // } - // if l != labels.LAuth { - // e = fmt.Errorf("expected '%s' envelope, got '%s'", - // labels.AUTH, labels.List[l]) - // log.D.Ln(e) - // return - // } - // var c enveloper.I - // if c, e = sentinel.Read(buf, l); log.Fail(e) { - // return - // } - // *a = *c.(*Response) - return -} +func (a *Response) String() string { return a.ToArray().String() } + +func (a *Response) Bytes() []byte { return a.ToArray().Bytes() } + +func (a *Response) MarshalJSON() ([]byte, error) { return a.Bytes(), nil } func (a *Response) Unmarshal(buf *text.Buffer) (e error) { if a == nil { diff --git a/pkg/nostr/envelopes/closed/closedenvelope.go b/pkg/nostr/envelopes/closed/closedenvelope.go index 7cd3798c..3e775adf 100644 --- a/pkg/nostr/envelopes/closed/closedenvelope.go +++ b/pkg/nostr/envelopes/closed/closedenvelope.go @@ -4,8 +4,8 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" @@ -21,36 +21,19 @@ type Envelope struct { var _ enveloper.I = &Envelope{} -func NewClosedEnvelope(s subscriptionid.T, reason string) (ce *Envelope) { - ce = &Envelope{T: s, Reason: reason} - return +func New(s subscriptionid.T, reason string) *Envelope { + return &Envelope{T: s, Reason: reason} } -// Label returns the label enum/type of the envelope. The relevant bytes could -// be retrieved using nip1.Labels[Label] -func (E *Envelope) Label() (l string) { return labels.CLOSED } +func (E *Envelope) ToArray() array.T { return array.T{l.CLOSED, E.T, E.Reason} } -func (E *Envelope) ToArray() (a array.T) { - return array.T{labels.CLOSED, E.T, E.Reason} -} +func (E *Envelope) Label() string { return l.CLOSED } -func (E *Envelope) String() (s string) { - return E.ToArray().String() -} +func (E *Envelope) String() (s string) { return E.ToArray().String() } -func (E *Envelope) Bytes() (s []byte) { - return E.ToArray().Bytes() -} +func (E *Envelope) Bytes() (s []byte) { return E.ToArray().Bytes() } -// MarshalJSON returns the JSON encoded form of the envelope. -func (E *Envelope) MarshalJSON() (bytes []byte, e error) { - return E.Bytes(), nil -} - -func (E *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} +func (E *Envelope) MarshalJSON() ([]byte, error) { return E.Bytes(), nil } // Unmarshal the envelope. func (E *Envelope) Unmarshal(buf *text.Buffer) (e error) { diff --git a/pkg/nostr/envelopes/closer/closeenvelope.go b/pkg/nostr/envelopes/closer/closeenvelope.go index b976d1b0..9e5e31a9 100644 --- a/pkg/nostr/envelopes/closer/closeenvelope.go +++ b/pkg/nostr/envelopes/closer/closeenvelope.go @@ -4,8 +4,8 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" @@ -20,27 +20,17 @@ type Envelope struct { var _ enveloper.I = &Envelope{} -func NewCloseEnvelope(s subscriptionid.T) (ce *Envelope) { - return &Envelope{T: s} -} +func New(s subscriptionid.T) (ce *Envelope) { return &Envelope{T: s} } -func (E *Envelope) Label() (l string) { return labels.CLOSE } -func (E *Envelope) String() (s string) { return E.ToArray().String() } -func (E *Envelope) Bytes() (s []byte) { return E.ToArray().Bytes() } +func (E *Envelope) Label() string { return l.CLOSE } -func (E *Envelope) ToArray() (a array.T) { - return array.T{labels.CLOSE, E.T} -} +func (E *Envelope) ToArray() array.T { return array.T{l.CLOSE, E.T} } -// MarshalJSON returns the JSON encoded form of the envelope. -func (E *Envelope) MarshalJSON() (bytes []byte, e error) { - return E.ToArray().Bytes(), nil -} +func (E *Envelope) String() string { return E.ToArray().String() } -func (E *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} +func (E *Envelope) Bytes() []byte { return E.ToArray().Bytes() } + +func (E *Envelope) MarshalJSON() ([]byte, error) { return E.Bytes(), nil } // Unmarshal the envelope. func (E *Envelope) Unmarshal(buf *text.Buffer) (e error) { diff --git a/pkg/nostr/envelopes/countrequest/countrequestenvelope.go b/pkg/nostr/envelopes/countrequest/countrequestenvelope.go index ed03feff..f0a1fa03 100644 --- a/pkg/nostr/envelopes/countrequest/countrequestenvelope.go +++ b/pkg/nostr/envelopes/countrequest/countrequestenvelope.go @@ -5,7 +5,7 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/filter" "github.com/Hubmakerlabs/replicatr/pkg/nostr/filters" "github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid" @@ -20,24 +20,17 @@ type Envelope struct { filters.T } -func (C *Envelope) Label() string { return labels.COUNT } -func (C *Envelope) String() (s string) { return C.ToArray().String() } -func (C *Envelope) Bytes() (s []byte) { return C.ToArray().Bytes() } +func (C *Envelope) Label() string { return l.COUNT } func (C *Envelope) ToArray() array.T { - return array.T{labels.COUNT, C.SubscriptionID, C.T} + return array.T{l.COUNT, C.SubscriptionID, C.T} } -// MarshalJSON returns the JSON encoded form of the envelope. -func (C *Envelope) MarshalJSON() (bytes []byte, e error) { - // log.D.F("count request envelope marshal") - return C.ToArray().Bytes(), nil -} +func (C *Envelope) String() string { return C.ToArray().String() } -func (C *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} +func (C *Envelope) Bytes() []byte { return C.ToArray().Bytes() } + +func (C *Envelope) MarshalJSON() ([]byte, error) { return C.Bytes(), nil } func (C *Envelope) Unmarshal(buf *text.Buffer) (e error) { log.D.Ln("ok envelope unmarshal", string(buf.Buf)) diff --git a/pkg/nostr/envelopes/countresponse/countresponse.go b/pkg/nostr/envelopes/countresponse/countresponse.go index 8b3f41af..545da97b 100644 --- a/pkg/nostr/envelopes/countresponse/countresponse.go +++ b/pkg/nostr/envelopes/countresponse/countresponse.go @@ -5,13 +5,14 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/object" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" ) + var log = log2.GetStd() type Envelope struct { @@ -20,15 +21,9 @@ type Envelope struct { Approximate bool } -func (env *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} - var _ enveloper.I = &Envelope{} -func NewCountResponseEnvelope(sid subscriptionid.T, count int64, - approx bool) (C *Envelope) { +func New(sid subscriptionid.T, count int64, approx bool) (C *Envelope) { C = &Envelope{ SubscriptionID: sid, Count: count, @@ -36,9 +31,7 @@ func NewCountResponseEnvelope(sid subscriptionid.T, count int64, } return } -func (env *Envelope) Label() string { return labels.EVENT } -func (env *Envelope) String() (s string) { return env.ToArray().String() } -func (env *Envelope) Bytes() (s []byte) { return env.ToArray().Bytes() } +func (env *Envelope) Label() string { return l.EVENT } func (env *Envelope) ToArray() array.T { count := object.T{ @@ -48,13 +41,14 @@ func (env *Envelope) ToArray() array.T { count = append(count, object.KV{Key: "approximate", Value: env.Approximate}) } - return array.T{labels.COUNT, env.SubscriptionID, count} + return array.T{l.COUNT, env.SubscriptionID, count} } -func (env *Envelope) MarshalJSON() (bytes []byte, e error) { - // log.D.F("count envelope marshal") - return env.ToArray().Bytes(), nil -} +func (env *Envelope) String() (s string) { return env.ToArray().String() } + +func (env *Envelope) Bytes() (s []byte) { return env.ToArray().Bytes() } + +func (env *Envelope) MarshalJSON() ([]byte, error) { return env.Bytes(), nil } func (env *Envelope) Unmarshal(buf *text.Buffer) (e error) { log.D.Ln("ok envelope unmarshal", string(buf.Buf)) diff --git a/pkg/nostr/enveloper/enveloper.go b/pkg/nostr/envelopes/enveloper/enveloper.go similarity index 74% rename from pkg/nostr/enveloper/enveloper.go rename to pkg/nostr/envelopes/enveloper/enveloper.go index 85facbf2..4cfc27af 100644 --- a/pkg/nostr/enveloper/enveloper.go +++ b/pkg/nostr/envelopes/enveloper/enveloper.go @@ -1,7 +1,11 @@ package enveloper import ( - "github.com/Hubmakerlabs/replicatr/pkg/go-nostr/envelopes" + "encoding/json" + "fmt" + + "github.com/Hubmakerlabs/replicatr/pkg/byter" + "github.com/Hubmakerlabs/replicatr/pkg/labeler" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" ) @@ -15,12 +19,10 @@ import ( // and with omitempty marking the mandatory ones, acting as a "kind" of // sentinel. type I interface { - - envelopes.E - - // Unmarshal the envelope. - Unmarshal(buf *text.Buffer) (e error) - - array.Arrayer + labeler.I + fmt.Stringer + byter.I + json.Marshaler + text.Unmarshaler + array.I } - diff --git a/pkg/nostr/enveloper/enveloper_test.go b/pkg/nostr/envelopes/enveloper/enveloper_test.go similarity index 92% rename from pkg/nostr/enveloper/enveloper_test.go rename to pkg/nostr/envelopes/enveloper/enveloper_test.go index f150a8e0..d54c1981 100644 --- a/pkg/nostr/enveloper/enveloper_test.go +++ b/pkg/nostr/envelopes/enveloper/enveloper_test.go @@ -5,10 +5,10 @@ import ( "testing" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/OK" close2 "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/closer" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/eose" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/event" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/notice" @@ -20,7 +20,7 @@ import ( var log = log2.GetStd() func TestEnveloper(t *testing.T) { - // log2.SetLogLevel(log2.Debug) + log2.SetLogLevel(log2.Debug) const sub = "subscription000001" envs := []enveloper.I{ &event.Envelope{SubscriptionID: sub, Event: eventest.D[0]}, @@ -43,7 +43,7 @@ func TestEnveloper(t *testing.T) { marshaled := string(b) log.D.Ln("marshaled ", marshaled) var env enveloper.I - env, _, _, e = envelopes.ProcessEnvelope(b) + env, _, e = envelopes.ProcessEnvelope(b) if e != nil { t.Fatal(e) } diff --git a/pkg/nostr/envelopes/eose/eoseenvelope.go b/pkg/nostr/envelopes/eose/eoseenvelope.go index 73994d94..41d9c5d4 100644 --- a/pkg/nostr/envelopes/eose/eoseenvelope.go +++ b/pkg/nostr/envelopes/eose/eoseenvelope.go @@ -4,8 +4,8 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" @@ -20,34 +20,17 @@ type Envelope struct { subscriptionid.T } -func (E *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} - var _ enveloper.I = (*Envelope)(nil) -// Label returns the label enum/type of the envelope. The relevant bytes could -// be retrieved using nip1.List[T] -func (E *Envelope) Label() (l string) { return labels.EOSE } +func (E *Envelope) Label() string { return l.EOSE } -func (E *Envelope) ToArray() (a array.T) { - a = array.T{labels.EOSE, E.T} - return -} +func (E *Envelope) ToArray() array.T { return array.T{l.EOSE, E.T} } -func (E *Envelope) String() (s string) { - return E.ToArray().String() -} +func (E *Envelope) String() (s string) { return E.ToArray().String() } -func (E *Envelope) Bytes() (s []byte) { - return E.ToArray().Bytes() -} +func (E *Envelope) Bytes() (s []byte) { return E.ToArray().Bytes() } -// MarshalJSON returns the JSON encoded form of the envelope. -func (E *Envelope) MarshalJSON() (bytes []byte, e error) { - return E.ToArray().Bytes(), nil -} +func (E *Envelope) MarshalJSON() ([]byte, error) { return E.Bytes(), nil } // Unmarshal the envelope. func (E *Envelope) Unmarshal(buf *text.Buffer) (e error) { diff --git a/pkg/nostr/envelopes/event/eventenvelope.go b/pkg/nostr/envelopes/event/eventenvelope.go index 3d35ee9f..982e9450 100644 --- a/pkg/nostr/envelopes/event/eventenvelope.go +++ b/pkg/nostr/envelopes/event/eventenvelope.go @@ -5,13 +5,14 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/event" "github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" ) + var log = log2.GetStd() var _ enveloper.I = (*Envelope)(nil) @@ -47,18 +48,11 @@ func NewEventEnvelope(si string, ev *event.T) (ee *Envelope, e error) { return &Envelope{SubscriptionID: sid, Event: ev}, nil } -// Label returns the label enum/type of the envelope. The relevant bytes could -// be retrieved using nip1.List[T] -func (env *Envelope) Label() (l string) { return labels.EVENT } +func (env *Envelope) Label() string { return l.EVENT } -// ToArray converts an Envelope to a form that has a JSON formatted String -// and Bytes function (array.T). To get the encoded form, invoke either of these -// methods on the returned value. func (env *Envelope) ToArray() (a array.T) { - - // T envelope has max 3 fields a = make(array.T, 0, 3) - a = append(a, labels.EVENT) + a = append(a, l.EVENT) if env.SubscriptionID.IsValid() { a = append(a, env.SubscriptionID) } @@ -66,18 +60,11 @@ func (env *Envelope) ToArray() (a array.T) { return } -func (env *Envelope) String() (s string) { - return env.ToArray().String() -} +func (env *Envelope) String() (s string) { return env.ToArray().String() } -func (env *Envelope) Bytes() (s []byte) { - return env.ToArray().Bytes() -} +func (env *Envelope) Bytes() (s []byte) { return env.ToArray().Bytes() } -// MarshalJSON returns the JSON encoded form of the envelope. -func (env *Envelope) MarshalJSON() (bytes []byte, e error) { - return env.ToArray().Bytes(), nil -} +func (env *Envelope) MarshalJSON() ([]byte, error) { return env.Bytes(), nil } // Unmarshal the envelope. func (env *Envelope) Unmarshal(buf *text.Buffer) (e error) { diff --git a/pkg/nostr/envelopes/notice/noticeenvelope.go b/pkg/nostr/envelopes/notice/noticeenvelope.go index d497c23f..95904160 100644 --- a/pkg/nostr/envelopes/notice/noticeenvelope.go +++ b/pkg/nostr/envelopes/notice/noticeenvelope.go @@ -4,7 +4,7 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" + l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/wire/array" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" ) @@ -17,11 +17,6 @@ type Envelope struct { Text string } -func (E *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} - func NewNoticeEnvelope(text string) (E *Envelope) { E = &Envelope{Text: text} return @@ -29,25 +24,15 @@ func NewNoticeEnvelope(text string) (E *Envelope) { // Label returns the label enum/type of the envelope. The relevant bytes could // be retrieved using nip1.List[T] -func (E *Envelope) Label() (l string) { return labels.NOTICE } +func (E *Envelope) Label() string { return l.NOTICE } -func (E *Envelope) ToArray() (a array.T) { - return array.T{labels.NOTICE, E.Text} -} +func (E *Envelope) ToArray() array.T { return array.T{l.NOTICE, E.Text} } -func (E *Envelope) String() (s string) { - return E.ToArray().String() -} +func (E *Envelope) String() (s string) { return E.ToArray().String() } -func (E *Envelope) Bytes() (s []byte) { - return E.ToArray().Bytes() -} +func (E *Envelope) Bytes() (s []byte) { return E.ToArray().Bytes() } -// MarshalJSON returns the JSON encoded form of the envelope. -func (E *Envelope) MarshalJSON() (bytes []byte, e error) { - bytes = E.ToArray().Bytes() - return -} +func (E *Envelope) MarshalJSON() ([]byte, error) { return E.Bytes(), nil } // Unmarshal the envelope. func (E *Envelope) Unmarshal(buf *text.Buffer) (e error) { diff --git a/pkg/nostr/envelopes/process.go b/pkg/nostr/envelopes/process.go index 8178ab97..3e02957d 100644 --- a/pkg/nostr/envelopes/process.go +++ b/pkg/nostr/envelopes/process.go @@ -4,10 +4,10 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/OK" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/closed" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/closer" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/eose" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/event" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" @@ -24,8 +24,7 @@ var log = log2.GetStd() // If it fails, it also returns the label bytes found and the buffer, which will // have the cursor at the next byte after the quote delimiter of the T, ready // for some other envelope outside of nip-01 to decode. -func ProcessEnvelope(b []byte) (env enveloper.I, label []byte, - buf *text.Buffer, e error) { +func ProcessEnvelope(b []byte) (env enveloper.I, buf *text.Buffer, e error) { log.D.F("processing envelope:\n%s", string(b)) // The bytes must be valid JSON but we can't assume they are free of @@ -68,9 +67,6 @@ matched: // no match e = fmt.Errorf("label '%s' not recognised as envelope label", string(candidate)) - // label is the string that was found in the first element of the JSON - // array. - label = candidate return } // We know what to expect now, the next thing to do is pass forward to the diff --git a/pkg/nostr/envelopes/req/reqenvelope.go b/pkg/nostr/envelopes/req/reqenvelope.go index e6d83b11..485f7cdd 100644 --- a/pkg/nostr/envelopes/req/reqenvelope.go +++ b/pkg/nostr/envelopes/req/reqenvelope.go @@ -5,7 +5,7 @@ import ( "fmt" log2 "github.com/Hubmakerlabs/replicatr/pkg/log" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" "github.com/Hubmakerlabs/replicatr/pkg/nostr/filter" "github.com/Hubmakerlabs/replicatr/pkg/nostr/filters" @@ -36,23 +36,11 @@ func (E *Envelope) ToArray() (arr array.T) { return } -func (E *Envelope) String() (s string) { - return E.ToArray().String() -} +func (E *Envelope) String() (s string) { return E.ToArray().String() } -func (E *Envelope) Bytes() (s []byte) { - return E.ToArray().Bytes() -} +func (E *Envelope) Bytes() (s []byte) { return E.ToArray().Bytes() } -// MarshalJSON returns the JSON encoded form of the envelope. -func (E *Envelope) MarshalJSON() (bytes []byte, e error) { - return E.ToArray().Bytes(), nil -} - -func (E *Envelope) UnmarshalJSON(bytes []byte) error { - // TODO implement me - panic("implement me") -} +func (E *Envelope) MarshalJSON() ([]byte, error) { return E.Bytes(), nil } // Unmarshal the envelope. func (E *Envelope) Unmarshal(buf *text.Buffer) (e error) { diff --git a/pkg/nostr/envelopes/sentinel/read.go b/pkg/nostr/envelopes/sentinel/read.go index 5a90f795..204f9297 100644 --- a/pkg/nostr/envelopes/sentinel/read.go +++ b/pkg/nostr/envelopes/sentinel/read.go @@ -5,13 +5,13 @@ import ( "bytes" "fmt" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/OK" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/auth" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/closed" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/closer" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/countrequest" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/countresponse" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/eose" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/event" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels" diff --git a/pkg/nostr/relay/relay.go b/pkg/nostr/relay/relay.go index e042589b..1c753796 100644 --- a/pkg/nostr/relay/relay.go +++ b/pkg/nostr/relay/relay.go @@ -9,9 +9,9 @@ import ( "time" "github.com/Hubmakerlabs/replicatr/pkg/context" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/go-nostr/connect" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/OK" auth2 "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/auth" @@ -255,7 +255,7 @@ func (r *Relay) Connect(c context.T) (e error) { message := buf.Bytes() log.D.F("{%s} %v", r.URL, string(message)) var envelope enveloper.I - envelope, _, _, e = envelopes.ProcessEnvelope(message) + envelope, _, e = envelopes.ProcessEnvelope(message) if envelope == nil || log.Fail(e) { continue } diff --git a/pkg/relay/handlers.go b/pkg/relay/handlers.go index d9533477..a7262203 100644 --- a/pkg/relay/handlers.go +++ b/pkg/relay/handlers.go @@ -11,8 +11,8 @@ import ( "time" "github.com/Hubmakerlabs/replicatr/pkg/context" + "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper" - "github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes" "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/OK" auth2 "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/auth" @@ -122,7 +122,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) { var e error var ok bool var envelope enveloper.I - if envelope, _, _, e = envelopes.ProcessEnvelope(message); log.Fail(e) || envelope == nil { + if envelope, _, e = envelopes.ProcessEnvelope(message); log.Fail(e) || envelope == nil { return } switch env := envelope.(type) { diff --git a/pkg/wire/array/array.go b/pkg/wire/array/array.go index 97db6323..1e237c93 100644 --- a/pkg/wire/array/array.go +++ b/pkg/wire/array/array.go @@ -16,23 +16,20 @@ import ( "github.com/Hubmakerlabs/replicatr/pkg/wire/object" "github.com/Hubmakerlabs/replicatr/pkg/wire/text" ) + var log = log2.GetStd() -// Arrayer is an interface for a type that can return an array.T - or in other +type T []interface{} + +// I is an interface for a type that can return an array.T - or in other // words []interface{} made into concrete. -type Arrayer interface { +type I interface { ToArray() T } -type T []interface{} +func (t T) String() string { return t.Buffer().String() } -func (t T) String() string { - return t.Buffer().String() -} - -func (t T) Bytes() []byte { - return t.Buffer().Bytes() -} +func (t T) Bytes() []byte { return t.Buffer().Bytes() } func (t T) Buffer() *bytes.Buffer { buf := new(bytes.Buffer) diff --git a/pkg/wire/text/mangle.go b/pkg/wire/text/mangle.go index a66c2144..0338ec4d 100644 --- a/pkg/wire/text/mangle.go +++ b/pkg/wire/text/mangle.go @@ -10,6 +10,10 @@ import ( "io" ) +type Unmarshaler interface { + Unmarshal(buf *Buffer) (e error) +} + type Buffer struct { Pos int Buf []byte