diff --git a/SPEC/FILES.md b/SPEC/FILES.md index e976a7ea..2857b289 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -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: diff --git a/js/src/files.js b/js/src/files.js index f47ffac1..83256fed 100644 --- a/js/src/files.js +++ b/js/src/files.js @@ -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 () { @@ -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()) + + return ipfs.files.add(Buffer.from(content), { onlyHash: true }) + .then(files => { + expect(files).to.have.length(1) + + // 'ipfs.object.get()' should timeout because content wasn't actually added + return expectTimeout(ipfs.object.get(files[0].hash), 4000) + }) + }) }) describe('.addReadableStream', () => { diff --git a/js/src/utils/expect-timeout.js b/js/src/utils/expect-timeout.js new file mode 100644 index 00000000..51c73307 --- /dev/null +++ b/js/src/utils/expect-timeout.js @@ -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)) + ]) +}