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

Commit 6f9a061

Browse files
committed
feat: adds data-encoding argument to control data encoding
1 parent 1a36375 commit 6f9a061

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/cli/commands/object/get.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ module.exports = {
77

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

10-
builder: {},
10+
builder: {
11+
'data-encoding': {
12+
type: 'string',
13+
default: 'base64'
14+
}
15+
},
1116

1217
handler (argv) {
1318
argv.ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => {
@@ -16,7 +21,9 @@ module.exports = {
1621
}
1722
const nodeJSON = node.toJSON()
1823

19-
nodeJSON.data = nodeJSON.data ? nodeJSON.data.toString() : ''
24+
if (Buffer.isBuffer(node.data)) {
25+
nodeJSON.data = node.data.toString(argv['data-encoding'] || undefined)
26+
}
2027

2128
const answer = {
2229
Data: nodeJSON.data,

src/http/api/resources/object.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ exports.get = {
8585

8686
const nodeJSON = node.toJSON()
8787

88-
nodeJSON.data = nodeJSON.data ? nodeJSON.data.toString() : ''
88+
if (Buffer.isBuffer(node.data)) {
89+
nodeJSON.data = node.data.toString(request.query['data-encoding'] || undefined)
90+
}
8991

9092
const answer = {
9193
Data: nodeJSON.data,

test/cli/object.js

+24
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ describe('object', () => runOnAndOff((thing) => {
4141
})
4242
})
4343

44+
it('get with data', () => {
45+
return ipfs('object new')
46+
.then((out) => out.trim())
47+
.then((hash) => ipfs(`object patch set-data ${hash} test/fixtures/test-data/hello`))
48+
.then((out) => out.trim())
49+
.then((hash) => ipfs(`object get ${hash}`))
50+
.then((out) => {
51+
const result = JSON.parse(out)
52+
expect(result.Data).to.eql('aGVsbG8gd29ybGQK')
53+
})
54+
})
55+
56+
it('get while overriding data-encoding', () => {
57+
return ipfs('object new')
58+
.then((out) => out.trim())
59+
.then((hash) => ipfs(`object patch set-data ${hash} test/fixtures/test-data/hello`))
60+
.then((out) => out.trim())
61+
.then((hash) => ipfs(`object get --data-encoding=utf8 ${hash}`))
62+
.then((out) => {
63+
const result = JSON.parse(out)
64+
expect(result.Data).to.eql('hello world\n')
65+
})
66+
})
67+
4468
it('put', () => {
4569
return ipfs('object put test/fixtures/test-data/node.json').then((out) => {
4670
expect(out).to.eql(

0 commit comments

Comments
 (0)