forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ipfs#29 from ipfs/add-ls-stream-methods
feat: adds ls streaming methods
- Loading branch information
Showing
5 changed files
with
207 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
'use strict' | ||
|
||
const waterfall = require('async/waterfall') | ||
const UnixFs = require('ipfs-unixfs') | ||
const exporter = require('ipfs-unixfs-exporter') | ||
const { | ||
loadNode, | ||
formatCid, | ||
toMfsPath, | ||
FILE_SEPARATOR, | ||
FILE_TYPES | ||
} = require('./utils') | ||
const pull = require('pull-stream/pull') | ||
const collect = require('pull-stream/sinks/collect') | ||
const asyncMap = require('pull-stream/throughs/async-map') | ||
const filter = require('pull-stream/throughs/filter') | ||
const once = require('pull-stream/sources/once') | ||
const error = require('pull-stream/sources/error') | ||
const defer = require('pull-defer') | ||
|
||
const defaultOptions = { | ||
long: false, | ||
cidBase: 'base58btc' | ||
} | ||
|
||
module.exports = (context) => { | ||
return function mfsLs (path, options = {}) { | ||
if (typeof path === 'object') { | ||
options = path | ||
path = FILE_SEPARATOR | ||
} | ||
|
||
options = Object.assign({}, defaultOptions, options) | ||
|
||
options.long = options.l || options.long | ||
|
||
const deferred = defer.source() | ||
|
||
waterfall([ | ||
(cb) => toMfsPath(context, path, cb), | ||
({ mfsPath, depth }, cb) => { | ||
pull( | ||
exporter(mfsPath, context.ipld, { | ||
maxDepth: depth | ||
}), | ||
|
||
collect((err, files) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
if (files.length > 1) { | ||
return cb(new Error(`Path ${path} had ${files.length} roots`)) | ||
} | ||
|
||
const file = files[0] | ||
|
||
if (!file) { | ||
return cb(new Error(`${path} does not exist`)) | ||
} | ||
|
||
if (file.type !== 'dir') { | ||
return cb(null, once(file)) | ||
} | ||
|
||
let first = true | ||
|
||
return cb(null, pull( | ||
exporter(mfsPath, context.ipld, { | ||
maxDepth: depth + 1 | ||
}), | ||
// first item in list is the directory node | ||
filter(() => { | ||
if (first) { | ||
first = false | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
)) | ||
}) | ||
) | ||
}, | ||
(source, cb) => { | ||
cb(null, | ||
pull( | ||
source, | ||
|
||
// load DAGNodes for each file | ||
asyncMap((file, cb) => { | ||
if (!options.long) { | ||
return cb(null, { | ||
name: file.name, | ||
type: 0, | ||
size: 0, | ||
hash: '' | ||
}) | ||
} | ||
|
||
loadNode(context, { | ||
cid: file.hash | ||
}, (err, result) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
const meta = UnixFs.unmarshal(result.node.data) | ||
|
||
cb(null, { | ||
name: file.name, | ||
type: FILE_TYPES[meta.type], | ||
hash: formatCid(file.hash, options.cidBase), | ||
size: meta.fileSize() || 0 | ||
}) | ||
}) | ||
}) | ||
) | ||
) | ||
} | ||
], (err, source) => { | ||
if (err) { | ||
return deferred.resolve(error(err)) | ||
} | ||
|
||
deferred.resolve(source) | ||
}) | ||
|
||
return deferred | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
const lsPullStream = require('./ls-pull-stream') | ||
const toStream = require('pull-stream-to-stream') | ||
|
||
module.exports = (context) => { | ||
return function mfsLsReadableStream (path, options = {}) { | ||
return toStream.source(lsPullStream(context)(path, options)) | ||
} | ||
} |
Oops, something went wrong.