Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
refactor: pass in IPLD Formats into the constructor
Browse files Browse the repository at this point in the history
Instead of having IPLD using all currently available [IPLD Format]
implementations automatically, pass the Formats that should be
used into the constructor as options.

This will also decrease the bundle size as every user of IPLD can
decide which IPLD Formats should be used. By default
[ipld-dag-cbor] and [ipld-dag-pb] are used as those are the ones
also heavily used within the IPFS ecosystem.

BREAKING CHANGE: Not all IPLD Formats are included by default

By default only the [ipld-dag-cbor], [ipld-dag-pb] and [raw]
[IPLD Format]s are included. If you want to use other IPLD Formats
you need to pass them into the constructor.

The code to restore the old behaviour could look like this:

```js
const ipldBitcoin = require('ipld-bitcoin')
const ipldDagCbor = require('ipld-dag-cbor')
const ipldDagPb = require('ipld-dag-pb')
const ipldEthAccountSnapshot = require('ipld-ethereum').ethAccountSnapshot
const ipldEthBlock = require('ipld-ethereum').ethBlock
const ipldEthBlockList = require('ipld-ethereum').ethBlockList
const ipldEthStateTrie = require('ipld-ethereum').ethStateTrie
const ipldEthStorageTrie = require('ipld-ethereum').ethStorageTrie
const ipldEthTrie = require('ipld-ethereum').ethTxTrie
const ipldEthTx = require('ipld-ethereum').ethTx
const ipldGit = require('ipld-git')
const ipldRaw = require('ipld-raw')
const ipldZcash = require('ipld-zcash')

…

const ipld = new Ipld({
  blockService: blockService,
  formats: [
    ipldBitcoin, ipldDagCbor, ipldDagPb, ipldEthAccountSnapshot,
    ipldEthBlock, ipldEthBlockList, ipldEthStateTrie, ipldEthStorageTrie,
    ipldEthTrie, ipldEthTx, ipldGit, ipldRaw, ipldZcash
  ]
})
```

[ipld-dag-cbor]: https://github.com/ipld/js-ipld-dag-cbor
[ipld-dag-pb]: https://github.com/ipld/js-ipld-dag-pb
[ipld-raw]: https://github.com/ipld/js-ipld-raw
[IPLD Format]: https://github.com/ipld/interface-ipld-format
  • Loading branch information
vmx committed Oct 25, 2018
1 parent f1c2e12 commit b003ad1
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 67 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Want to get started? Check our examples folder. You can check the development st
- [`.remove(cid, callback)`](#removecid-callback)
- [`.support.add(multicodec, formatResolver, formatUtil)`](#supportaddmulticodec-formatresolver-formatutil)
- [`.support.rm(multicodec)`](#supportrmmulticodec)
- [Properties](#properties)
- [`defaultOptions`](#defaultoptions)
- [Packages](#packages)
- [Contribute](#contribute)
- [License](#license)
Expand Down Expand Up @@ -115,6 +117,23 @@ const blockService = new IpfsBlockService(repo)
const ipld = new Ipld({blockService: blockService})
```

##### `options.formats`

| Type | Default |
|------|---------|
| Array of [IPLD Format](https://github.com/ipld/interface-ipld-format) implementations | `[require('ipld-dag-cbor'), require('ipld-dag-pb'), require('ipld-raw')]` |

By default only the [dag-cbor](https://github.com/ipld/js-ipld-dag-cbor)), [dag-pb](https://github.com/ipld/js-ipld-dag-pb)) and [raw](https://github.com/ipld/js-ipld-raw)) IPLD Formats are supported. Other formats need to be added manually. Here is an example if you want to have support for [ipld-git](https://github.com/ipld/js-ipld-git) only:

```js
const ipldGit = require('ipld-git')

const ipld = new Ipld({
formats: [ipldGit],
})
```

### `.put(node, options, callback)`

> Store the given node of a recognized IPLD Format.
Expand Down Expand Up @@ -161,6 +180,12 @@ const ipld = new Ipld({blockService: blockService})

> Removes support of an IPLD Format
### Properties

#### `defaultOptions`

> Default options for IPLD.
## Packages

Listing of dependencies from the IPLD ecosystem.
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@
"license": "MIT",
"devDependencies": {
"aegir": "^15.2.0",
"bitcoinjs-lib": "^4.0.2",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"eth-hash-to-cid": "~0.1.1",
"ethereumjs-block": "^2.0.1",
"ipld-bitcoin": "~0.1.7",
"ipld-ethereum": "^2.0.1",
"ipld-git": "~0.2.1",
"ipld-zcash": "~0.1.6",
"merkle-patricia-tree": "^2.3.2",
"multihashes": "~0.4.14",
"ncp": "^2.0.0",
"rimraf": "^2.6.2",
"rlp": "^2.1.0"
"rlp": "^2.1.0",
"zcash-bitcore-lib": "~0.13.20-rc3"
},
"dependencies": {
"async": "^2.6.1",
Expand All @@ -50,13 +57,10 @@
"ipfs-block": "~0.7.1",
"ipfs-block-service": "~0.14.0",
"ipfs-repo": "~0.24.0",
"ipld-bitcoin": "~0.1.7",
"ipld-dag-cbor": "~0.13.0",
"ipld-dag-pb": "~0.14.10",
"ipld-ethereum": "^2.0.1",
"ipld-git": "~0.2.1",
"ipld-raw": "^2.0.1",
"ipld-zcash": "~0.1.6",
"merge-options": "^1.0.1",
"pull-defer": "~0.2.3",
"pull-stream": "^3.6.9",
"pull-traverse": "^1.0.3"
Expand Down
81 changes: 24 additions & 57 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,26 @@ const map = require('async/map')
const series = require('async/series')
const waterfall = require('async/waterfall')
const MemoryStore = require('interface-datastore').MemoryDatastore
const mergeOptions = require('merge-options')
const ipldDagCbor = require('ipld-dag-cbor')
const ipldDagPb = require('ipld-dag-pb')
const ipldRaw = require('ipld-raw')

function noop () {}

class IPLDResolver {
constructor (options) {
constructor (userOptions) {
const options = mergeOptions(IPLDResolver.defaultOptions, userOptions)

if (!options.blockService) {
throw new Error('Missing blockservice')
}

this.bs = options.blockService

// Support by default dag-pb, dag-cbor, git, and eth-*
this.resolvers = {
get 'dag-pb' () {
const format = require('ipld-dag-pb')
return { resolver: format.resolver, util: format.util }
},
get 'dag-cbor' () {
const format = require('ipld-dag-cbor')
return { resolver: format.resolver, util: format.util }
},
get 'git-raw' () {
const format = require('ipld-git')
return { resolver: format.resolver, util: format.util }
},
get 'bitcoin-block' () {
const format = require('ipld-bitcoin')
return { resolver: format.resolver, util: format.util }
},
get 'eth-account-snapshot' () {
const format = require('ipld-ethereum').ethAccountSnapshot
return { resolver: format.resolver, util: format.util }
},
get 'eth-block' () {
const format = require('ipld-ethereum').ethBlock
return { resolver: format.resolver, util: format.util }
},
get 'eth-block-list' () {
const format = require('ipld-ethereum').ethBlockList
return { resolver: format.resolver, util: format.util }
},
get 'eth-state-trie' () {
const format = require('ipld-ethereum').ethStateTrie
return { resolver: format.resolver, util: format.util }
},
get 'eth-storage-trie' () {
const format = require('ipld-ethereum').ethStorageTrie
return { resolver: format.resolver, util: format.util }
},
get 'eth-tx' () {
const format = require('ipld-ethereum').ethTx
return { resolver: format.resolver, util: format.util }
},
get 'eth-tx-trie' () {
const format = require('ipld-ethereum').ethTxTrie
return { resolver: format.resolver, util: format.util }
},
get raw () {
const format = require('ipld-raw')
return { resolver: format.resolver, util: format.util }
},
get 'zcash-block' () {
const format = require('ipld-zcash')
return { resolver: format.resolver, util: format.util }
}
}
// Object with current list of active resolvers
this.resolvers = {}

// API entry point
this.support = {}

// Adds support for an IPLD format
Expand All @@ -100,6 +53,13 @@ class IPLDResolver {
delete this.resolvers[multicodec]
}
}

// Enable all supplied formats
for (const format of options.formats) {
const {resolver, util} = format
const multicodec = resolver.multicodec
this.support.add(multicodec, resolver, util)
}
}

get (cid, path, options, callback) {
Expand Down Expand Up @@ -414,6 +374,13 @@ class IPLDResolver {
}
}

/**
* Default options for IPLD.
*/
IPLDResolver.defaultOptions = {
formats: [ipldDagCbor, ipldDagPb, ipldRaw]
}

/**
* Create an IPLD resolver with an inmemory blockservice and
* repo.
Expand Down
5 changes: 4 additions & 1 deletion test/ipld-bitcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ module.exports = (repo) => {

before((done) => {
const bs = new BlockService(repo)
resolver = new IPLDResolver({blockService: bs})
resolver = new IPLDResolver({
blockService: bs,
formats: [ipldBitcoin]
})

series([
(cb) => {
Expand Down
5 changes: 4 additions & 1 deletion test/ipld-eth-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ module.exports = (repo) => {

before((done) => {
const bs = new BlockService(repo)
resolver = new IPLDResolver({blockService: bs})
resolver = new IPLDResolver({
blockService: bs,
formats: [ipldEthBlock]
})

series([
(cb) => {
Expand Down
7 changes: 6 additions & 1 deletion test/ipld-eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const expect = chai.expect
chai.use(dirtyChai)
const rlp = require('rlp')
const BlockService = require('ipfs-block-service')
const ipldEthBlock = require('ipld-ethereum').ethBlock
const ipldEthStateTrie = require('ipld-ethereum').ethStateTrie
const loadFixture = require('aegir/fixtures')
const async = require('async')
const cidForHash = require('eth-hash-to-cid')
Expand All @@ -24,7 +26,10 @@ module.exports = (repo) => {
before(function (done) {
this.timeout(10 * 1000)
const bs = new BlockService(repo)
resolver = new IPLDResolver({blockService: bs})
resolver = new IPLDResolver({
blockService: bs,
formats: [ipldEthBlock, ipldEthStateTrie]
})

async.waterfall([
readFilesFixture,
Expand Down
5 changes: 4 additions & 1 deletion test/ipld-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ module.exports = (repo) => {
before((done) => {
const bs = new BlockService(repo)

resolver = new IPLDResolver({blockService: bs})
resolver = new IPLDResolver({
blockService: bs,
formats: [ipldGit]
})

series([
(cb) => {
Expand Down
5 changes: 4 additions & 1 deletion test/ipld-zcash.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ module.exports = (repo) => {

before((done) => {
const bs = new BlockService(repo)
resolver = new IPLDResolver({blockService: bs})
resolver = new IPLDResolver({
blockService: bs,
formats: [ipldZcash]
})

series([
(cb) => {
Expand Down

0 comments on commit b003ad1

Please sign in to comment.