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

Commit 5f71752

Browse files
committed
feat(internals): use promisify for every exposed interface and apply CR
1 parent 5d0a823 commit 5f71752

25 files changed

+264
-224
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ This is very similar to `ipfs.files.add({path:'', content: stream})`. It is like
138138

139139
```JavaScript
140140
```
141-
>>>>>> refactor(module+tests): Enable running the tests
142141

143142
### Callbacks and promises
144143

src/api/add-files.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
const isNode = require('detect-node')
44
const addToDagNodesTransform = require('../add-to-dagnode-transform')
5+
const promisify = require('promisify-es6')
56

67
module.exports = (send) => {
7-
return function add (path, opts, callback) {
8-
if (typeof (opts) === 'function' &&
8+
return promisify((path, opts, callback) => {
9+
if (typeof opts === 'function' &&
910
callback === undefined) {
1011
callback = opts
1112
opts = {}
@@ -21,10 +22,10 @@ module.exports = (send) => {
2122

2223
const sendWithTransform = send.withTransform(addToDagNodesTransform)
2324

24-
return sendWithTransform({
25+
sendWithTransform({
2526
path: 'add',
2627
qs: opts,
2728
files: path
2829
}, callback)
29-
}
30+
})
3031
}

src/api/add-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const promisify = require('promisify-es6')
77
module.exports = (send) => {
88
const add = addCmd(send)
99

10-
return promisify(function createAddStream (callback) {
10+
return promisify((callback) => {
1111
const tuples = []
1212

1313
const ds = new Duplex({ objectMode: true })

src/api/add-url.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
const Wreck = require('wreck')
44
const addToDagNodesTransform = require('../add-to-dagnode-transform')
55

6+
const promisify = require('promisify-es6')
7+
68
module.exports = (send) => {
7-
return function add (url, opts, callback) {
9+
return promisify((url, opts, callback) => {
810
if (typeof (opts) === 'function' &&
911
callback === undefined) {
1012
callback = opts
@@ -29,5 +31,5 @@ module.exports = (send) => {
2931
files: res
3032
}, callback)
3133
})
32-
}
34+
})
3335
}

src/api/bitswap.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = (send) => {
46
return {
5-
wantlist (callback) {
6-
return send({
7+
wantlist: promisify((callback) => {
8+
send({
79
path: 'bitswap/wantlist'
810
}, callback)
9-
},
10-
stat (callback) {
11-
return send({
11+
}),
12+
stat: promisify((callback) => {
13+
send({
1214
path: 'bitswap/stat'
1315
}, callback)
14-
},
15-
unwant (args, opts, callback) {
16+
}),
17+
unwant: promisify((args, opts, callback) => {
1618
if (typeof (opts) === 'function') {
1719
callback = opts
1820
opts = {}
1921
}
20-
return send({
22+
send({
2123
path: 'bitswap/unwant',
2224
args: args,
2325
qs: opts
2426
}, callback)
25-
}
27+
})
2628
}
2729
}

src/api/block.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = (send) => {
46
return {
5-
get (args, opts, callback) {
7+
get: promisify((args, opts, callback) => {
68
if (typeof (opts) === 'function') {
79
callback = opts
810
opts = {}
@@ -12,8 +14,8 @@ module.exports = (send) => {
1214
args: args,
1315
qs: opts
1416
}, callback)
15-
},
16-
stat (args, opts, callback) {
17+
}),
18+
stat: promisify((args, opts, callback) => {
1719
if (typeof (opts) === 'function') {
1820
callback = opts
1921
opts = {}
@@ -23,21 +25,17 @@ module.exports = (send) => {
2325
args: args,
2426
qs: opts
2527
}, callback)
26-
},
27-
put (file, callback) {
28+
}),
29+
put: promisify((file, callback) => {
2830
if (Array.isArray(file)) {
2931
const err = new Error('block.put() only accepts 1 file')
30-
if (typeof callback !== 'function' &&
31-
typeof Promise !== 'undefined') {
32-
return new Promise((resolve, reject) => reject(err))
33-
}
3432
return callback(err)
3533
}
3634

3735
return send({
3836
path: 'block/put',
3937
files: file
4038
}, callback)
41-
}
39+
})
4240
}
4341
}

src/api/bootstrap.js

+41-11
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,70 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = (send) => {
46
return {
5-
add (args, opts, callback) {
7+
add: promisify((args, opts, callback) => {
68
if (typeof opts === 'function' &&
7-
callback === undefined) {
9+
!callback) {
810
callback = opts
911
opts = {}
1012
}
11-
return send({
13+
14+
// opts is the real callback --
15+
// 'callback' is being injected by promisify
16+
if (typeof opts === 'function' &&
17+
typeof callback === 'function') {
18+
callback = opts
19+
opts = {}
20+
}
21+
22+
if (args && typeof args === 'object') {
23+
opts = args
24+
args = undefined
25+
}
26+
27+
send({
1228
path: 'bootstrap/add',
1329
args: args,
1430
qs: opts
1531
}, callback)
16-
},
17-
rm (args, opts, callback) {
32+
}),
33+
rm: promisify((args, opts, callback) => {
1834
if (typeof opts === 'function' &&
19-
callback === undefined) {
35+
!callback) {
2036
callback = opts
2137
opts = {}
2238
}
23-
return send({
39+
40+
// opts is the real callback --
41+
// 'callback' is being injected by promisify
42+
if (typeof opts === 'function' &&
43+
typeof callback === 'function') {
44+
callback = opts
45+
opts = {}
46+
}
47+
48+
if (args && typeof args === 'object') {
49+
opts = args
50+
args = undefined
51+
}
52+
53+
send({
2454
path: 'bootstrap/rm',
2555
args: args,
2656
qs: opts
2757
}, callback)
28-
},
29-
list (opts, callback) {
58+
}),
59+
list: promisify((opts, callback) => {
3060
if (typeof (opts) === 'function') {
3161
callback = opts
3262
opts = {}
3363
}
34-
return send({
64+
send({
3565
path: 'bootstrap/list',
3666
qs: opts
3767
}, callback)
38-
}
68+
})
3969
}
4070
}

src/api/cat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = (send) => {
1111
return callback(err)
1212
}
1313

14-
return send({
14+
send({
1515
path: 'cat',
1616
args: hash
1717
}, callback)

src/api/commands.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = (send) => {
4-
return (callback) => {
5-
return send({
6+
return promisify((callback) => {
7+
send({
68
path: 'commands'
79
}, callback)
8-
}
10+
})
911
}

src/api/config.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
'use strict'
22

33
const streamifier = require('streamifier')
4+
const promisify = require('promisify-es6')
45

56
module.exports = (send) => {
67
return {
7-
get (key, callback) {
8+
get: promisify((key, callback) => {
89
if (typeof key === 'function') {
910
callback = key
1011
key = undefined
1112
}
1213

1314
if (!key) {
14-
return send({
15+
send({
1516
path: 'config/show',
1617
buffer: true
1718
}, callback)
19+
return
1820
}
1921

20-
return send({
22+
send({
2123
path: 'config',
2224
args: key,
2325
buffer: true
@@ -27,8 +29,8 @@ module.exports = (send) => {
2729
}
2830
callback(null, response.Value)
2931
})
30-
},
31-
set (key, value, opts, callback) {
32+
}),
33+
set: promisify((key, value, opts, callback) => {
3234
if (typeof opts === 'function') {
3335
callback = opts
3436
opts = {}
@@ -53,24 +55,24 @@ module.exports = (send) => {
5355
opts = { bool: true }
5456
}
5557

56-
return send({
58+
send({
5759
path: 'config',
5860
args: [key, value],
5961
qs: opts,
6062
files: undefined,
6163
buffer: true
6264
}, callback)
63-
},
64-
replace (config, callback) {
65+
}),
66+
replace: promisify((config, callback) => {
6567
if (typeof config === 'object') {
6668
config = streamifier.createReadStream(new Buffer(JSON.stringify(config)))
6769
}
6870

69-
return send({
71+
send({
7072
path: 'config/replace',
7173
files: config,
7274
buffer: true
7375
}, callback)
74-
}
76+
})
7577
}
7678
}

0 commit comments

Comments
 (0)