This repository has been 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
/
index.js
159 lines (147 loc) · 5.51 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict'
const isIPFS = require('is-ipfs')
const { Buffer } = require('buffer')
const CID = require('cids')
const multiaddr = require('multiaddr')
const multibase = require('multibase')
const multicodec = require('multicodec')
const multihash = require('multihashes')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const nodeify = require('promise-nodeify')
const callbackify = require('callbackify')
const all = require('it-all')
const toPullStream = require('async-iterator-to-pull-stream')
const toStream = require('it-to-stream')
const BufferList = require('bl/BufferList')
const { concatify, collectify, pullify, streamify } = require('./lib/converters')
function ipfsClient (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 ping = require('./ping')(config)
const refs = require('./refs')(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,
bitswap: require('./bitswap')(config),
block: require('./block')(config),
bootstrap: require('./bootstrap')(config),
cat: callbackify.variadic((path, options) => concatify(cat)(path, options)),
catReadableStream: streamify.readable(cat),
catPullStream: pullify.source(cat),
_catAsyncIterator: cat,
commands: callbackify.variadic(require('./commands')(config)),
config: require('./config')(config),
dag: require('./dag')(config),
dht: require('./dht')(config),
diag: require('./diag')(config),
dns: callbackify.variadic(require('./dns')(config)),
files: require('./files')(config),
get: callbackify.variadic(async (path, options) => {
const output = []
for await (const entry of get(path, options)) {
if (entry.content) {
entry.content = new BufferList(await all(entry.content)).slice()
}
output.push(entry)
}
return output
}),
getEndpointConfig: require('./get-endpoint-config')(config),
getReadableStream: streamify.readable(async function * (path, options) {
for await (const file of get(path, options)) {
if (file.content) {
const { content } = file
file.content = toStream((async function * () {
for await (const chunk of content) {
yield chunk.slice() // Convert bl to Buffer
}
})())
}
yield file
}
}),
getPullStream: pullify.source(async function * (path, options) {
for await (const file of get(path, options)) {
if (file.content) {
const { content } = file
file.content = toPullStream((async function * () {
for await (const chunk of content) {
yield chunk.slice() // Convert bl to Buffer
}
})())
}
yield file
}
}),
_getAsyncIterator: get,
id: callbackify.variadic(require('./id')(config)),
key: require('./key')(config),
log: require('./log')(config),
ls: callbackify.variadic((path, options) => collectify(ls)(path, options)),
lsReadableStream: streamify.readable(ls),
lsPullStream: pullify.source(ls),
_lsAsyncIterator: ls,
mount: callbackify.variadic(require('./mount')(config)),
name: require('./name')(config),
object: require('./object')(config),
pin: require('./pin')(config),
ping: callbackify.variadic(collectify(ping)),
pingReadableStream: streamify.readable(ping),
pingPullStream: pullify.source(ping),
pubsub: require('./pubsub')(config),
refs: callbackify.variadic((path, options) => collectify(refs)(path, options)),
refsReadableStream: streamify.readable(refs),
refsPullStream: pullify.source(refs),
_refsAsyncIterator: refs,
repo: require('./repo')(config),
resolve: callbackify.variadic(require('./resolve')(config)),
stats: require('./stats')(config),
stop: callbackify.variadic(require('./stop')(config)),
shutdown: callbackify.variadic(require('./stop')(config)),
swarm: require('./swarm')(config),
version: callbackify.variadic(require('./version')(config))
}
Object.assign(api.refs, {
local: callbackify.variadic(options => collectify(refs.local)(options)),
localReadableStream: streamify.readable(refs.local),
localPullStream: pullify.source(refs.local),
_localAsyncIterator: refs.local
})
return api
}
Object.assign(ipfsClient, { isIPFS, Buffer, CID, multiaddr, multibase, multicodec, multihash, PeerId, PeerInfo })
module.exports = ipfsClient