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

Commit 355b602

Browse files
committed
chore: do not stringify output of object data
Without this change: ```sh $ jsipfs object data `dd if=/dev/urandom bs=307200 count=1 | jsipfs files write --create /some-file && jsipfs files ls -h /some-file` | unixfs {"type":"file","blockSizes":[72481833443311,10909182287855]} ``` e.g. ``` 00000000: 0802 18ef bfbd efbf bd12 20ef bfbd efbf .......... ..... 00000010: bd10 20ef bfbd efbf bd02 .. ....... ``` With this change: ```sh $ jsipfs object data `dd if=/dev/urandom bs=307200 count=1 | jsipfs files write --create /some-file && jsipfs files ls -h /some-file` | unixfs {"type":"file","blockSizes":[262144,45056]} ``` e.g. ``` 00000000: 0802 1880 e012 2080 8010 2080 e002 ...... ... ... ``` Stringifying the data changes it's encoding. Been banging my head against this all morning.
1 parent 2be4a0f commit 355b602

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/cli/commands/object/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
throw err
1818
}
1919

20-
print(data.toString(), false)
20+
print(data, false)
2121
})
2222
}
2323
}

test/cli/object.js

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
const expect = require('chai').expect
66
const runOnAndOff = require('../utils/on-and-off')
7+
const UnixFs = require('ipfs-unixfs')
8+
const path = require('path')
9+
const fs = require('fs')
10+
const crypto = require('crypto')
11+
const os = require('os')
712

813
describe('object', () => runOnAndOff((thing) => {
914
let ipfs
@@ -64,6 +69,25 @@ describe('object', () => runOnAndOff((thing) => {
6469
})
6570
})
6671

72+
it('unaulterated data', () => {
73+
// has to be big enough to span several DAGNodes
74+
const data = crypto.randomBytes(1024 * 300)
75+
const file = path.join(os.tmpdir(), `file-${Math.random()}.txt`)
76+
77+
fs.writeFileSync(file, data)
78+
79+
return ipfs(`add ${file}`)
80+
.then((out) => {
81+
return ipfs.raw(`object data ${out.split(' ')[1]}`)
82+
})
83+
.then((out) => {
84+
const meta = UnixFs.unmarshal(out)
85+
86+
expect(meta.type).to.equal('file')
87+
expect(meta.fileSize()).to.equal(data.length)
88+
})
89+
})
90+
6791
it('links', () => {
6892
return ipfs('object links QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm').then((out) => {
6993
expect(out).to.eql(

test/utils/ipfs-exec.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ module.exports = (repoPath, opts) => {
2929
}, opts)
3030

3131
const exec = (args) => execa(`${process.cwd()}/src/cli/bin.js`, args, config)
32+
const execRaw = (args) => execa(`${process.cwd()}/src/cli/bin.js`, args, Object.assign({}, config, {
33+
encoding: null
34+
}))
3235

33-
function ipfs () {
34-
let args = Array.from(arguments)
36+
const execute = (exec, args) => {
3537
if (args.length === 1) {
3638
args = args[0].split(' ')
3739
}
@@ -51,6 +53,15 @@ module.exports = (repoPath, opts) => {
5153
return res
5254
}
5355

56+
function ipfs () {
57+
return execute(exec, Array.from(arguments))
58+
}
59+
60+
// Will return buffers instead of strings
61+
ipfs.raw = function () {
62+
return execute(execRaw, Array.from(arguments))
63+
}
64+
5465
/**
5566
* Expect the command passed as @param arguments to fail.
5667
* @return {Promise} Resolves if the command passed as @param arguments fails,

0 commit comments

Comments
 (0)