From 5a461364c1d3544bcd3b0e4ec18ed3335fcd18db Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Fri, 11 Jan 2019 23:35:39 -0500 Subject: [PATCH] chore: change from callbacks to promises Works https://github.com/ipfs/js-ipfs/issues/1670 --- README.md | 5 +- package.json | 1 - src/resolver.js | 195 ++++++++++++-------------- src/util.js | 63 +++------ src/util/commit.js | 21 ++- src/util/tag.js | 12 +- src/util/tree.js | 16 +-- test/parse.spec.js | 53 +++---- test/resolver.spec.js | 311 ++++++++++++++++-------------------------- test/util.spec.js | 60 ++++---- 10 files changed, 294 insertions(+), 443 deletions(-) diff --git a/README.md b/README.md index 69fc6f9..b0344df 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,10 @@ const zlib = require('zlib') // `gitObject` is a Buffer containing a git object inflatedObject = zlib.inflateSync(gitObject) -IpldGit.util.deserialize(inflatedObject, (err, dagNode) => { - if (err) throw err +IpldGit.util.deserialize(inflatedObject).then(dagNode => { console.log(dagNode) +}, error => { + console.error(error) }) ``` diff --git a/package.json b/package.json index 660f37d..ba62980 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,6 @@ }, "homepage": "https://github.com/ipld/js-ipld-git", "dependencies": { - "async": "^2.6.0", "cids": "~0.5.2", "multicodec": "~0.4.0", "multihashes": "~0.4.12", diff --git a/src/resolver.js b/src/resolver.js index b44f0cb..cf369fa 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -15,139 +15,116 @@ const personInfoPaths = [ 'date' ] -exports.resolve = (binaryBlob, path, callback) => { - if (typeof path === 'function') { - callback = path - path = undefined - } - - util.deserialize(binaryBlob, (err, node) => { - if (err) { - return callback(err) - } +exports.resolve = async (binaryBlob, path) => { + let node = await util.deserialize(binaryBlob) - if (!path || path === '/') { - return callback(null, { - value: node, - remainderPath: '' - }) + if (!path || path === '/') { + return { + value: node, + remainderPath: '' } + } - if (Buffer.isBuffer(node)) { // git blob - return callback(null, { - value: node, - remainderPath: path - }) + if (Buffer.isBuffer(node)) { // git blob + return { + value: node, + remainderPath: path } + } - const parts = path.split('/') - const val = traverse(node).get(parts) + const parts = path.split('/') + const val = traverse(node).get(parts) - if (val) { - return callback(null, { - value: val, - remainderPath: '' - }) + if (val) { + return { + value: val, + remainderPath: '' } + } - let value - let len = parts.length + let value + let len = parts.length - for (let i = 0; i < len; i++) { - const partialPath = parts.shift() + for (let i = 0; i < len; i++) { + const partialPath = parts.shift() - if (Array.isArray(node)) { - value = node[Number(partialPath)] - } if (node[partialPath]) { - value = node[partialPath] + if (Array.isArray(node)) { + value = node[Number(partialPath)] + } if (node[partialPath]) { + value = node[partialPath] + } else { + // can't traverse more + if (!value) { + throw new Error('path not available at root') } else { - // can't traverse more - if (!value) { - return callback(new Error('path not available at root')) - } else { - parts.unshift(partialPath) - return callback(null, { - value: value, - remainderPath: parts.join('/') - }) + parts.unshift(partialPath) + return { + value: value, + remainderPath: parts.join('/') } } - node = value } - }) -} - -exports.tree = (binaryBlob, options, callback) => { - if (typeof options === 'function') { - callback = options - options = undefined + node = value } +} +exports.tree = async (binaryBlob, options) => { options = options || {} - util.deserialize(binaryBlob, (err, node) => { - if (err) { - return callback(err) - } + const node = await util.deserialize(binaryBlob) - if (Buffer.isBuffer(node)) { // git blob - return callback(null, []) - } + if (Buffer.isBuffer(node)) { // git blob + return [] + } - let paths = [] - switch (node.gitType) { - case 'commit': - paths = [ - 'message', - 'tree' - ] + let paths = [] + switch (node.gitType) { + case 'commit': + paths = [ + 'message', + 'tree' + ] - paths = paths.concat(personInfoPaths.map((e) => 'author/' + e)) - paths = paths.concat(personInfoPaths.map((e) => 'committer/' + e)) - paths = paths.concat(node.parents.map((_, e) => 'parents/' + e)) + paths = paths.concat(personInfoPaths.map((e) => 'author/' + e)) + paths = paths.concat(personInfoPaths.map((e) => 'committer/' + e)) + paths = paths.concat(node.parents.map((_, e) => 'parents/' + e)) - if (node.encoding) { - paths.push('encoding') - } - break - case 'tag': - paths = [ - 'object', - 'type', - 'tag', - 'message' - ] - - if (node.tagger) { - paths = paths.concat(personInfoPaths.map((e) => 'tagger/' + e)) - } + if (node.encoding) { + paths.push('encoding') + } + break + case 'tag': + paths = [ + 'object', + 'type', + 'tag', + 'message' + ] + + if (node.tagger) { + paths = paths.concat(personInfoPaths.map((e) => 'tagger/' + e)) + } - break - default: // tree - Object.keys(node).forEach(dir => { - paths.push(dir) - paths.push(dir + '/hash') - paths.push(dir + '/mode') - }) - } - callback(null, paths) - }) + break + default: // tree + Object.keys(node).forEach(dir => { + paths.push(dir) + paths.push(dir + '/hash') + paths.push(dir + '/mode') + }) + } + return paths } -exports.isLink = (binaryBlob, path, callback) => { - exports.resolve(binaryBlob, path, (err, result) => { - if (err) { - return callback(err) - } - - if (result.remainderPath.length > 0) { - return callback(new Error('path out of scope')) - } +exports.isLink = async (binaryBlob, path) => { + const result = await exports.resolve(binaryBlob, path) + if (result.remainderPath.length > 0) { + throw new Error('path out of scope') + } - if (typeof result.value === 'object' && result.value['/']) { - callback(null, result.value) - } else { - callback(null, false) - } - }) + if (typeof result.value === 'object' && result.value['/']) { + return result.value + } else { + return false + } } diff --git a/src/util.js b/src/util.js index b153569..e1eb9e8 100644 --- a/src/util.js +++ b/src/util.js @@ -1,7 +1,5 @@ 'use strict' -const setImmediate = require('async/setImmediate') -const waterfall = require('async/waterfall') const multihashing = require('multihashing-async') const CID = require('cids') @@ -14,66 +12,52 @@ const tree = require('./util/tree') exports = module.exports -exports.serialize = (dagNode, callback) => { +exports.serialize = async (dagNode) => { if (dagNode === null) { - setImmediate(() => callback(new Error('dagNode passed to serialize was null'), null)) - return + throw new Error('dagNode passed to serialize was null') } if (Buffer.isBuffer(dagNode)) { if (dagNode.slice(0, 4).toString() === 'blob') { - setImmediate(() => callback(null, dagNode)) + return dagNode } else { - setImmediate(() => callback(new Error('unexpected dagNode passed to serialize'), null)) + throw new Error('unexpected dagNode passed to serialize') } - return } switch (dagNode.gitType) { case 'commit': - commit.serialize(dagNode, callback) - break + return commit.serialize(dagNode) case 'tag': - tag.serialize(dagNode, callback) - break + return tag.serialize(dagNode) default: // assume tree as a file named 'type' is legal - tree.serialize(dagNode, callback) + return tree.serialize(dagNode) } } -exports.deserialize = (data, callback) => { +exports.deserialize = async (data) => { let headLen = gitUtil.find(data, 0) let head = data.slice(0, headLen).toString() let typeLen = head.match(/([^ ]+) (\d+)/) if (!typeLen) { - setImmediate(() => callback(new Error('invalid object header'), null)) - return + throw new Error('invalid object header') } switch (typeLen[1]) { case 'blob': - callback(null, data) - break + return data case 'commit': - commit.deserialize(data.slice(headLen + 1), callback) - break + return commit.deserialize(data.slice(headLen + 1)) case 'tag': - tag.deserialize(data.slice(headLen + 1), callback) - break + return tag.deserialize(data.slice(headLen + 1)) case 'tree': - tree.deserialize(data.slice(headLen + 1), callback) - break + return tree.deserialize(data.slice(headLen + 1)) default: - setImmediate(() => callback(new Error('unknown object type ' + typeLen[1]), null)) + throw new Error('unknown object type ' + typeLen[1]) } } -/** - * @callback CidCallback - * @param {?Error} error - Error if getting the CID failed - * @param {?CID} cid - CID if call was successful - */ /** * Get the CID of the DAG-Node. * @@ -81,20 +65,15 @@ exports.deserialize = (data, callback) => { * @param {Object} [options] - Options to create the CID * @param {number} [options.version=1] - CID version number * @param {string} [options.hashAlg='sha1'] - Hashing algorithm - * @param {CidCallback} callback - Callback that handles the return value - * @returns {void} + * @returns {Promise} that resolves the CID */ -exports.cid = (dagNode, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } +exports.cid = async (dagNode, options) => { options = options || {} const hashAlg = options.hashAlg || resolver.defaultHashAlg const version = typeof options.version === 'undefined' ? 1 : options.version - waterfall([ - (cb) => exports.serialize(dagNode, cb), - (serialized, cb) => multihashing(serialized, hashAlg, cb), - (mh, cb) => cb(null, new CID(version, resolver.multicodec, mh)) - ], callback) + const serialized = await exports.serialize(dagNode) + const mh = await new Promise((resolve, reject) => { + multihashing(serialized, hashAlg, (err, data) => err ? reject(err) : resolve(data)) + }) + return new CID(version, resolver.multicodec, mh) } diff --git a/src/util/commit.js b/src/util/commit.js index 29fd44b..688b3da 100644 --- a/src/util/commit.js +++ b/src/util/commit.js @@ -1,12 +1,11 @@ 'use strict' -const setImmediate = require('async/setImmediate') const SmartBuffer = require('smart-buffer').SmartBuffer const gitUtil = require('./util') exports = module.exports -exports.serialize = (dagNode, callback) => { +exports.serialize = async (dagNode) => { let lines = [] lines.push('tree ' + gitUtil.cidToSha(dagNode.tree['/']).toString('hex')) dagNode.parents.forEach((parent) => { @@ -37,10 +36,10 @@ exports.serialize = (dagNode, callback) => { outBuf.writeString(data.length.toString()) outBuf.writeUInt8(0) outBuf.writeString(data) - setImmediate(() => callback(null, outBuf.toBuffer())) + return outBuf.toBuffer() } -exports.deserialize = (data, callback) => { +exports.deserialize = async (data) => { let lines = data.toString().split('\n') let res = { gitType: 'commit', parents: [] } @@ -48,7 +47,7 @@ exports.deserialize = (data, callback) => { let m = lines[line].match(/^([^ ]+) (.+)$/) if (!m) { if (lines[line] !== '') { - setImmediate(() => callback(new Error('Invalid commit line ' + line))) + throw new Error('Invalid commit line ' + line) } res.message = lines.slice(line + 1).join('\n') break @@ -71,7 +70,7 @@ exports.deserialize = (data, callback) => { break case 'gpgsig': { if (value !== '-----BEGIN PGP SIGNATURE-----') { - setImmediate(() => callback(new Error('Invalid commit line ' + line))) + throw new Error('Invalid commit line ' + line) } res.signature = {} @@ -87,11 +86,12 @@ exports.deserialize = (data, callback) => { case 'mergetag': { let mt = value.match(/^object ([0-9a-f]{40})$/) if (!mt) { - setImmediate(() => callback(new Error('Invalid commit line ' + line))) + throw new Error('Invalid commit line ' + line) } - let tag = { object: - { '/': gitUtil.shaToCid(Buffer.from(mt[1], 'hex')) } + let tag = { + object: + { '/': gitUtil.shaToCid(Buffer.from(mt[1], 'hex')) } } let startLine = line @@ -114,6 +114,5 @@ exports.deserialize = (data, callback) => { res[key] = value } } - - setImmediate(() => callback(null, res)) + return res } diff --git a/src/util/tag.js b/src/util/tag.js index c084cfe..950ebe3 100644 --- a/src/util/tag.js +++ b/src/util/tag.js @@ -1,12 +1,11 @@ 'use strict' -const setImmediate = require('async/setImmediate') const SmartBuffer = require('smart-buffer').SmartBuffer const gitUtil = require('./util') exports = module.exports -exports.serialize = (dagNode, callback) => { +exports.serialize = async (dagNode) => { let lines = [] lines.push('object ' + gitUtil.cidToSha(dagNode.object['/']).toString('hex')) lines.push('type ' + dagNode.type) @@ -24,10 +23,10 @@ exports.serialize = (dagNode, callback) => { outBuf.writeString(data.length.toString()) outBuf.writeUInt8(0) outBuf.writeString(data) - setImmediate(() => callback(null, outBuf.toBuffer())) + return outBuf.toBuffer() } -exports.deserialize = (data, callback) => { +exports.deserialize = async (data) => { let lines = data.toString().split('\n') let res = { gitType: 'tag' } @@ -35,7 +34,7 @@ exports.deserialize = (data, callback) => { let m = lines[line].match(/^([^ ]+) (.+)$/) if (m === null) { if (lines[line] !== '') { - setImmediate(() => callback(new Error('Invalid tag line ' + line))) + throw new Error('Invalid tag line ' + line) } res.message = lines.slice(line + 1).join('\n') break @@ -60,6 +59,5 @@ exports.deserialize = (data, callback) => { res[key] = value } } - - setImmediate(() => callback(null, res)) + return res } diff --git a/src/util/tree.js b/src/util/tree.js index fdde586..d5a909c 100644 --- a/src/util/tree.js +++ b/src/util/tree.js @@ -1,12 +1,11 @@ 'use strict' -const setImmediate = require('async/setImmediate') const SmartBuffer = require('smart-buffer').SmartBuffer const gitUtil = require('./util') exports = module.exports -exports.serialize = (dagNode, callback) => { +exports.serialize = (dagNode) => { let entries = [] Object.keys(dagNode).forEach((name) => { entries.push([name, dagNode[name]]) @@ -23,14 +22,14 @@ exports.serialize = (dagNode, callback) => { outBuf.writeString(buf.length.toString()) outBuf.writeUInt8(0) outBuf.writeBuffer(buf.toBuffer()) - setImmediate(() => callback(null, outBuf.toBuffer())) + return outBuf.toBuffer() } -exports.deserialize = (data, callback) => { +exports.deserialize = async (data) => { let res = {} let buf = SmartBuffer.fromBuffer(data, 'utf8') - for (;;) { + for (; ;) { let modeName = buf.readStringNT() if (modeName === '') { break @@ -39,11 +38,11 @@ exports.deserialize = (data, callback) => { let hash = buf.readBuffer(gitUtil.SHA1_LENGTH) let modNameMatched = modeName.match(/^(\d+) (.+)$/) if (!modNameMatched) { - setImmediate(() => callback(new Error('invalid file mode/name'))) + throw new Error('invalid file mode/name') } if (res[modNameMatched[2]]) { - setImmediate(() => callback(new Error('duplicate file in tree'))) + throw new Error('duplicate file in tree') } res[modNameMatched[2]] = { @@ -51,6 +50,5 @@ exports.deserialize = (data, callback) => { hash: { '/': gitUtil.shaToCid(hash) } } } - - setImmediate(() => callback(null, res)) + return res } diff --git a/test/parse.spec.js b/test/parse.spec.js index 17d7051..d93fc2e 100644 --- a/test/parse.spec.js +++ b/test/parse.spec.js @@ -1,5 +1,4 @@ /* eslint-env mocha */ -/* eslint max-nested-callbacks: ["error", 8] */ 'use strict' @@ -11,64 +10,57 @@ const loadFixture = require('aegir/fixtures') const zlib = require('zlib') const ipldGit = require('../src') const util = require('../src/util/util') -const waterfall = require('async/waterfall') const testObjectsJSON = require('./fixtures/objects.json') describe('utils', () => { describe('person line parsing', () => { - it('parses generic line', (done) => { + it('parses generic line', async () => { let info = util.parsePersonLine('Someone 123456 +0123') expect(info).to.exist() expect(info.name).to.equal('Someone') expect(info.email).to.equal('some@one.somewhere') expect(info.date).to.equal('123456 +0123') - done() }) - it('parses 3 segment name', (done) => { + it('parses 3 segment name', async () => { let info = util.parsePersonLine('So Me One 123456 +0123') expect(info).to.exist() expect(info.name).to.equal('So Me One') expect(info.email).to.equal('some@one.somewhere') expect(info.date).to.equal('123456 +0123') - done() }) - it('parses no name line', (done) => { + it('parses no name line', async () => { let info = util.parsePersonLine(' 123456 +0123') expect(info).to.exist() expect(info.name).to.not.exist() expect(info.email).to.equal('some@one.somewhere') expect(info.date).to.equal('123456 +0123') - done() }) - it('parses no name line with space in front', (done) => { + it('parses no name line with space in front', async () => { let info = util.parsePersonLine(' 123456 +0123') expect(info).to.exist() expect(info.name).to.not.exist() expect(info.email).to.equal('some@one.somewhere') expect(info.date).to.equal('123456 +0123') - done() }) - it('parses line with nonstandard info', (done) => { + it('parses line with nonstandard info', async () => { let info = util.parsePersonLine('Some One & Other One 987654 +4321') expect(info).to.exist() expect(info.name).to.equal('Some One & Other One') expect(info.email).to.equal('some@one.somewhere, other@one.elsewhere') expect(info.date).to.equal('987654 +4321') - done() }) - it('parses line without date info', (done) => { + it('parses line without date info', async () => { let info = util.parsePersonLine('Someone ') expect(info).to.exist() expect(info.name).to.equal('Someone') expect(info.email).to.equal('some.one@some.where') expect(info.date).to.not.exist() - done() }) }) }) @@ -76,32 +68,23 @@ describe('utils', () => { describe('git object parsing', () => { let objects - before((done) => { + before(() => { objects = testObjectsJSON.map(o => [o, zlib.inflateSync(loadFixture('test/fixtures/' + o))]) - done() }) - it('is parsing and serializing properly', (done) => { - waterfall(objects.map((object) => { - return (cb) => { - ipldGit.util.deserialize(object[1], (err, node) => { - expect(err).to.not.exist() - expect(node).to.exist() + it('is parsing and serializing properly', async () => { + for (const object of objects) { + const node = await ipldGit.util.deserialize(object[1]) + expect(node).to.exist() - let expCid = util.shaToCid(Buffer.from(object[0], 'hex')) + let expCid = util.shaToCid(Buffer.from(object[0], 'hex')) - ipldGit.util.cid(node, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.exist() + const cid = await ipldGit.util.cid(node) + expect(cid).to.exist() - expect(cid.buffer.toString('hex')).to.equal(expCid.toString('hex'), 'expected ' + - object[0] + ', got ' + cid.toBaseEncodedString('base16') + ', objtype ' + - node._objtype + ', blob:' + Buffer.isBuffer(node)) - - cb(null) - }) - }) - } - }), done) + expect(cid.buffer.toString('hex')).to.equal(expCid.toString('hex'), 'expected ' + + object[0] + ', got ' + cid.toBaseEncodedString('base16') + ', objtype ' + + node._objtype + ', blob:' + Buffer.isBuffer(node)) + } }) }) diff --git a/test/resolver.spec.js b/test/resolver.spec.js index 66e409d..526fab7 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -1,4 +1,3 @@ -/* eslint max-nested-callbacks: ["error", 8] */ /* eslint-env mocha */ 'use strict' @@ -7,10 +6,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const waterfall = require('async/waterfall') -const parallel = require('async/parallel') const CID = require('cids') - const ipldGit = require('../src') const resolver = ipldGit.resolver @@ -20,7 +16,7 @@ describe('IPLD format resolver (local)', () => { let treeBlob let blobBlob - before((done) => { + before(async () => { const commitNode = { gitType: 'commit', tree: { '/': new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr').buffer }, @@ -66,246 +62,175 @@ describe('IPLD format resolver (local)', () => { } const blobNode = Buffer.from('626c6f62203800736f6d6564617461', 'hex') // blob 8\0somedata - - waterfall([ - (cb) => parallel([ - (cb) => ipldGit.util.serialize(commitNode, cb), - (cb) => ipldGit.util.serialize(tagNode, cb), - (cb) => ipldGit.util.serialize(treeNode, cb), - (cb) => ipldGit.util.serialize(blobNode, cb) - ], cb), - (blocks, cb) => { - commitBlob = blocks[0] - tagBlob = blocks[1] - treeBlob = blocks[2] - blobBlob = blocks[3] - cb() - } - ], done) + const blocks = await Promise.all([ + ipldGit.util.serialize(commitNode), + ipldGit.util.serialize(tagNode), + ipldGit.util.serialize(treeNode), + ipldGit.util.serialize(blobNode) + ]) + commitBlob = blocks[0] + tagBlob = blocks[1] + treeBlob = blocks[2] + blobBlob = blocks[3] }) describe('commit', () => { - it('resolver.tree', (done) => { - resolver.tree(commitBlob, (err, paths) => { - expect(err).to.not.exist() - - expect(paths).to.eql([ - 'message', - 'tree', - 'author/original', - 'author/name', - 'author/email', - 'author/date', - 'committer/original', - 'committer/name', - 'committer/email', - 'committer/date', - 'parents/0', - 'encoding' - ]) - - done() - }) + it('resolver.tree', async () => { + const paths = await resolver.tree(commitBlob) + expect(paths).to.eql([ + 'message', + 'tree', + 'author/original', + 'author/name', + 'author/email', + 'author/date', + 'committer/original', + 'committer/name', + 'committer/email', + 'committer/date', + 'parents/0', + 'encoding' + ]) }) - it('resolver.isLink with valid Link', (done) => { - resolver.isLink(commitBlob, 'tree', (err, link) => { - expect(err).to.not.exist() - const linkCID = new CID(link['/']) - expect(CID.isCID(linkCID)).to.equal(true) - done() - }) + it('resolver.isLink with valid Link', async () => { + const link = await resolver.isLink(commitBlob, 'tree') + const linkCID = new CID(link['/']) + expect(CID.isCID(linkCID)).to.equal(true) }) - it('resolver.isLink with invalid Link', (done) => { - resolver.isLink(commitBlob, '', (err, link) => { - expect(err).to.not.exist() - expect(link).to.equal(false) - done() - }) + it('resolver.isLink with invalid Link', async () => { + const link = await resolver.isLink(commitBlob, '') + expect(link).to.equal(false) }) describe('resolver.resolve', () => { - it('path within scope', (done) => { - resolver.resolve(commitBlob, 'message', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.equal('Encoded\n') - done() - }) + it('path within scope', async () => { + const result = await resolver.resolve(commitBlob, 'message') + expect(result.value).to.equal('Encoded\n') }) - it('path within scope, but nested', (done) => { - resolver.resolve(commitBlob, 'author/name', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.equal('John Doe') - done() - }) + it('path within scope, but nested', async () => { + const result = await resolver.resolve(commitBlob, 'author/name') + expect(result.value).to.equal('John Doe') }) - it('path out of scope', (done) => { - resolver.resolve(commitBlob, 'tree/foo/hash/bar/mode', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.eql({ - '/': new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr').buffer - }) - expect(result.remainderPath).to.equal('foo/hash/bar/mode') - done() + it('path out of scope', async () => { + const result = await resolver.resolve(commitBlob, 'tree/foo/hash/bar/mode') + expect(result.value).to.eql({ + '/': new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr').buffer }) + expect(result.remainderPath).to.equal('foo/hash/bar/mode') }) }) }) describe('tag', () => { - it('resolver.tree', (done) => { - resolver.tree(tagBlob, (err, paths) => { - expect(err).to.not.exist() - - expect(paths).to.eql([ - 'object', - 'type', - 'tag', - 'message', - 'tagger/original', - 'tagger/name', - 'tagger/email', - 'tagger/date' - ]) - - done() - }) + it('resolver.tree', async () => { + const paths = await resolver.tree(tagBlob) + expect(paths).to.eql([ + 'object', + 'type', + 'tag', + 'message', + 'tagger/original', + 'tagger/name', + 'tagger/email', + 'tagger/date' + ]) }) - it('resolver.isLink with valid Link', (done) => { - resolver.isLink(tagBlob, 'object', (err, link) => { - expect(err).to.not.exist() - const linkCID = new CID(link['/']) - expect(CID.isCID(linkCID)).to.equal(true) - done() - }) + it('resolver.isLink with valid Link', async () => { + const link = await resolver.isLink(tagBlob, 'object') + const linkCID = new CID(link['/']) + expect(CID.isCID(linkCID)).to.equal(true) }) + }) - it('resolver.isLink with invalid Link', (done) => { - resolver.isLink(tagBlob, '', (err, link) => { - expect(err).to.not.exist() - expect(link).to.equal(false) - done() - }) - }) + it('resolver.isLink with invalid Link', async () => { + const link = await resolver.isLink(tagBlob, '') + expect(link).to.equal(false) + }) - describe('resolver.resolve', () => { - it('path within scope', (done) => { - resolver.resolve(tagBlob, 'message', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.equal('A message\n') - done() - }) - }) + describe('resolver.resolve', () => { + it('path within scope', async () => { + const result = await resolver.resolve(tagBlob, 'message') + expect(result.value).to.equal('A message\n') + }) - it('path within scope, but nested', (done) => { - resolver.resolve(tagBlob, 'tagger/name', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.equal('John Doe') - done() - }) - }) + it('path within scope, but nested', async () => { + const result = await resolver.resolve(tagBlob, 'tagger/name') + expect(result.value).to.equal('John Doe') + }) - it('path out of scope', (done) => { - resolver.resolve(tagBlob, 'object/tree/foo/mode', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.eql({ - '/': new CID('z8mWaHQaEAKd5KMRNU3npB3saSZmhFh3e').buffer - }) - expect(result.remainderPath).to.equal('tree/foo/mode') - done() - }) + it('path out of scope', async () => { + const result = await resolver.resolve(tagBlob, 'object/tree/foo/mode') + expect(result.value).to.eql({ + '/': new CID('z8mWaHQaEAKd5KMRNU3npB3saSZmhFh3e').buffer }) + expect(result.remainderPath).to.equal('tree/foo/mode') }) }) describe('tree', () => { - it('resolver.tree', (done) => { - resolver.tree(treeBlob, (err, paths) => { - expect(err).to.not.exist() - - expect(paths).to.eql([ - 'somedir', - 'somedir/hash', - 'somedir/mode', - 'somefile', - 'somefile/hash', - 'somefile/mode' - ]) - - done() - }) + it('resolver.tree', async () => { + const paths = await resolver.tree(treeBlob) + expect(paths).to.eql([ + 'somedir', + 'somedir/hash', + 'somedir/mode', + 'somefile', + 'somefile/hash', + 'somefile/mode' + ]) }) - it('resolver.isLink with valid Link', (done) => { - resolver.isLink(treeBlob, 'somefile/hash', (err, link) => { - expect(err).to.not.exist() - const linkCID = new CID(link['/']) - expect(CID.isCID(linkCID)).to.equal(true) - done() - }) + it('resolver.isLink with valid Link', async () => { + const link = await resolver.isLink(treeBlob, 'somefile/hash') + const linkCID = new CID(link['/']) + expect(CID.isCID(linkCID)).to.equal(true) }) - it('resolver.isLink with invalid Link', (done) => { - resolver.isLink(treeBlob, '', (err, link) => { - expect(err).to.not.exist() - expect(link).to.equal(false) - done() - }) + it('resolver.isLink with invalid Link', async () => { + const link = await resolver.isLink(treeBlob, '') + expect(link).to.equal(false) }) describe('resolver.resolve', () => { - it('path within scope, nested', (done) => { - resolver.resolve(treeBlob, 'somedir/mode', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.equal('40000') - done() - }) + it('path within scope, nested', async () => { + const result = await resolver.resolve(treeBlob, 'somedir/mode') + expect(result.value).to.equal('40000') }) - it('path out of scope', (done) => { - resolver.resolve(treeBlob, 'somedir/hash/subfile/mode', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.eql({ - '/': new CID('z8mWaFY1zpiZSXTBrz8i6A3o9vNvAs2CH').buffer - }) - expect(result.remainderPath).to.equal('subfile/mode') - done() + it('path out of scope', async () => { + const result = await resolver.resolve(treeBlob, 'somedir/hash/subfile/mode') + expect(result.value).to.eql({ + '/': new CID('z8mWaFY1zpiZSXTBrz8i6A3o9vNvAs2CH').buffer }) + expect(result.remainderPath).to.equal('subfile/mode') }) }) }) describe('blob', () => { - it('resolver.tree', (done) => { - resolver.tree(blobBlob, (err, paths) => { - expect(err).to.not.exist() - expect(paths).to.eql([]) - done() - }) + it('resolver.tree', async () => { + const paths = await resolver.tree(blobBlob) + expect(paths).to.eql([]) }) - it('resolver.isLink with invalid Link', (done) => { - resolver.isLink(treeBlob, '', (err, link) => { - expect(err).to.not.exist() - expect(link).to.equal(false) - done() - }) + it('resolver.isLink with invalid Link', async () => { + const link = await resolver.isLink(treeBlob, '') + expect(link).to.equal(false) }) }) -}) -describe('IPLD format resolver API properties', () => { - it('should have `multicodec` defined correctly', (done) => { - expect(resolver.multicodec).to.equal('git-raw') - done() - }) + describe('IPLD format resolver API properties', () => { + it('should have `multicodec` defined correctly', async () => { + expect(resolver.multicodec).to.equal('git-raw') + }) - it('should have `defaultHashAlg` defined correctly', (done) => { - expect(resolver.defaultHashAlg).to.equal('sha1') - done() + it('should have `defaultHashAlg` defined correctly', async () => { + expect(resolver.defaultHashAlg).to.equal('sha1') + }) }) }) diff --git a/test/util.spec.js b/test/util.spec.js index a222167..1481b87 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -23,46 +23,38 @@ describe('IPLD format util', () => { message: 'A message\n' } - it('.serialize and .deserialize', (done) => { - ipldGit.util.serialize(tagNode, (err, serialized) => { - expect(err).to.not.exist() - expect(Buffer.isBuffer(serialized)).to.equal(true) - ipldGit.util.deserialize(serialized, (err, deserialized) => { - expect(err).to.not.exist() - expect(tagNode).to.eql(deserialized) - done() - }) - }) + it('.serialize and .deserialize', async () => { + const serialized = await ipldGit.util.serialize(tagNode) + expect(Buffer.isBuffer(serialized)).to.equal(true) + const deserialized = await ipldGit.util.deserialize(serialized) + expect(tagNode).to.eql(deserialized) }) - it('.cid', (done) => { - ipldGit.util.cid(tagNode, (err, cid) => { - expect(err).to.not.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('git-raw') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha1') - done() - }) + it('.cid', async () => { + const cid = await ipldGit.util.cid(tagNode) + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('git-raw') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha1') }) - it('.cid with options', (done) => { - ipldGit.util.cid(tagNode, { hashAlg: 'sha3-512' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid.version).to.equal(1) - expect(cid.codec).to.equal('git-raw') - expect(cid.multihash).to.exist() - const mh = multihash.decode(cid.multihash) - expect(mh.name).to.equal('sha3-512') - done() - }) + it('.cid with options', async () => { + const cid = await ipldGit.util.cid(tagNode, { hashAlg: 'sha3-512' }) + expect(cid.version).to.equal(1) + expect(cid.codec).to.equal('git-raw') + expect(cid.multihash).to.exist() + const mh = multihash.decode(cid.multihash) + expect(mh.name).to.equal('sha3-512') }) - it('.cid errors unknown hashAlg', (done) => { - ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }, (err, cid) => { + it('.cid errors unknown hashAlg', async () => { + try { + await ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }) + } catch (err) { expect(err).to.exist() - done() - }) + return + } + throw new Error('Error did not exist') }) })