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

Commit

Permalink
fix: complete migration to async API
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 26, 2016
1 parent 3ee74d7 commit 2e91d7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"devDependencies": {
"aegir": "8.1.2",
"async": "^2.1.2",
"chai": "^3.5.0",
"pre-commit": "^1.1.3"
},
Expand Down
14 changes: 9 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ const resolver = require('./resolver')

exports = module.exports

exports.serialize = (dagNode) => {
return cbor.encode(dagNode)
exports.serialize = (dagNode, callback) => {
callback(null, cbor.encode(dagNode))
}

exports.deserialize = (data, callback) => {
cbor.decodeFirst(data, callback)
}

exports.cid = (dagNode, callback) => {
const serialized = exports.serialize(dagNode)
const mh = multihashing(serialized, 'sha2-256')
callback(null, new CID(1, resolver.multicodec, mh))
exports.serialize(dagNode, (err, serialized) => {
if (err) {
return callback(err)
}
const mh = multihashing(serialized, 'sha2-256')
callback(null, new CID(1, resolver.multicodec, mh))
})
}
22 changes: 18 additions & 4 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const expect = require('chai').expect
const dagCBOR = require('../src')
const resolver = dagCBOR.resolver
const Block = require('ipfs-block')
const series = require('async/series')

describe('IPLD format resolver (local)', () => {
let emptyNodeBlock
let nodeBlock

before(() => {
before((done) => {
const emptyNode = {}
const node = {
name: 'I am a node',
Expand All @@ -25,11 +26,24 @@ describe('IPLD format resolver (local)', () => {
{ a: 'b' },
2
]

}

emptyNodeBlock = new Block(dagCBOR.util.serialize(emptyNode))
nodeBlock = new Block(dagCBOR.util.serialize(node))
series([
(cb) => {
dagCBOR.util.serialize(emptyNode, (err, serialized) => {
expect(err).to.not.exist
emptyNodeBlock = new Block(serialized)
cb()
})
},
(cb) => {
dagCBOR.util.serialize(node, (err, serialized) => {
expect(err).to.not.exist
nodeBlock = new Block(serialized)
cb()
})
}
], done)
})

it('multicodec is dag-cbor', () => {
Expand Down
14 changes: 8 additions & 6 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ describe('util', () => {
}

it('.serialize and .deserialize', (done) => {
const serialized = dagCBOR.util.serialize(obj)
expect(Buffer.isBuffer(serialized)).to.be.true

dagCBOR.util.deserialize(serialized, (err, deserialized) => {
dagCBOR.util.serialize(obj, (err, serialized) => {
expect(err).to.not.exist
expect(obj).to.eql(deserialized)
done()
expect(Buffer.isBuffer(serialized)).to.be.true

dagCBOR.util.deserialize(serialized, (err, deserialized) => {
expect(err).to.not.exist
expect(obj).to.eql(deserialized)
done()
})
})
})

Expand Down

0 comments on commit 2e91d7c

Please sign in to comment.