Skip to content

feat/complete #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
},
"devDependencies": {
"aegir": "^8.1.1",
"chai": "^3.5.0",
"pre-commit": "^1.1.3"
},
"contributors": [
"David Dias <[email protected]>",
"Richard Littauer <[email protected]>"
]
}
}
32 changes: 28 additions & 4 deletions src/base-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,45 @@

const varint = require('varint')

// spec and table at: https://github.com/multiformats/multicodec

// TODO revisit all of these once https://github.com/multiformats/multicodec/pull/16 is merged

exports = module.exports

// Miscellaneous
exports.raw = varintBuf(0) // 0x00

// Multiformats
exports.multicodec = varintBuf(64) // 0x40
exports.multihash = varintBuf(65) // 0x41
exports.multiaddr = varintBuf(66) // 0x42
// bases encodings

// Serialization formats
exports.protobuf = varintBuf(80) // 0x50
exports.cbor = varintBuf(81) // 0x51
exports.rlp = varintBuf(96) // 0x60

// Multiformats
exports.multicodec = varintBuf(64) // 0x40
exports.multihash = varintBuf(65) // 0x41
exports.multiaddr = varintBuf(66) // 0x42

// multihashes

// multiaddrs

// archiving formats

// image formats

// video formats

// VCS formats

// IPLD formats
exports['dag-pb'] = new Buffer('70', 'hex')
exports['dag-cbor'] = new Buffer('71', 'hex')
exports['eth-block'] = new Buffer('90', 'hex')
exports['eth-tx'] = new Buffer('91', 'hex')

function varintBuf (n) {
return new Buffer(varint.encode(n))
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ exports.rmPrefix = (data) => {
}

exports.getCodec = (prefixedData) => {
const code = new Buffer(varint.decode(prefixedData))

const v = varint.decode(prefixedData)
const code = new Buffer(v.toString(16), 'hex')
let codec

Object.keys(table)
Expand Down
20 changes: 20 additions & 0 deletions test/multicodec.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const multicodec = require('../src')

describe('multicodec', () => {
it('add prefix through multicodec (string)', () => {
const buf = new Buffer('hey')
const prefixedBuf = multicodec.addPrefix('protobuf', buf)
expect(multicodec.getCodec(prefixedBuf)).to.equal('protobuf')
expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf))
})

it('add prefix through code (code)', () => {
const buf = new Buffer('hey')
const prefixedBuf = multicodec.addPrefix(new Buffer('70', 'hex'), buf)
expect(multicodec.getCodec(prefixedBuf)).to.equal('dag-pb')
expect(buf).to.eql(multicodec.rmPrefix(prefixedBuf))
})
})