UNPKG

multicodec

Version:

JavaScript implementation of the multicodec specification

134 lines (120 loc) 2.96 kB
/** * Implementation of the multicodec specification. * * @module multicodec * @example * const multicodec = require('multicodec') * * const prefixedProtobuf = multicodec.addPrefix('protobuf', protobufBuffer) * // prefixedProtobuf 0x50... * */ 'use strict' const varint = require('varint') const intTable = require('./int-table') const codecNameToCodeVarint = require('./varint-table') const util = require('./util') const uint8ArrayConcat = require('uint8arrays/concat') exports = module.exports /** * Prefix a buffer with a multicodec-packed. * * @param {string|number} multicodecStrOrCode * @param {Uint8Array} data * @returns {Uint8Array} */ exports.addPrefix = (multicodecStrOrCode, data) => { let prefix if (multicodecStrOrCode instanceof Uint8Array) { prefix = util.varintUint8ArrayEncode(multicodecStrOrCode) } else { if (codecNameToCodeVarint[multicodecStrOrCode]) { prefix = codecNameToCodeVarint[multicodecStrOrCode] } else { throw new Error('multicodec not recognized') } } return uint8ArrayConcat([prefix, data], prefix.length + data.length) } /** * Decapsulate the multicodec-packed prefix from the data. * * @param {Uint8Array} data * @returns {Uint8Array} */ exports.rmPrefix = (data) => { varint.decode(data) return data.slice(varint.decode.bytes) } /** * Get the codec of the prefixed data. * * @param {Uint8Array} prefixedData * @returns {string} */ exports.getCodec = (prefixedData) => { const code = varint.decode(prefixedData) const codecName = intTable.get(code) if (codecName === undefined) { throw new Error(`Code ${code} not found`) } return codecName } /** * Get the name of the codec. * * @param {number} codec * @returns {string} */ exports.getName = (codec) => { return intTable.get(codec) } /** * Get the code of the codec * * @param {string} name * @returns {number} */ exports.getNumber = (name) => { const code = codecNameToCodeVarint[name] if (code === undefined) { throw new Error('Codec `' + name + '` not found') } return varint.decode(code) } /** * Get the code of the prefixed data. * * @param {Uint8Array} prefixedData * @returns {number} */ exports.getCode = (prefixedData) => { return varint.decode(prefixedData) } /** * Get the code as varint of a codec name. * * @param {string} codecName * @returns {Uint8Array} */ exports.getCodeVarint = (codecName) => { const code = codecNameToCodeVarint[codecName] if (code === undefined) { throw new Error('Codec `' + codecName + '` not found') } return code } /** * Get the varint of a code. * * @param {number} code * @returns {Array.<number>} */ exports.getVarint = (code) => { return varint.encode(code) } // Make the constants top-level constants const constants = require('./constants') Object.assign(exports, constants) // Human friendly names for printing, e.g. in error messages exports.print = require('./print')