From cc6008a15a03cb2055644c865f622cf0f53bc8b8 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 30 Apr 2018 08:19:30 +0100 Subject: [PATCH] feat: add onlyHash option to files.add --- SPEC/FILES.md | 5 +++-- js/src/files.js | 14 ++++++++++++++ js/src/utils/expect-timeout.js | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 js/src/utils/expect-timeout.js diff --git a/SPEC/FILES.md b/SPEC/FILES.md index e976a7eaa..2857b289e 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 f47ffac1e..83256fede 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 000000000..51c733075 --- /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)) + ]) +}