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

Commit 8fa7cb7

Browse files
author
Alan Shaw
committed
fix: enable preload on MFS commands that accept IPFS paths
fixes #2335 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 3cade67 commit 8fa7cb7

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

src/core/components/files-mfs.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ const callbackify = require('callbackify')
1010
const PassThrough = require('stream').PassThrough
1111
const pull = require('pull-stream/pull')
1212
const map = require('pull-stream/throughs/map')
13+
const isIpfs = require('is-ipfs')
14+
const { cidToString } = require('../../utils/cid')
1315

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

1719
return (file) => {
1820
return {
19-
hash: long ? file.cid.toBaseEncodedString(options.cidBase) : '',
21+
hash: long ? cidToString(file.cid, { base: options.cidBase }) : '',
2022
name: file.name,
2123
type: long ? file.type : 0,
2224
size: long ? file.size || 0 : 0
@@ -32,15 +34,37 @@ module.exports = self => {
3234
repoOwner: self._options.repoOwner
3335
})
3436

37+
const withPreload = fn => (...args) => {
38+
const cids = args.reduce((cids, p) => {
39+
if (isIpfs.ipfsPath(p)) {
40+
cids.push(p.split('/')[2])
41+
} else if (isIpfs.cid(p)) {
42+
cids.push(p)
43+
} else if (isIpfs.cidPath(p)) {
44+
cids.push(p.split('/')[0])
45+
}
46+
return cids
47+
}, [])
48+
49+
if (cids.length) {
50+
const options = args[args.length - 1]
51+
if (options.preload !== false) {
52+
cids.forEach(cid => self._preload(cid))
53+
}
54+
}
55+
56+
return fn(...args)
57+
}
58+
3559
return {
36-
cp: callbackify.variadic(methods.cp),
60+
cp: callbackify.variadic(withPreload(methods.cp)),
3761
flush: callbackify.variadic(methods.flush),
38-
ls: callbackify.variadic(async (path, options = {}) => {
62+
ls: callbackify.variadic(withPreload(async (path, options = {}) => {
3963
const files = await all(methods.ls(path, options))
4064

4165
return files.map(mapLsFile(options))
42-
}),
43-
lsReadableStream: (path, options = {}) => {
66+
})),
67+
lsReadableStream: withPreload((path, options = {}) => {
4468
const stream = toReadableStream.obj(methods.ls(path, options))
4569
const through = new PassThrough({
4670
objectMode: true
@@ -60,33 +84,33 @@ module.exports = self => {
6084
})
6185

6286
return through
63-
},
64-
lsPullStream: (path, options = {}) => {
87+
}),
88+
lsPullStream: withPreload((path, options = {}) => {
6589
return pull(
6690
toPullStream.source(methods.ls(path, options)),
6791
map(mapLsFile(options))
6892
)
69-
},
93+
}),
7094
mkdir: callbackify.variadic(methods.mkdir),
71-
mv: callbackify.variadic(methods.mv),
72-
read: callbackify(async (path, options = {}) => {
95+
mv: callbackify.variadic(withPreload(methods.mv)),
96+
read: callbackify(withPreload(async (path, options = {}) => {
7397
return Buffer.concat(await all(methods.read(path, options)))
74-
}),
75-
readPullStream: (path, options = {}) => {
98+
})),
99+
readPullStream: withPreload((path, options = {}) => {
76100
return toPullStream.source(methods.read(path, options))
77-
},
78-
readReadableStream: (path, options = {}) => {
101+
}),
102+
readReadableStream: withPreload((path, options = {}) => {
79103
return toReadableStream(methods.read(path, options))
80-
},
104+
}),
81105
rm: callbackify.variadic(methods.rm),
82-
stat: callbackify(async (path, options = {}) => {
106+
stat: callbackify(withPreload(async (path, options = {}) => {
83107
const stats = await methods.stat(path, options)
84108

85-
stats.hash = stats.cid.toBaseEncodedString(options && options.cidBase)
109+
stats.hash = cidToString(stats.cid, { base: options.cidBase })
86110
delete stats.cid
87111

88112
return stats
89-
}),
113+
})),
90114
write: callbackify.variadic(async (path, content, options = {}) => {
91115
if (isPullStream.isSource(content)) {
92116
content = pullStreamToAsyncIterator(content)

0 commit comments

Comments
 (0)