From bc6ff32f86abbb2e73d0f958e8f2ecfc2a4ef3c5 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 13 Mar 2019 19:25:03 +0000 Subject: [PATCH 1/4] feat: add support for js-ipfs dag api and also some tests --- package.json | 1 + src/dag/get.js | 7 +++-- test/dag.spec.js | 69 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index c4ba1493e..d5204f783 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "ipfs-utils": "~0.0.3", "ipld-dag-cbor": "~0.15.0", "ipld-dag-pb": "~0.17.3", + "ipld-raw": "^4.0.0", "is-ipfs": "~0.6.1", "is-pull-stream": "0.0.0", "is-stream": "^2.0.0", diff --git a/src/dag/get.js b/src/dag/get.js index c46ada33c..d0a1dac13 100644 --- a/src/dag/get.js +++ b/src/dag/get.js @@ -2,6 +2,7 @@ const dagPB = require('ipld-dag-pb') const dagCBOR = require('ipld-dag-cbor') +const raw = require('ipld-raw') const promisify = require('promisify-es6') const CID = require('cids') const waterfall = require('async/waterfall') @@ -9,7 +10,8 @@ const block = require('../block') const resolvers = { 'dag-cbor': dagCBOR.resolver, - 'dag-pb': dagPB.resolver + 'dag-pb': dagPB.resolver, + raw: raw.resolver } module.exports = (send) => { @@ -48,7 +50,7 @@ module.exports = (send) => { const dagResolver = resolvers[ipfsBlock.cid.codec] if (!dagResolver) { - const error = new Error('ipfs-http-client is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec') + const error = new Error(`Missing IPLD format "${ipfsBlock.cid.codec}"`) error.missingMulticodec = ipfsBlock.cid.codec return cb(error) } @@ -59,6 +61,7 @@ module.exports = (send) => { } catch (err) { return cb(err) } + cb(null, res) } ], callback) diff --git a/test/dag.spec.js b/test/dag.spec.js index 1b0cfc683..66106ab1a 100644 --- a/test/dag.spec.js +++ b/test/dag.spec.js @@ -68,16 +68,75 @@ describe('.dag', function () { }) }) - it('should callback with error when missing DAG resolver for raw multicodec', (done) => { - ipfs.dag.put(Buffer.from([0, 1, 2, 3]), { - // CIDv1 with multicodec = raw - cid: new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy') + it('should be able to get part of a dag-cbor node', (done) => { + const cbor = { + foo: 'dag-cbor-bar' + } + ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => { + expect(err).to.not.exist() + expect(cid.codec).to.equal('dag-cbor') + cid = cid.toBaseEncodedString('base32') + expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce') + ipfs.dag.get(cid, 'foo', (err, result) => { + expect(err).to.not.exist() + expect(result.value).to.equal('dag-cbor-bar') + done() + }) + }) + }) + + it('should be able to traverse from one dag-cbor node to another', (done) => { + const cbor1 = { + foo: 'dag-cbor-bar' + } + + ipfs.dag.put(cbor1, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid1) => { + expect(err).to.not.exist() + + const cbor2 = { + other: { + '/': cid1.toBaseEncodedString('base32') + } + } + + ipfs.dag.put(cbor2, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid2) => { + expect(err).to.not.exist() + + ipfs.dag.get(cid2, 'other/foo', (err, result) => { + expect(err).to.not.exist() + expect(result.value).to.equal('dag-cbor-bar') + done() + }) + }) + }) + }) + + it('should be able to put and get a DAG node with format raw', (done) => { + const buf = Buffer.from([0, 1, 2, 3]) + + ipfs.dag.put(buf, { + format: 'raw', + hashAlg: 'sha2-256' }, (err, cid) => { expect(err).to.not.exist() ipfs.dag.get(cid, (err, result) => { + expect(err).to.not.exist() + expect(result.value).to.deep.equal(buf) + done() + }) + }) + }) + + it('should callback with error when missing DAG resolver for multicodec from requested CID', (done) => { + ipfs.block.put(Buffer.from([0, 1, 2, 3]), { + cid: new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr') + }, (err, block) => { + expect(err).to.not.exist() + + ipfs.dag.get(block.cid, (err, result) => { expect(result).to.not.exist() - expect(err.message).to.equal('ipfs-http-client is missing DAG resolver for "raw" multicodec') + expect(err.message).to.equal('Missing IPLD format "git-raw"') done() }) }) From 056abe4b302d95f53a633176a2cf34e2c5987767 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 19 Mar 2019 09:13:15 +0000 Subject: [PATCH 2/4] test: move tests to interface suite --- test/dag.spec.js | 60 ------------------------------------------------ 1 file changed, 60 deletions(-) diff --git a/test/dag.spec.js b/test/dag.spec.js index 66106ab1a..eb69a959c 100644 --- a/test/dag.spec.js +++ b/test/dag.spec.js @@ -68,66 +68,6 @@ describe('.dag', function () { }) }) - it('should be able to get part of a dag-cbor node', (done) => { - const cbor = { - foo: 'dag-cbor-bar' - } - ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid.codec).to.equal('dag-cbor') - cid = cid.toBaseEncodedString('base32') - expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce') - ipfs.dag.get(cid, 'foo', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.equal('dag-cbor-bar') - done() - }) - }) - }) - - it('should be able to traverse from one dag-cbor node to another', (done) => { - const cbor1 = { - foo: 'dag-cbor-bar' - } - - ipfs.dag.put(cbor1, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid1) => { - expect(err).to.not.exist() - - const cbor2 = { - other: { - '/': cid1.toBaseEncodedString('base32') - } - } - - ipfs.dag.put(cbor2, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid2) => { - expect(err).to.not.exist() - - ipfs.dag.get(cid2, 'other/foo', (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.equal('dag-cbor-bar') - done() - }) - }) - }) - }) - - it('should be able to put and get a DAG node with format raw', (done) => { - const buf = Buffer.from([0, 1, 2, 3]) - - ipfs.dag.put(buf, { - format: 'raw', - hashAlg: 'sha2-256' - }, (err, cid) => { - expect(err).to.not.exist() - - ipfs.dag.get(cid, (err, result) => { - expect(err).to.not.exist() - expect(result.value).to.deep.equal(buf) - done() - }) - }) - }) - it('should callback with error when missing DAG resolver for multicodec from requested CID', (done) => { ipfs.block.put(Buffer.from([0, 1, 2, 3]), { cid: new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr') From 1a72d9f26523b42e8b34dd29329e8108f18d878f Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 11 Jul 2019 11:30:31 +0100 Subject: [PATCH 3/4] chore: update interface-ipfs-core dep License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d5204f783..bc6eb9d7a 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "cross-env": "^5.2.0", "dirty-chai": "^2.0.1", "go-ipfs-dep": "~0.4.21", - "interface-ipfs-core": "~0.107.0", + "interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#test/add-dag-cbor-and-raw-tests", "ipfsd-ctl": "~0.43.0", "nock": "^10.0.2", "stream-equal": "^1.1.1" From 2cbf9bb242a3db3871a1e7fe95474756884640ca Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 11 Jul 2019 12:13:52 +0100 Subject: [PATCH 4/4] chore: update interface-ipfs-core License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bc6eb9d7a..7df0ab15e 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "cross-env": "^5.2.0", "dirty-chai": "^2.0.1", "go-ipfs-dep": "~0.4.21", - "interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#test/add-dag-cbor-and-raw-tests", + "interface-ipfs-core": "^0.107.1", "ipfsd-ctl": "~0.43.0", "nock": "^10.0.2", "stream-equal": "^1.1.1"