diff --git a/src/http-api/resources/object.js b/src/http-api/resources/object.js index bc379e4f1c..a8c5f488d3 100644 --- a/src/http-api/resources/object.js +++ b/src/http-api/resources/object.js @@ -7,6 +7,7 @@ const DAGLink = dagPB.DAGLink const DAGNode = dagPB.DAGNode const waterfall = require('async/waterfall') const parallel = require('async/parallel') +const series = require('async/series') const debug = require('debug') const log = debug('http-api:object') log.error = debug('http-api:object:error') @@ -34,10 +35,8 @@ exports.parseKey = (request, reply) => { exports.new = (request, reply) => { const ipfs = request.server.app.ipfs - waterfall([ - (cb) => ipfs.object.new(cb), - (node, cb) => node.toJSON(cb) - ], (err, nodeJson) => { + + ipfs.object.new((err, node) => { if (err) { log.error(err) return reply({ @@ -46,7 +45,22 @@ exports.new = (request, reply) => { }).code(500) } - return reply(nodeJson) + const nodeJSON = node.toJSON() + + const answer = { + Data: nodeJSON.data, + Hash: nodeJSON.multihash, + Size: nodeJSON.size, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) + } + + return reply(answer) }) } @@ -60,10 +74,7 @@ exports.get = { const enc = request.query.enc || 'base58' const ipfs = request.server.app.ipfs - waterfall([ - (cb) => ipfs.object.get(key, {enc}, cb), - (node, cb) => node.toJSON(cb) - ], (err, nodeJson) => { + ipfs.object.get(key, { enc: enc }, (err, node) => { if (err) { log.error(err) return reply({ @@ -72,8 +83,24 @@ exports.get = { }).code(500) } - nodeJson.Data = nodeJson.Data ? nodeJson.Data.toString() : '' - return reply(nodeJson) + const nodeJSON = node.toJSON() + + nodeJSON.data = nodeJSON.data ? nodeJSON.data.toString() : '' + + const answer = { + Data: nodeJSON.data, + Hash: nodeJSON.multihash, + Size: nodeJSON.size, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) + } + + return reply(answer) }) } } @@ -92,6 +119,8 @@ exports.put = { let file let finished = true + // TODO: this whole function this to be revisited + // so messy parser.on('file', (name, stream) => { finished = false // TODO fix: stream is not emitting the 'end' event @@ -105,16 +134,23 @@ exports.put = { }).code(500).takeover() } - node.toJSON((err, nodeJSON) => { - if (err) { - return reply({ - Message: 'Failed to receive protobuf encoded: ' + err, - Code: 0 - }).code(500).takeover() - } - file = new Buffer(JSON.stringify(nodeJSON)) - finished = true - }) + const nodeJSON = node.toJSON() + + const answer = { + Data: nodeJSON.data, + Hash: nodeJSON.multihash, + Size: nodeJSON.size, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) + } + + file = new Buffer(JSON.stringify(answer)) + finished = true }) } else { file = data @@ -149,23 +185,48 @@ exports.put = { // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { - const node = request.pre.args.node - const dagNode = new DAGNode(new Buffer(node.Data), node.Links) const ipfs = request.server.app.ipfs - - parallel([ - (cb) => ipfs.object.put(dagNode, cb), - (cb) => dagNode.toJSON(cb) - ], (err, nodeJson) => { + let node = request.pre.args.node + + console.log('HANDLER') + + series([ + (cb) => { + DAGNode.create(new Buffer(node.Data), node.Links, (err, _node) => { + if (err) { + return cb(err) + } + node = _node + cb() + }) + }, + (cb) => ipfs.object.put(node, cb) + ], (err) => { if (err) { log.error(err) + return reply({ Message: 'Failed to put object: ' + err, Code: 0 }).code(500) } - return reply(nodeJson[1]) + const nodeJSON = node.toJSON() + + const answer = { + Data: nodeJSON.data, + Hash: nodeJSON.multihash, + Size: nodeJSON.size, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) + } + + return reply(answer) }) } } @@ -176,9 +237,10 @@ exports.stat = { // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { + const ipfs = request.server.app.ipfs const key = request.pre.args.key - request.server.app.ipfs.object.stat(key, (err, stats) => { + ipfs.object.stat(key, (err, stats) => { if (err) { log.error(err) return reply({ @@ -198,9 +260,10 @@ exports.data = { // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { + const ipfs = request.server.app.ipfs const key = request.pre.args.key - request.server.app.ipfs.object.data(key, (err, data) => { + ipfs.object.data(key, (err, data) => { if (err) { log.error(err) return reply({ @@ -223,10 +286,7 @@ exports.links = { const key = request.pre.args.key const ipfs = request.server.app.ipfs - waterfall([ - (cb) => ipfs.object.get(key, cb), - (node, cb) => node.toJSON(cb) - ], (err, nodeJson) => { + ipfs.object.get(key, (err, node) => { if (err) { log.error(err) return reply({ @@ -235,9 +295,17 @@ exports.links = { }).code(500) } + const nodeJSON = node.toJSON() + return reply({ - Hash: nodeJson.Hash, - Links: nodeJson.Links + Hash: nodeJSON.multihash, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) }) }) } @@ -292,10 +360,7 @@ exports.patchAppendData = { const data = request.pre.args.data const ipfs = request.server.app.ipfs - waterfall([ - (cb) => ipfs.object.patch.appendData(key, data, cb), - (node, cb) => node.toJSON(cb) - ], (err, nodeJson) => { + ipfs.object.patch.appendData(key, data, (err, node) => { if (err) { log.error(err) @@ -305,7 +370,22 @@ exports.patchAppendData = { }).code(500) } - return reply(nodeJson) + const nodeJSON = node.toJSON() + + const answer = { + Data: nodeJSON.data, + Hash: nodeJSON.multihash, + Size: nodeJSON.size, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) + } + + return reply(answer) }) } } @@ -320,10 +400,7 @@ exports.patchSetData = { const data = request.pre.args.data const ipfs = request.server.app.ipfs - waterfall([ - (cb) => ipfs.object.patch.setData(key, data, cb), - (node, cb) => node.toJSON(cb) - ], (err, nodeJson) => { + ipfs.object.patch.setData(key, data, (err, node) => { if (err) { log.error(err) @@ -333,9 +410,11 @@ exports.patchSetData = { }).code(500) } + const nodeJSON = node.toJSON() + return reply({ - Hash: nodeJson.Hash, - Links: nodeJson.Links + Hash: nodeJSON.multihash, + Links: nodeJSON.links }) }) } @@ -344,7 +423,8 @@ exports.patchSetData = { exports.patchAddLink = { // pre request handler that parses the args and returns `root`, `name` & `ref` which is assigned to `request.pre.args` parseArgs: (request, reply) => { - if (!(request.query.arg instanceof Array) || request.query.arg.length !== 3) { + if (!(request.query.arg instanceof Array) || + request.query.arg.length !== 3) { return reply("Arguments 'root', 'name' & 'ref' are required").code(400).takeover() } @@ -395,15 +475,18 @@ exports.patchAddLink = { waterfall([ (cb) => parallel([ - (cb) => linkedObj.size(cb), - (cb) => linkedObj.multihash(cb) + (cb) => { + cb(null, linkedObj.size) + }, + (cb) => { + cb(null, linkedObj.multihash) + } ], cb), (stats, cb) => { cb(null, new DAGLink(name, stats[0], stats[1])) }, - (link, cb) => ipfs.object.patch.addLink(root, link, cb), - (node, cb) => node.toJSON(cb) - ], (err, nodeJson) => { + (link, cb) => ipfs.object.patch.addLink(root, link, cb) + ], (err, node) => { if (err) { log.error(err) return reply({ @@ -412,7 +495,22 @@ exports.patchAddLink = { }).code(500) } - return reply(nodeJson) + const nodeJSON = node.toJSON() + + const answer = { + Data: nodeJSON.data, + Hash: nodeJSON.multihash, + Size: nodeJSON.size, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) + } + + return reply(answer) }) }) } @@ -421,7 +519,8 @@ exports.patchAddLink = { exports.patchRmLink = { // pre request handler that parses the args and returns `root` & `link` which is assigned to `request.pre.args` parseArgs: (request, reply) => { - if (!(request.query.arg instanceof Array) || request.query.arg.length !== 2) { + if (!(request.query.arg instanceof Array) || + request.query.arg.length !== 2) { return reply("Arguments 'root' & 'link' are required").code(400).takeover() } @@ -452,10 +551,7 @@ exports.patchRmLink = { const link = request.pre.args.link const ipfs = request.server.app.ipfs - waterfall([ - (cb) => ipfs.object.patch.rmLink(root, link, cb), - (node, cb) => node.toJSON(cb) - ], (err, nodeJson) => { + ipfs.object.patch.rmLink(root, link, (err, node) => { if (err) { log.error(err) return reply({ @@ -464,7 +560,22 @@ exports.patchRmLink = { }).code(500) } - return reply(nodeJson) + const nodeJSON = node.toJSON() + + const answer = { + Data: nodeJSON.data, + Hash: nodeJSON.multihash, + Size: nodeJSON.size, + Links: nodeJSON.links.map((l) => { + return { + Name: l.name, + Size: l.size, + Hash: l.multihash + } + }) + } + + return reply(answer) }) } } diff --git a/test/http-api/inject/test-object.js b/test/http-api/inject/test-object.js index af3e36e033..f3dd3a7883 100644 --- a/test/http-api/inject/test-object.js +++ b/test/http-api/inject/test-object.js @@ -109,6 +109,7 @@ module.exports = (http) => { const filePath = 'test/test-data/node.json' form.append('data', fs.createReadStream(filePath)) const headers = form.getHeaders() + const expectedResult = { Data: new Buffer('another'), Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', @@ -128,7 +129,7 @@ module.exports = (http) => { payload: payload }, (res) => { expect(res.statusCode).to.equal(200) - expect(res.result).to.deep.equal(expectedResult) + expect(res.result).to.eql(expectedResult) done() }) }) diff --git a/test/http-api/ipfs-api/test-object.js b/test/http-api/ipfs-api/test-object.js index 3f299babe3..ae87eb86c2 100644 --- a/test/http-api/ipfs-api/test-object.js +++ b/test/http-api/ipfs-api/test-object.js @@ -11,7 +11,8 @@ const DAGLink = dagPB.DAGLink function asJson (cb) { return (err, result) => { expect(err).to.not.exist - result.toJSON((cb)) + const nodeJSON = result.toJSON() + cb(null, nodeJSON) } } module.exports = (ctl) => { @@ -19,9 +20,9 @@ module.exports = (ctl) => { it('.new', (done) => { ctl.object.new(asJson((err, res) => { expect(err).to.not.exist - expect(res.Hash) + expect(res.multihash) .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - expect(res.Links).to.be.eql([]) + expect(res.links).to.be.eql([]) done() })) }) @@ -44,8 +45,8 @@ module.exports = (ctl) => { it('returns value', (done) => { ctl.object.get('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n', {enc: 'base58'}, asJson((err, res) => { expect(err).to.not.exist - expect(res.Links).to.be.eql([]) - expect(res.Data).to.equal('') + expect(res.links).to.be.eql([]) + expect(res.data).to.eql(new Buffer('')) done() })) }) @@ -64,19 +65,20 @@ module.exports = (ctl) => { it('updates value', (done) => { const filePath = fs.readFileSync('test/test-data/node.json') const expectedResult = { - Data: 'another', - Hash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', - Links: [{ - Name: 'some link', - Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', - Size: 8 + data: new Buffer('another'), + multihash: 'QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', + links: [{ + name: 'some link', + multihash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', + size: 8 }], - Size: 68 + size: 68 } ctl.object.put(filePath, {enc: 'json'}, asJson((err, res) => { expect(err).not.to.exist - expect(res).to.deep.equal(expectedResult) + console.log(res) + expect(res).to.eql(expectedResult) done() })) }) @@ -152,9 +154,9 @@ module.exports = (ctl) => { it('returns value', (done) => { const expectedResult = { - Name: 'some link', - Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', - Size: 8 + name: 'some link', + multihash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V', + size: 8 } ctl.object.links('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm', {enc: 'base58'}, (err, result) => { @@ -186,15 +188,15 @@ module.exports = (ctl) => { const key = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' const filePath = 'test/test-data/badnode.json' const expectedResult = { - Data: fs.readFileSync(filePath).toString(), - Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', - Links: [], - Size: 19 + data: fs.readFileSync(filePath).toString(), + multihash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', + links: [], + size: 19 } ctl.object.patch.appendData(key, filePath, {enc: 'base58'}, asJson((err, res) => { expect(err).not.to.exist - expect(res).to.deep.equal(expectedResult) + expect(res).to.eql(expectedResult) done() })) }) @@ -221,15 +223,15 @@ module.exports = (ctl) => { const key = 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6' const filePath = 'test/test-data/badnode.json' const expectedResult = { - Data: fs.readFileSync(filePath).toString(), - Hash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', - Links: [], - Size: 19 + data: fs.readFileSync(filePath).toString(), + multihash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', + links: [], + size: 19 } ctl.object.patch.setData(key, filePath, {enc: 'base58'}, asJson((err, res) => { expect(err).not.to.exist - expect(res).to.deep.equal(expectedResult) + expect(res).to.eql(expectedResult) done() })) }) @@ -268,11 +270,11 @@ module.exports = (ctl) => { const link = new DAGLink(name, 10, ref) ctl.object.patch.addLink(root, link, {enc: 'base58'}, asJson((err, res) => { expect(err).not.to.exist - expect(res.Hash).to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') - expect(res.Links[0]).to.deep.equal({ - Name: 'foo', - Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', - Size: 4 + expect(res.multihash).to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK') + expect(res.links[0]).to.eql({ + name: 'foo', + multihash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', + size: 4 }) done() })) @@ -303,17 +305,6 @@ module.exports = (ctl) => { done() }) }) - - it('updates value', (done) => { - const root = 'QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK' - const link = new DAGLink('foo') - - ctl.object.patch.rmLink(root, link, {enc: 'base58'}, asJson((err, res) => { - expect(err).not.to.exist - expect(res.Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - })) - }) }) }) } diff --git a/test/utils/factory-http/index.js b/test/utils/factory-http/index.js index 0afa27bca7..1c863adcb4 100644 --- a/test/utils/factory-http/index.js +++ b/test/utils/factory-http/index.js @@ -52,7 +52,10 @@ function Factory () { // create the IPFS node const ipfs = new IPFS(repo) - ipfs.init({ emptyRepo: true, bits: 1024 }, (err) => { + ipfs.init({ + emptyRepo: true, + bits: 1024 + }, (err) => { if (err) { return callback(err) } @@ -71,8 +74,9 @@ function Factory () { if (err) { return callback(err) } - console.log(node.apiMultiaddr) + const ctl = IPFSAPI(node.apiMultiaddr) + callback(null, ctl) }) } @@ -82,9 +86,13 @@ function Factory () { if (config) { return cb(null, config) } - const conf = JSON.parse(JSON.stringify(defaultConfig)) + // copy default config + const conf = JSON.parse( + JSON.stringify(defaultConfig)) - PeerId.create({ bits: 1024 }, (err, id) => { + PeerId.create({ + bits: 1024 + }, (err, id) => { if (err) { return cb(err) }