What is multicodec?
The multicodec npm package is a self-describing multiformat, which is useful for identifying the format of a given piece of data. It is used extensively in the IPFS ecosystem to handle various data formats in a consistent manner.
What are multicodec's main functionalities?
Encoding and Decoding
This feature allows you to encode a buffer with a specific codec and decode it to find out the codec used. The `addPrefix` method adds a prefix to the buffer, and the `rmPrefix` method removes it.
const multicodec = require('multicodec');
// Encoding a buffer with a specific codec
const buffer = Buffer.from('hello world');
const encoded = multicodec.addPrefix('raw', buffer);
console.log(encoded);
// Decoding a buffer to find out the codec
const decoded = multicodec.rmPrefix(encoded);
console.log(decoded);
Listing Supported Codecs
This feature allows you to list all the supported codecs in the multicodec package. The `print` property contains a list of all the codecs.
const multicodec = require('multicodec');
// Listing all supported codecs
const codecs = multicodec.print;
console.log(codecs);
Resolving Codec Names and Codes
This feature allows you to resolve codec names to their corresponding codes and vice versa. The `getCodeVarint` method returns the code for a given codec name, and the `getName` method returns the name for a given codec code.
const multicodec = require('multicodec');
// Resolving codec name to code
const code = multicodec.getCodeVarint('dag-pb');
console.log(code);
// Resolving codec code to name
const name = multicodec.getName(0x70);
console.log(name);
Other packages similar to multicodec
multihashes
The multihashes package is used for creating and working with self-describing hashes. It is similar to multicodec in that it provides a way to handle multiple formats in a consistent manner, but it is specifically focused on hash functions.
multibase
The multibase package is used for encoding and decoding data in various base encodings. It is similar to multicodec in that it provides a way to handle multiple formats, but it is specifically focused on base encodings.
multiformats
The multiformats package is a collection of self-describing formats including multibase, multihash, and multicodec. It provides a unified interface for working with these formats, making it a more comprehensive solution compared to using multicodec alone.
js-multicodec
JavaScript implementation of the multicodec specification
Lead Maintainer
Henrique Dias
Table of Contents
TODO™
Install
> npm install multicodec
const multicodec = require('multicodec')
Usage
Example
const multicodec = require('multicodec')
const prefixedProtobuf = multicodec.addPrefix('protobuf', protobufBuffer)
Updating the lookup table
Updating the lookup table is a manual process. The source of truth is the
multicodec default table. To make the process easier, there’s an AWK script in the tools directory that does a basic conversion of the default table. The result can’t be used as-is, but serves as a template for manual diffing. The workflow is:
API
https://multiformats.github.io/js-multicodec/
Contribute
Contributions welcome. Please check out the issues.
Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.
Small note: If editing the README, please conform to the standard-readme specification.
License
MIT © 2016 Protocol Labs Inc.