|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const multiaddr = require('multiaddr')
|
| 4 | + |
| 5 | +const loadCommands = require('./load-commands') |
4 | 6 | const getConfig = require('./config')
|
5 | 7 | const getRequestAPI = require('./request-api')
|
6 |
| -const Wreck = require('wreck') |
7 |
| -const ndjson = require('ndjson') |
8 | 8 |
|
9 | 9 | exports = module.exports = IpfsAPI
|
10 | 10 |
|
11 | 11 | function IpfsAPI (host_or_multiaddr, port, opts) {
|
12 |
| - const self = this |
13 | 12 | const config = getConfig()
|
14 | 13 |
|
15 |
| - if (!(self instanceof IpfsAPI)) { |
16 |
| - return new IpfsAPI(host_or_multiaddr, port, opts) |
17 |
| - } |
18 |
| - |
19 | 14 | try {
|
20 | 15 | const maddr = multiaddr(host_or_multiaddr).nodeAddress()
|
21 | 16 | config.host = maddr.address
|
@@ -44,247 +39,9 @@ function IpfsAPI (host_or_multiaddr, port, opts) {
|
44 | 39 | }
|
45 | 40 |
|
46 | 41 | const requestAPI = getRequestAPI(config)
|
| 42 | + const cmds = loadCommands(requestAPI) |
| 43 | + cmds.send = requestAPI |
| 44 | + cmds.Buffer = Buffer |
47 | 45 |
|
48 |
| - // -- Internal |
49 |
| - |
50 |
| - function command (name) { |
51 |
| - return (opts, cb) => { |
52 |
| - if (typeof (opts) === 'function') { |
53 |
| - cb = opts |
54 |
| - opts = {} |
55 |
| - } |
56 |
| - return requestAPI(name, null, opts, null, cb) |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - function argCommand (name) { |
61 |
| - return (arg, opts, cb) => { |
62 |
| - if (typeof (opts) === 'function') { |
63 |
| - cb = opts |
64 |
| - opts = {} |
65 |
| - } |
66 |
| - return requestAPI(name, arg, opts, null, cb) |
67 |
| - } |
68 |
| - } |
69 |
| - |
70 |
| - // -- Interface |
71 |
| - |
72 |
| - self.send = requestAPI |
73 |
| - |
74 |
| - self.add = (files, opts, cb) => { |
75 |
| - if (typeof (opts) === 'function' && cb === undefined) { |
76 |
| - cb = opts |
77 |
| - opts = {} |
78 |
| - } |
79 |
| - |
80 |
| - if (typeof files === 'string' && files.startsWith('http')) { |
81 |
| - Wreck.request('GET', files, null, (err, res) => { |
82 |
| - if (err) return cb(err) |
83 |
| - |
84 |
| - requestAPI('add', null, opts, res, cb) |
85 |
| - }) |
86 |
| - |
87 |
| - return |
88 |
| - } |
89 |
| - |
90 |
| - requestAPI('add', null, opts, files, cb) |
91 |
| - } |
92 |
| - |
93 |
| - self.cat = argCommand('cat') |
94 |
| - self.ls = argCommand('ls') |
95 |
| - |
96 |
| - self.config = { |
97 |
| - get: argCommand('config'), |
98 |
| - set (key, value, opts, cb) { |
99 |
| - if (typeof (opts) === 'function') { |
100 |
| - cb = opts |
101 |
| - opts = {} |
102 |
| - } |
103 |
| - return requestAPI('config', [key, value], opts, null, cb) |
104 |
| - }, |
105 |
| - show (cb) { |
106 |
| - return requestAPI('config/show', null, null, null, true, cb) |
107 |
| - }, |
108 |
| - replace (file, cb) { |
109 |
| - return requestAPI('config/replace', null, null, file, cb) |
110 |
| - } |
111 |
| - } |
112 |
| - |
113 |
| - self.update = { |
114 |
| - apply: command('update'), |
115 |
| - check: command('update/check'), |
116 |
| - log: command('update/log') |
117 |
| - } |
118 |
| - |
119 |
| - self.version = command('version') |
120 |
| - self.commands = command('commands') |
121 |
| - |
122 |
| - self.mount = (ipfs, ipns, cb) => { |
123 |
| - if (typeof ipfs === 'function') { |
124 |
| - cb = ipfs |
125 |
| - ipfs = null |
126 |
| - } else if (typeof ipns === 'function') { |
127 |
| - cb = ipns |
128 |
| - ipns = null |
129 |
| - } |
130 |
| - const opts = {} |
131 |
| - if (ipfs) opts.f = ipfs |
132 |
| - if (ipns) opts.n = ipns |
133 |
| - return requestAPI('mount', null, opts, null, cb) |
134 |
| - } |
135 |
| - |
136 |
| - self.diag = { |
137 |
| - net: command('diag/net'), |
138 |
| - sys: command('diag/sys') |
139 |
| - } |
140 |
| - |
141 |
| - self.block = { |
142 |
| - get: argCommand('block/get'), |
143 |
| - put (file, cb) { |
144 |
| - if (Array.isArray(file)) { |
145 |
| - return cb(null, new Error('block.put() only accepts 1 file')) |
146 |
| - } |
147 |
| - return requestAPI('block/put', null, null, file, cb) |
148 |
| - } |
149 |
| - } |
150 |
| - |
151 |
| - self.object = { |
152 |
| - get: argCommand('object/get'), |
153 |
| - put (file, encoding, cb) { |
154 |
| - if (typeof encoding === 'function') { |
155 |
| - return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')")) |
156 |
| - } |
157 |
| - return requestAPI('object/put', encoding, null, file, cb) |
158 |
| - }, |
159 |
| - data: argCommand('object/data'), |
160 |
| - stat: argCommand('object/stat'), |
161 |
| - links: argCommand('object/links'), |
162 |
| - patch (file, opts, cb) { |
163 |
| - return requestAPI('object/patch', [file].concat(opts), null, null, cb) |
164 |
| - } |
165 |
| - } |
166 |
| - |
167 |
| - self.swarm = { |
168 |
| - peers: command('swarm/peers'), |
169 |
| - connect: argCommand('swarm/connect') |
170 |
| - } |
171 |
| - |
172 |
| - self.ping = (id, cb) => { |
173 |
| - return requestAPI('ping', id, { n: 1 }, null, function (err, res) { |
174 |
| - if (err) return cb(err, null) |
175 |
| - cb(null, res[1]) |
176 |
| - }) |
177 |
| - } |
178 |
| - |
179 |
| - self.id = (id, cb) => { |
180 |
| - if (typeof id === 'function') { |
181 |
| - cb = id |
182 |
| - id = null |
183 |
| - } |
184 |
| - return requestAPI('id', id, null, null, cb) |
185 |
| - } |
186 |
| - |
187 |
| - self.pin = { |
188 |
| - add (hash, opts, cb) { |
189 |
| - if (typeof opts === 'function') { |
190 |
| - cb = opts |
191 |
| - opts = null |
192 |
| - } |
193 |
| - |
194 |
| - requestAPI('pin/add', hash, opts, null, cb) |
195 |
| - }, |
196 |
| - remove (hash, opts, cb) { |
197 |
| - if (typeof opts === 'function') { |
198 |
| - cb = opts |
199 |
| - opts = null |
200 |
| - } |
201 |
| - |
202 |
| - requestAPI('pin/rm', hash, opts, null, cb) |
203 |
| - }, |
204 |
| - list (type, cb) { |
205 |
| - if (typeof type === 'function') { |
206 |
| - cb = type |
207 |
| - type = null |
208 |
| - } |
209 |
| - let opts = null |
210 |
| - if (type) opts = { type: type } |
211 |
| - return requestAPI('pin/ls', null, opts, null, cb) |
212 |
| - } |
213 |
| - } |
214 |
| - |
215 |
| - self.log = { |
216 |
| - tail (cb) { |
217 |
| - requestAPI('log/tail', null, {}, null, false, (err, res) => { |
218 |
| - if (err) return cb(err) |
219 |
| - cb(null, res.pipe(ndjson.parse())) |
220 |
| - }) |
221 |
| - } |
222 |
| - } |
223 |
| - |
224 |
| - self.name = { |
225 |
| - publish: argCommand('name/publish'), |
226 |
| - resolve: argCommand('name/resolve') |
227 |
| - } |
228 |
| - |
229 |
| - self.Buffer = Buffer |
230 |
| - |
231 |
| - self.refs = argCommand('refs') |
232 |
| - self.refs.local = command('refs/local') |
233 |
| - |
234 |
| - self.dht = { |
235 |
| - findprovs: argCommand('dht/findprovs'), |
236 |
| - |
237 |
| - get (key, opts, cb) { |
238 |
| - if (typeof (opts) === 'function' && !cb) { |
239 |
| - cb = opts |
240 |
| - opts = null |
241 |
| - } |
242 |
| - |
243 |
| - return requestAPI('dht/get', key, opts, null, (err, res) => { |
244 |
| - if (err) return cb(err) |
245 |
| - if (!res) return cb(new Error('empty response')) |
246 |
| - if (res.length === 0) return cb(new Error('no value returned for key')) |
247 |
| - |
248 |
| - // Inconsistent return values in the browser vs node |
249 |
| - if (Array.isArray(res)) { |
250 |
| - res = res[0] |
251 |
| - } |
252 |
| - |
253 |
| - if (res.Type === 5) { |
254 |
| - cb(null, res.Extra) |
255 |
| - } else { |
256 |
| - cb(res) |
257 |
| - } |
258 |
| - }) |
259 |
| - }, |
260 |
| - |
261 |
| - put (key, value, opts, cb) { |
262 |
| - if (typeof (opts) === 'function' && !cb) { |
263 |
| - cb = opts |
264 |
| - opts = null |
265 |
| - } |
266 |
| - |
267 |
| - return requestAPI('dht/put', [key, value], opts, null, cb) |
268 |
| - } |
269 |
| - } |
270 |
| - |
271 |
| - self.files = { |
272 |
| - cp: argCommand('files/cp'), |
273 |
| - ls: argCommand('files/ls'), |
274 |
| - mkdir: argCommand('files/mkdir'), |
275 |
| - stat: argCommand('files/stat'), |
276 |
| - rm: function (path, opts, cb) { |
277 |
| - return requestAPI('files/rm', path, opts, null, cb) |
278 |
| - }, |
279 |
| - read: argCommand('files/read'), |
280 |
| - write: function (pathDst, files, opts, cb) { |
281 |
| - if (typeof (opts) === 'function' && cb === undefined) { |
282 |
| - cb = opts |
283 |
| - opts = {} |
284 |
| - } |
285 |
| - |
286 |
| - return requestAPI('files/write', pathDst, opts, files, cb) |
287 |
| - }, |
288 |
| - mv: argCommand('files/mv') |
289 |
| - } |
| 46 | + return cmds |
290 | 47 | }
|
0 commit comments