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

Commit 1eb8485

Browse files
achingbrainalanshaw
authored andcommitted
feat: adds data-encoding argument to control data encoding (#1420)
* 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
1 parent 04d8ce3 commit 1eb8485

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"expose-loader": "~0.7.5",
7171
"form-data": "^2.3.2",
7272
"hat": "0.0.3",
73-
"interface-ipfs-core": "~0.75.2",
73+
"interface-ipfs-core": "~0.76.1",
7474
"ipfsd-ctl": "~0.39.1",
7575
"mocha": "^5.2.0",
7676
"ncp": "^2.0.0",
@@ -105,7 +105,7 @@
105105
"hoek": "^5.0.3",
106106
"human-to-milliseconds": "^1.0.0",
107107
"interface-datastore": "~0.4.2",
108-
"ipfs-api": "^22.2.4",
108+
"ipfs-api": "^24.0.0",
109109
"ipfs-bitswap": "~0.20.3",
110110
"ipfs-block": "~0.7.1",
111111
"ipfs-block-service": "~0.14.0",

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

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

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

0 commit comments

Comments
 (0)