Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: enable preload on MFS commands that accept IPFS paths
Browse files Browse the repository at this point in the history
fixes #2335

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw committed Aug 14, 2019
1 parent 3cade67 commit 8fa7cb7
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions src/core/components/files-mfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ const callbackify = require('callbackify')
const PassThrough = require('stream').PassThrough
const pull = require('pull-stream/pull')
const map = require('pull-stream/throughs/map')
const isIpfs = require('is-ipfs')
const { cidToString } = require('../../utils/cid')

const mapLsFile = (options = {}) => {
const long = options.long || options.l

return (file) => {
return {
hash: long ? file.cid.toBaseEncodedString(options.cidBase) : '',
hash: long ? cidToString(file.cid, { base: options.cidBase }) : '',
name: file.name,
type: long ? file.type : 0,
size: long ? file.size || 0 : 0
Expand All @@ -32,15 +34,37 @@ module.exports = self => {
repoOwner: self._options.repoOwner
})

const withPreload = fn => (...args) => {
const cids = args.reduce((cids, p) => {
if (isIpfs.ipfsPath(p)) {
cids.push(p.split('/')[2])
} else if (isIpfs.cid(p)) {
cids.push(p)
} else if (isIpfs.cidPath(p)) {
cids.push(p.split('/')[0])
}
return cids
}, [])

if (cids.length) {
const options = args[args.length - 1]
if (options.preload !== false) {
cids.forEach(cid => self._preload(cid))
}
}

return fn(...args)
}

return {
cp: callbackify.variadic(methods.cp),
cp: callbackify.variadic(withPreload(methods.cp)),
flush: callbackify.variadic(methods.flush),
ls: callbackify.variadic(async (path, options = {}) => {
ls: callbackify.variadic(withPreload(async (path, options = {}) => {
const files = await all(methods.ls(path, options))

return files.map(mapLsFile(options))
}),
lsReadableStream: (path, options = {}) => {
})),
lsReadableStream: withPreload((path, options = {}) => {
const stream = toReadableStream.obj(methods.ls(path, options))
const through = new PassThrough({
objectMode: true
Expand All @@ -60,33 +84,33 @@ module.exports = self => {
})

return through
},
lsPullStream: (path, options = {}) => {
}),
lsPullStream: withPreload((path, options = {}) => {
return pull(
toPullStream.source(methods.ls(path, options)),
map(mapLsFile(options))
)
},
}),
mkdir: callbackify.variadic(methods.mkdir),
mv: callbackify.variadic(methods.mv),
read: callbackify(async (path, options = {}) => {
mv: callbackify.variadic(withPreload(methods.mv)),
read: callbackify(withPreload(async (path, options = {}) => {
return Buffer.concat(await all(methods.read(path, options)))
}),
readPullStream: (path, options = {}) => {
})),
readPullStream: withPreload((path, options = {}) => {
return toPullStream.source(methods.read(path, options))
},
readReadableStream: (path, options = {}) => {
}),
readReadableStream: withPreload((path, options = {}) => {
return toReadableStream(methods.read(path, options))
},
}),
rm: callbackify.variadic(methods.rm),
stat: callbackify(async (path, options = {}) => {
stat: callbackify(withPreload(async (path, options = {}) => {
const stats = await methods.stat(path, options)

stats.hash = stats.cid.toBaseEncodedString(options && options.cidBase)
stats.hash = cidToString(stats.cid, { base: options.cidBase })
delete stats.cid

return stats
}),
})),
write: callbackify.variadic(async (path, content, options = {}) => {
if (isPullStream.isSource(content)) {
content = pullStreamToAsyncIterator(content)
Expand Down

0 comments on commit 8fa7cb7

Please sign in to comment.