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

Commit d797667

Browse files
committed
refactor: store codecs by their code
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) … }
1 parent 996e9dc commit d797667

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"ipld-dag-pb": "~0.15.2",
6262
"ipld-raw": "^2.0.1",
6363
"merge-options": "^1.0.1",
64+
"multicodec": "~0.5.0",
6465
"pull-defer": "~0.2.3",
6566
"pull-stream": "^3.6.9",
6667
"pull-traverse": "^1.0.3"

src/index.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const mergeOptions = require('merge-options')
1111
const ipldDagCbor = require('ipld-dag-cbor')
1212
const ipldDagPb = require('ipld-dag-pb')
1313
const ipldRaw = require('ipld-raw')
14+
const multicodec = require('multicodec')
1415
const { fancyIterator } = require('./util')
1516

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

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

39-
this.resolvers[multicodec] = {
41+
this.resolvers[codec] = {
4042
resolver: resolver,
4143
util: util
4244
}
4345
}
4446

4547
if (options.loadFormat === undefined) {
4648
this.support.load = async (codec) => {
47-
throw new Error(`No resolver found for codec "${codec}"`)
49+
const codecName = multicodec.print[codec]
50+
throw new Error(`No resolver found for codec "${codecName}"`)
4851
}
4952
} else {
5053
this.support.load = options.loadFormat
5154
}
5255

53-
this.support.rm = (multicodec) => {
54-
if (this.resolvers[multicodec]) {
55-
delete this.resolvers[multicodec]
56+
this.support.rm = (codec) => {
57+
if (this.resolvers[codec]) {
58+
delete this.resolvers[codec]
5659
}
5760
}
5861

5962
// Enable all supplied formats
6063
for (const format of options.formats) {
6164
const { resolver, util } = format
62-
const multicodec = resolver.multicodec
63-
this.support.add(multicodec, resolver, util)
65+
// IPLD Formats are using strings instead of constants for the multicodec
66+
const codecBuffer = multicodec.getCodeVarint(resolver.multicodec)
67+
const codec = multicodec.getCode(codecBuffer)
68+
this.support.add(codec, resolver, util)
6469
}
6570
}
6671

@@ -325,6 +330,13 @@ class IPLDResolver {
325330
/* internals */
326331
/* */
327332
async _getFormat (codec) {
333+
// TODO vmx 2019-01-24: Once all CIDs support accessing the codec code
334+
// instead of the name, remove this part
335+
if (typeof codec === 'string') {
336+
const constantName = codec.toUpperCase().replace(/-/g, '_')
337+
codec = multicodec[constantName]
338+
}
339+
328340
if (this.resolvers[codec]) {
329341
return this.resolvers[codec]
330342
}

test/basics.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ module.exports = (repo) => {
3939
const bs = new BlockService(repo)
4040
const r = new IPLDResolver({ blockService: bs })
4141
// choosing a format that is not supported
42-
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
42+
const cid = new CID(
43+
1,
44+
'blake2b-8',
45+
multihash.encode(Buffer.from('abcd', 'hex'), 'sha1')
46+
)
4347
const result = r.resolve(cid, '')
4448
await expect(result.next()).to.be.rejectedWith(
45-
'No resolver found for codec "base1"')
49+
'No resolver found for codec "blake2b-8"')
4650
})
4751

4852
// TODO vmx 2018-11-29 Change this test to use `get()`.
@@ -62,9 +66,9 @@ module.exports = (repo) => {
6266
const bs = new BlockService(repo)
6367
const r = new IPLDResolver({ blockService: bs })
6468
// choosing a format that is not supported
65-
r.put(null, { format: 'base1' }, (err, result) => {
69+
r.put(null, { format: 'blake2b-8' }, (err, result) => {
6670
expect(err).to.exist()
67-
expect(err.message).to.eql('No resolver found for codec "base1"')
71+
expect(err.message).to.eql('No resolver found for codec "blake2b-8"')
6872
done()
6973
})
7074
})
@@ -83,10 +87,14 @@ module.exports = (repo) => {
8387
const bs = new BlockService(repo)
8488
const r = new IPLDResolver({ blockService: bs })
8589
// choosing a format that is not supported
86-
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
90+
const cid = new CID(
91+
1,
92+
'blake2b-8',
93+
multihash.encode(Buffer.from('abcd', 'hex'), 'sha1')
94+
)
8795
r._put(cid, null, (err, result) => {
8896
expect(err).to.exist()
89-
expect(err.message).to.eql('No resolver found for codec "base1"')
97+
expect(err.message).to.eql('No resolver found for codec "blake2b-8"')
9098
done()
9199
})
92100
})
@@ -95,12 +103,16 @@ module.exports = (repo) => {
95103
const bs = new BlockService(repo)
96104
const r = new IPLDResolver({ blockService: bs })
97105
// choosing a format that is not supported
98-
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
106+
const cid = new CID(
107+
1,
108+
'blake2b-8',
109+
multihash.encode(Buffer.from('abcd', 'hex'), 'sha1')
110+
)
99111
pull(
100112
r.treeStream(cid, '/', {}),
101113
pull.collect(function (err) {
102114
expect(err).to.exist()
103-
expect(err.message).to.eql('No resolver found for codec "base1"')
115+
expect(err.message).to.eql('No resolver found for codec "blake2b-8"')
104116
done()
105117
})
106118
)

test/format-support.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ chai.use(dirtyChai)
99
chai.use(chaiAsProised)
1010
const BlockService = require('ipfs-block-service')
1111
const dagCBOR = require('ipld-dag-cbor')
12+
const multicodec = require('multicodec')
1213

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

@@ -49,7 +50,9 @@ module.exports = (repo) => {
4950
blockService: bs,
5051
formats: [],
5152
async loadFormat (codec) {
52-
if (codec !== 'dag-cbor') throw new Error('unexpected codec')
53+
if (codec !== multicodec.DAG_CBOR) {
54+
throw new Error('unexpected codec')
55+
}
5356
throw new Error(errMsg)
5457
}
5558
})
@@ -64,7 +67,9 @@ module.exports = (repo) => {
6467
blockService: bs,
6568
formats: [],
6669
async loadFormat (codec) {
67-
if (codec !== 'dag-cbor') throw new Error('unexpected codec')
70+
if (codec !== multicodec.DAG_CBOR) {
71+
throw new Error('unexpected codec')
72+
}
6873
return dagCBOR
6974
}
7075
})

0 commit comments

Comments
 (0)