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 297
/
Copy pathindex.js
61 lines (47 loc) · 1.5 KB
/
index.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
'use strict'
const configure = require('../lib/configure')
const { Buffer } = require('buffer')
const CID = require('cids')
const ndjson = require('iterable-ndjson')
const toIterable = require('../lib/stream-to-iterable')
const toCamel = require('../lib/object-to-camel')
module.exports = config => {
const refs = (configure(({ ky }) => {
return async function * refs (args, options) {
options = options || {}
const searchParams = new URLSearchParams()
if (options.format !== undefined) {
searchParams.set('format', options.format)
}
if (options.edges !== undefined) {
searchParams.set('edges', options.edges)
}
if (options.unique !== undefined) {
searchParams.set('unique', options.unique)
}
if (options.recursive !== undefined) {
searchParams.set('recursive', options.recursive)
}
if (options.maxDepth !== undefined) {
searchParams.set('max-depth', options.maxDepth)
}
if (!Array.isArray(args)) {
args = [args]
}
for (const arg of args) {
searchParams.append('arg', `${Buffer.isBuffer(arg) ? new CID(arg) : arg}`)
}
const res = await ky.post('refs', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
})
for await (const file of ndjson(toIterable(res.body))) {
yield toCamel(file)
}
}
}))(config)
refs.local = require('./local')(config)
return refs
}