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

Commit d1817ef

Browse files
committed
Add ipfs.files.get tests.
1 parent e35b4a2 commit d1817ef

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
},
2929
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
3030
"dependencies": {
31-
"bl": "^1.1.2",
3231
"bs58": "^3.0.0",
3332
"chai": "^3.5.0",
33+
"concat-stream": "^1.5.1",
3434
"detect-node": "^2.0.3",
3535
"ipfs-merkle-dag": "^0.6.0",
3636
"readable-stream": "1.1.13"

src/files.js

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const Readable = require('readable-stream')
77
const path = require('path')
88
const fs = require('fs')
99
const isNode = require('detect-node')
10-
const bl = require('bl')
10+
const concat = require('concat-stream')
1111

1212
module.exports = (common) => {
13-
describe('.files', () => {
13+
describe.only('.files', () => {
1414
let smallFile
1515
let bigFile
1616
let ipfs
@@ -209,8 +209,7 @@ module.exports = (common) => {
209209
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
210210
ipfs.cat(hash, (err, stream) => {
211211
expect(err).to.not.exist
212-
stream.pipe(bl((err, data) => {
213-
expect(err).to.not.exist
212+
stream.pipe(concat((data) => {
214213
expect(data.toString()).to.contain('Check out some of the other files in this directory:')
215214
done()
216215
}))
@@ -221,8 +220,7 @@ module.exports = (common) => {
221220
const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
222221
ipfs.cat(mhBuf, (err, stream) => {
223222
expect(err).to.not.exist
224-
stream.pipe(bl((err, data) => {
225-
expect(err).to.not.exist
223+
stream.pipe(concat((data) => {
226224
expect(data.toString()).to.contain('Check out some of the other files in this directory:')
227225
done()
228226
}))
@@ -233,8 +231,7 @@ module.exports = (common) => {
233231
const hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
234232
ipfs.cat(hash, (err, stream) => {
235233
expect(err).to.not.exist
236-
stream.pipe(bl((err, data) => {
237-
expect(err).to.not.exist
234+
stream.pipe(concat((data) => {
238235
expect(data).to.deep.equal(bigFile)
239236
done()
240237
}))
@@ -246,8 +243,7 @@ module.exports = (common) => {
246243
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
247244
ipfs.cat(hash)
248245
.then((stream) => {
249-
stream.pipe(bl((err, data) => {
250-
expect(err).to.not.exist
246+
stream.pipe(concat((data) => {
251247
expect(data.toString()).to.contain('Check out some of the other files in this directory:')
252248
done()
253249
}))
@@ -278,8 +274,7 @@ module.exports = (common) => {
278274
const hash = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
279275
ipfs.cat(hash)
280276
.then((stream) => {
281-
stream.pipe(bl((err, bldata) => {
282-
expect(err).to.not.exist
277+
stream.pipe(concat((bldata) => {
283278
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
284279
done()
285280
}))
@@ -290,5 +285,91 @@ module.exports = (common) => {
290285
})
291286
})
292287
})
288+
289+
describe('.get', () => {
290+
it('with a base58 encoded multihash', (done) => {
291+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
292+
ipfs.files.get(hash, (err, stream) => {
293+
expect(err).to.not.exist
294+
stream.pipe(concat((files) => {
295+
expect(err).to.not.exist
296+
expect(files).to.be.length(1)
297+
expect(files[0].path).to.equal(hash)
298+
files[0].content.pipe(concat((content) => {
299+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
300+
done()
301+
}))
302+
}))
303+
})
304+
})
305+
306+
it('with a multihash', (done) => {
307+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
308+
const mhBuf = new Buffer(bs58.decode(hash))
309+
ipfs.files.get(mhBuf, (err, stream) => {
310+
expect(err).to.not.exist
311+
stream.pipe(concat((files) => {
312+
expect(files).to.be.length(1)
313+
expect(files[0].path).to.deep.equal(mhBuf)
314+
files[0].content.pipe(concat((content) => {
315+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
316+
done()
317+
}))
318+
}))
319+
})
320+
})
321+
322+
it('large file', (done) => {
323+
const hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
324+
ipfs.files.get(hash, (err, stream) => {
325+
expect(err).to.not.exist
326+
stream.pipe(concat((files) => {
327+
expect(files).to.be.length(1)
328+
expect(files[0].path).to.equal(hash)
329+
files[0].content.pipe(concat((content) => {
330+
expect(content).to.deep.equal(bigFile)
331+
done()
332+
}))
333+
}))
334+
})
335+
})
336+
337+
describe('promise', () => {
338+
it('with a base58 encoded string', (done) => {
339+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
340+
ipfs.files.get(hash)
341+
.then((stream) => {
342+
stream.pipe(concat((files) => {
343+
expect(files).to.be.length(1)
344+
expect(files[0].path).to.equal(hash)
345+
files[0].content.pipe(concat((content) => {
346+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
347+
done()
348+
}))
349+
}))
350+
})
351+
.catch((err) => {
352+
expect(err).to.not.exist
353+
})
354+
})
355+
356+
it('errors on invalid key', (done) => {
357+
const hash = 'somethingNotMultihash'
358+
ipfs.files.get(hash)
359+
.then((stream) => {})
360+
.catch((err) => {
361+
expect(err).to.exist
362+
const errString = err.toString()
363+
if (errString === 'Error: invalid ipfs ref path') {
364+
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
365+
}
366+
if (errString === 'Error: Invalid Key') {
367+
expect(err.toString()).to.contain('Error: Invalid Key')
368+
}
369+
done()
370+
})
371+
})
372+
})
373+
})
293374
})
294375
}

0 commit comments

Comments
 (0)