From 96d157e989a21589cb2e5c13f8dc34313f35fb6a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 4 Jul 2018 17:15:06 +0100 Subject: [PATCH 1/3] feat: adds data-encoding argument to control data encoding Allows the user to specify how object data is returned to prevent default byte encoding from emitting characters that are not valid in JSON. --- package.json | 2 +- src/cli/commands/object/get.js | 11 +++++++++-- src/http/api/resources/object.js | 4 +++- test/cli/object.js | 24 ++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 3cd1773ac3..cb7425d4e8 100644 --- a/package.json +++ b/package.json @@ -104,7 +104,7 @@ "hoek": "^5.0.3", "human-to-milliseconds": "^1.0.0", "interface-datastore": "~0.4.2", - "ipfs-api": "^22.2.4", + "ipfs-api": "^24.0.0", "ipfs-bitswap": "~0.20.3", "ipfs-block": "~0.7.1", "ipfs-block-service": "~0.14.0", diff --git a/src/cli/commands/object/get.js b/src/cli/commands/object/get.js index 629a3a82d1..9bb9e71542 100644 --- a/src/cli/commands/object/get.js +++ b/src/cli/commands/object/get.js @@ -7,7 +7,12 @@ module.exports = { describe: 'Get and serialize the DAG node named by ', - builder: {}, + builder: { + 'data-encoding': { + type: 'string', + default: 'base64' + } + }, handler (argv) { argv.ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => { @@ -16,7 +21,9 @@ module.exports = { } const nodeJSON = node.toJSON() - nodeJSON.data = nodeJSON.data ? nodeJSON.data.toString() : '' + if (Buffer.isBuffer(node.data)) { + nodeJSON.data = node.data.toString(argv['data-encoding'] || undefined) + } const answer = { Data: nodeJSON.data, diff --git a/src/http/api/resources/object.js b/src/http/api/resources/object.js index 9f9e7f2e32..dc68c71251 100644 --- a/src/http/api/resources/object.js +++ b/src/http/api/resources/object.js @@ -85,7 +85,9 @@ exports.get = { const nodeJSON = node.toJSON() - nodeJSON.data = nodeJSON.data ? nodeJSON.data.toString() : '' + if (Buffer.isBuffer(node.data)) { + nodeJSON.data = node.data.toString(request.query['data-encoding'] || undefined) + } const answer = { Data: nodeJSON.data, diff --git a/test/cli/object.js b/test/cli/object.js index 4173ecd816..5bb5c7787b 100644 --- a/test/cli/object.js +++ b/test/cli/object.js @@ -41,6 +41,30 @@ describe('object', () => runOnAndOff((thing) => { }) }) + it('get with data', () => { + return ipfs('object new') + .then((out) => out.trim()) + .then((hash) => ipfs(`object patch set-data ${hash} test/fixtures/test-data/hello`)) + .then((out) => out.trim()) + .then((hash) => ipfs(`object get ${hash}`)) + .then((out) => { + const result = JSON.parse(out) + expect(result.Data).to.eql('aGVsbG8gd29ybGQK') + }) + }) + + it('get while overriding data-encoding', () => { + return ipfs('object new') + .then((out) => out.trim()) + .then((hash) => ipfs(`object patch set-data ${hash} test/fixtures/test-data/hello`)) + .then((out) => out.trim()) + .then((hash) => ipfs(`object get --data-encoding=utf8 ${hash}`)) + .then((out) => { + const result = JSON.parse(out) + expect(result.Data).to.eql('hello world\n') + }) + }) + it('put', () => { return ipfs('object put test/fixtures/test-data/node.json').then((out) => { expect(out).to.eql( From 6b6fde26c1380e4c912b0af026ca04430b4a9436 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 16 Aug 2018 08:07:36 +0100 Subject: [PATCH 2/3] fix: increase test timeouts as they are slow --- test/cli/object.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/cli/object.js b/test/cli/object.js index 5bb5c7787b..1462d00d55 100644 --- a/test/cli/object.js +++ b/test/cli/object.js @@ -41,7 +41,9 @@ describe('object', () => runOnAndOff((thing) => { }) }) - it('get with data', () => { + it('get with data', function () { + this.timeout(15 * 1000) + return ipfs('object new') .then((out) => out.trim()) .then((hash) => ipfs(`object patch set-data ${hash} test/fixtures/test-data/hello`)) @@ -53,7 +55,9 @@ describe('object', () => runOnAndOff((thing) => { }) }) - it('get while overriding data-encoding', () => { + it('get while overriding data-encoding', function () { + this.timeout(15 * 1000) + return ipfs('object new') .then((out) => out.trim()) .then((hash) => ipfs(`object patch set-data ${hash} test/fixtures/test-data/hello`)) From 2daf20a356cfbdc5a7079d50240836b0dfe77af4 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 16 Aug 2018 10:44:36 +0100 Subject: [PATCH 3/3] chore: update deps --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cb7425d4e8..6f7ae61afd 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "expose-loader": "~0.7.5", "form-data": "^2.3.2", "hat": "0.0.3", - "interface-ipfs-core": "~0.75.2", + "interface-ipfs-core": "~0.76.1", "ipfsd-ctl": "~0.39.1", "mocha": "^5.2.0", "ncp": "^2.0.0",