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

Commit

Permalink
refactor: store codecs by their code
Browse files Browse the repository at this point in the history
Instead of storing a codec by their name, use their code instead.

The tests are changed from `base1` to `blake2b-8` as codecs for different
bases are no longer part of multicodec, but are part of multibase now.

BREAKING CHANGE: The `codec` parameter in `options.loadFormat()` is a number

Instead of returnign the name of the codec as string, the codec code (a number)
is now returned.

So if you e.g. check within the function for a certain format, it changes from:

    async loadFormat (codec) {
      if (codec !== 'dag-cbor') …
    }

To:

    async loadFormat (codec) {
      if (codec !== multicodec.DAG_CBOR) …
    }
  • Loading branch information
vmx committed Mar 21, 2019
1 parent 996e9dc commit d797667
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"ipld-dag-pb": "~0.15.2",
"ipld-raw": "^2.0.1",
"merge-options": "^1.0.1",
"multicodec": "~0.5.0",
"pull-defer": "~0.2.3",
"pull-stream": "^3.6.9",
"pull-traverse": "^1.0.3"
Expand Down
32 changes: 22 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mergeOptions = require('merge-options')
const ipldDagCbor = require('ipld-dag-cbor')
const ipldDagPb = require('ipld-dag-pb')
const ipldRaw = require('ipld-raw')
const multicodec = require('multicodec')
const { fancyIterator } = require('./util')

function noop () {}
Expand All @@ -31,36 +32,40 @@ class IPLDResolver {
this.support = {}

// Adds support for an IPLD format
this.support.add = (multicodec, resolver, util) => {
if (this.resolvers[multicodec]) {
throw new Error('Resolver already exists for codec "' + multicodec + '"')
this.support.add = (codec, resolver, util) => {
if (this.resolvers[codec]) {
const codecName = multicodec.print[codec]
throw new Error(`Resolver already exists for codec "${codecName}"`)
}

this.resolvers[multicodec] = {
this.resolvers[codec] = {
resolver: resolver,
util: util
}
}

if (options.loadFormat === undefined) {
this.support.load = async (codec) => {
throw new Error(`No resolver found for codec "${codec}"`)
const codecName = multicodec.print[codec]
throw new Error(`No resolver found for codec "${codecName}"`)
}
} else {
this.support.load = options.loadFormat
}

this.support.rm = (multicodec) => {
if (this.resolvers[multicodec]) {
delete this.resolvers[multicodec]
this.support.rm = (codec) => {
if (this.resolvers[codec]) {
delete this.resolvers[codec]
}
}

// Enable all supplied formats
for (const format of options.formats) {
const { resolver, util } = format
const multicodec = resolver.multicodec
this.support.add(multicodec, resolver, util)
// IPLD Formats are using strings instead of constants for the multicodec
const codecBuffer = multicodec.getCodeVarint(resolver.multicodec)
const codec = multicodec.getCode(codecBuffer)
this.support.add(codec, resolver, util)
}
}

Expand Down Expand Up @@ -325,6 +330,13 @@ class IPLDResolver {
/* internals */
/* */
async _getFormat (codec) {
// TODO vmx 2019-01-24: Once all CIDs support accessing the codec code
// instead of the name, remove this part
if (typeof codec === 'string') {
const constantName = codec.toUpperCase().replace(/-/g, '_')
codec = multicodec[constantName]
}

if (this.resolvers[codec]) {
return this.resolvers[codec]
}
Expand Down
28 changes: 20 additions & 8 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ module.exports = (repo) => {
const bs = new BlockService(repo)
const r = new IPLDResolver({ blockService: bs })
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
const cid = new CID(
1,
'blake2b-8',
multihash.encode(Buffer.from('abcd', 'hex'), 'sha1')
)
const result = r.resolve(cid, '')
await expect(result.next()).to.be.rejectedWith(
'No resolver found for codec "base1"')
'No resolver found for codec "blake2b-8"')
})

// TODO vmx 2018-11-29 Change this test to use `get()`.
Expand All @@ -62,9 +66,9 @@ module.exports = (repo) => {
const bs = new BlockService(repo)
const r = new IPLDResolver({ blockService: bs })
// choosing a format that is not supported
r.put(null, { format: 'base1' }, (err, result) => {
r.put(null, { format: 'blake2b-8' }, (err, result) => {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
expect(err.message).to.eql('No resolver found for codec "blake2b-8"')
done()
})
})
Expand All @@ -83,10 +87,14 @@ module.exports = (repo) => {
const bs = new BlockService(repo)
const r = new IPLDResolver({ blockService: bs })
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
const cid = new CID(
1,
'blake2b-8',
multihash.encode(Buffer.from('abcd', 'hex'), 'sha1')
)
r._put(cid, null, (err, result) => {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
expect(err.message).to.eql('No resolver found for codec "blake2b-8"')
done()
})
})
Expand All @@ -95,12 +103,16 @@ module.exports = (repo) => {
const bs = new BlockService(repo)
const r = new IPLDResolver({ blockService: bs })
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
const cid = new CID(
1,
'blake2b-8',
multihash.encode(Buffer.from('abcd', 'hex'), 'sha1')
)
pull(
r.treeStream(cid, '/', {}),
pull.collect(function (err) {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
expect(err.message).to.eql('No resolver found for codec "blake2b-8"')
done()
})
)
Expand Down
9 changes: 7 additions & 2 deletions test/format-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ chai.use(dirtyChai)
chai.use(chaiAsProised)
const BlockService = require('ipfs-block-service')
const dagCBOR = require('ipld-dag-cbor')
const multicodec = require('multicodec')

const IPLDResolver = require('../src')

Expand Down Expand Up @@ -49,7 +50,9 @@ module.exports = (repo) => {
blockService: bs,
formats: [],
async loadFormat (codec) {
if (codec !== 'dag-cbor') throw new Error('unexpected codec')
if (codec !== multicodec.DAG_CBOR) {
throw new Error('unexpected codec')
}
throw new Error(errMsg)
}
})
Expand All @@ -64,7 +67,9 @@ module.exports = (repo) => {
blockService: bs,
formats: [],
async loadFormat (codec) {
if (codec !== 'dag-cbor') throw new Error('unexpected codec')
if (codec !== multicodec.DAG_CBOR) {
throw new Error('unexpected codec')
}
return dagCBOR
}
})
Expand Down

0 comments on commit d797667

Please sign in to comment.