Skip to content

Commit

Permalink
Merge pull request #71 from ipld/rvagg/calculateHeaderLength
Browse files Browse the repository at this point in the history
feat: add precise calculateHeaderLength()
  • Loading branch information
Gozala authored Mar 30, 2022
2 parents 4c722d5 + 1520cde commit d28a12a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/buffer-writer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import varint from 'varint'
import { Token, Type } from 'cborg'
import { tokensToLength } from 'cborg/length'
import * as CBOR from '@ipld/dag-cbor'

// Number of bytes required without any roots.
Expand Down Expand Up @@ -250,6 +252,28 @@ const totalByteLength = (cids) => {
return total
}

const headerPreludeTokens = [
new Token(Type.map, 2),
new Token(Type.string, 'version'),
new Token(Type.uint, 1),
new Token(Type.string, 'roots')
]

/**
* @param {number[]} rootLengths
* @returns {number}
*/
export const calculateHeaderLength = (rootLengths) => {
const tokens = [...headerPreludeTokens]
tokens.push(new Token(Type.array, rootLengths.length))
for (const rootLength of rootLengths) {
tokens.push(new Token(Type.tag, 42))
tokens.push(new Token(Type.bytes, { length: rootLength + 1 }))
}
const length = tokensToLength(tokens) // no options needed here because we have simple tokens
return varint.encodingLength(length) + length
}

/**
* Creates synchronous CAR writer that can be used to encode blocks into a given
* buffer. Optionally you could pass `byteOffset` and `byteLength` to specify a
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
},
"dependencies": {
"@ipld/dag-cbor": "^7.0.0",
"cborg": "^1.9.0",
"multiformats": "^9.5.4",
"varint": "^6.0.0"
},
Expand Down
5 changes: 5 additions & 0 deletions test/test-buffer-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe('CarBufferWriter', () => {
const roots = new Array(count).fill(cid)
assert.deepEqual(CarBufferWriter.estimateHeaderSize(count), createHeader(roots).byteLength)
})
it(`calculateHeaderLength(${count})`, () => {
const roots = new Array(count).fill(cid)
const rootLengths = roots.map((c) => c.bytes.byteLength)
assert.deepEqual(CarBufferWriter.calculateHeaderLength(rootLengths), createHeader(roots).byteLength)
})
}
it('estimate on large CIDs', () => {
const largeCID = CID.parse(`bafkqbbac${'a'.repeat(416)}`)
Expand Down

0 comments on commit d28a12a

Please sign in to comment.