multicodec
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "multicodec", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "JavaScript implementation of the multicodec-packed specification", | ||
@@ -46,3 +46,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"aegir": "^8.1.1", | ||
"aegir": "^9.1.2", | ||
"chai": "^3.5.0", | ||
@@ -53,4 +53,6 @@ "pre-commit": "^1.1.3" | ||
"David Dias <daviddias.p@gmail.com>", | ||
"Richard Littauer <richard.littauer@gmail.com>" | ||
"Richard Littauer <richard.littauer@gmail.com>", | ||
"kumavis <aaron@kumavis.me>", | ||
"wanderer <mjbecze@gmail.com>" | ||
] | ||
} |
'use strict' | ||
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 | ||
exports['raw'] = new Buffer('00', 'hex') | ||
// bases encodings | ||
exports['base1'] = new Buffer('01', 'hex') | ||
exports['base2'] = new Buffer('55', 'hex') | ||
exports['base8'] = new Buffer('07', 'hex') | ||
exports['base10'] = new Buffer('09', 'hex') | ||
// Serialization formats | ||
exports.protobuf = varintBuf(80) // 0x50 | ||
exports.cbor = varintBuf(81) // 0x51 | ||
exports.rlp = varintBuf(96) // 0x60 | ||
exports['protobuf'] = new Buffer('50', 'hex') | ||
exports['cbor'] = new Buffer('51', 'hex') | ||
exports['rlp'] = new Buffer('60', 'hex') | ||
// Multiformats | ||
exports.multicodec = varintBuf(64) // 0x40 | ||
exports.multihash = varintBuf(65) // 0x41 | ||
exports.multiaddr = varintBuf(66) // 0x42 | ||
exports['multicodec'] = new Buffer('30', 'hex') | ||
exports['multihash'] = new Buffer('31', 'hex') | ||
exports['multiaddr'] = new Buffer('32', 'hex') | ||
exports['multibase'] = new Buffer('33', 'hex') | ||
// multihashes | ||
exports['sha1'] = new Buffer('11', 'hex') | ||
exports['sha2-256'] = new Buffer('12', 'hex') | ||
exports['sha2-512'] = new Buffer('13', 'hex') | ||
exports['sha3-224'] = new Buffer('17', 'hex') | ||
exports['sha3-256'] = new Buffer('16', 'hex') | ||
exports['sha3-384'] = new Buffer('15', 'hex') | ||
exports['sha3-512'] = new Buffer('14', 'hex') | ||
exports['shake-128'] = new Buffer('18', 'hex') | ||
exports['shake-256'] = new Buffer('19', 'hex') | ||
exports['keccak-224'] = new Buffer('1a', 'hex') | ||
exports['keccak-256'] = new Buffer('1b', 'hex') | ||
exports['keccak-384'] = new Buffer('1c', 'hex') | ||
exports['keccak-512'] = new Buffer('1d', 'hex') | ||
exports['blake2b'] = new Buffer('40', 'hex') | ||
exports['blake2s'] = new Buffer('41', 'hex') | ||
// multiaddrs | ||
exports['ip4'] = new Buffer('04', 'hex') | ||
exports['ip6'] = new Buffer('29', 'hex') | ||
exports['tcp'] = new Buffer('06', 'hex') | ||
exports['udp'] = new Buffer('0111', 'hex') | ||
exports['dccp'] = new Buffer('21', 'hex') | ||
exports['sctp'] = new Buffer('84', 'hex') | ||
exports['udt'] = new Buffer('012d', 'hex') | ||
exports['utp'] = new Buffer('012e', 'hex') | ||
exports['ipfs'] = new Buffer('2a', 'hex') | ||
exports['http'] = new Buffer('01e0', 'hex') | ||
exports['https'] = new Buffer('01bb', 'hex') | ||
exports['ws'] = new Buffer('01dd', 'hex') | ||
exports['onion'] = new Buffer('01bc', 'hex') | ||
@@ -43,5 +72,6 @@ // archiving formats | ||
exports['eth-tx'] = new Buffer('91', 'hex') | ||
function varintBuf (n) { | ||
return new Buffer(varint.encode(n)) | ||
} | ||
exports['eth-account'] = new Buffer('92', 'hex') | ||
exports['bitcoin-block'] = new Buffer('b0', 'hex') | ||
exports['bitcoin-tx'] = new Buffer('b1', 'hex') | ||
exports['stellar-block'] = new Buffer('d0', 'hex') | ||
exports['stellar-tx'] = new Buffer('d1', 'hex') |
'use strict' | ||
const table = require('./base-table') | ||
const varint = require('varint') | ||
const codecNameToCodeVarint = require('./varint-table') | ||
const codeToCodecName = require('./name-table') | ||
const util = require('./util') | ||
@@ -9,9 +10,9 @@ exports = module.exports | ||
exports.addPrefix = (multicodecStrOrCode, data) => { | ||
let pfx | ||
let prefix | ||
if (Buffer.isBuffer(multicodecStrOrCode)) { | ||
pfx = multicodecStrOrCode | ||
prefix = util.varintBufferEncode(multicodecStrOrCode) | ||
} else { | ||
if (table[multicodecStrOrCode]) { | ||
pfx = table[multicodecStrOrCode] | ||
if (codecNameToCodeVarint[multicodecStrOrCode]) { | ||
prefix = codecNameToCodeVarint[multicodecStrOrCode] | ||
} else { | ||
@@ -21,3 +22,3 @@ throw new Error('multicodec not recognized') | ||
} | ||
return Buffer.concat([pfx, data]) | ||
return Buffer.concat([prefix, data]) | ||
} | ||
@@ -31,14 +32,6 @@ | ||
exports.getCodec = (prefixedData) => { | ||
const v = varint.decode(prefixedData) | ||
const code = new Buffer(v.toString(16), 'hex') | ||
let codec | ||
const code = util.varintBufferDecode(prefixedData) | ||
const codecName = codeToCodecName[code.toString('hex')] | ||
return codecName | ||
} | ||
Object.keys(table) | ||
.forEach((mc) => { | ||
if (code.equals(table[mc])) { | ||
codec = mc | ||
} | ||
}) | ||
return codec | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16
196951
2370
1