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

Commit 70739bc

Browse files
author
Alan Shaw
committed
refactor: convert bootstrap API to async/await
1 parent 920e399 commit 70739bc

File tree

8 files changed

+101
-66
lines changed

8 files changed

+101
-66
lines changed

src/core/components/bootstrap.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/core/components/bootstrap/add.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
const defaultConfig = require('../runtime/config-nodejs.js')
4+
const isMultiaddr = require('mafmt').IPFS.matches
5+
6+
function isValidMultiaddr (ma) {
7+
try {
8+
return isMultiaddr(ma)
9+
} catch (err) {
10+
return false
11+
}
12+
}
13+
14+
module.exports = ({ repo }) => {
15+
return async function add (multiaddr, options) {
16+
options = options || {}
17+
18+
if (multiaddr && !isValidMultiaddr(multiaddr)) {
19+
throw new Error(`${multiaddr} is not a valid Multiaddr`)
20+
}
21+
22+
const config = await repo.config.get()
23+
if (options.default) {
24+
config.Bootstrap = defaultConfig().Bootstrap
25+
} else if (multiaddr && config.Bootstrap.indexOf(multiaddr) === -1) {
26+
config.Bootstrap.push(multiaddr)
27+
}
28+
await repo.config.set(config)
29+
30+
return {
31+
Peers: options.default ? defaultConfig().Bootstrap : [multiaddr]
32+
}
33+
}
34+
}

src/core/components/bootstrap/list.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = ({ repo }) => {
4+
return async function list () {
5+
const config = await repo.config.get()
6+
return { Peers: config.Bootstrap || [] }
7+
}
8+
}

src/core/components/bootstrap/rm.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const isMultiaddr = require('mafmt').IPFS.matches
4+
5+
function isValidMultiaddr (ma) {
6+
try {
7+
return isMultiaddr(ma)
8+
} catch (err) {
9+
return false
10+
}
11+
}
12+
13+
module.exports = ({ repo }) => {
14+
return async function rm (multiaddr, options) {
15+
options = options || {}
16+
17+
if (multiaddr && !isValidMultiaddr(multiaddr)) {
18+
throw new Error(`${multiaddr} is not a valid Multiaddr`)
19+
}
20+
21+
let res = []
22+
const config = await repo.config.get()
23+
24+
if (options.all) {
25+
res = config.Bootstrap || []
26+
config.Bootstrap = []
27+
} else {
28+
config.Bootstrap = (config.Bootstrap || []).filter(ma => ma !== multiaddr)
29+
}
30+
31+
await repo.config.set(config)
32+
33+
if (!options.all && multiaddr) {
34+
res.push(multiaddr)
35+
}
36+
37+
return { Peers: res }
38+
}
39+
}

src/core/components/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ exports.bitswap = {
66
unwant: require('./bitswap/unwant'),
77
wantlist: require('./bitswap/wantlist')
88
}
9+
exports.bootstrap = {
10+
add: require('./bootstrap/add'),
11+
list: require('./bootstrap/list'),
12+
rm: require('./bootstrap/rm')
13+
}
914
exports.config = require('./config')
1015
exports.id = require('./id')
1116
exports.init = require('./init')

src/core/components/init.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ function createApi ({
314314

315315
const api = {
316316
add,
317+
bootstrap: {
318+
add: Commands.bootstrap.add({ repo }),
319+
list: Commands.bootstrap.list({ repo }),
320+
rm: Commands.bootstrap.rm({ repo })
321+
},
317322
config: Commands.config({ repo }),
318323
id: Commands.id({ peerInfo }),
319324
init: () => { throw new AlreadyInitializedError() },

src/core/components/start.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ function createApi ({
150150
unwant: Commands.bitswap.unwant({ bitswap }),
151151
wantlist: Commands.bitswap.wantlist({ bitswap })
152152
},
153+
bootstrap: {
154+
add: Commands.bootstrap.add({ repo }),
155+
list: Commands.bootstrap.list({ repo }),
156+
rm: Commands.bootstrap.rm({ repo })
157+
},
153158
config: Commands.config({ repo }),
154159
id: Commands.id({ peerInfo }),
155160
init: () => { throw new AlreadyInitializedError() },

src/core/components/stop.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ function createApi ({
112112

113113
const api = {
114114
add,
115+
bootstrap: {
116+
add: Commands.bootstrap.add({ repo }),
117+
list: Commands.bootstrap.list({ repo }),
118+
rm: Commands.bootstrap.rm({ repo })
119+
},
115120
config: Commands.config({ repo }),
116121
id: Commands.id({ peerInfo }),
117122
init: () => { throw new AlreadyInitializedError() },

0 commit comments

Comments
 (0)