# multiformat-multicodec

> An interface for (de)encoding a wide range of multicodecs simultaneously

Latest version **1.0.0** (published 2022-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install multiformat-multicodec
pnpm add multiformat-multicodec
yarn add multiformat-multicodec
bun add multiformat-multicodec
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-08-24 |
| First published | 2022-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 56.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Upkeep |
| Maintainers | misterupkeep |
| Keywords | multiformat, multicodec, ipld, decoder, encoder, codec |

## Links

- npm: https://www.npmjs.com/package/multiformat-multicodec
- Repository: https://github.com/misterupkeep/multiformat-multicodec
- Homepage: https://github.com/misterupkeep/multiformat-multicodec#readme
- Issues: https://github.com/misterupkeep/multiformat-multicodec/issues
- npm.io page: https://npm.io/package/multiformat-multicodec

## Dependencies (1)

- [multiformats](https://npm.io/package/multiformats.md) ^9.7.1

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-08-24

## README

# multiformat-multicodec

An interface for encoding/decoding across multiple multiformat [BlockEncoders, BlockDecoders, BlockCodecs](https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts), and [Hashers](https://github.com/multiformats/js-multiformats/blob/master/src/hashes/hasher.js#L22).

This interface lets library authors expose a dynamic dependency injection system to their users, so as to not be locked into any particular suite of codecs/hashers -- much like the [IPFS modular interfaces](https://github.com/ipfs/js-ipfs/tree/master/packages/interface-ipfs-core). The library also provides a default trivial implementation of the interfaces.

If you need to decode blocks which may come encoded using many different codecs,
or need your library users to give you the codecs and hashers they want you to
use, this is probably what you're looking for.

## Interfaces

The library exports three interfaces which encapsulate the notion of
(de)encoding for given codec and hasher codes:

`Multidecoder` is an interface that can decode using multiple decoders/hashers:
```ts
interface Multidecoder {
  addDecoder(decoder: BlockDecoder): void;
  addHasher(hasher: Hasher): void;
  decode(opts: {
    codec: number;
    hasher: number;
    bytes: ByteView<T>;
  }): Promise<Block<T>>;
}
```

`Multiencoder` is its dual, and can encode using multiple encoders/hashers (you pick which one):
```ts
interface Multiencoder {
  addEncoder(encoder: BlockEncoder): void;
  addHasher(hasher: Hasher): void;
  encode(opts: { codec: number; hasher: number; value: T }): Promise<Block<T>>;
}
```

If you need both interfaces simultaneously, the `Multicodec` interface simply extends the two:
```ts
interface Multicodec extends Multidecoder, Multiencoder {}
```

## Implementation

The library also exports a default implementation of `Multicodec`, called
`BlockMulticodec`. By default, it already has `sha256` hasher support. It has an
extra method `addCodec()` which isn't in the `Multicodec` interface.

```ts
import { BlockMulticodec, Multidecoder } from "multiformat-multicodec";

import { CID } from "multiformats";

import * as json from "@ipld/dag-json";
import * as cbor from "@ipld/dag-cbor";
import * as pb from "@ipld/dag-pb";

const multidecoder = new BlockMulticodec<any>({
  codecs: [json, cbor, pb],
});

const cid = CID.parse("QmQy6xmJhrcC5QLboAcGFcAE1tC8CrwDVkrHdEYJkLscrQ");

await multidecoder.decode({
  codec: cid.code,
  hasher: cid.multihash.code,
  block.get(cid),
});
```

---
_Source: https://npm.io/package/multiformat-multicodec · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
