Improving init (aka, how do I get a js-ipfs node instance!) #556
Description
There has been a lot of feedback when it comes to the API for creating an IPFS node instance, some feedback from myself included. It is true, the process is a bit cumbersome, instead of having something beautiful as
// # Option 1
const options = {
init: true
online: true
}
const node = new IPFS(options)
We have:
const repo = <IPFS Repo instance or repo path>
const ipfs = new IPFS(repo)
ipfs.init({ emptyRepo: true, bits: 512 }, (err) => {
if (err) { throw err }
ipfs.load((err) => {
if (err) { throw err }
ipfs.goOnline((err) => {
if (err) { throw err }
// finally something you can work with!
})
})
Yes, it is not nice, but why is it this way right now? Why can't it be sync
, well for some reasons:
init
needs to get the repo started if it doesn't exist, since IPFS Repo use pull-blob-store adapters and these are async (because a Repo can be a network storage), this call needs to be asyncload
is when we take a repo and load all the config values. This also can't be sync because we only store the Private Key on the config and the generation of the PublicKey needs to be async (Thanks WebCrypto! see more at: Awesome Async Crypto + Less magic to 'run in the browser' #485))goOnline
this is where we turn on bitswap, it means that we are ready to fetch and serve blocks from and to other nodes.
This can be way more nicer, however, starting from documentation of this methods, examples and explaining what the functions are doing inside the readme.
Another thing to consider is making the IPFS instance an event emitter with a 'ready' event, so that Option 1
can be achieved by loading everything as options and emitting a ready
event.
Other things to consider
- It would be sweet if the options passed to init or to new IPFS(options) would also accept values to overload the config with. Right now we always create the repo and then use the config API, it works but this option would reduce the steps, making code easier to read.
goOnline
should be bitswap.on andbitswap.off
, as we will need soon to be able to enable and disable things like thedht
.
I would love to hear everyone's thoughts on this, I might be missing something that might get us to a even nicer start process, looking forward for feedback :) Thank you!