From a423d7f4d8aef0a39e297dfb114b09b7e7e56f05 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 21 May 2019 10:06:44 +0100 Subject: [PATCH] chore: update ipld formats (#1010) BREAKING CHANGE: The default string encoding for version 1 CIDs has changed to `base32`. IPLD formats have been updated to the latest versions. IPLD nodes returned by `ipfs.dag` and `ipfs.object` commands have significant breaking changes. If you are using these commands in your application you are likely to encounter the following changes to `dag-pb` nodes (the default node type that IPFS creates): * `DAGNode` properties have been renamed as follows: * `data` => `Data` * `links` => `Links` * `size` => `size` (Note: no change) * `DAGLink` properties have been renamed as follows: * `cid` => `Hash` * `name` => `Name` * `size` => `Tsize` See CHANGELOGs for each IPLD format for it's respective changes, you can read more about the [`dag-pb` changes in the CHANGELOG](https://github.com/ipld/js-ipld-dag-pb/blob/master) License: MIT Signed-off-by: Alan Shaw --- package.json | 14 +++++------ src/dag/get.js | 13 +++++++--- src/dag/put.js | 54 +++++++++++++++++++++++------------------- src/object/addLink.js | 15 ++++++------ src/object/get.js | 20 ++++------------ src/object/links.js | 7 +++--- src/object/put.js | 12 +++++----- src/object/rmLink.js | 2 +- test/dag.spec.js | 26 ++++++++++---------- test/interface.spec.js | 5 ++++ 10 files changed, 86 insertions(+), 82 deletions(-) diff --git a/package.json b/package.json index 01cbaeefe..52a4a4ed6 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "bl": "^3.0.0", "bs58": "^4.0.1", "buffer": "^5.2.1", - "cids": "~0.5.8", + "cids": "~0.7.1", "concat-stream": "github:hugomrdias/concat-stream#feat/smaller", "debug": "^4.1.0", "detect-node": "^2.0.4", @@ -39,10 +39,10 @@ "err-code": "^1.1.2", "flatmap": "0.0.3", "glob": "^7.1.3", - "ipfs-block": "~0.8.0", - "ipld-dag-cbor": "~0.13.1", - "ipld-dag-pb": "~0.15.3", - "is-ipfs": "~0.6.0", + "ipfs-block": "~0.8.1", + "ipld-dag-cbor": "~0.15.0", + "ipld-dag-pb": "~0.17.3", + "is-ipfs": "~0.6.1", "is-pull-stream": "0.0.0", "is-stream": "^2.0.0", "iso-stream-http": "~0.1.2", @@ -54,7 +54,7 @@ "lru-cache": "^5.1.1", "multiaddr": "^6.0.6", "multibase": "~0.6.0", - "multicodec": "~0.5.0", + "multicodec": "~0.5.1", "multihashes": "~0.4.14", "ndjson": "github:hugomrdias/ndjson#feat/readable-stream3", "once": "^1.4.0", @@ -86,7 +86,7 @@ "cross-env": "^5.2.0", "dirty-chai": "^2.0.1", "go-ipfs-dep": "0.4.19", - "interface-ipfs-core": "~0.102.0", + "interface-ipfs-core": "~0.103.0", "ipfsd-ctl": "~0.42.0", "nock": "^10.0.2", "stream-equal": "^1.1.1" diff --git a/src/dag/get.js b/src/dag/get.js index dc4e46b4a..c46ada33c 100644 --- a/src/dag/get.js +++ b/src/dag/get.js @@ -46,13 +46,20 @@ module.exports = (send) => { }, (ipfsBlock, path, cb) => { const dagResolver = resolvers[ipfsBlock.cid.codec] + if (!dagResolver) { const error = new Error('ipfs-http-client is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec') error.missingMulticodec = ipfsBlock.cid.codec - cb(error) - return + return cb(error) + } + + let res + try { + res = dagResolver.resolve(ipfsBlock.data, path) + } catch (err) { + return cb(err) } - dagResolver.resolve(ipfsBlock.data, path, cb) + cb(null, res) } ], callback) }) diff --git a/src/dag/put.js b/src/dag/put.js index 636d39ae4..42d92b285 100644 --- a/src/dag/put.js +++ b/src/dag/put.js @@ -50,34 +50,38 @@ module.exports = (send) => { options = Object.assign(optionDefaults, options) - if (options.format === 'dag-cbor') { - dagCBOR.util.serialize(dagNode, finalize) - } else if (options.format === 'dag-pb') { - dagPB.util.serialize(dagNode, finalize) - } else { - // FIXME Hopefully already serialized...can we use IPLD to serialise instead? - finalize(null, dagNode) + let serialized + + try { + if (options.format === 'dag-cbor') { + serialized = dagCBOR.util.serialize(dagNode) + } else if (options.format === 'dag-pb') { + serialized = dagPB.util.serialize(dagNode) + } else { + // FIXME Hopefully already serialized...can we use IPLD to serialise instead? + serialized = dagNode + } + } catch (err) { + return callback(err) } - function finalize (err, serialized) { - if (err) { return callback(err) } - const sendOptions = { - qs: { - hash: options.hashAlg, - format: options.format, - 'input-enc': options.inputEnc - } + const sendOptions = { + qs: { + hash: options.hashAlg, + format: options.format, + 'input-enc': options.inputEnc } - sendOneFile(serialized, sendOptions, (err, result) => { - if (err) { - return callback(err) - } - if (result['Cid']) { - return callback(null, new CID(result['Cid']['/'])) - } else { - return callback(result) - } - }) } + + sendOneFile(serialized, sendOptions, (err, result) => { + if (err) { + return callback(err) + } + if (result['Cid']) { + return callback(null, new CID(result['Cid']['/'])) + } else { + return callback(result) + } + }) }) } diff --git a/src/object/addLink.js b/src/object/addLink.js index 75087578b..bda202bce 100644 --- a/src/object/addLink.js +++ b/src/object/addLink.js @@ -19,14 +19,13 @@ module.exports = (send) => { return callback(err) } - send({ - path: 'object/patch/add-link', - args: [ - cid.toString(), - dLink.name, - dLink.cid.toString() - ] - }, (err, result) => { + const args = [ + cid.toString(), + dLink.Name || dLink.name || null, + (dLink.Hash || dLink.cid || '').toString() || null + ] + + send({ path: 'object/patch/add-link', args }, (err, result) => { if (err) { return callback(err) } diff --git a/src/object/get.js b/src/object/get.js index a8d44db92..1450c3792 100644 --- a/src/object/get.js +++ b/src/object/get.js @@ -1,9 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode -const DAGLink = dagPB.DAGLink +const { DAGNode, DAGLink } = require('ipld-dag-pb') const CID = require('cids') const LRU = require('lru-cache') const lruOptions = { @@ -49,19 +47,11 @@ module.exports = (send) => { return callback(err) } - result.Data = Buffer.from(result.Data, 'base64') + const links = result.Links.map(l => new DAGLink(l.Name, l.Size, l.Hash)) + const node = DAGNode.create(Buffer.from(result.Data, 'base64'), links) - const links = result.Links.map((l) => { - return new DAGLink(l.Name, l.Size, l.Hash) - }) - - DAGNode.create(result.Data, links, (err, node) => { - if (err) { - return callback(err) - } - cache.set(cidB58Str, node) - callback(null, node) - }) + cache.set(cidB58Str, node) + callback(null, node) }) }) } diff --git a/src/object/links.js b/src/object/links.js index 41527d998..0b0366c81 100644 --- a/src/object/links.js +++ b/src/object/links.js @@ -1,8 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const dagPB = require('ipld-dag-pb') -const DAGLink = dagPB.DAGLink +const { DAGLink } = require('ipld-dag-pb') const CID = require('cids') const LRU = require('lru-cache') const lruOptions = { @@ -44,7 +43,9 @@ module.exports = (send) => { let links = [] if (result.Links) { - links = result.Links.map((l) => new DAGLink(l.Name, l.Size, l.Hash)) + links = result.Links.map((l) => { + return new DAGLink(l.Name, l.Size, l.Hash) + }) } callback(null, links) }) diff --git a/src/object/put.js b/src/object/put.js index 0b624f7f9..40b9ffac4 100644 --- a/src/object/put.js +++ b/src/object/put.js @@ -36,12 +36,12 @@ module.exports = (send) => { } } else if (DAGNode.isDAGNode(obj)) { tmpObj = { - Data: obj.data.toString(), - Links: obj.links.map((l) => { - const link = l.toJSON() - link.hash = link.cid - return link - }) + Data: obj.Data.toString(), + Links: obj.Links.map(l => ({ + Name: l.Name, + Hash: l.Hash.toString(), + Size: l.Tsize + })) } } else if (typeof obj === 'object') { tmpObj.Data = obj.Data.toString() diff --git a/src/object/rmLink.js b/src/object/rmLink.js index 4726bb216..db699feeb 100644 --- a/src/object/rmLink.js +++ b/src/object/rmLink.js @@ -23,7 +23,7 @@ module.exports = (send) => { path: 'object/patch/rm-link', args: [ cid.toString(), - dLink.name + dLink.Name || dLink.name || null ] }, (err, result) => { if (err) { diff --git a/test/dag.spec.js b/test/dag.spec.js index 0b1a2f314..1b0cfc683 100644 --- a/test/dag.spec.js +++ b/test/dag.spec.js @@ -8,8 +8,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const series = require('async/series') -const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode +const { DAGNode } = require('ipld-dag-pb') const CID = require('cids') const ipfsClient = require('../src') const f = require('./utils/factory') @@ -37,20 +36,19 @@ describe('.dag', function () { it('should be able to put and get a DAG node with format dag-pb', (done) => { const data = Buffer.from('some data') - DAGNode.create(data, (err, node) => { + const node = DAGNode.create(data) + + ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => { expect(err).to.not.exist() - ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => { + cid = cid.toV0() + expect(cid.codec).to.equal('dag-pb') + cid = cid.toBaseEncodedString('base58btc') + // expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u') + expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr') + ipfs.dag.get(cid, (err, result) => { expect(err).to.not.exist() - cid = cid.toV0() - expect(cid.codec).to.equal('dag-pb') - cid = cid.toBaseEncodedString('base58btc') - // expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u') - expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr') - ipfs.dag.get(cid, (err, result) => { - expect(err).to.not.exist() - expect(result.value.data).to.deep.equal(data) - done() - }) + expect(result.value.Data).to.deep.equal(data) + done() }) }) }) diff --git a/test/interface.spec.js b/test/interface.spec.js index a5eed428c..f960238d7 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -173,6 +173,11 @@ describe('interface-ipfs-core tests', () => { isNode ? null : { name: 'should readable stream ls with a base58 encoded CID', reason: 'FIXME https://github.com/ipfs/js-ipfs-http-client/issues/339' + }, + // .refs + { + name: 'dag refs test', + reason: 'FIXME unskip when 0.4.21 is released' } ] })