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
  • Loading branch information
achingbrain committed Aug 10, 2018
1 parent 31097fe commit a9eaa03
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
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 a9eaa03

Please sign in to comment.