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

Commit

Permalink
fix: do not stringify output of object data (#1398)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achingbrain authored Jun 19, 2018
1 parent 2be4a0f commit 4e51a69
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/cli/commands/object/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
throw err
}

print(data.toString(), false)
print(data, false)
})
}
}
24 changes: 24 additions & 0 deletions test/cli/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')
const UnixFs = require('ipfs-unixfs')
const path = require('path')
const fs = require('fs')
const crypto = require('crypto')
const os = require('os')

describe('object', () => runOnAndOff((thing) => {
let ipfs
Expand Down Expand Up @@ -64,6 +69,25 @@ describe('object', () => runOnAndOff((thing) => {
})
})

it('unaulterated data', () => {
// has to be big enough to span several DAGNodes
const data = crypto.randomBytes(1024 * 300)
const file = path.join(os.tmpdir(), `file-${Math.random()}.txt`)

fs.writeFileSync(file, data)

return ipfs(`add ${file}`)
.then((out) => {
return ipfs.raw(`object data ${out.split(' ')[1]}`)
})
.then((out) => {
const meta = UnixFs.unmarshal(out)

expect(meta.type).to.equal('file')
expect(meta.fileSize()).to.equal(data.length)
})
})

it('links', () => {
return ipfs('object links QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm').then((out) => {
expect(out).to.eql(
Expand Down
15 changes: 13 additions & 2 deletions test/utils/ipfs-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ module.exports = (repoPath, opts) => {
}, opts)

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

function ipfs () {
let args = Array.from(arguments)
const execute = (exec, args) => {
if (args.length === 1) {
args = args[0].split(' ')
}
Expand All @@ -51,6 +53,15 @@ module.exports = (repoPath, opts) => {
return res
}

function ipfs () {
return execute(exec, Array.from(arguments))
}

// Will return buffers instead of strings
ipfs.raw = function () {
return execute(execRaw, Array.from(arguments))
}

/**
* Expect the command passed as @param arguments to fail.
* @return {Promise} Resolves if the command passed as @param arguments fails,
Expand Down

0 comments on commit 4e51a69

Please sign in to comment.