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

Commit 7f71e1a

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

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
if (typeof (opts) === 'function') {
9+
callback = opts
10+
opts = {}
11+
}
12+
13+
const p = deferred.source()
14+
15+
send({
16+
path: 'files/read',
17+
args: args,
18+
qs: opts
19+
}, (err, stream) => {
20+
if (err) {
21+
return p.abort(err)
22+
}
23+
24+
p.resolve(toPull(stream))
25+
})
26+
27+
return p
28+
}
29+
}

src/files/read-readable-stream.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
if (typeof (opts) === 'function') {
9+
callback = opts
10+
opts = {}
11+
}
12+
13+
const pt = new Stream.PassThrough()
14+
15+
send({
16+
path: 'files/read',
17+
args: args,
18+
qs: opts
19+
}, (err, stream) => {
20+
if (err) {
21+
return pt.destroy(err)
22+
}
23+
24+
pump(stream, pt)
25+
})
26+
27+
return pt
28+
}
29+
}

0 commit comments

Comments
 (0)