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

Binary resolver #90

Merged
merged 11 commits into from
Oct 7, 2017
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const ipldEthStateTrie = require('ipld-eth-star').ethStateTrie
const ipldEthStorageTrie = require('ipld-eth-star').ethStorageTrie
const ipldEthTx = require('ipld-eth-star').ethTx
const ipldEthTxTrie = require('ipld-eth-star').ethTxTrie
const ipldBin = require('./ipldBin')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumavis mind moving this resolver to here https://github.com/ipld/js-ipld-raw ?


function noop () {}

Expand Down Expand Up @@ -91,6 +92,10 @@ class IPLDResolver {
this.support.add(ipldEthTxTrie.resolver.multicodec,
ipldEthTxTrie.resolver,
ipldEthTxTrie.util)

this.support.add(ipldBin.resolver.multicodec,
ipldBin.resolver,
ipldBin.util)
}

get (cid, path, options, callback) {
Expand Down
29 changes: 29 additions & 0 deletions src/ipldBin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'
const CID = require('cids')

// binary resolver
module.exports = {
resolver: {
multicodec: 'base2',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that discussed this in great length and the reasons why raw is different from base2. If I remember correctly, you pointed out that base2 is actually incorrect .

I'm not finding the issue where that discussion happened (I think it was in IRC, our bad for not logging it).

All of that said, let's use the codec raw here just to avoid any future confusion https://github.com/multiformats/multicodec/blob/master/table.csv#L3

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base2 codec is literal stream of ASCI: 010100111010110.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kubuxu does base2 have a code yet? in the multiformat table it has the same value as bin

resolve: (ipfsBlock, path, callback) => {
callback(null, {
value: ipfsBlock.data,
remainderPath: path
})
},
tree: (ipfsBlock, options, callback) => {
callback(null, [])
}
},
util: {
deserialize: (data, cb) => {
cb(null, data)
},
serialize: (data, cb) => {
cb(null, data)
},
cid: (data, cb) => {
cb(null, new CID(1, 'base2', data))
}
}
}
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('Browser', () => {
], done)
})

require('./ipld-bin')
require('./basics')(repo)
require('./ipld-dag-pb')(repo)
require('./ipld-dag-cbor')(repo)
Expand Down
76 changes: 76 additions & 0 deletions test/ipld-bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const IpfsBlock = require('ipfs-block')
const CID = require('cids')
const ipldBin = require('../src/ipldBin')

module.exports = () => {
describe('IPLD Resolver with ipld-bin (binary resolver)', () => {
it('resolves an ipfsBlock to its data', (done) => {
const buffer = Buffer.from('deadbeef', 'hex')
const cid = new CID('zDvjKAA4iXx')
const ipfsBlock = new IpfsBlock(buffer, cid)
ipldBin.resolver.resolve(ipfsBlock, '/hello/world', (err, result) => {
expect(err).to.not.exist()
expect(result).to.exist()
expect(Buffer.isBuffer(result.value)).to.be.true()
expect(result.value.toString('hex')).to.be.eql('deadbeef')
expect(result.remainderPath).to.be.eql('/hello/world')
done()
})
})

it('trees an empty array', (done) => {
const buffer = Buffer.from('deadbeef', 'hex')
const cid = new CID('zDvjKAA4iXx')
const ipfsBlock = new IpfsBlock(buffer, cid)
ipldBin.resolver.tree(ipfsBlock, {}, (err, result) => {
expect(err).to.not.exist()
expect(result).to.exist()
expect(result.length).to.eql(0)
done()
})
})

it('creates a cid with the currect multicodec', (done) => {
const buffer = Buffer.from('deadbeef', 'hex')
// const cid = new CID('zDvjKAA4iXx')
// const ipfsBlock = new IpfsBlock(buffer, cid)
ipldBin.util.cid(buffer, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.codec).to.eql('base2')
done()
})
})

it('serializes to the same value', (done) => {
const buffer = Buffer.from('deadbeef', 'hex')
// const cid = new CID('zDvjKAA4iXx')
// const ipfsBlock = new IpfsBlock(buffer, cid)
ipldBin.util.serialize(buffer, (err, result) => {
expect(err).to.not.exist()
expect(result).to.exist()
expect(result.toString('hex')).to.eql(buffer.toString('hex'))
done()
})
})

it('deserializes to the same value', (done) => {
const buffer = Buffer.from('deadbeef', 'hex')
// const cid = new CID('zDvjKAA4iXx')
// const ipfsBlock = new IpfsBlock(buffer, cid)
ipldBin.util.deserialize(buffer, (err, result) => {
expect(err).to.not.exist()
expect(result).to.exist()
expect(result.toString('hex')).to.eql(buffer.toString('hex'))
done()
})
})
})
}
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Node.js', () => {
], done)
})

require('./ipld-bin')
require('./basics')(repo)
require('./ipld-dag-pb')(repo)
require('./ipld-dag-cbor')(repo)
Expand Down