feat(client/rpc): switch rpc client to use go-ipld-prime global decoders via go-ipld-legacy instead of go-ipld-format ones
Adin Schmahmann committed
Jun 2, 2023 at 10:35 UTC
de59ac1b44d6f3e8deaed0b2cbddb4d09a6bb11a
2 files changed
+16
-1
client/rpc/api.go
+15
@@ -10,6 +10,12 @@ import (
10
11
iface "github.com/ipfs/boxo/coreiface"
12
caopts "github.com/ipfs/boxo/coreiface/options"
13
+ "github.com/ipfs/boxo/ipld/merkledag"
14
+ "github.com/ipfs/go-cid"
15
+ legacy "github.com/ipfs/go-ipld-legacy"
16
+ dagpb "github.com/ipld/go-codec-dagpb"
17
+ _ "github.com/ipld/go-ipld-prime/codec/dagcbor"
18
+ "github.com/ipld/go-ipld-prime/node/basicnode"
19
"github.com/mitchellh/go-homedir"
20
ma "github.com/multiformats/go-multiaddr"
21
manet "github.com/multiformats/go-multiaddr/net"
@@ -35,6 +41,7 @@ type HttpApi struct {
41
httpcli http.Client
42
Headers http.Header
43
applyGlobal func(*requestBuilder)
44
+ ipldDecoder *legacy.Decoder
45
}
46
47
// NewLocalApi tries to construct new HttpApi instance communicating with local
@@ -125,11 +132,19 @@ func NewApiWithClient(a ma.Multiaddr, c *http.Client) (*HttpApi, error) {
132
}
133
134
func NewURLApiWithClient(url string, c *http.Client) (*HttpApi, error) {
135
+ decoder := legacy.NewDecoder()
136
+ // Add support for these codecs to match what is done in the merkledag library
137
+ // Note: to match prior behavior the go-ipld-prime CBOR decoder is manually included
138
+ // TODO: allow the codec registry used to be configured by the caller not through a global variable
139
+ decoder.RegisterCodec(cid.DagProtobuf, dagpb.Type.PBNode, merkledag.ProtoNodeConverter)
140
+ decoder.RegisterCodec(cid.Raw, basicnode.Prototype.Bytes, merkledag.RawNodeConverter)
141
+
142
api := &HttpApi{
143
url: url,
144
httpcli: *c,
145
Headers: make(map[string][]string),
146
applyGlobal: func(*requestBuilder) {},
147
+ ipldDecoder: decoder,
148
}
149
150
// We don't support redirects.
client/rpc/dag.go
+1
-1
@@ -34,7 +34,7 @@ func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error)
34
return nil, err
35
}
36
37
- return format.DefaultBlockDecoder.Decode(blk)
37
+ return api.ipldDecoder.DecodeNode(ctx, blk)
38
}
39
40
func (api *HttpDagServ) GetMany(ctx context.Context, cids []cid.Cid) <-chan *format.NodeOption {