This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathls.js
73 lines (62 loc) · 1.49 KB
/
ls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
const promisify = require('promisify-es6')
const IsIpfs = require('is-ipfs')
const moduleConfig = require('../utils/module-config')
const cleanCID = require('../utils/clean-cid')
module.exports = (arg) => {
const send = moduleConfig(arg)
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
try {
args = cleanCID(args)
} catch (err) {
if (!IsIpfs.ipfsPath(args)) {
return callback(err)
}
}
send({
path: 'ls',
args: args,
qs: opts
}, (err, results) => {
if (err) {
return callback(err)
}
let result = results.Objects
if (!result) {
return callback(new Error('expected .Objects in results'))
}
result = result[0]
if (!result) {
return callback(new Error('expected one array in results.Objects'))
}
result = result.Links
if (!Array.isArray(result)) {
return callback(new Error('expected one array in results.Objects[0].Links'))
}
result = result.map((link) => ({
name: link.Name,
path: args + '/' + link.Name,
size: link.Size,
hash: link.Hash,
type: typeOf(link),
depth: link.Depth || 1
}))
callback(null, result)
})
})
}
function typeOf (link) {
switch (link.Type) {
case 1:
case 5:
return 'dir'
case 2:
return 'file'
default:
return 'unknown'
}
}