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
Browse files Browse the repository at this point in the history
Allows the user to specify how object data is returned to prevent
default byte encoding from emitting characters that are not valid
in JSON.
  • Loading branch information
achingbrain committed Aug 15, 2018
1 parent 59bc6d5 commit dd1e57e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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
24 changes: 24 additions & 0 deletions test/cli/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit dd1e57e

Please sign in to comment.