This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathread-readable-stream.js
67 lines (53 loc) · 1.72 KB
/
read-readable-stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* eslint-env mocha */
'use strict'
const series = require('async/series')
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const bl = require('bl')
module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()
describe('.files.readReadableStream', function () {
this.timeout(40 * 1000)
let ipfs
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)
common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})
after((done) => common.teardown(done))
it('should not read not found, expect error', (done) => {
const testDir = `/test-${hat()}`
const stream = ipfs.files.readReadableStream(`${testDir}/404`)
stream.once('error', (err) => {
expect(err).to.exist()
expect(err.message).to.contain('does not exist')
done()
})
})
it('should read file', (done) => {
const testDir = `/test-${hat()}`
series([
(cb) => ipfs.files.mkdir(testDir, cb),
(cb) => ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true }, cb)
], (err) => {
expect(err).to.not.exist()
const stream = ipfs.files.readReadableStream(`${testDir}/a`)
stream.pipe(bl((err, buf) => {
expect(err).to.not.exist()
expect(buf).to.eql(Buffer.from('Hello, world!'))
done()
}))
})
})
})
}