Add reqenvelope package with marshal/unmarshal support and tests, implement GenFilters utility, and improve filter-related methods for robustness

This commit is contained in:
2025-08-30 15:29:09 +01:00
parent faa527756b
commit 6c39ea4332
8 changed files with 346 additions and 84 deletions

View File

@@ -0,0 +1,111 @@
// Package reqenvelope is a message from a client to a relay containing a
// subscription identifier and an array of filters to search for events.
package reqenvelope
import (
"io"
"lol.mleku.dev/chk"
"next.orly.dev/pkg/encoders/envelopes"
"next.orly.dev/pkg/encoders/filter"
"next.orly.dev/pkg/encoders/text"
"next.orly.dev/pkg/interfaces/codec"
)
// L is the label associated with this type of codec.Envelope.
const L = "REQ"
// T is a filter/subscription request envelope that can contain multiple
// filters. These prompt the relay to search its event store and return all
// events and if the limit is unset or large enough, it will continue to return
// newly received events after it returns an eoseenvelope.T.
type T struct {
Subscription []byte
Filters filter.S
}
var _ codec.Envelope = (*T)(nil)
// New creates a new reqenvelope.T with a standard subscription.Id and empty
// filters.T.
func New() *T { return new(T) }
// NewFrom creates a new reqenvelope.T with a provided subscription.Id and
// filters.T.
func NewFrom(id []byte, ff filter.S) *T {
return &T{
Subscription: id,
Filters: ff,
}
}
func NewWithId[V string | []byte](id V, ff filter.S) (sub *T) {
return &T{
Subscription: []byte(id),
Filters: ff,
}
}
// Label returns the label of a reqenvelope.T.
func (en *T) Label() string { return L }
// Write the REQ T to a provided io.Writer.
func (en *T) Write(w io.Writer) (err error) {
_, err = w.Write(en.Marshal(nil))
return
}
// Marshal a reqenvelope.T envelope into minified JSON, appending to a provided
// destination slice. Note that this ensures correct string escaping on the
// subscription.Id field.
func (en *T) Marshal(dst []byte) (b []byte) {
var err error
_ = err
b = dst
b = envelopes.Marshal(
b, L,
func(bst []byte) (o []byte) {
o = bst
o = append(o, '"')
o = append(o, en.Subscription...)
o = append(o, '"')
for _, f := range en.Filters {
o = append(o, ',')
o = f.Marshal(o)
}
return
},
)
return
}
// Unmarshal into a reqenvelope.T from minified JSON, returning the remainder
// after the end of the envelope. Note that this ensures the subscription.Id
// string is correctly unescaped by NIP-01 escaping rules.
func (en *T) Unmarshal(b []byte) (r []byte, err error) {
r = b
if en.Subscription, r, err = text.UnmarshalQuoted(r); chk.E(err) {
return
}
if r, err = text.Comma(r); chk.E(err) {
return
}
if r, err = en.Filters.Unmarshal(r); chk.E(err) {
return
}
if r, err = envelopes.SkipToTheEnd(r); chk.E(err) {
return
}
return
}
// Parse reads a REQ envelope from minified JSON into a newly allocated
// reqenvelope.T.
func (en *T) Parse(b []byte) (t *T, rem []byte, err error) {
t = New()
if rem, err = t.Unmarshal(b); chk.E(err) {
return
}
return
}

View File

@@ -0,0 +1,69 @@
package reqenvelope
import (
"testing"
"lol.mleku.dev/chk"
"next.orly.dev/pkg/encoders/envelopes"
"next.orly.dev/pkg/encoders/filter"
"next.orly.dev/pkg/utils"
)
func TestMarshalUnmarshal(t *testing.T) {
var err error
rb, rb1, rb2 := make([]byte, 0, 65535), make([]byte, 0, 65535), make(
[]byte, 0, 65535,
)
for i := range 1000 {
var f filter.S
if f, err = filter.GenFilters(); chk.E(err) {
t.Fatal(err)
}
s := utils.NewSubscription(i)
req := NewFrom(s, f)
rb = req.Marshal(rb)
rb1 = rb1[:len(rb)]
copy(rb1, rb)
var rem []byte
var l string
if l, rb, err = envelopes.Identify(rb); chk.E(err) {
t.Fatal(err)
}
if l != L {
t.Fatalf("invalid sentinel %s, expect %s", l, L)
}
req2 := New()
if rem, err = req2.Unmarshal(rb); chk.E(err) {
t.Fatal(err)
}
if len(rem) > 0 {
t.Fatalf(
"unmarshal failed, remainder\n%d %s",
len(rem), rem,
)
}
rb2 = req2.Marshal(rb2)
if !utils.FastEqual(rb1, rb2) {
if len(rb1) != len(rb2) {
t.Fatalf(
"unmarshal failed, different lengths\n%d %s\n%d %s\n",
len(rb1), rb1, len(rb2), rb2,
)
}
for i := range rb1 {
if rb1[i] != rb2[i] {
t.Fatalf(
"unmarshal failed, difference at position %d\n%d %s\n%s\n%d %s\n%s\n",
i, len(rb1), rb1[:i], rb1[i:], len(rb2), rb2[:i],
rb2[i:],
)
}
}
t.Fatalf(
"unmarshal failed\n%d %s\n%d %s\n",
len(rb1), rb1, len(rb2), rb2,
)
}
rb, rb1, rb2 = rb[:0], rb1[:0], rb2[:0]
}
}