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

Commit 036b0fd

Browse files
JonKronedaviddias
authored andcommitted
feat: only-hash option for add/addFromFS/addFromURL (#700)
1 parent 8469fbd commit 036b0fd

9 files changed

+142
-46
lines changed

src/files/add-pull-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ module.exports = (send) => {
88
return (options) => {
99
options = options || {}
1010
options.converter = FileResultStreamConverter
11-
return toPull(SendFilesStream(send, 'add')(options))
11+
return toPull(SendFilesStream(send, 'add')({ qs: options }))
1212
}
1313
}

src/files/add.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = (send) => {
3636

3737
const files = [].concat(_files)
3838

39-
const stream = createAddStream(options)
39+
const stream = createAddStream({ qs: options })
4040
const concat = ConcatStream((result) => callback(null, result))
4141
stream.once('error', callback)
4242
stream.pipe(concat)

src/files/write.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = (send) => {
3333
converter: FileResultStreamConverter
3434
}
3535

36-
const stream = sendFilesStream(options)
36+
const stream = sendFilesStream({ qs: options })
3737
const concat = concatStream((result) => callback(null, result))
3838
stream.once('error', callback)
3939
stream.pipe(concat)

src/util/fs-add.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
const isNode = require('detect-node')
44
const promisify = require('promisify-es6')
5-
const moduleConfig = require('../utils/module-config')
65
const SendOneFile = require('../utils/send-one-file-multiple-results')
76
const FileResultStreamConverter = require('../utils/file-result-stream-converter')
87

9-
module.exports = (arg) => {
10-
const sendOneFile = SendOneFile(moduleConfig(arg), 'add')
8+
module.exports = (send) => {
9+
const sendOneFile = SendOneFile(send, 'add')
1110

1211
return promisify((path, opts, callback) => {
1312
if (typeof opts === 'function' &&

src/util/url-add.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
const promisify = require('promisify-es6')
44
const parseUrl = require('url').parse
55
const request = require('../utils/request')
6-
const moduleConfig = require('../utils/module-config')
76
const SendOneFile = require('../utils/send-one-file-multiple-results')
87
const FileResultStreamConverter = require('../utils/file-result-stream-converter')
98

10-
module.exports = (arg) => {
11-
const sendOneFile = SendOneFile(moduleConfig(arg), 'add')
9+
module.exports = (send) => {
10+
const sendOneFile = SendOneFile(send, 'add')
1211

1312
return promisify((url, opts, callback) => {
1413
if (typeof (opts) === 'function' &&

src/utils/send-files-stream.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = (send, path) => {
3131
let ended = false
3232
let writing = false
3333

34-
options = options || {}
34+
options = options ? Object.assign({}, options, options.qs) : {}
3535

3636
const multipart = new Multipart()
3737

@@ -42,7 +42,7 @@ module.exports = (send, path) => {
4242
retStream._write = (file, enc, _next) => {
4343
const next = once(_next)
4444
try {
45-
const files = prepareFile(file, Object.assign({}, options, options.qs))
45+
const files = prepareFile(file, options)
4646
.map((file) => Object.assign({headers: headers(file)}, file))
4747

4848
writing = true
@@ -75,23 +75,10 @@ module.exports = (send, path) => {
7575

7676
const qs = options.qs || {}
7777

78-
if (options['cid-version'] != null) {
79-
qs['cid-version'] = options['cid-version']
80-
} else if (options.cidVersion != null) {
81-
qs['cid-version'] = options.cidVersion
82-
}
83-
84-
if (options['raw-leaves'] != null) {
85-
qs['raw-leaves'] = options['raw-leaves']
86-
} else if (options.rawLeaves != null) {
87-
qs['raw-leaves'] = options.rawLeaves
88-
}
89-
90-
if (options.hash != null) {
91-
qs.hash = options.hash
92-
} else if (options.hashAlg != null) {
93-
qs.hash = options.hashAlg
94-
}
78+
qs['cid-version'] = propOrProp(options, 'cid-version', 'cidVersion')
79+
qs['raw-leaves'] = propOrProp(options, 'raw-leaves', 'rawLeaves')
80+
qs['only-hash'] = propOrProp(options, 'only-hash', 'onlyHash')
81+
qs.hash = propOrProp(options, 'hash', 'hashAlg')
9582

9683
const args = {
9784
path: path,
@@ -158,3 +145,11 @@ module.exports = (send, path) => {
158145
return retStream
159146
}
160147
}
148+
149+
function propOrProp (source, prop1, prop2) {
150+
if (prop1 in source) {
151+
return source[prop1]
152+
} else if (prop2 in source) {
153+
return source[prop2]
154+
}
155+
}

test/files.spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const CID = require('cids')
1313

1414
const IPFSApi = require('../src')
1515
const f = require('./utils/factory')
16+
const expectTimeout = require('./utils/expect-timeout')
1617

1718
const testfile = loadFixture('test/fixtures/testfile.txt')
1819

@@ -102,6 +103,19 @@ describe('.files (the MFS API part)', function () {
102103
})
103104
})
104105

106+
it('files.add with only-hash=true', function () {
107+
this.slow(10 * 1000)
108+
const content = String(Math.random() + Date.now())
109+
110+
return ipfs.files.add(Buffer.from(content), { onlyHash: true })
111+
.then(files => {
112+
expect(files).to.have.length(1)
113+
114+
// 'ipfs.object.get(<hash>)' should timeout because content wasn't actually added
115+
return expectTimeout(ipfs.object.get(files[0].hash), 4000)
116+
})
117+
})
118+
105119
it('files.add with options', (done) => {
106120
ipfs.files.add(testfile, { pin: false }, (err, res) => {
107121
expect(err).to.not.exist()
@@ -113,6 +127,42 @@ describe('.files (the MFS API part)', function () {
113127
})
114128
})
115129

130+
it('files.add pins by default', (done) => {
131+
const newContent = Buffer.from(String(Math.random()))
132+
133+
ipfs.pin.ls((err, pins) => {
134+
expect(err).to.not.exist()
135+
const initialPinCount = pins.length
136+
ipfs.files.add(newContent, (err, res) => {
137+
expect(err).to.not.exist()
138+
139+
ipfs.pin.ls((err, pins) => {
140+
expect(err).to.not.exist()
141+
expect(pins.length).to.eql(initialPinCount + 1)
142+
done()
143+
})
144+
})
145+
})
146+
})
147+
148+
it('files.add with pin=false', (done) => {
149+
const newContent = Buffer.from(String(Math.random()))
150+
151+
ipfs.pin.ls((err, pins) => {
152+
expect(err).to.not.exist()
153+
const initialPinCount = pins.length
154+
ipfs.files.add(newContent, { pin: false }, (err, res) => {
155+
expect(err).to.not.exist()
156+
157+
ipfs.pin.ls((err, pins) => {
158+
expect(err).to.not.exist()
159+
expect(pins.length).to.eql(initialPinCount)
160+
done()
161+
})
162+
})
163+
})
164+
})
165+
116166
HASH_ALGS.forEach((name) => {
117167
it(`files.add with hash=${name} and raw-leaves=false`, (done) => {
118168
const content = String(Math.random() + Date.now())

test/util.spec.js

+55-18
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ chai.use(dirtyChai)
99
const isNode = require('detect-node')
1010
const path = require('path')
1111
const fs = require('fs')
12+
const os = require('os')
1213

1314
const IPFSApi = require('../src')
1415
const f = require('./utils/factory')
16+
const expectTimeout = require('./utils/expect-timeout')
1517

1618
describe('.util', () => {
1719
if (!isNode) { return }
@@ -92,31 +94,66 @@ describe('.util', () => {
9294
done()
9395
})
9496
})
97+
98+
it('with only-hash=true', function () {
99+
this.slow(10 * 1000)
100+
const content = String(Math.random() + Date.now())
101+
const filepath = path.join(os.tmpdir(), `${content}.txt`)
102+
fs.writeFileSync(filepath, content)
103+
104+
return ipfs.util.addFromFs(filepath, { onlyHash: true })
105+
.then(out => {
106+
fs.unlinkSync(filepath)
107+
return expectTimeout(ipfs.object.get(out[0].hash), 4000)
108+
})
109+
})
95110
})
96111

97-
it('.urlAdd http', function (done) {
98-
this.timeout(20 * 1000)
112+
describe('.urlAdd', () => {
113+
it('http', function (done) {
114+
this.timeout(20 * 1000)
99115

100-
ipfs.util.addFromURL('http://example.com/', (err, result) => {
101-
expect(err).to.not.exist()
102-
expect(result.length).to.equal(1)
103-
done()
116+
ipfs.util.addFromURL('http://example.com/', (err, result) => {
117+
expect(err).to.not.exist()
118+
expect(result.length).to.equal(1)
119+
done()
120+
})
104121
})
105-
})
106122

107-
it('.urlAdd https', (done) => {
108-
ipfs.util.addFromURL('https://example.com/', (err, result) => {
109-
expect(err).to.not.exist()
110-
expect(result.length).to.equal(1)
111-
done()
123+
it('https', function (done) {
124+
this.timeout(20 * 1000)
125+
126+
ipfs.util.addFromURL('https://example.com/', (err, result) => {
127+
expect(err).to.not.exist()
128+
expect(result.length).to.equal(1)
129+
done()
130+
})
112131
})
113-
})
114132

115-
it('.urlAdd http with redirection', (done) => {
116-
ipfs.util.addFromURL('https://coverartarchive.org/release/6e2a1694-d8b9-466a-aa33-b1077b2333c1', (err, result) => {
117-
expect(err).to.not.exist()
118-
expect(result[0].hash).to.equal('QmSUdDvmXuq5YGrL4M3SEz7UZh5eT9WMuAsd9K34sambSj')
119-
done()
133+
it('http with redirection', function (done) {
134+
this.timeout(20 * 1000)
135+
136+
ipfs.util.addFromURL('http://covers.openlibrary.org/book/id/969165.jpg', (err, result) => {
137+
expect(err).to.not.exist()
138+
expect(result[0].hash).to.equal('QmaL9zy7YUfvWmtD5ZXp42buP7P4xmZJWFkm78p8FJqgjg')
139+
done()
140+
})
141+
})
142+
143+
it('.urlAdd http with redirection', (done) => {
144+
ipfs.util.addFromURL('https://coverartarchive.org/release/6e2a1694-d8b9-466a-aa33-b1077b2333c1', (err, result) => {
145+
expect(err).to.not.exist()
146+
expect(result[0].hash).to.equal('QmSUdDvmXuq5YGrL4M3SEz7UZh5eT9WMuAsd9K34sambSj')
147+
done()
148+
})
149+
})
150+
151+
it('with only-hash=true', function () {
152+
this.timeout(10 * 1000)
153+
this.slow(10 * 1000)
154+
155+
return ipfs.util.addFromURL('http://www.randomtext.me/#/gibberish', { onlyHash: true })
156+
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
120157
})
121158
})
122159
})

test/utils/expect-timeout.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
/**
4+
* Resolve if @param promise hangs for at least @param ms, throw otherwise
5+
* @param {Promise} promise promise that you expect to hang
6+
* @param {Number} ms millis to wait
7+
* @return {Promise}
8+
*/
9+
module.exports = (promise, ms) => {
10+
return Promise.race([
11+
promise.then((out) => {
12+
throw new Error('Expected Promise to timeout but it was successful.')
13+
}),
14+
new Promise((resolve, reject) => setTimeout(resolve, ms))
15+
])
16+
}

0 commit comments

Comments
 (0)