Skip to content

Commit b10e247

Browse files
ikreymerachingbrain
andcommitted
fix: fix ipfs.ls() for a single file object (#3440)
This fixes a typo where `ipfs.ls()` returns instead of yielding from a generator. The result is that ipfs.ls() does not work when the path is a file. I think this should fix it, with `ls` returning a generator with a single file result. Co-authored-by: achingbrain <[email protected]>
1 parent abb64f3 commit b10e247

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

src/ls.js

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,54 @@
33
const CID = require('cids')
44
const configure = require('./lib/configure')
55
const toUrlSearchParams = require('./lib/to-url-search-params')
6+
const stat = require('./files/stat')
67

7-
module.exports = configure(api => {
8+
module.exports = configure((api, opts) => {
89
return async function * ls (path, options = {}) {
10+
const pathStr = `${path instanceof Uint8Array ? new CID(path) : path}`
11+
12+
async function mapLink (link) {
13+
let hash = link.Hash
14+
15+
if (hash.includes('/')) {
16+
// the hash is a path, but we need the CID
17+
const ipfsPath = hash.startsWith('/ipfs/') ? hash : `/ipfs/${hash}`
18+
const stats = await stat(opts)(ipfsPath)
19+
20+
hash = stats.cid
21+
}
22+
23+
const entry = {
24+
name: link.Name,
25+
path: pathStr + (link.Name ? `/${link.Name}` : ''),
26+
size: link.Size,
27+
cid: new CID(hash),
28+
type: typeOf(link),
29+
depth: link.Depth || 1
30+
}
31+
32+
if (link.Mode) {
33+
entry.mode = parseInt(link.Mode, 8)
34+
}
35+
36+
if (link.Mtime !== undefined && link.Mtime !== null) {
37+
entry.mtime = {
38+
secs: link.Mtime
39+
}
40+
41+
if (link.MtimeNsecs !== undefined && link.MtimeNsecs !== null) {
42+
entry.mtime.nsecs = link.MtimeNsecs
43+
}
44+
}
45+
46+
return entry
47+
}
48+
949
const res = await api.post('ls', {
1050
timeout: options.timeout,
1151
signal: options.signal,
1252
searchParams: toUrlSearchParams({
13-
arg: `${path instanceof Uint8Array ? new CID(path) : path}`,
53+
arg: pathStr,
1454
...options
1555
}),
1656
headers: options.headers
@@ -28,37 +68,19 @@ module.exports = configure(api => {
2868
throw new Error('expected one array in results.Objects')
2969
}
3070

31-
result = result.Links
32-
if (!Array.isArray(result)) {
71+
const links = result.Links
72+
if (!Array.isArray(links)) {
3373
throw new Error('expected one array in results.Objects[0].Links')
3474
}
3575

36-
for (const link of result) {
37-
const entry = {
38-
name: link.Name,
39-
path: path + '/' + link.Name,
40-
size: link.Size,
41-
cid: new CID(link.Hash),
42-
type: typeOf(link),
43-
depth: link.Depth || 1
44-
}
76+
if (!links.length) {
77+
// no links, this is a file, yield a single result
78+
yield mapLink(result)
4579

46-
if (link.Mode) {
47-
entry.mode = parseInt(link.Mode, 8)
48-
}
49-
50-
if (link.Mtime !== undefined && link.Mtime !== null) {
51-
entry.mtime = {
52-
secs: link.Mtime
53-
}
54-
55-
if (link.MtimeNsecs !== undefined && link.MtimeNsecs !== null) {
56-
entry.mtime.nsecs = link.MtimeNsecs
57-
}
58-
}
59-
60-
yield entry
80+
return
6181
}
82+
83+
yield * links.map(mapLink)
6284
}
6385
}
6486
})

0 commit comments

Comments
 (0)