forked from ipfs-inactive/js-ipfs-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
269 lines (230 loc) · 5.91 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use strict'
const Promise = require('promise')
const multiaddr = require('multiaddr')
const getConfig = require('./config')
const getRequestAPI = require('./request-api')
const isNode = !global.window
// -- Internal
function command (send, name) {
return (opts, cb) => {
if (typeof (opts) === 'function') {
cb = opts
opts = {}
}
return send(name, null, opts, null, cb)
}
}
function argCommand (send, name) {
return (arg, opts, cb) => {
if (typeof (opts) === 'function') {
cb = opts
opts = {}
}
return send(name, arg, opts, null, cb)
}
}
class API {
constructor (host_or_multiaddr, port) {
const config = getConfig()
try {
const maddr = multiaddr(host_or_multiaddr).nodeAddress()
config.host = maddr.address
config.port = maddr.port
} catch (e) {
config.host = host_or_multiaddr
config.port = port || config.port
}
// autoconfigure in browser
if (!config.host && !isNode) {
const split = window.location.host.split(':')
config.host = split[0]
config.port = split[1]
}
this.send = getRequestAPI(config)
this.Buffer = Buffer
}
// -- Interface
add (files, opts, cb) {
if (typeof (opts) === 'function' && cb === undefined) {
cb = opts
opts = {}
}
return this.send('add', null, opts, files, cb)
}
mount (ipfs, ipns, cb) {
if (typeof ipfs === 'function') {
cb = ipfs
ipfs = null
} else if (typeof ipns === 'function') {
cb = ipns
ipns = null
}
const opts = {}
if (ipfs) opts.f = ipfs
if (ipns) opts.n = ipns
return this.send('mount', null, opts, null, cb)
}
ping (id, cb) {
return this.send('ping', id, { n: 1 }, null, function (err, res) {
if (err) return cb(err, null)
cb(null, res[1])
})
}
get id () {
return Promise.nodeify(function (id) {
if (typeof id === 'function') {
id = null
}
return this.send('id', id, null, null)
})
}
get cat () {
return argCommand(this.send, 'cat')
}
get ls () {
return argCommand(this.send, 'ls')
}
get version () {
return command(this.send, 'version')
}
get commands () {
return command(this.send, 'commands')
}
get config () {
return {
get: argCommand(this.send, 'config'),
set: (key, value, opts, cb) => {
if (typeof (opts) === 'function') {
cb = opts
opts = {}
}
return this.send('config', [key, value], opts, null, cb)
},
show: cb => {
return this.send('config/show', null, null, null, true, cb)
},
replace: (file, cb) => {
return this.send('config/replace', null, null, file, cb)
}
}
}
get update () {
return {
apply: command(this.send, 'update'),
check: command(this.send, 'update/check'),
log: command(this.send, 'update/log')
}
}
get diag () {
return {
net: command(this.send, 'diag/net')
}
}
get block () {
return {
get: argCommand(this.send, 'block/get'),
put: (file, cb) => {
if (Array.isArray(file)) {
return cb(null, new Error('block.put() only accepts 1 file'))
}
return this.send('block/put', null, null, file, cb)
}
}
}
get object () {
return {
get: argCommand(this.send, 'object/get'),
put: (file, encoding, cb) => {
if (typeof encoding === 'function') {
return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')"))
}
return this.send('object/put', encoding, null, file, cb)
},
data: argCommand(this.send, 'object/data'),
stat: argCommand(this.send, 'object/stat'),
links: argCommand(this.send, 'object/links')
}
}
get swarm () {
return {
peers: command(this.send, 'swarm/peers'),
connect: argCommand(this.send, 'swarm/connect')
}
}
get pin () {
return {
add: (hash, opts, cb) => {
if (typeof opts === 'function') {
cb = opts
opts = null
}
this.send('pin/add', hash, opts, null, cb)
},
remove: (hash, opts, cb) => {
if (typeof opts === 'function') {
cb = opts
opts = null
}
this.send('pin/rm', hash, opts, null, cb)
},
list: (type, cb) => {
if (typeof type === 'function') {
cb = type
type = null
}
let opts = null
if (type) opts = { type: type }
return this.send('pin/ls', null, opts, null, cb)
}
}
}
get log () {
return {
tail: cb => this.send('log/tail', null, {enc: 'text'}, null, true, cb)
}
}
get name () {
return {
publish: argCommand(this.send, 'name/publish'),
resolve: argCommand(this.send, 'name/resolve')
}
}
get refs () {
const cmd = argCommand(this.send, 'refs')
cmd.local = command(this.send, 'refs/local')
return cmd
}
get dht () {
return {
findprovs: argCommand(this.send, 'dht/findprovs'),
get: (key, opts, cb) => {
if (typeof (opts) === 'function' && !cb) {
cb = opts
opts = null
}
return this.send('dht/get', key, opts, null, function (err, res) {
if (err) return cb(err)
if (!res) return cb(new Error('empty response'))
if (res.length === 0) return cb(new Error('no value returned for key'))
// Inconsistent return values in the browser vs node
if (Array.isArray(res)) {
res = res[0]
}
if (res.Type === 5) {
cb(null, res.Extra)
} else {
cb(res)
}
})
},
put: (key, value, opts, cb) => {
if (typeof (opts) === 'function' && !cb) {
cb = opts
opts = null
}
return this.send('dht/put', [key, value], opts, null, cb)
}
}
}
}
exports = module.exports = API