+1
-1
atproto/lexicon/resolve.go
+1
-1
atproto/lexicon/resolve.go
···
16
//
17
// The current implementation uses a naive 'getRepo' fetch to the relevant PDS instance, without validating MST proof chain.
18
//
19
-
// Calling code should usually use ResolvingCatalog, which handles basing caching.
20
func ResolveLexiconData(ctx context.Context, dir identity.Directory, nsid syntax.NSID) (map[string]any, error) {
21
22
baseDir := identity.BaseDirectory{}
···
16
//
17
// The current implementation uses a naive 'getRepo' fetch to the relevant PDS instance, without validating MST proof chain.
18
//
19
+
// Calling code should usually use ResolvingCatalog, which handles basic caching and validation of the Lexicon language itself.
20
func ResolveLexiconData(ctx context.Context, dir identity.Directory, nsid syntax.NSID) (map[string]any, error) {
21
22
baseDir := identity.BaseDirectory{}
+20
-14
atproto/lexicon/resolving_catalog.go
+20
-14
atproto/lexicon/resolving_catalog.go
···
4
"context"
5
"encoding/json"
6
"fmt"
7
-
"net"
8
-
"time"
9
10
"github.com/bluesky-social/indigo/atproto/identity"
11
"github.com/bluesky-social/indigo/atproto/syntax"
···
14
// Catalog which supplements an in-memory BaseCatalog with live resolution from the network
15
type ResolvingCatalog struct {
16
Base BaseCatalog
17
-
Resolver net.Resolver
18
Directory identity.Directory
19
}
20
21
-
// TODO: maybe this should take a base catalog as an arg?
22
func NewResolvingCatalog() ResolvingCatalog {
23
return ResolvingCatalog{
24
-
Base: NewBaseCatalog(),
25
-
Resolver: net.Resolver{
26
-
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
27
-
d := net.Dialer{Timeout: time.Second * 5}
28
-
return d.DialContext(ctx, network, address)
29
-
},
30
-
},
31
Directory: identity.DefaultDirectory(),
32
}
33
}
34
35
func (rc *ResolvingCatalog) Resolve(ref string) (*Schema, error) {
36
-
// TODO: not passed through!
37
ctx := context.Background()
38
39
if ref == "" {
40
return nil, fmt.Errorf("tried to resolve empty string name")
41
}
42
43
-
// TODO: split on '#'
44
-
nsid, err := syntax.ParseNSID(ref)
45
if err != nil {
46
return nil, err
47
}
···
60
if err = json.Unmarshal(recordJSON, &sf); err != nil {
61
return nil, err
62
}
63
if err = rc.Base.AddSchemaFile(sf); err != nil {
64
return nil, err
65
}
66
67
return rc.Base.Resolve(ref)
68
}
···
4
"context"
5
"encoding/json"
6
"fmt"
7
+
"strings"
8
9
"github.com/bluesky-social/indigo/atproto/identity"
10
"github.com/bluesky-social/indigo/atproto/syntax"
···
13
// Catalog which supplements an in-memory BaseCatalog with live resolution from the network
14
type ResolvingCatalog struct {
15
Base BaseCatalog
16
Directory identity.Directory
17
}
18
19
func NewResolvingCatalog() ResolvingCatalog {
20
return ResolvingCatalog{
21
+
Base: NewBaseCatalog(),
22
Directory: identity.DefaultDirectory(),
23
}
24
}
25
26
func (rc *ResolvingCatalog) Resolve(ref string) (*Schema, error) {
27
+
// NOTE: not passed through!
28
ctx := context.Background()
29
30
if ref == "" {
31
return nil, fmt.Errorf("tried to resolve empty string name")
32
}
33
34
+
// first try existing catalog
35
+
schema, err := rc.Base.Resolve(ref)
36
+
if nil == err { // no error: found a hit
37
+
return schema, nil
38
+
}
39
+
40
+
// split any ref from the end '#'
41
+
parts := strings.SplitN(ref, "#", 2)
42
+
nsid, err := syntax.ParseNSID(parts[0])
43
if err != nil {
44
return nil, err
45
}
···
58
if err = json.Unmarshal(recordJSON, &sf); err != nil {
59
return nil, err
60
}
61
+
62
+
if sf.Lexicon != 1 {
63
+
return nil, fmt.Errorf("unsupported lexicon language version: %d", sf.Lexicon)
64
+
}
65
+
if sf.ID != nsid.String() {
66
+
return nil, fmt.Errorf("lexicon ID does not match NSID: %s != %s", sf.ID, nsid)
67
+
}
68
if err = rc.Base.AddSchemaFile(sf); err != nil {
69
return nil, err
70
}
71
72
+
// re-resolving from the raw ref ensures that fragments are handled
73
return rc.Base.Resolve(ref)
74
}