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

refactor: convert bootstrap API to async/await #2661

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 0 additions & 66 deletions src/core/components/bootstrap.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/core/components/bootstrap/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const defaultConfig = require('../runtime/config-nodejs.js')
const { isValidMultiaddr } = require('./utils')

module.exports = ({ repo }) => {
return async function add (multiaddr, options) {
options = options || {}

if (multiaddr && !isValidMultiaddr(multiaddr)) {
throw new Error(`${multiaddr} is not a valid Multiaddr`)
}

const config = await repo.config.get()
if (options.default) {
config.Bootstrap = defaultConfig().Bootstrap
} else if (multiaddr && config.Bootstrap.indexOf(multiaddr) === -1) {
config.Bootstrap.push(multiaddr)
}
await repo.config.set(config)

return {
Peers: options.default ? defaultConfig().Bootstrap : [multiaddr]
}
}
}
8 changes: 8 additions & 0 deletions src/core/components/bootstrap/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = ({ repo }) => {
return async function list () {
const config = await repo.config.get()
return { Peers: config.Bootstrap || [] }
}
}
31 changes: 31 additions & 0 deletions src/core/components/bootstrap/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const { isValidMultiaddr } = require('./utils')

module.exports = ({ repo }) => {
return async function rm (multiaddr, options) {
options = options || {}

if (multiaddr && !isValidMultiaddr(multiaddr)) {
throw new Error(`${multiaddr} is not a valid Multiaddr`)
}

let res = []
const config = await repo.config.get()

if (options.all) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this function is doing two different things, it feels like maybe it should be two different functions:

  • if the all option is not set it deletes a single multiaddress
  • if the all option is set it deletes the whole bootstrap list
    However you still have to supply a null / undefined value as the first parameter. Feels a little weird.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this really needs to be ipfs.bootstrap.rm.all() like https://docs.ipfs.io/reference/api/http/#api-v0-bootstrap-rm-all

One for a future refactoring :)

res = config.Bootstrap || []
config.Bootstrap = []
} else {
config.Bootstrap = (config.Bootstrap || []).filter(ma => ma !== multiaddr)
}

await repo.config.set(config)

if (!options.all && multiaddr) {
res.push(multiaddr)
}

return { Peers: res }
}
}
11 changes: 11 additions & 0 deletions src/core/components/bootstrap/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const isMultiaddr = require('mafmt').IPFS.matches

exports.isValidMultiaddr = ma => {
try {
return isMultiaddr(ma)
} catch (err) {
return false
}
}
5 changes: 5 additions & 0 deletions src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ exports.bitswap = {
unwant: require('./bitswap/unwant'),
wantlist: require('./bitswap/wantlist')
}
exports.bootstrap = {
add: require('./bootstrap/add'),
list: require('./bootstrap/list'),
rm: require('./bootstrap/rm')
}
exports.config = require('./config')
exports.id = require('./id')
exports.init = require('./init')
Expand Down
5 changes: 5 additions & 0 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ function createApi ({

const api = {
add,
bootstrap: {
add: Commands.bootstrap.add({ repo }),
list: Commands.bootstrap.list({ repo }),
rm: Commands.bootstrap.rm({ repo })
},
config: Commands.config({ repo }),
id: Commands.id({ peerInfo }),
init: () => { throw new AlreadyInitializedError() },
Expand Down
5 changes: 5 additions & 0 deletions src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ function createApi ({
unwant: Commands.bitswap.unwant({ bitswap }),
wantlist: Commands.bitswap.wantlist({ bitswap })
},
bootstrap: {
add: Commands.bootstrap.add({ repo }),
list: Commands.bootstrap.list({ repo }),
rm: Commands.bootstrap.rm({ repo })
},
config: Commands.config({ repo }),
id: Commands.id({ peerInfo }),
init: () => { throw new AlreadyInitializedError() },
Expand Down
5 changes: 5 additions & 0 deletions src/core/components/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function createApi ({

const api = {
add,
bootstrap: {
add: Commands.bootstrap.add({ repo }),
list: Commands.bootstrap.list({ repo }),
rm: Commands.bootstrap.rm({ repo })
},
config: Commands.config({ repo }),
id: Commands.id({ peerInfo }),
init: () => { throw new AlreadyInitializedError() },
Expand Down