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

feat: add onlyHash option to files.add #259

Merged
merged 1 commit into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())

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

// '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))
])
}