From 38f89bad3bcd0fd1f5e615652d2407160e49692b Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 30 Oct 2018 21:42:14 +0000 Subject: [PATCH] feat: async await License: MIT Signed-off-by: Henrique Dias --- .travis.yml | 4 +- appveyor.yml | 2 +- src/index.js | 36 ++++++------------ test/index.spec.js | 91 ++++++++++++++++++---------------------------- 4 files changed, 50 insertions(+), 83 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5102ee5..b6370b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,10 @@ language: node_js matrix: include: - - node_js: 6 - env: CXX=g++-4.8 - node_js: 8 env: CXX=g++-4.8 + - node_js: 10 + env: CXX=g++-4.8 # - node_js: stable # env: CXX=g++-4.8 diff --git a/appveyor.yml b/appveyor.yml index 046bf91..8ab0d6c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,8 +3,8 @@ version: "{build}" environment: matrix: - - nodejs_version: "6" - nodejs_version: "8" + - nodejs_version: "10" matrix: fast_finish: true diff --git a/src/index.js b/src/index.js index ead6c9d..6584d1d 100644 --- a/src/index.js +++ b/src/index.js @@ -7,37 +7,25 @@ module.exports = { resolver: { multicodec: 'raw', defaultHashAlg: 'sha2-256', - resolve: (binaryBlob, path, callback) => { - callback(null, { + resolve: async (binaryBlob, path) => { + return { value: binaryBlob, remainderPath: '' - }) - }, - tree: (binaryBlob, options, callback) => { - if (typeof options === 'function') { - callback = options } - callback(null, []) - } + }, + tree: async (binaryBlob, options) => [] }, util: { - deserialize: (data, cb) => { - cb(null, data) - }, - serialize: (data, cb) => { - cb(null, data) - }, - cid: (data, options, cb) => { - if (typeof options === 'function') { - cb = options - options = {} - } - options = options || {} + deserialize: async (data) => data, + serialize: async (data) => data, + cid: async (data, 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)) + return new Promise((resolve, reject) => { + multihash(data, hashAlg, (err, mh) => { + if (err) return reject(err) + resolve(new CID(version, 'raw', mh)) + }) }) } } diff --git a/test/index.spec.js b/test/index.spec.js index 39be805..6a03bae 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -13,12 +13,9 @@ describe('raw codec', () => { let testData = Buffer.from('test data') let testBlob - before((done) => { - ipldRaw.util.serialize(testData, (err, result) => { - expect(err).to.not.exist() - testBlob = result - done() - }) + before(async () => { + const result = await ipldRaw.util.serialize(testData) + testBlob = result }) it('multicodec is raw', () => { @@ -29,71 +26,53 @@ describe('raw codec', () => { expect(resolver.defaultHashAlg).to.equal('sha2-256') }) - it('resolver.resolve', () => { - resolver.resolve(testBlob, 'a/b/c/d', (err, result) => { - expect(err).to.not.exist() - expect(result.value.toString('hex')).to.equal(testData.toString('hex')) - expect(result.remainderPath).to.equal('') - }) + it('resolver.resolve', async () => { + const result = await resolver.resolve(testBlob, 'a/b/c/d') + expect(result.value.toString('hex')).to.equal(testData.toString('hex')) + expect(result.remainderPath).to.equal('') }) - it('resolver.tree', () => { - resolver.tree(testBlob, {}, (err, paths) => { - expect(err).to.not.exist() - expect(Array.isArray(paths)).to.eql(true) - expect(paths.length).to.eql(0) - }) + it('resolver.tree', async () => { + const paths = await resolver.tree(testBlob, {}) + expect(Array.isArray(paths)).to.eql(true) + expect(paths.length).to.eql(0) }) - it('resolver.tree option parameter can be ignored', () => { - resolver.tree(testBlob, (err, paths) => { - expect(err).to.not.exist() - expect(Array.isArray(paths)).to.eql(true) - expect(paths.length).to.eql(0) - }) + it('resolver.tree option parameter can be ignored', async () => { + const paths = await resolver.tree(testBlob) + expect(Array.isArray(paths)).to.eql(true) + expect(paths.length).to.eql(0) }) }) 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('serialize is noop', async () => { + const result = await ipldRaw.util.serialize(rawData) + expect(result).to.equal(rawData) }) - it('deserialize is noop', (done) => { - ipldRaw.util.deserialize(rawData, (err, result) => { - expect(err).to.not.exist() - expect(result).to.equal(rawData) - done() - }) + it('deserialize is noop', async () => { + const result = await ipldRaw.util.deserialize(rawData) + expect(result).to.equal(rawData) }) - 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', async () => { + const cid = await ipldRaw.util.cid(rawData) + 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') }) - 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() - }) + it('create cid with hashAlg', async () => { + const cid = await ipldRaw.util.cid(rawData, { hashAlg: 'sha2-512' }) + 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') }) })