Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat: add onlyHash option to files.add (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored and daviddias committed Apr 30, 2018
1 parent 647e399 commit 63179b9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions SPEC/FILES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ If no `content` is passed, then the path is treated as an empty directory
- cid-version (integer, default 0): the CID version to use when storing the data (storage keys are based on the CID, including it's version)
- progress (function): a function that will be called with the byte length of chunks as a file is added to ipfs.
- recursive (boolean): for when a Path is passed, this option can be enabled to add recursively all the files.
- hashAlg || hash (string): multihash hashing algorithm to use
- wrapWithDirectory (boolean): adds a wrapping node around the content
- hashAlg || hash (string): multihash hashing algorithm to use.
- wrapWithDirectory (boolean): adds a wrapping node around the content.
- onlyHash (boolean): doesn't actually add the file to IPFS, but rather calculates its hash.

`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` will be an array of:

Expand Down
14 changes: 14 additions & 0 deletions js/src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const path = require('path')
const bl = require('bl')
const isNode = require('detect-node')
const CID = require('cids')
const expectTimeout = require('./utils/expect-timeout')

module.exports = (common) => {
describe('.files', function () {
Expand Down Expand Up @@ -334,6 +335,19 @@ module.exports = (common) => {
expect(file.path).to.equal(smallFile.cid)
})
})

it('files.add with only-hash=true', () => {
this.slow(10 * 1000)
const content = String(Math.random() + Date.now())

This comment has been minimized.

Copy link
@victorb

victorb May 5, 2018

Contributor

Tests should generally not have any random data inside them as they should be predicable


return ipfs.files.add(Buffer.from(content), { onlyHash: true })
.then(files => {
expect(files).to.have.length(1)

This comment has been minimized.

Copy link
@victorb

victorb May 5, 2018

Contributor

Missing a assertion of the resulting hash. Right now files could be [1] and the tests would pass.


// 'ipfs.object.get(<hash>)' should timeout because content wasn't actually added
return expectTimeout(ipfs.object.get(files[0].hash), 4000)
})
})
})

describe('.addReadableStream', () => {
Expand Down
16 changes: 16 additions & 0 deletions js/src/utils/expect-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

/**
* Resolve if @param promise hangs for at least @param ms, throw otherwise
* @param {Promise} promise promise that you expect to hang
* @param {Number} ms millis to wait
* @return {Promise}
*/
module.exports = (promise, ms) => {
return Promise.race([
promise.then((out) => {
throw new Error('Expected Promise to timeout but it was successful.')
}),
new Promise((resolve, reject) => setTimeout(resolve, ms))
])
}

0 comments on commit 63179b9

Please sign in to comment.