Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: adds data-encoding argument to control data encoding (#1420)
Browse files Browse the repository at this point in the history
* 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.

* fix: increase test timeouts as they are slow

* chore: update deps
  • Loading branch information
achingbrain authored and alanshaw committed Aug 17, 2018
1 parent 04d8ce3 commit 1eb8485
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -105,7 +105,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",
Expand Down
11 changes: 9 additions & 2 deletions src/cli/commands/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ module.exports = {

describe: 'Get and serialize the DAG node named by <key>',

builder: {},
builder: {
'data-encoding': {
type: 'string',
default: 'base64'
}
},

handler (argv) {
argv.ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => {
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/http/api/resources/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions test/cli/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ describe('object', () => runOnAndOff((thing) => {
})
})

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`))
.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', 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`))
.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(
Expand Down

0 comments on commit 1eb8485

Please sign in to comment.