Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 2211f06

Browse files
committedNov 23, 2015
Merge pull request #136 from ipfs/ref/api-split
refactor: Split api definitions into separate files
2 parents 06094d1 + ebd915e commit 2211f06

24 files changed

+345
-231
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"multipart-stream": "^2.0.0",
1010
"ndjson": "^1.4.3",
1111
"qs": "^6.0.0",
12+
"require-dir": "^0.3.0",
1213
"vinyl": "^1.1.0",
1314
"vinyl-fs-browser": "^2.1.1-1",
1415
"vinyl-multipart-stream": "^1.2.6",
@@ -50,7 +51,6 @@
5051
"mocha": "^2.3.3",
5152
"pre-commit": "^1.0.6",
5253
"raw-loader": "^0.5.1",
53-
"require-dir": "^0.3.0",
5454
"rimraf": "^2.4.3",
5555
"run-sequence": "^1.1.4",
5656
"stream-equal": "^0.1.7",

‎src/api/add.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Wreck = require('wreck')
2+
3+
module.exports = send => {
4+
return function add (files, opts, cb) {
5+
if (typeof (opts) === 'function' && cb === undefined) {
6+
cb = opts
7+
opts = {}
8+
}
9+
10+
if (typeof files === 'string' && files.startsWith('http')) {
11+
Wreck.request('GET', files, null, (err, res) => {
12+
if (err) return cb(err)
13+
14+
send('add', null, opts, res, cb)
15+
})
16+
17+
return
18+
}
19+
20+
send('add', null, opts, files, cb)
21+
}
22+
}

‎src/api/block.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'block/get'),
8+
put (file, cb) {
9+
if (Array.isArray(file)) {
10+
return cb(null, new Error('block.put() only accepts 1 file'))
11+
}
12+
return send('block/put', null, null, file, cb)
13+
}
14+
}
15+
}

‎src/api/cat.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return argCommand(send, 'cat')
7+
}

‎src/api/commands.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return command(send, 'commands')
7+
}

‎src/api/config.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'config'),
8+
set (key, value, opts, cb) {
9+
if (typeof (opts) === 'function') {
10+
cb = opts
11+
opts = {}
12+
}
13+
return send('config', [key, value], opts, null, cb)
14+
},
15+
show (cb) {
16+
return send('config/show', null, null, null, true, cb)
17+
},
18+
replace (file, cb) {
19+
return send('config/replace', null, null, file, cb)
20+
}
21+
}
22+
}

‎src/api/dht.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
findprovs: argCommand(send, 'dht/findprovs'),
8+
get (key, opts, cb) {
9+
if (typeof (opts) === 'function' && !cb) {
10+
cb = opts
11+
opts = null
12+
}
13+
14+
return send('dht/get', key, opts, null, (err, res) => {
15+
if (err) return cb(err)
16+
if (!res) return cb(new Error('empty response'))
17+
if (res.length === 0) return cb(new Error('no value returned for key'))
18+
19+
// Inconsistent return values in the browser vs node
20+
if (Array.isArray(res)) {
21+
res = res[0]
22+
}
23+
24+
if (res.Type === 5) {
25+
cb(null, res.Extra)
26+
} else {
27+
cb(res)
28+
}
29+
})
30+
},
31+
put (key, value, opts, cb) {
32+
if (typeof (opts) === 'function' && !cb) {
33+
cb = opts
34+
opts = null
35+
}
36+
37+
return send('dht/put', [key, value], opts, null, cb)
38+
}
39+
}
40+
}

‎src/api/diag.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return {
7+
net: command(send, 'diag/net'),
8+
sys: command(send, 'diag/sys')
9+
}
10+
}

‎src/api/id.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function id (id, cb) {
5+
if (typeof id === 'function') {
6+
cb = id
7+
id = null
8+
}
9+
return send('id', id, null, null, cb)
10+
}
11+
}

‎src/api/log.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
const ndjson = require('ndjson')
4+
5+
module.exports = send => {
6+
return {
7+
tail (cb) {
8+
send('log/tail', null, {}, null, false, (err, res) => {
9+
if (err) return cb(err)
10+
cb(null, res.pipe(ndjson.parse()))
11+
})
12+
}
13+
}
14+
}

‎src/api/ls.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return argCommand(send, 'ls')
7+
}

‎src/api/mount.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function mount (ipfs, ipns, cb) {
5+
if (typeof ipfs === 'function') {
6+
cb = ipfs
7+
ipfs = null
8+
} else if (typeof ipns === 'function') {
9+
cb = ipns
10+
ipns = null
11+
}
12+
const opts = {}
13+
if (ipfs) opts.f = ipfs
14+
if (ipns) opts.n = ipns
15+
return send('mount', null, opts, null, cb)
16+
}
17+
}

‎src/api/name.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
publish: argCommand(send, 'name/publish'),
8+
resolve: argCommand(send, 'name/resolve')
9+
}
10+
}

‎src/api/object.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'object/get'),
8+
put (file, encoding, cb) {
9+
if (typeof encoding === 'function') {
10+
return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')"))
11+
}
12+
return send('object/put', encoding, null, file, cb)
13+
},
14+
data: argCommand(send, 'object/data'),
15+
stat: argCommand(send, 'object/stat'),
16+
links: argCommand(send, 'object/links'),
17+
patch (file, opts, cb) {
18+
return send('object/patch', [file].concat(opts), null, null, cb)
19+
}
20+
}
21+
}

‎src/api/pin.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return {
5+
add (hash, opts, cb) {
6+
if (typeof opts === 'function') {
7+
cb = opts
8+
opts = null
9+
}
10+
11+
send('pin/add', hash, opts, null, cb)
12+
},
13+
remove (hash, opts, cb) {
14+
if (typeof opts === 'function') {
15+
cb = opts
16+
opts = null
17+
}
18+
19+
send('pin/rm', hash, opts, null, cb)
20+
},
21+
list (type, cb) {
22+
if (typeof type === 'function') {
23+
cb = type
24+
type = null
25+
}
26+
let opts = null
27+
if (type) opts = { type: type }
28+
return send('pin/ls', null, opts, null, cb)
29+
}
30+
}
31+
}

‎src/api/ping.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function ping (id, cb) {
5+
return send('ping', id, { n: 1 }, null, function (err, res) {
6+
if (err) return cb(err, null)
7+
cb(null, res[1])
8+
})
9+
}
10+
}

‎src/api/refs.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const cmds = require('../cmd-helpers')
4+
5+
module.exports = send => {
6+
const refs = cmds.argCommand(send, 'refs')
7+
refs.local = cmds.command(send, 'refs/local')
8+
9+
return refs
10+
}

‎src/api/swarm.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const cmds = require('../cmd-helpers')
4+
5+
module.exports = send => {
6+
return {
7+
peers: cmds.command(send, 'swarm/peers'),
8+
connect: cmds.argCommand(send, 'swarm/connect')
9+
}
10+
}

‎src/api/update.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return {
7+
apply: command(send, 'update'),
8+
check: command(send, 'update/check'),
9+
log: command(send, 'update/log')
10+
}
11+
}

‎src/api/version.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return command(send, 'version')
7+
}

‎src/cmd-helpers.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
exports.command = function command (send, name) {
4+
return (opts, cb) => {
5+
if (typeof (opts) === 'function') {
6+
cb = opts
7+
opts = {}
8+
}
9+
return send(name, null, opts, null, cb)
10+
}
11+
}
12+
13+
exports.argCommand = function argCommand (send, name) {
14+
return (arg, opts, cb) => {
15+
if (typeof (opts) === 'function') {
16+
cb = opts
17+
opts = {}
18+
}
19+
return send(name, arg, opts, null, cb)
20+
}
21+
}

‎src/index.js

+6-229
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
'use strict'
22

33
const multiaddr = require('multiaddr')
4+
5+
const loadCommands = require('./load-commands')
46
const getConfig = require('./config')
57
const getRequestAPI = require('./request-api')
6-
const Wreck = require('wreck')
7-
const ndjson = require('ndjson')
88

99
exports = module.exports = IpfsAPI
1010

1111
function IpfsAPI (host_or_multiaddr, port, opts) {
12-
const self = this
1312
const config = getConfig()
1413

15-
if (!(self instanceof IpfsAPI)) {
16-
return new IpfsAPI(host_or_multiaddr, port, opts)
17-
}
18-
1914
try {
2015
const maddr = multiaddr(host_or_multiaddr).nodeAddress()
2116
config.host = maddr.address
@@ -44,227 +39,9 @@ function IpfsAPI (host_or_multiaddr, port, opts) {
4439
}
4540

4641
const requestAPI = getRequestAPI(config)
42+
const cmds = loadCommands(requestAPI)
43+
cmds.send = requestAPI
44+
cmds.Buffer = Buffer
4745

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-
}
46+
return cmds
27047
}

‎src/load-commands.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const isNode = !global.window
4+
5+
function requireDir () {
6+
if (isNode) return require('require-dir')('./api')
7+
8+
// Webpack specific require of a directory
9+
const req = require.context('./api', false, /\.js$/)
10+
11+
const files = {}
12+
req.keys().forEach(key => {
13+
const name = key
14+
.replace(/^\.\//, '')
15+
.replace(/\.js$/, '')
16+
files[name] = req(key)
17+
})
18+
19+
return files
20+
}
21+
22+
function loadCommands (send) {
23+
const files = requireDir()
24+
const cmds = {}
25+
26+
Object.keys(files).forEach(file => {
27+
cmds[file] = files[file](send)
28+
})
29+
30+
return cmds
31+
}
32+
33+
module.exports = loadCommands

‎tasks/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const shared = {
4040
net: '{}',
4141
fs: '{}',
4242
tls: '{}',
43-
console: '{}'
43+
console: '{}',
44+
'require-dir': '{}'
4445
}
4546
}
4647

0 commit comments

Comments
 (0)
This repository has been archived.