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 (60 loc) · 1.59 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 { Buffer } = require('buffer')
const CID = require('cids')
const configure = require('./lib/configure')
module.exports = configure(({ ky }) => {
return async function * ls (path, options) {
options = options || {}
const searchParams = new URLSearchParams()
searchParams.set('arg', `${Buffer.isBuffer(path) ? new CID(path) : path}`)
if (options.long !== undefined) {
searchParams.set('long', options.long)
}
if (options.unsorted !== undefined) {
searchParams.set('unsorted', options.unsorted)
}
if (options.recursive !== undefined) {
searchParams.set('recursive', options.recursive)
}
const res = await ky.post('ls', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
})
let result = await res.json()
result = result.Objects
if (!result) {
throw new Error('expected .Objects in results')
}
result = result[0]
if (!result) {
throw new Error('expected one array in results.Objects')
}
result = result.Links
if (!Array.isArray(result)) {
throw new Error('expected one array in results.Objects[0].Links')
}
for (const link of result) {
yield {
name: link.Name,
path: path + '/' + link.Name,
size: link.Size,
hash: link.Hash,
type: typeOf(link),
depth: link.Depth || 1
}
}
}
})
function typeOf (link) {
switch (link.Type) {
case 1:
case 5:
return 'dir'
case 2:
return 'file'
default:
return 'unknown'
}
}