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

Commit

Permalink
feat: add files.ls*Stream methods (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored and Alan Shaw committed Dec 5, 2018
1 parent c191eea commit 705855e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"eslint-plugin-react": "^7.11.1",
"go-ipfs-dep": "~0.4.18",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.88.0",
"interface-ipfs-core": "~0.90.0",
"ipfsd-ctl": "~0.40.0",
"nock": "^10.0.2",
"pull-stream": "^3.6.9",
Expand Down
2 changes: 2 additions & 0 deletions src/files-mfs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = (arg) => {
stat: require('./stat')(send),
rm: require('./rm')(send),
ls: require('./ls')(send),
lsReadableStream: require('./ls-readable-stream')(send),
lsPullStream: require('./ls-pull-stream')(send),
read: require('./read')(send),
readReadableStream: require('./read-readable-stream')(send),
readPullStream: require('./read-pull-stream')(send),
Expand Down
12 changes: 12 additions & 0 deletions src/files-mfs/ls-pull-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const toPull = require('stream-to-pull-stream')
const lsReadableStream = require('./ls-readable-stream')

module.exports = (send) => {
return (args, opts) => {
opts = opts || {}

return toPull.source(lsReadableStream(send)(args, opts))
}
}
65 changes: 65 additions & 0 deletions src/files-mfs/ls-readable-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

const {
Transform,
PassThrough
} = require('stream')
const pump = require('pump')
const ndjson = require('ndjson')
const isStream = require('is-stream')

const toEntry = (entry) => {
return {
name: entry.Name,
type: entry.Type,
size: entry.Size,
hash: entry.Hash
}
}

module.exports = (send) => {
return (args, opts) => {
opts = opts || {}

const transform = new Transform({
objectMode: true,

transform (entry, encoding, callback) {
callback(null, toEntry(entry))
}
})

const output = new PassThrough({
objectMode: true
})

send({
path: 'files/ls',
args: args,
qs: {
...opts,
stream: true
}
}, (err, res) => {
if (err) {
return output.destroy(err)
}

if (isStream(res)) {
const parse = ndjson.parse()

pump(res, parse, transform, output)
} else {
const entries = res.Entries || []

entries.forEach((entry) => {
output.write(toEntry(entry))
})

output.end()
}
})

return output
}
}
9 changes: 8 additions & 1 deletion test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,14 @@ describe('interface-ipfs-core tests', () => {
]
})

tests.filesMFS(defaultCommonFactory)
tests.filesMFS(defaultCommonFactory, {
only: [
{
name: 'should ls directory',
reason: 'TODO not impemented in go-ipfs yet'
}
]
})

tests.key(defaultCommonFactory, {
skip: [
Expand Down

0 comments on commit 705855e

Please sign in to comment.