From 4e51a69cf908f10cac8570ed61c5cf87cdc99810 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 19 Jun 2018 15:58:16 +0200 Subject: [PATCH] fix: do not stringify output of object data (#1398) 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. --- src/cli/commands/object/data.js | 2 +- test/cli/object.js | 24 ++++++++++++++++++++++++ test/utils/ipfs-exec.js | 15 +++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/cli/commands/object/data.js b/src/cli/commands/object/data.js index f47cad4035..13d0bfa7c7 100644 --- a/src/cli/commands/object/data.js +++ b/src/cli/commands/object/data.js @@ -17,7 +17,7 @@ module.exports = { throw err } - print(data.toString(), false) + print(data, false) }) } } diff --git a/test/cli/object.js b/test/cli/object.js index 4c69f7b770..df49543a65 100644 --- a/test/cli/object.js +++ b/test/cli/object.js @@ -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 @@ -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( diff --git a/test/utils/ipfs-exec.js b/test/utils/ipfs-exec.js index 2437f136a9..38742b76b5 100644 --- a/test/utils/ipfs-exec.js +++ b/test/utils/ipfs-exec.js @@ -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(' ') } @@ -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,