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

Commit 705855e

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
feat: add files.ls*Stream methods (#903)
1 parent c191eea commit 705855e

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"eslint-plugin-react": "^7.11.1",
8686
"go-ipfs-dep": "~0.4.18",
8787
"gulp": "^3.9.1",
88-
"interface-ipfs-core": "~0.88.0",
88+
"interface-ipfs-core": "~0.90.0",
8989
"ipfsd-ctl": "~0.40.0",
9090
"nock": "^10.0.2",
9191
"pull-stream": "^3.6.9",

src/files-mfs/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = (arg) => {
1212
stat: require('./stat')(send),
1313
rm: require('./rm')(send),
1414
ls: require('./ls')(send),
15+
lsReadableStream: require('./ls-readable-stream')(send),
16+
lsPullStream: require('./ls-pull-stream')(send),
1517
read: require('./read')(send),
1618
readReadableStream: require('./read-readable-stream')(send),
1719
readPullStream: require('./read-pull-stream')(send),

src/files-mfs/ls-pull-stream.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
const toPull = require('stream-to-pull-stream')
4+
const lsReadableStream = require('./ls-readable-stream')
5+
6+
module.exports = (send) => {
7+
return (args, opts) => {
8+
opts = opts || {}
9+
10+
return toPull.source(lsReadableStream(send)(args, opts))
11+
}
12+
}

src/files-mfs/ls-readable-stream.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
const {
4+
Transform,
5+
PassThrough
6+
} = require('stream')
7+
const pump = require('pump')
8+
const ndjson = require('ndjson')
9+
const isStream = require('is-stream')
10+
11+
const toEntry = (entry) => {
12+
return {
13+
name: entry.Name,
14+
type: entry.Type,
15+
size: entry.Size,
16+
hash: entry.Hash
17+
}
18+
}
19+
20+
module.exports = (send) => {
21+
return (args, opts) => {
22+
opts = opts || {}
23+
24+
const transform = new Transform({
25+
objectMode: true,
26+
27+
transform (entry, encoding, callback) {
28+
callback(null, toEntry(entry))
29+
}
30+
})
31+
32+
const output = new PassThrough({
33+
objectMode: true
34+
})
35+
36+
send({
37+
path: 'files/ls',
38+
args: args,
39+
qs: {
40+
...opts,
41+
stream: true
42+
}
43+
}, (err, res) => {
44+
if (err) {
45+
return output.destroy(err)
46+
}
47+
48+
if (isStream(res)) {
49+
const parse = ndjson.parse()
50+
51+
pump(res, parse, transform, output)
52+
} else {
53+
const entries = res.Entries || []
54+
55+
entries.forEach((entry) => {
56+
output.write(toEntry(entry))
57+
})
58+
59+
output.end()
60+
}
61+
})
62+
63+
return output
64+
}
65+
}

test/interface.spec.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,14 @@ describe('interface-ipfs-core tests', () => {
163163
]
164164
})
165165

166-
tests.filesMFS(defaultCommonFactory)
166+
tests.filesMFS(defaultCommonFactory, {
167+
only: [
168+
{
169+
name: 'should ls directory',
170+
reason: 'TODO not impemented in go-ipfs yet'
171+
}
172+
]
173+
})
167174

168175
tests.key(defaultCommonFactory, {
169176
skip: [

0 commit comments

Comments
 (0)