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

feat: add util.cid options #13

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
})