-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Using constants for multicodec #1809
Comments
Sounds great to me! |
@vmx agreed! |
I'm all for exporting constants to reduce programmer error. I'm less for what might appear to users as change for the sake of change from an API point of view, but as long as it's backwards compatible with using strings then all good.
I might be missing something, but
+1 to removing Buffers wherever possible. Ideally we'd just operate on array-like objects instead so we can use Buffers, Arrays, Uint8Array, bl, whatever. |
Could you explain why it isn't already?
As @achingbrain said, I think you'd still need to register it to be able to use it - there's separate discussions to be had around supporting missing codecs in our domain objects e.g. multiformats/multiaddr#70.
Could you explain a bit more I'm not sure I understand why it's more browser friendly? IMHO it's better DX to use a string. It's easier to remember than the number, and if you use the number directly (and people will) anyone looking at your code will have to look up what that number refers to i.e. harder to maintain, understand and less readable. If you use constants like I'm maginally pro being flexible and allowing use of either the code or the name, but I just know that people will use the number directly and not the exported constant and so I'm less happy about giving developers the rope to make their code less maintainable. I am -1 on deprecating and removing the use of the string. |
Yes. Sorry I was talking on the js-ipld level, not the js-ipfs one. |
It might be the source of truth, but it's not used in all libraries that should use it. For example: https://github.com/multiformats/js-multihash/blob/443a8ebdd145582f2cac6e708ffe890401d5de08/src/constants.js
You wouldn't need to use the Buffer polyfill, but could work with JavaScript native data types like TypedArrays.
If it becomes the source of truth, it's very likely that it is bundled already anyway. Also js-cid has a dependency on it, which is likely to be included anyway.
Or they are happy to have a constant as their IDE can autocomplete it.
I'd remove the strings to make the bundle size smaller. This way we'd also get rid of the dependency on Buffer in the Browser (though, you can make the same argument as I did above that Buffer is very likely to be bundled anyway). |
Understood, but I don't think using numbers expressly enables that change. Or am I missing something?
Yes fair enough.
Yes of course, this is the big win, and outweighs the chance of people using
Yeah, maybe, I think after gzip the difference would probably be negligable.
In this case I'm less concerned about bundle size and more about DX - |
Not at the moment. So it's more a "if we don't get rid of Buffers as multicodec values, we might not be able to gt rid of Buffers at the core of things". Of course you don't have to use constants for this, you could just define the current strings to use numbers instead of Buffers. Though this would be a hard to upgrade breaking change. |
I propose using constants for multicodec related things.
Currently we use strings. E.g. if you create a new CID you do:
Instead I'd like to change it to
Reasons:
js-multicodec really becomes the source of truth
custom codecs are easier. As we always operate on numbers, someone could e.g. do for prototyping purpose:
You wouldn't need to register a string somewhere.
Current js-multicodec is based around Node Buffers. With using constants (with numbers as values) it becomes more browser friendly.
I've opened a PR at multiformats/js-multicodec#35 implementing it. Please note that it even isn't a breaking change, as it just adds additional things. I'd then like to deprecate the old stuff in the future, once we update the whole code base. Though there surely is no rush in doing that.
I'd like to get feedback from the whole @ipfs/javascript-team.
The text was updated successfully, but these errors were encountered: