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

Commit 297b134

Browse files
committed
feat: expose mfs files.read*Stream methods
1 parent 8536ee4 commit 297b134

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/files/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module.exports = (arg) => {
2424
rm: require('./rm')(send),
2525
ls: require('./ls')(send),
2626
read: require('./read')(send),
27+
readReadableStream: require('./read-readable-stream')(send),
28+
readPullStream: require('./read-pull-stream')(send),
2729
write: require('./write')(send),
2830
mv: require('./mv')(send)
2931
}

src/files/read-pull-stream.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const toPull = require('stream-to-pull-stream')
4+
const deferred = require('pull-defer')
5+
6+
module.exports = (send) => {
7+
return (args, opts) => {
8+
opts = opts || {}
9+
10+
const p = deferred.source()
11+
12+
send({
13+
path: 'files/read',
14+
args: args,
15+
qs: opts
16+
}, (err, stream) => {
17+
if (err) {
18+
return p.abort(err)
19+
}
20+
21+
p.resolve(toPull(stream))
22+
})
23+
24+
return p
25+
}
26+
}

src/files/read-readable-stream.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const Stream = require('readable-stream')
4+
const pump = require('pump')
5+
6+
module.exports = (send) => {
7+
return (args, opts) => {
8+
opts = opts || {}
9+
10+
const pt = new Stream.PassThrough()
11+
12+
send({
13+
path: 'files/read',
14+
args: args,
15+
qs: opts
16+
}, (err, stream) => {
17+
if (err) {
18+
return pt.destroy(err)
19+
}
20+
21+
pump(stream, pt)
22+
})
23+
24+
return pt
25+
}
26+
}

0 commit comments

Comments
 (0)