Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 08b3e16

Browse files
fix(core/bootstrap): promisfy API and validate mulliaddr param
1 parent 42545dc commit 08b3e16

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/core/components/bootstrap.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
'use strict'
22

33
const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap
4+
const MultiAddr = require('multiaddr')
5+
const promisify = require('promisify-es6')
46

57
module.exports = function bootstrap (self) {
68
return {
7-
list: (callback) => {
9+
list: promisify((callback) => {
810
self._repo.config.get((err, config) => {
911
if (err) {
1012
return callback(err)
1113
}
1214
callback(null, {Peers: config.Bootstrap})
1315
})
14-
},
15-
add: (multiaddr, args, callback) => {
16+
}),
17+
add: promisify((multiaddr, args, callback) => {
1618
if (typeof args === 'function') {
1719
callback = args
1820
args = {default: false}
1921
}
22+
try {
23+
if (multiaddr)
24+
new MultiAddr(multiaddr)
25+
}
26+
catch (err) {
27+
return setImmediate(() => callback(err))
28+
}
2029
self._repo.config.get((err, config) => {
2130
if (err) {
2231
return callback(err)
@@ -36,12 +45,19 @@ module.exports = function bootstrap (self) {
3645
})
3746
})
3847
})
39-
},
40-
rm: (multiaddr, args, callback) => {
48+
}),
49+
rm: promisify((multiaddr, args, callback) => {
4150
if (typeof args === 'function') {
4251
callback = args
4352
args = {all: false}
4453
}
54+
try {
55+
if (multiaddr)
56+
new MultiAddr(multiaddr)
57+
}
58+
catch (err) {
59+
return setImmediate(() => callback(err))
60+
}
4561
self._repo.config.get((err, config) => {
4662
if (err) {
4763
return callback(err)
@@ -65,6 +81,6 @@ module.exports = function bootstrap (self) {
6581
callback(null, {Peers: res})
6682
})
6783
})
68-
}
84+
})
6985
}
7086
}

test/core/interface/bootstrap.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const test = require('interface-ipfs-core')
5+
const parallel = require('async/parallel')
6+
7+
const IPFS = require('../../../src')
8+
9+
const DaemonFactory = require('ipfsd-ctl')
10+
const df = DaemonFactory.create({ type: 'proc', exec: IPFS })
11+
12+
const nodes = []
13+
const common = {
14+
setup: function (callback) {
15+
callback(null, {
16+
spawnNode: (cb) => {
17+
df.spawn((err, _ipfsd) => {
18+
if (err) {
19+
return cb(err)
20+
}
21+
22+
nodes.push(_ipfsd)
23+
cb(null, _ipfsd.api)
24+
})
25+
}
26+
})
27+
},
28+
teardown: function (callback) {
29+
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
30+
}
31+
}
32+
33+
test.bootstrap(common)

test/core/interface/interface.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const isNode = require('detect-node')
66

77
describe('interface-ipfs-core tests', () => {
88
require('./block')
9+
require('./bootstrap')
910
require('./config')
1011
require('./files')
1112
require('./generic')

0 commit comments

Comments
 (0)