Skip to content

Commit

Permalink
fix: size for single block raw node (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw authored Feb 6, 2022
1 parent 061cd7e commit 708aea7
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/api/src/car.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,20 @@ async function carStat (carBlob) {
let size
const decoder = decoders.find(d => d.code === rootCid.code)
if (decoder) {
const rootBlock = new Block({ cid: rootCid, bytes: rawRootBlock.bytes, value: decoder.decode(rawRootBlock.bytes) })
const hasLinks = !rootBlock.links()[Symbol.iterator]().next().done
// if the root block has links, then we should have at least 2 blocks in the CAR
if (hasLinks && blocks < 2) {
throw new Error('CAR must contain at least one non-root block')
}
// get the size of the full dag for this root, even if we only have a partial CAR.
if (rootBlock.cid.code === pb.code) {
size = cumulativeSize(rootBlock.bytes, rootBlock.value)
// if there's only 1 block (the root block) and it's a raw node, we know the size.
if (blocks === 1 && rootCid.code === raw.code) {
size = rawRootBlock.bytes.byteLength
} else {
const rootBlock = new Block({ cid: rootCid, bytes: rawRootBlock.bytes, value: decoder.decode(rawRootBlock.bytes) })
const hasLinks = !rootBlock.links()[Symbol.iterator]().next().done
// if the root block has links, then we should have at least 2 blocks in the CAR
if (hasLinks && blocks < 2) {
throw new Error('CAR must contain at least one non-root block')
}
// get the size of the full dag for this root, even if we only have a partial CAR.
if (rootBlock.cid.code === pb.code) {
size = cumulativeSize(rootBlock.bytes, rootBlock.value)
}
}
}
return { size, blocks, rootCid }
Expand Down

0 comments on commit 708aea7

Please sign in to comment.