-
Notifications
You must be signed in to change notification settings - Fork 2
Dependency injection patterns for reducing bundle size #10
Description
This issue is broader than just the Block API (it touches js-mutliformats, js-cid, js-ipfs, and js-libp2p) but this is as good a place as any to kick off the conversation.
Currently, we ship with support for a large set of:
- IPLD codecs
- Hashing functions
- Multiformat table entries
This is not sustainable. Even the multiformat table, which is only metadata, has become large enough to be a bundle size concern. The number of potential codecs and hashing functions will only grow over time.
In all the libraries we’ve written for IPLD in the last year, we depend on Block API through require(‘@/ipld/block’). It’s how we create blocks which encode data (codecs) and CID’s (hashing and multiformats). Currently, these libraries just require(‘@ipld/block’) which includes all the default codecs and has an interface for adding more codecs. There is no way to pair the default set down and no way to remove or add hashing functions.
In considering this problem, I’ve been writing some of my newer libraries a little different. I’ve been doing the core implementation in a file called bare.js which exports a single function that accepts the Block class and the default codec. https://github.com/ipld/js-fbl/blob/master/src/bare.js#L8 Then in index.js I just return this module with the default require(‘@ipld/block’) class. The idea is, we can expose a Block class in the future that comes without any codecs, hashing functions, or multiformat table entries, and the user can add entries for each that it would like to support. Then that class can be passed into the relevant IPLD libraries that need to create or decode blocks.
const Block = require(‘@ipld/block/bare’)
Block.add(require(‘@ipld/dag-cbor’))
Block.add(require(‘@ipld/blake2’), { default: true })
const fbl = require(‘@ipld/fbl/bare’)(Block, ‘dag-cbor’)As support for each format is added to the Block API the multiformat entries for each is added to the internal multiformat table. This means we’ll only have complete information for multiformat entries we actually have support for and this would cause a breaking change in CID because the .codec property would no longer work for codecs you didn’t explicitly add support for (the migration for this involves moving away from matching against the string and using the integer for the multiformat entry).
I’d like get feedback on this approach and everyone’s thoughts on the API before I dig in and implement it everywhere.
I also want to make sure this is going to work for the other projects once they adopt the new Block API.
/cc @alanshaw @achingbrain @vmx @rvagg @jacobheun @carsonfarmer