diff --git a/package.json b/package.json index c12949d..dc900ea 100644 --- a/package.json +++ b/package.json @@ -39,11 +39,12 @@ "devDependencies": { "aegir": "^18.0.2", "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "cids": "~0.5.5", "detect-node": "^2.0.4", "dirty-chai": "^2.0.1", "ipfs-unixfs-exporter": "~0.35.4", - "ipld": "~0.20.2", + "ipld": "git+https://github.com/ipld/js-ipld.git#new-api-impl", "ipld-in-memory": "^2.0.0", "multihashes": "~0.4.14", "pull-buffer-stream": "^1.0.1", @@ -60,6 +61,7 @@ "ipfs-unixfs": "~0.1.16", "ipld-dag-pb": "~0.15.2", "left-pad": "^1.3.0", + "multicodec": "~0.5.0", "multihashing-async": "~0.5.1", "pull-batch": "^1.0.0", "pull-pair": "^1.1.0", diff --git a/src/utils/persist.js b/src/utils/persist.js index 5fa987c..69d898a 100644 --- a/src/utils/persist.js +++ b/src/utils/persist.js @@ -1,10 +1,9 @@ 'use strict' const { - util: { - cid - } + util } = require('ipld-dag-pb') +const multicodec = require('multicodec') const defaultOptions = { cidVersion: 0, @@ -27,7 +26,7 @@ const persist = (node, ipld, options, callback) => { } if (options.onlyHash) { - return cid(node, { + return util.cid(node, { version: cidVersion, hashAlg: hashAlg }, (err, cid) => { @@ -38,16 +37,28 @@ const persist = (node, ipld, options, callback) => { }) } - ipld.put(node, { + // The IPLD expects the format and hashAlg as constants + if (typeof codec === 'string') { + const constantName = codec.toUpperCase().replace(/-/g, '_') + codec = multicodec[constantName] + } + if (typeof hashAlg === 'string') { + const constantName = hashAlg.toUpperCase().replace(/-/g, '_') + hashAlg = multicodec[constantName] + } + + const result = ipld.put([node], { version: cidVersion, hashAlg: hashAlg, format: codec - }, (error, cid) => { - callback(error, { + }) + result.first().then( + (cid) => callback(null, { cid, node - }) - }) + }), + (error) => callback(error) + ) } module.exports = persist diff --git a/test/builder-only-hash.spec.js b/test/builder-only-hash.spec.js index 42fc4c8..4177236 100644 --- a/test/builder-only-hash.spec.js +++ b/test/builder-only-hash.spec.js @@ -2,6 +2,7 @@ 'use strict' const chai = require('chai') +chai.use(require('chai-as-promised')) chai.use(require('dirty-chai')) const expect = chai.expect const pull = require('pull-stream/pull') @@ -27,16 +28,14 @@ describe('builder: onlyHash', () => { }) it('will only chunk and hash if passed an "onlyHash" option', (done) => { - const onCollected = (err, nodes) => { + const onCollected = async (err, nodes) => { if (err) return done(err) const node = nodes[0] expect(node).to.exist() - ipld.get(new CID(node.multihash), (err, res) => { - expect(err).to.exist() - done() - }) + const result = ipld.get([new CID(node.multihash)]) + expect(result.first()).to.be.rejectedWith('Not Found').notify(done) } const content = String(Math.random() + Date.now()) diff --git a/test/builder.spec.js b/test/builder.spec.js index 4a3c68b..79f9d3a 100644 --- a/test/builder.spec.js +++ b/test/builder.spec.js @@ -52,12 +52,15 @@ describe('builder', () => { expect(mh.decode(cid.multihash).name).to.equal(hashAlg) // Fetch using hashAlg encoded multihash - ipld.get(cid, (err, res) => { - if (err) return cb(err) - const content = UnixFS.unmarshal(res.value.data).data - expect(content.equals(inputFile.content)).to.be.true() - cb() - }) + const result = ipld.get([cid]) + result.first().then( + (node) => { + const content = UnixFS.unmarshal(node.data).data + expect(content.equals(inputFile.content)).to.be.true() + cb() + }, + (error) => cb(error) + ) } pull( @@ -124,12 +127,15 @@ describe('builder', () => { expect(mh.decode(cid.multihash).name).to.equal(hashAlg) // Fetch using hashAlg encoded multihash - ipld.get(cid, (err, res) => { - if (err) return cb(err) - const meta = UnixFS.unmarshal(res.value.data) - expect(meta.type).to.equal('directory') - cb() - }) + const result = ipld.get([cid]) + result.first().then( + (node) => { + const meta = UnixFS.unmarshal(node.data) + expect(meta.type).to.equal('directory') + cb() + }, + (error) => cb(error) + ) } pull( diff --git a/test/helpers/collect-leaf-cids.js b/test/helpers/collect-leaf-cids.js index 47cec1c..9ab855f 100644 --- a/test/helpers/collect-leaf-cids.js +++ b/test/helpers/collect-leaf-cids.js @@ -15,9 +15,10 @@ module.exports = (ipld, multihash, callback) => { return pull( values([cid]), asyncMap((cid, callback) => { - ipld.get(cid, (error, result) => { - callback(error, !error && result.value) - }) + ipld.get([cid]).first().then( + (node) => callback(null, node), + (error) => callback(error) + ) }), asyncMap((node, callback) => { if (!node.links) { diff --git a/test/importer.spec.js b/test/importer.spec.js index 8a8fc1f..9d6177a 100644 --- a/test/importer.spec.js +++ b/test/importer.spec.js @@ -125,9 +125,8 @@ const checkLeafNodeTypes = (ipld, options, expected, done) => { importer(ipld, options), collect(cb) ), - (files, cb) => ipld.get(new CID(files[0].multihash), cb), - (result, cb) => { - const node = result.value + async (files) => ipld.get([new CID(files[0].multihash)]).first(), + (node, cb) => { const meta = UnixFs.unmarshal(node.data) expect(meta.type).to.equal('file') @@ -137,9 +136,8 @@ const checkLeafNodeTypes = (ipld, options, expected, done) => { node.links.map(link => { return (done) => { waterfall([ - (next) => ipld.get(link.cid, next), - (result, next) => { - const node = result.value + async () => ipld.get([link.cid]).first(), + (node, next) => { const meta = UnixFs.unmarshal(node.data) expect(meta.type).to.equal(expected) @@ -163,9 +161,8 @@ const checkNodeLinks = (ipld, options, expected, done) => { importer(ipld, options), collect(cb) ), - (files, cb) => ipld.get(new CID(files[0].multihash), cb), - (result, cb) => { - const node = result.value + async (files) => ipld.get([new CID(files[0].multihash)]).first(), + (node, cb) => { const meta = UnixFs.unmarshal(node.data) expect(meta.type).to.equal('file') @@ -587,10 +584,8 @@ strategies.forEach((strategy) => { const file = files[0] expect(file).to.exist() - ipld.get(new CID(file.multihash), (err) => { - expect(err).to.exist() - done() - }) + const result = ipld.get([new CID(file.multihash)]) + expect(result.first()).to.be.rejectedWith('Not Found').notify(done) } pull( @@ -658,7 +653,11 @@ strategies.forEach((strategy) => { // Just check the intermediate directory can be retrieved if (!inputFile) { - return ipld.get(cid, cb) + const result = ipld.get([new CID(file.multihash)]) + return result.first().then( + (_cid) => cb(), + (_error) => cb() + ) } // Check the imported content is correct