Skip to content

Commit

Permalink
feat(client/rpc): switch rpc client to use go-ipld-prime global decod…
Browse files Browse the repository at this point in the history
…ers via go-ipld-legacy instead of go-ipld-format ones
  • Loading branch information
aschmahmann authored and Jorropo committed Jun 8, 2023
1 parent 4ebde42 commit de59ac1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions client/rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (

iface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/go-cid"
legacy "github.com/ipfs/go-ipld-legacy"
dagpb "github.com/ipld/go-codec-dagpb"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/mitchellh/go-homedir"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
Expand All @@ -35,6 +41,7 @@ type HttpApi struct {
httpcli http.Client
Headers http.Header
applyGlobal func(*requestBuilder)
ipldDecoder *legacy.Decoder
}

// NewLocalApi tries to construct new HttpApi instance communicating with local
Expand Down Expand Up @@ -125,11 +132,19 @@ func NewApiWithClient(a ma.Multiaddr, c *http.Client) (*HttpApi, error) {
}

func NewURLApiWithClient(url string, c *http.Client) (*HttpApi, error) {
decoder := legacy.NewDecoder()
// Add support for these codecs to match what is done in the merkledag library
// Note: to match prior behavior the go-ipld-prime CBOR decoder is manually included
// TODO: allow the codec registry used to be configured by the caller not through a global variable
decoder.RegisterCodec(cid.DagProtobuf, dagpb.Type.PBNode, merkledag.ProtoNodeConverter)
decoder.RegisterCodec(cid.Raw, basicnode.Prototype.Bytes, merkledag.RawNodeConverter)

api := &HttpApi{
url: url,
httpcli: *c,
Headers: make(map[string][]string),
applyGlobal: func(*requestBuilder) {},
ipldDecoder: decoder,
}

// We don't support redirects.
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error)
return nil, err
}

return format.DefaultBlockDecoder.Decode(blk)
return api.ipldDecoder.DecodeNode(ctx, blk)
}

func (api *HttpDagServ) GetMany(ctx context.Context, cids []cid.Cid) <-chan *format.NodeOption {
Expand Down

0 comments on commit de59ac1

Please sign in to comment.