From 1b0b22dc7b882300e881858c718edf9677000883 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 29 Oct 2016 22:05:18 +0100 Subject: [PATCH] feat: migrate cli to use new async DAGNode interface --- src/cli/commands/object/get.js | 10 +++-- src/cli/commands/object/new.js | 7 +++- src/cli/commands/object/patch/add-link.js | 42 ++++++++++++++------ src/cli/commands/object/patch/append-data.js | 8 +++- src/cli/commands/object/patch/rm-link.js | 9 ++++- src/cli/commands/object/patch/set-data.js | 8 +++- src/cli/commands/object/put.js | 8 +++- src/http-api/resources/object.js | 6 ++- test/http-api/ipfs-api/test-object.js | 1 + 9 files changed, 76 insertions(+), 23 deletions(-) diff --git a/src/cli/commands/object/get.js b/src/cli/commands/object/get.js index 59796e9821..30ee4b8147 100644 --- a/src/cli/commands/object/get.js +++ b/src/cli/commands/object/get.js @@ -23,9 +23,13 @@ module.exports = { throw err } - const res = node.toJSON() - res.Data = res.Data ? res.Data.toString() : '' - console.log(JSON.stringify(res)) + node.toJSON((err, nodeJSON) => { + if (err) { + throw err + } + nodeJSON.Data = nodeJSON.Data ? nodeJSON.Data.toString() : '' + console.log(JSON.stringify(nodeJSON)) + }) }) }) } diff --git a/src/cli/commands/object/new.js b/src/cli/commands/object/new.js index 4cb570497a..56440a66dd 100644 --- a/src/cli/commands/object/new.js +++ b/src/cli/commands/object/new.js @@ -23,7 +23,12 @@ module.exports = { throw err } - console.log(node.toJSON().Hash) + node.toJSON((err, nodeJSON) => { + if (err) { + throw err + } + console.log(nodeJSON.Hash) + }) }) }) } diff --git a/src/cli/commands/object/patch/add-link.js b/src/cli/commands/object/patch/add-link.js index 8ec24eebb8..fb8fb9d242 100644 --- a/src/cli/commands/object/patch/add-link.js +++ b/src/cli/commands/object/patch/add-link.js @@ -3,8 +3,8 @@ const utils = require('../../../utils') const debug = require('debug') const log = debug('cli:object') -const mDAG = require('ipfs-merkle-dag') -const DAGLink = mDAG.DAGLink +const dagPB = require('ipld-dag-pb') +const DAGLink = dagPB.DAGLink log.error = debug('cli:object:error') module.exports = { @@ -15,23 +15,41 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + utils.getIPFS(gotIPFS) + + function gotIPFS (err, ipfs) { if (err) { throw err } ipfs.object.get(argv.ref, {enc: 'base58'}).then((linkedObj) => { - const link = new DAGLink( - argv.name, - linkedObj.size(), - linkedObj.multihash() - ) - return ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'}) - }).then((node) => { - console.log(node.toJSON().Hash) + linkedObj.size((err, size) => { + if (err) { + throw err + } + linkedObj.multihash((err, multihash) => { + if (err) { + throw err + } + + const link = new DAGLink(argv.name, size, multihash) + + ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'}) + .then((node) => { + node.toJSON(gotJSON) + + function gotJSON (err, nodeJSON) { + if (err) { + throw err + } + console.log(nodeJSON.Hash) + } + }) + }) + }) }).catch((err) => { throw err }) - }) + } } } diff --git a/src/cli/commands/object/patch/append-data.js b/src/cli/commands/object/patch/append-data.js index 4aafbfbb16..7ee26a140d 100644 --- a/src/cli/commands/object/patch/append-data.js +++ b/src/cli/commands/object/patch/append-data.js @@ -18,7 +18,13 @@ function appendData (key, data) { throw err } - console.log(node.toJSON().Hash) + node.toJSON((err, nodeJSON) => { + if (err) { + throw err + } + + console.log(nodeJSON.Hash) + }) }) }) } diff --git a/src/cli/commands/object/patch/rm-link.js b/src/cli/commands/object/patch/rm-link.js index 9aa22645cc..5d66921de5 100644 --- a/src/cli/commands/object/patch/rm-link.js +++ b/src/cli/commands/object/patch/rm-link.js @@ -1,6 +1,6 @@ 'use strict' -const DAGLink = require('ipfs-merkle-dag').DAGLink +const DAGLink = require('ipld-dag-pb').DAGLink const utils = require('../../../utils') const debug = require('debug') const log = debug('cli:object') @@ -26,7 +26,12 @@ module.exports = { throw err } - console.log(node.toJSON().Hash) + node.toJSON((err, nodeJSON) => { + if (err) { + throw err + } + console.log(nodeJSON.Hash) + }) }) }) } diff --git a/src/cli/commands/object/patch/set-data.js b/src/cli/commands/object/patch/set-data.js index d24791c8eb..c2508b36a9 100644 --- a/src/cli/commands/object/patch/set-data.js +++ b/src/cli/commands/object/patch/set-data.js @@ -18,7 +18,13 @@ function parseAndAddNode (key, data) { throw err } - console.log(node.toJSON().Hash) + node.toJSON((err, nodeJSON) => { + if (err) { + throw err + } + + console.log(nodeJSON.Hash) + }) }) }) } diff --git a/src/cli/commands/object/put.js b/src/cli/commands/object/put.js index 5ce124f261..63918a5465 100644 --- a/src/cli/commands/object/put.js +++ b/src/cli/commands/object/put.js @@ -18,7 +18,13 @@ function putNode (buf, enc) { throw err } - console.log('added', node.toJSON().Hash) + node.toJSON((err, nodeJSON) => { + if (err) { + throw err + } + + console.log('added', nodeJSON.Hash) + }) }) }) } diff --git a/src/http-api/resources/object.js b/src/http-api/resources/object.js index 0b0904e666..7cc6ebc127 100644 --- a/src/http-api/resources/object.js +++ b/src/http-api/resources/object.js @@ -443,7 +443,9 @@ exports.patchAddLink = { }).code(500) } - node.toJSON((err, nodeJSON) => { + node.toJSON(gotJSON) + + function gotJSON (err, nodeJSON) { if (err) { return reply({ Message: 'Failed to get object: ' + err, @@ -451,7 +453,7 @@ exports.patchAddLink = { }).code(500) } return reply(nodeJSON) - }) + } }) }) }) diff --git a/test/http-api/ipfs-api/test-object.js b/test/http-api/ipfs-api/test-object.js index ae84052f32..19ac6cd155 100644 --- a/test/http-api/ipfs-api/test-object.js +++ b/test/http-api/ipfs-api/test-object.js @@ -1,4 +1,5 @@ /* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 8] */ 'use strict' const expect = require('chai').expect