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

Commit

Permalink
feat: add util.cid options (#13)
Browse files Browse the repository at this point in the history
* feat: add util.cid options

See ipld/interface-ipld-format#40
  • Loading branch information
richardschneider authored Jun 25, 2018
1 parent b7db79b commit bb2fbf7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
},
"homepage": "https://github.com/ipld/js-ipld-raw#readme",
"dependencies": {
"cids": "~0.5.2"
"cids": "~0.5.2",
"multihashing-async": "~0.5.1"
},
"devDependencies": {
"aegir": "^12.4.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1"
"dirty-chai": "^2.0.1",
"multihashes": "~0.4.12"
}
}
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
const CID = require('cids')
const multihash = require('multihashing-async')

// binary resolver
module.exports = {
Expand All @@ -26,8 +27,18 @@ module.exports = {
serialize: (data, cb) => {
cb(null, data)
},
cid: (data, cb) => {
cb(null, new CID(1, 'raw', data))
cid: (data, options, cb) => {
if (typeof options === 'function') {
cb = options
options = {}
}
options = options || {}
const hashAlg = options.hashAlg || 'sha2-256'
const version = typeof options.version === 'undefined' ? 1 : options.version
multihash(data, hashAlg, (err, mh) => {
if (err) return cb(err)
cb(null, new CID(version, 'raw', mh))
})
}
}
}
45 changes: 45 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ chai.use(dirtyChai)

const ipldRaw = require('../src/index')
const resolver = ipldRaw.resolver
const multihash = require('multihashes')

describe('raw codec', () => {
let testData = Buffer.from('test data')
Expand Down Expand Up @@ -52,3 +53,47 @@ describe('raw codec', () => {
})
})
})

describe('raw util', () => {
let rawData = Buffer.from('some raw data')

it('serialize is noop', (done) => {
ipldRaw.util.serialize(rawData, (err, result) => {
expect(err).to.not.exist()
expect(result).to.equal(rawData)
done()
})
})

it('deserialize is noop', (done) => {
ipldRaw.util.deserialize(rawData, (err, result) => {
expect(err).to.not.exist()
expect(result).to.equal(rawData)
done()
})
})

it('create cid', (done) => {
ipldRaw.util.cid(rawData, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})

it('create cid with hashAlg', (done) => {
ipldRaw.util.cid(rawData, { hashAlg: 'sha2-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})
})

0 comments on commit bb2fbf7

Please sign in to comment.