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 298
/
Copy pathindex.js
100 lines (91 loc) · 3.21 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict'
const nodeify = require('promise-nodeify')
const callbackify = require('callbackify')
const all = require('async-iterator-all')
const { concatify, collectify, pullify, streamify } = require('../lib/converters')
const toPullStream = require('async-iterator-to-pull-stream')
const pull = require('pull-stream/pull')
const map = require('pull-stream/throughs/map')
module.exports = (config) => {
const add = require('../add')(config)
const addFromFs = require('../add-from-fs')(config)
const addFromURL = require('../add-from-url')(config)
const cat = require('../cat')(config)
const get = require('./get')(config)
const ls = require('./ls')(config)
const refs = require('./refs')(config)
const refsLocal = require('./refs-local')(config)
const api = {
add: (input, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
return nodeify(collectify(add)(input, options), callback)
},
addReadableStream: streamify.transform(add),
addPullStream: pullify.transform(add),
addFromFs: (path, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
return nodeify(collectify(addFromFs)(path, options), callback)
},
addFromURL: (url, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
return nodeify(collectify(addFromURL)(url, options), callback)
},
addFromStream: (input, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
return nodeify(collectify(add)(input, options), callback)
},
_addAsyncIterator: add,
cat: callbackify.variadic((path, options) => concatify(cat)(path, options)),
catReadableStream: streamify.readable(cat),
catPullStream: pullify.source(cat),
_catAsyncIterator: cat,
get: callbackify.variadic(async (path, options) => {
const output = []
for await (const entry of get(path, options)) {
if (entry.content) {
entry.content = Buffer.concat(await all(entry.content))
}
output.push(entry)
}
return output
}),
getReadableStream: streamify.readable(get),
getPullStream: (path, options) => {
return pull(
toPullStream(get(path, options)),
map(file => {
if (file.content) {
file.content = toPullStream(file.content)
}
return file
})
)
},
_getAsyncIterator: get,
ls: callbackify.variadic((path, options) => collectify(ls)(path, options)),
lsReadableStream: streamify.readable(ls),
lsPullStream: pullify.source(ls),
_lsAsyncIterator: ls,
refs: callbackify.variadic((path, options) => collectify(refs)(path, options)),
refsReadableStream: streamify.readable(refs),
refsPullStream: pullify.source(refs),
_refsAsyncIterator: refs
}
api.refs.local = callbackify.variadic((options) => collectify(refsLocal)(options))
api.refs.localReadableStream = streamify.readable(refsLocal)
api.refs.localPullStream = pullify.source(refsLocal)
api.refs._localAsyncIterator = refsLocal
return api
}