This repository was archived by the owner on Feb 12, 2024. It is now read-only.
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
Discussion: IPFS.create({ ... }) should return same API regardless of the init
and start
options. #3285
Closed
Description
At the moment IPFS.create
will return API object that has different interface depending on init
and start
options:
js-ipfs/packages/ipfs/src/core/index.js
Lines 101 to 118 in 3ff833d
It is kind of cool that one could do that in JS (and even make TS infer it properly), but I think it has enough drawbacks that I would like to make an argument against it:
- It is difficult to document this kind of behavior. In fact documentation seems to incorrectly suggest that it returned object will implement IPFS Core API. And only vaguely mentions that initialization is different from creating because many special properties are set during initialization.
- Unsurprisingly (per @lidel) some (of even our) code assumes that returned API object implements same interface.
- This introduces side effects into the system. Some APIs will not be available before init and will be after init. This specific aspet is impossible to type in TS because types can just change in side effect of a call.
- I think this is primary reason for
ApiManager
which introduces quite a bit complexity in exchange for unclear (at least to me) benefits.
I understand that IPFS node can not provide all the core functionality when it's not initialized / started. However I would propose to either (or both):
- Have the same core API and throw error e.g.
not initialized
/not started
error(s) when specific functionality is in-operational. - Have a way to distinguish between
'unitialized | started | initialized'
states so that function that is passedIPFS API
object can asses what API subset can be used (without try catch orsubapi in
) - Have a different factory functions, for different API objects.
- Remove side effects e.g.
api.init()
can returnInitilaizedAPI
instance, without mutating theapi
itself.