From 43148150ec65d143481f6fab6e78bae461b8d745 Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 21 Oct 2016 13:27:50 +0100 Subject: [PATCH 1/4] feat: support new CID block API --- package.json | 4 ++-- src/api/block.js | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 499e3f32e..ac1777e5f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "ipfs-api", "version": "9.0.0", "description": "A client library for the IPFS HTTP API. Follows interface-ipfs-core spec", - "main": "lib/index.js", + "main": "src/index.js", "jsnext:main": "src/index.js", "scripts": { "test": "node --max_old_space_size=4096 node_modules/.bin/gulp test:node", @@ -103,4 +103,4 @@ "url": "https://github.com/ipfs/js-ipfs-api/issues" }, "homepage": "https://github.com/ipfs/js-ipfs-api" -} \ No newline at end of file +} diff --git a/src/api/block.js b/src/api/block.js index e7458c023..41c880408 100644 --- a/src/api/block.js +++ b/src/api/block.js @@ -3,14 +3,21 @@ const promisify = require('promisify-es6') const bl = require('bl') const Block = require('ipfs-block') +const multihash = require('multihashes') +const CID = require('cids') module.exports = (send) => { return { get: promisify((args, opts, callback) => { + // TODO this needs to be adjusted with the new go-ipfs http-api + if (args && CID.isCID(args)) { + args = multihash.toB58String(args.multihash) + } if (typeof (opts) === 'function') { callback = opts opts = {} } + return send({ path: 'block/get', args: args, @@ -32,6 +39,11 @@ module.exports = (send) => { }) }), stat: promisify((args, opts, callback) => { + // TODO this needs to be adjusted with the new go-ipfs http-api + if (args && CID.isCID(args)) { + args = multihash.toB58String(args.multihash) + } + if (typeof (opts) === 'function') { callback = opts opts = {} @@ -50,7 +62,13 @@ module.exports = (send) => { }) }) }), - put: promisify((block, callback) => { + put: promisify((block, cid, callback) => { + // TODO this needs to be adjusted with the new go-ipfs http-api + if (typeof cid === 'function') { + callback = cid + cid = {} + } + if (Array.isArray(block)) { const err = new Error('block.put() only accepts 1 file') return callback(err) From 813ab69beaa7e5b8bd5a682c839c2454863c457b Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 27 Oct 2016 12:41:51 +0200 Subject: [PATCH 2/4] chore: update dependencies --- package.json | 22 +++++++++++----------- src/api/object.js | 5 +++-- src/get-dagnode.js | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index ac1777e5f..ae2a60f07 100644 --- a/package.json +++ b/package.json @@ -17,15 +17,15 @@ "coverage-publish": "aegir-coverage publish" }, "dependencies": { - "async": "^2.0.1", - "babel-runtime": "^6.11.6", + "async": "^2.1.2", + "babel-runtime": "^6.18.0", "bl": "^1.1.2", "bs58": "^3.0.0", "detect-node": "^2.0.3", "flatmap": "0.0.3", - "glob": "^7.0.5", - "ipfs-block": "^0.3.0", - "ipfs-merkle-dag": "^0.7.1", + "glob": "^7.1.1", + "ipfs-block": "^0.4.0", + "ipld-dag-pb": "^0.1.2", "is-ipfs": "^0.2.0", "isstream": "^0.1.2", "multiaddr": "^2.0.2", @@ -34,7 +34,7 @@ "peer-id": "^0.7.0", "peer-info": "^0.7.1", "promisify-es6": "^1.0.1", - "qs": "^6.2.1", + "qs": "^6.3.0", "streamifier": "^0.1.1", "tar-stream": "^1.5.2", "wreck": "^10.0.0" @@ -47,17 +47,17 @@ "url": "https://github.com/ipfs/js-ipfs-api" }, "devDependencies": { - "aegir": "^8.0.0", + "aegir": "^8.1.2", "chai": "^3.5.0", "gulp": "^3.9.1", - "hapi": "^15.0.2", + "hapi": "^15.2.0", "interface-ipfs-core": "^0.15.0", "ipfsd-ctl": "^0.16.0", "pre-commit": "^1.1.3", - "socket.io": "^1.4.8", - "socket.io-client": "^1.4.8", + "socket.io": "^1.5.1", + "socket.io-client": "^1.5.1", "stream-equal": "^0.1.8", - "stream-http": "^2.3.1" + "stream-http": "^2.4.0" }, "pre-commit": [ "lint", diff --git a/src/api/object.js b/src/api/object.js index 787b36276..d795c40cd 100644 --- a/src/api/object.js +++ b/src/api/object.js @@ -1,7 +1,8 @@ 'use strict' -const DAGNode = require('ipfs-merkle-dag').DAGNode -const DAGLink = require('ipfs-merkle-dag').DAGLink +const dagPB = require('ipld-dag-pb') +const DAGNode = dagPB.DAGNode +const DAGLink = dagPB.DAGLink const promisify = require('promisify-es6') const bs58 = require('bs58') const bl = require('bl') diff --git a/src/get-dagnode.js b/src/get-dagnode.js index 2d9e937e2..3c0896ecf 100644 --- a/src/get-dagnode.js +++ b/src/get-dagnode.js @@ -1,6 +1,6 @@ 'use strict' -const DAGNode = require('ipfs-merkle-dag').DAGNode +const DAGNode = require('ipld-dag-pb').DAGNode const bl = require('bl') const parallel = require('async/parallel') From ad96d228906d08310dc0ef804c42f751f8854600 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 27 Oct 2016 13:08:53 +0200 Subject: [PATCH 3/4] feat: update files to use async dag-pb api --- src/add-to-dagnode-transform.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/add-to-dagnode-transform.js b/src/add-to-dagnode-transform.js index 32de7f698..5f2e65785 100644 --- a/src/add-to-dagnode-transform.js +++ b/src/add-to-dagnode-transform.js @@ -4,24 +4,29 @@ const map = require('async/map') const getDagNode = require('./get-dagnode') // transform { Hash: '...' } objects into { path: 'string', node: DAGNode } -module.exports = function (err, res, send, done) { +module.exports = (err, res, send, done) => { if (err) { return done(err) } - map(res, function map (entry, next) { - getDagNode(send, entry.Hash, function (err, node) { + map(res, (entry, next) => { + getDagNode(send, entry.Hash, (err, node) => { if (err) { return next(err) } - var obj = { - path: entry.Name, - hash: entry.Hash, - size: node.size() - } - next(null, obj) + node.size((err, size) => { + if (err) { + return next(err) + } + const obj = { + path: entry.Name, + hash: entry.Hash, + size: size + } + next(null, obj) + }) }) - }, function (err, res) { + }, (err, res) => { done(err, res) }) } From baa72d06fb5e01f86cf489bf8ea8a09bc4ac6ccd Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 27 Oct 2016 14:39:26 +0200 Subject: [PATCH 4/4] migrate object api to async dag-pb --- package.json | 6 +++--- src/api/object.js | 44 +++++++++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index ae2a60f07..713266513 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "ipfs-api", "version": "9.0.0", "description": "A client library for the IPFS HTTP API. Follows interface-ipfs-core spec", - "main": "src/index.js", + "main": "lib/index.js", "jsnext:main": "src/index.js", "scripts": { "test": "node --max_old_space_size=4096 node_modules/.bin/gulp test:node", @@ -25,7 +25,7 @@ "flatmap": "0.0.3", "glob": "^7.1.1", "ipfs-block": "^0.4.0", - "ipld-dag-pb": "^0.1.2", + "ipld-dag-pb": "^0.1.3", "is-ipfs": "^0.2.0", "isstream": "^0.1.2", "multiaddr": "^2.0.2", @@ -51,7 +51,7 @@ "chai": "^3.5.0", "gulp": "^3.9.1", "hapi": "^15.2.0", - "interface-ipfs-core": "^0.15.0", + "interface-ipfs-core": "^0.16.1", "ipfsd-ctl": "^0.16.0", "pre-commit": "^1.1.3", "socket.io": "^1.5.1", diff --git a/src/api/object.js b/src/api/object.js index d795c40cd..4841c8330 100644 --- a/src/api/object.js +++ b/src/api/object.js @@ -94,21 +94,37 @@ module.exports = (send) => { obj = JSON.parse(obj.toString()) } } + let node + if (obj.multihash) { node = obj } else if (options.enc === 'protobuf') { - node = new DAGNode() - node.unMarshal(obj) + dagPB.util.deserialize(obj, (err, _node) => { + if (err) { + return callback(err) + } + node = _node + next() + }) + return } else { node = new DAGNode(obj.Data, obj.Links) } - - if (node.toJSON().Hash !== result.Hash) { - return callback(new Error('Stored object was different from constructed object')) + next() + + function next () { + node.toJSON((err, nodeJSON) => { + if (err) { + return callback(err) + } + if (nodeJSON.Hash !== result.Hash) { + return callback(new Error('Stored object was different from constructed object')) + } + + callback(null, node) + }) } - - callback(null, node) }) }), data: promisify((multihash, options, callback) => { @@ -201,13 +217,19 @@ module.exports = (send) => { if (err) { return callback(err) } + const node = new DAGNode() + node.toJSON((err, nodeJSON) => { + if (err) { + return callback(err) + } - if (node.toJSON().Hash !== result.Hash) { - return callback(new Error('Stored object was different from constructed object')) - } + if (nodeJSON.Hash !== result.Hash) { + return callback(new Error('Stored object was different from constructed object')) + } - callback(null, node) + callback(null, node) + }) }) }), patch: {