Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit bf23e48

Browse files
authored
docs: update ipld/multiformats docs (#3771)
Improve docs around how to configure custom block codecs/multibase codecs, etc.
1 parent 349cbd0 commit bf23e48

File tree

1 file changed

+86
-25
lines changed

1 file changed

+86
-25
lines changed

docs/IPLD.md

+86-25
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,46 @@
33
## Table of Contents <!-- omit in toc -->
44

55
- [Overview](#overview)
6-
- [Bundled Codecs](#bundled-codecs)
7-
- [Adding additional codecs](#adding-additional-codecs)
6+
- [Bundled BlockCodecs](#bundled-blockcodecs)
7+
- [Bundled Multihashes](#bundled-multihashes)
8+
- [Bundled Multibases](#bundled-multibases)
9+
- [Adding additional BlockCodecs, Multihashes and Multibases](#adding-additional-blockcodecs-multihashes-and-multibases)
810
- [Next steps](#next-steps)
911

1012
## Overview
1113

12-
The IPFS repo contains a blockstore that holds [Blocks](https://github.com/ipld/js-ipld-block). These blocks can be thought of as a [CID][] and associated byte array.
14+
The IPFS repo contains a blockstore that holds the data that makes up the files on the IPFS network. These blocks can be thought of as a [CID][] and associated byte array.
1315

14-
The [CID][] contains a `codec` property that lets us know how to interpret the byte array associated with it.
16+
The [CID][] contains a `code` property that lets us know how to interpret the byte array associated with it.
1517

16-
In order to perform that interpretation, an [IPLD Format][] must be loaded that corresponds to the `codec` property of the [CID][].
18+
In order to perform that interpretation, a [BlockCodec][] must be loaded that corresponds to the `code` property of the [CID][].
1719

18-
## Bundled Codecs
20+
Similarly implementations of [Multihash][]es or [Multibase][]s must be available to be used.
1921

20-
js-IPFS ships with three bundled codecs, the ones that are required to create and interpret [UnixFS][] structures.
22+
## Bundled BlockCodecs
23+
24+
js-IPFS ships with four bundled codecs, the ones that are required to create and interpret [UnixFS][] structures.
2125

2226
These are:
2327

24-
1. [ipld-dag-pb](https://github.com/ipld/js-ipld-dag-pb) - used for file and directory structures
25-
2. [ipld-raw](https://github.com/ipld/js-ipld-raw) - used for file data where imported with `raw-leaves=true`
26-
3. [ipld-dag-cbor](https://github.com/ipld/js-ipld-dag-cbor) - used for general storage of JavaScript Objects
28+
1. [@ipld/dag-pb](https://github.com/ipld/js-dag-pb) - used for file and directory structures
29+
2. [raw](https://github.com/multiformats/js-multiformats/blob/master/src/codecs/raw.js) - used for file data where imported with `--raw-leaves=true`
30+
3. [@ipld/dag-cbor](https://github.com/ipld/js-dag-cbor) - used for storage of JavaScript Objects with [CID] links to other blocks
31+
4. [json](https://github.com/multiformats/js-multiformats/blob/master/src/codecs/json.js) - used for storage of plain JavaScript Objects
32+
33+
## Bundled Multihashes
34+
35+
js-IPFS ships with all multihashes [exported by js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes), including `sha2-256` and others.
36+
37+
Additional hashers can be configured using the `hashers` config property.
38+
39+
## Bundled Multibases
40+
41+
js-IPFS ships with all multibases [exported by js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/bases), including `base58btc`, `base32` and others.
2742

28-
## Adding additional codecs
43+
Additional bases can be configured using the `bases` config property.
44+
45+
## Adding additional BlockCodecs, Multihashes and Multibases
2946

3047
If your application requires support for extra codecs, you can configure them as follows:
3148

@@ -35,14 +52,34 @@ If your application requires support for extra codecs, you can configure them as
3552

3653
const node = await ipfs({
3754
ipld: {
38-
// either specify them as part of the `formats` list
39-
formats: [
40-
require('my-format')
55+
// either specify BlockCodecs as part of the `codecs` list
56+
codecs: [
57+
require('custom-blockcodec')
4158
],
4259

43-
// or supply a function to load them dynamically
44-
loadFormat: async (format) => {
45-
return require(format)
60+
// and/or supply a function to load them dynamically
61+
loadCodec: async (codecNameOrCode) => {
62+
return require(codecNameOrCode)
63+
},
64+
65+
// either specify Multibase codecs as part of the `bases` list
66+
bases: [
67+
require('custom-multibase')
68+
],
69+
70+
// and/or supply a function to load them dynamically
71+
loadBase: async (baseNameOrCode) => {
72+
return require(baseNameOrCode)
73+
},
74+
75+
// either specify Multihash hashers as part of the `hashers` list
76+
hashers: [
77+
require('custom-multibase')
78+
],
79+
80+
// and/or supply a function to load them dynamically
81+
loadHasher: async (hashNameOrCode) => {
82+
return require(hashNameOrCode)
4683
}
4784
}
4885
})
@@ -53,14 +90,34 @@ If your application requires support for extra codecs, you can configure them as
5390
const client = ipfsHttpClient({
5491
url: 'http://127.0.0.1:5002',
5592
ipld: {
56-
// either specify them as part of the `formats` list
57-
formats: [
58-
require('my-format')
93+
// either specify BlockCodecs as part of the `codecs` list
94+
codecs: [
95+
require('custom-blockcodec')
96+
],
97+
98+
// and/or supply a function to load them dynamically
99+
loadCodec: async (codecNameOrCode) => {
100+
return require(codecNameOrCode)
101+
},
102+
103+
// either specify Multibase codecs as part of the `bases` list
104+
bases: [
105+
require('custom-multibase')
59106
],
60107
61-
// or supply a function to load them dynamically
62-
loadFormat: async (format) => {
63-
return require(format)
108+
// and/or supply a function to load them dynamically
109+
loadBase: async (baseNameOrCode) => {
110+
return require(baseNameOrCode)
111+
},
112+
113+
// either specify Multihash hashers as part of the `hashers` list
114+
hashers: [
115+
require('custom-multibase')
116+
],
117+
118+
// and/or supply a function to load them dynamically
119+
loadHasher: async (hashNameOrCode) => {
120+
return require(hashNameOrCode)
64121
}
65122
}
66123
})
@@ -69,7 +126,11 @@ If your application requires support for extra codecs, you can configure them as
69126
## Next steps
70127

71128
* See [examples/custom-ipld-formats](https://github.com/ipfs/js-ipfs/tree/master/examples/custom-ipld-formats) for runnable code that demonstrates the above with in-process IPFS nodes, IPFS run as a daemon and also the http client
129+
* Also [examples/traverse-ipld-graphs](https://github.com/ipfs/js-ipfs/tree/master/examples/traverse-ipld-graphs) which uses the [ipld-format-to-blockcodec](https://www.npmjs.com/package/ipld-format-to-blockcodec) module to use older [IPLD format][]s that have not been ported over to the new [BlockCodec][] interface, as well as additional [Multihash Hashers](https://www.npmjs.com/package/multiformats#multihash-hashers).
72130

73-
[cid]: https://www.npmjs.com/package/cids
74-
[ipld format]: https://github.com/ipld/interface-ipld-format
131+
[cid]: https://docs.ipfs.io/concepts/content-addressing/
132+
[blockcodec]: https://www.npmjs.com/package/multiformats#multicodec-encoders--decoders--codecs
75133
[unixfs]: https://github.com/ipfs/specs/blob/master/UNIXFS.md
134+
[ipld format]: https://github.com/ipld/interface-ipld-format
135+
[multihash]: https://github.com/multiformats/multihash
136+
[multibase]: https://github.com/multiformats/multibase

0 commit comments

Comments
 (0)