diff --git a/src/files/add.js b/src/files/add.js index 81e8734f4..d9cde39b9 100644 --- a/src/files/add.js +++ b/src/files/add.js @@ -41,8 +41,15 @@ module.exports = (send) => { qs.hash = opts.hashAlg } + if (opts['only-hash'] != null) { + qs['only-hash'] = opts['only-hash'] + } else if (opts.onlyHash != null) { + qs['only-hash'] = opts.onlyHash + } + const request = { path: 'add', files: files, qs: qs } + if (qs['only-hash']) return send(request, callback) // Transform the response stream to DAGNode values const transform = (res, callback) => DAGNodeStream.streamToValue(send, res, callback) send.andTransform(request, transform, callback) diff --git a/test/files.spec.js b/test/files.spec.js index 629e5e9b0..ae72e4f3f 100644 --- a/test/files.spec.js +++ b/test/files.spec.js @@ -8,15 +8,23 @@ const expect = chai.expect chai.use(dirtyChai) const isNode = require('detect-node') const loadFixture = require('aegir/fixtures') +const streamToValue = require('../src/utils/stream-to-value') const mh = require('multihashes') const CID = require('cids') - const FactoryClient = require('./ipfs-factory/client') const testfile = isNode ? loadFixture(__dirname, '/fixtures/testfile.txt') : loadFixture(__dirname, 'fixtures/testfile.txt') +function createTestFile (content) { + content = content || String(Math.random() + Date.now()) + return { + path: content + '.txt', + content: Buffer.from(content) + } +} + // TODO: Test against all algorithms Object.keys(mh.names) // This subset is known to work with both go-ipfs and js-ipfs as of 2017-09-05 const HASH_ALGS = [ @@ -87,6 +95,43 @@ describe('.files (the MFS API part)', function () { }) }) + it('files.add without only-hash', (done) => { + const content = String(Math.random() + Date.now()) + const inputFile = createTestFile(content) + + ipfs.files.add([inputFile], { onlyHash: false }, (err, res) => { + expect(err).to.not.exist() + + const hash = res[0].hash + + ipfs.refs.local().then((refs) => { + const hashes = refs.map(r => r.Ref) + expect(hashes.indexOf(hash)).to.be.above(-1) + done() + }) + }) + }) + + it('files.add with only-hash', (done) => { + const inputFile = createTestFile() + + ipfs.files.add([inputFile], {'only-hash': true}, (err, res) => { + expect(err).to.not.exist() + + streamToValue(res, (err, collected) => { + expect(err).to.not.exist() + + const hash = collected[0].Hash + + ipfs.refs.local().then((refs) => { + const hashes = refs.map(r => r.Ref) + expect(hashes.indexOf(hash)).to.equal(-1) + done() + }) + }) + }) + }) + HASH_ALGS.forEach((name) => { it(`files.add with hash=${name} and raw-leaves=false`, (done) => { const content = String(Math.random() + Date.now()) @@ -263,6 +308,40 @@ describe('.files (the MFS API part)', function () { }) }) + it('files.add without only-hash', (done) => { + const inputFile = createTestFile() + + ipfs.files.add([inputFile], { onlyHash: false }) + .then((res) => { + const hash = res[0].hash + + ipfs.refs.local().then((refs) => { + const hashes = refs.map(r => r.Ref) + expect(hashes.indexOf(hash)).to.be.above(-1) + done() + }) + }) + }) + + it('files.add with only-hash', (done) => { + const inputFile = createTestFile() + + ipfs.files.add([inputFile], {'only-hash': true}) + .then((res) => { + streamToValue(res, (err, collected) => { + expect(err).to.not.exist() + + const hash = collected[0].Hash + + ipfs.refs.local().then((refs) => { + const hashes = refs.map(r => r.Ref) + expect(hashes.indexOf(hash)).to.equal(-1) + done() + }) + }) + }) + }) + HASH_ALGS.forEach((name) => { it(`files.add with hash=${name} and raw-leaves=false`, () => { const content = String(Math.random() + Date.now())