Skip to content

Only hash option #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions src/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
81 changes: 80 additions & 1 deletion test/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down