Skip to content

Commit

Permalink
resolving catalog: remove unused net.Resolver, validate some fields
Browse files Browse the repository at this point in the history
  • Loading branch information
bnewbold committed Dec 24, 2024
1 parent 6fd79d6 commit a8f53f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion atproto/lexicon/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
//
// The current implementation uses a naive 'getRepo' fetch to the relevant PDS instance, without validating MST proof chain.
//
// Calling code should usually use ResolvingCatalog, which handles basing caching.
// Calling code should usually use ResolvingCatalog, which handles basic caching and validation of the Lexicon language itself.
func ResolveLexiconData(ctx context.Context, dir identity.Directory, nsid syntax.NSID) (map[string]any, error) {

baseDir := identity.BaseDirectory{}
Expand Down
34 changes: 20 additions & 14 deletions atproto/lexicon/resolving_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"time"
"strings"

"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
Expand All @@ -14,34 +13,33 @@ import (
// Catalog which supplements an in-memory BaseCatalog with live resolution from the network
type ResolvingCatalog struct {
Base BaseCatalog
Resolver net.Resolver
Directory identity.Directory
}

// TODO: maybe this should take a base catalog as an arg?
func NewResolvingCatalog() ResolvingCatalog {
return ResolvingCatalog{
Base: NewBaseCatalog(),
Resolver: net.Resolver{
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{Timeout: time.Second * 5}
return d.DialContext(ctx, network, address)
},
},
Base: NewBaseCatalog(),
Directory: identity.DefaultDirectory(),
}
}

func (rc *ResolvingCatalog) Resolve(ref string) (*Schema, error) {
// TODO: not passed through!
// NOTE: not passed through!
ctx := context.Background()

if ref == "" {
return nil, fmt.Errorf("tried to resolve empty string name")
}

// TODO: split on '#'
nsid, err := syntax.ParseNSID(ref)
// first try existing catalog
schema, err := rc.Base.Resolve(ref)
if nil == err { // no error: found a hit
return schema, nil
}

// split any ref from the end '#'
parts := strings.SplitN(ref, "#", 2)
nsid, err := syntax.ParseNSID(parts[0])
if err != nil {
return nil, err
}
Expand All @@ -60,9 +58,17 @@ func (rc *ResolvingCatalog) Resolve(ref string) (*Schema, error) {
if err = json.Unmarshal(recordJSON, &sf); err != nil {
return nil, err
}

if sf.Lexicon != 1 {
return nil, fmt.Errorf("unsupported lexicon language version: %d", sf.Lexicon)
}
if sf.ID != nsid.String() {
return nil, fmt.Errorf("lexicon ID does not match NSID: %s != %s", sf.ID, nsid)
}
if err = rc.Base.AddSchemaFile(sf); err != nil {
return nil, err
}

// re-resolving from the raw ref ensures that fragments are handled
return rc.Base.Resolve(ref)
}

0 comments on commit a8f53f6

Please # to comment.