Commit ec8dd23e authored by hannahhoward's avatar hannahhoward

fix(cidlink): check for byte buffer

parent e2992c32
...@@ -30,8 +30,15 @@ func (lnk Link) Load(ctx context.Context, lnkCtx ipld.LinkContext, na ipld.NodeA ...@@ -30,8 +30,15 @@ func (lnk Link) Load(ctx context.Context, lnkCtx ipld.LinkContext, na ipld.NodeA
if !exists { if !exists {
return fmt.Errorf("no decoder registered for multicodec %d", lnk.Prefix().Codec) return fmt.Errorf("no decoder registered for multicodec %d", lnk.Prefix().Codec)
} }
var hasherBytes []byte
var decodeErr error
byteBuf, ok := r.(*bytes.Buffer)
if ok {
hasherBytes = byteBuf.Bytes()
decodeErr = mcDecoder(na, r)
} else {
var hasher bytes.Buffer // multihash only exports bulk use, which is... really inefficient and should be fixed. var hasher bytes.Buffer // multihash only exports bulk use, which is... really inefficient and should be fixed.
decodeErr := mcDecoder(na, io.TeeReader(r, &hasher)) decodeErr = mcDecoder(na, io.TeeReader(r, &hasher))
// Error checking order here is tricky. // Error checking order here is tricky.
// If decoding errored out, we should still run the reader to the end, to check the hash. // If decoding errored out, we should still run the reader to the end, to check the hash.
// (We still don't implement this by running the hash to the end first, because that would increase the high-water memory requirement.) // (We still don't implement this by running the hash to the end first, because that would increase the high-water memory requirement.)
...@@ -43,7 +50,10 @@ func (lnk Link) Load(ctx context.Context, lnkCtx ipld.LinkContext, na ipld.NodeA ...@@ -43,7 +50,10 @@ func (lnk Link) Load(ctx context.Context, lnkCtx ipld.LinkContext, na ipld.NodeA
return err return err
} }
} }
cid, err := lnk.Prefix().Sum(hasher.Bytes()) hasherBytes = hasher.Bytes()
}
cid, err := lnk.Prefix().Sum(hasherBytes)
if err != nil { if err != nil {
return err return err
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment