Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
fix: convert output of multihash.decode to buffer
Browse files Browse the repository at this point in the history
The multihash module returns Uint8Arrays from it's decode method but
smart-buffer requires everything to be a buffer so convert the output.
  • Loading branch information
achingbrain authored and vmx committed Aug 5, 2020
1 parent 989e20d commit 20dd68f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"buffer": "^5.6.0",
"cids": "^1.0.0",
"multicodec": "^2.0.0",
"multihashing-async": "^2.0.0",
"multihashing-async": "^2.0.1",
"smart-buffer": "^4.1.0",
"strftime": "^0.10.0",
"uint8arrays": "^1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports.serialize = (dagNode) => {
*/
exports.deserialize = (data) => {
if (!Buffer.isBuffer(data)) {
data = Buffer.from(data, data.byteOffset, data.byteLength)
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
}

const headLen = gitUtil.find(data, 0)
Expand Down
3 changes: 2 additions & 1 deletion src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const multihash = require('multihashing-async').multihash
const CID = require('cids')
const strftime = require('strftime')
const { Buffer } = require('buffer')

exports = module.exports

Expand Down Expand Up @@ -65,5 +66,5 @@ exports.cidToSha = (cid) => {
return null
}

return mh.digest
return Buffer.from(mh.digest.buffer, mh.digest.byteOffset, mh.digest.byteLength)
}
13 changes: 13 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ describe('IPLD format util', () => {
expect(blob).to.deep.equal(node)
})

it('.serialize from tree', () => {
const node = {
'file.txt': {
hash: new CID('baf4bcfe5cqe5giojiciib5mci7gbb53xcxqot2i'),
mode: '644'
}
}
const blob = ipldGit.util.serialize(node)
const deserialized = ipldGit.util.deserialize(blob)

expect(deserialized).to.deep.equal(node)
})

it('.serialize and .deserialize', () => {
expect(Buffer.isBuffer(tagBlob)).to.be.true()
const deserialized = ipldGit.util.deserialize(tagBlob)
Expand Down

0 comments on commit 20dd68f

Please sign in to comment.