fix: fix some mistakes in the spec

This commit is contained in:
David Dias 2016-09-25 15:46:07 +08:00
parent 5d3d477199
commit 7d08aa4206

View File

@ -4,16 +4,16 @@
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](http://github.com/multiformats/multiformats) [![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](http://github.com/multiformats/multiformats)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
> self-describing codecs > Make data and streams self-described by prefixing them with human readable or binary packed codecs. `multicodec` offers a base table, but can also be extended with extra tables by application basis.
## Table of Contents ## Table of Contents
- [Motivation](#motivation) - [Motivation](#motivation)
- [How does it work? - Protocol Description](#how-does-it-work---protocol-description) - [How does it work? - Protocol Description](#how-does-it-work---protocol-description)
- [Prefix examples](#prefix-examples)
- [prefix - codec - desc](#prefix---codec---desc)
- [The protocol path](#the-protocol-path) - [The protocol path](#the-protocol-path)
- [Multicodec table](#multicodec-table)
- [Implementations](#implementations) - [Implementations](#implementations)
- [Implementation details]()
- [FAQ](#faq) - [FAQ](#faq)
- [Maintainers](#maintainers) - [Maintainers](#maintainers)
- [Contribute](#contribute) - [Contribute](#contribute)
@ -38,46 +38,68 @@ Moreover, this self-description allows straightforward layering of protocols wit
`multicodec` is a _self-describing multiformat_, it wraps other formats with a tiny bit of self-description: `multicodec` is a _self-describing multiformat_, it wraps other formats with a tiny bit of self-description:
```sh ```sh
<multicodec-header><encoded-data> <varint-len><multicodec><encoded-data>
# or
<varint-len><code>\n<encoded-data>
``` ```
For example, let's encode a json doc: For example, let's encode a json doc:
```node ```JavaScript
> // encode some json // encode some json
> var str = JSON.stringify({"hello":"world"}) const buf = new Buffer(JSON.stringify({ hello: 'world' }))
> var buf = multicodec.encode('json', str) // prepends multistream.header('/json')
> buf const prefixedBuf = multicodec.addPrefix('json', str) // prepends multicodec ('json')
<Buffer 06 2f 6a 73 6f 6e 2f 7b 22 68 65 6c 6c 6f 22 3a 22 77 6f 72 6c 64 22 7d> console.log(prefixedBuf)
> buf.toString('hex') // <Buffer 06 2f 6a 73 6f 6e 2f 7b 22 68 65 6c 6c 6f 22 3a 22 77 6f 72 6c 64 22 7d>
062f6a736f6e2f7b2268656c6c6f223a22776f726c64227d
> // decode, and find out what is in buf const.log(prefixedBuf.toString('hex'))
> multicodec.decode(buf) // 062f6a736f6e2f7b2268656c6c6f223a22776f726c64227d
{ "codec": "json", "data": '{"hello": "world"}' }
// let's get the Multicodec, and get the data back
const codec = multicodec.getMulticodec(prefixedBuf)
console.log(codec)
// json
console.log(multicodec.rmPrefix(prefixedBuf).toString())
// "{ \"hello\": \"world\" }
``` ```
So, `buf` is: So, `buf` is:
``` ```
hex: 062f6a736f6e2f7b2268656c6c6f223a22776f726c64227d hex: 062f6a736f6e2f7b2268656c6c6f223a22776f726c64227d
ascii: json/\n{"hello":"world"} ascii: /json/"{\"hello\":\"world\"}
``` ```
The more you know! Let's try it again, this time with protobuf: Note that on the ascii version, the varint at the beginning is not being represented, you should account that.
For a binary packed version of the multicodecs, see [multicodec-packed](./multicodec-packed.md).
## The protocol path
`multicodec` allows us to specify different protocols in a universal namespace, that way being able to recognize, multiplex, and embed them easily. We use the notion of a `path` instead of an `id` because it is meant to be a Unix-friendly URI.
A good path name should be decipherable -- meaning that if some machine or developer -- who has no idea about your protocol -- encounters the path string, they should be able to look it up and resolve how to use it.
An example of a good path name is:
``` ```
cat proto.c /bittorrent.org/1.0
``` ```
See also: [multicodec-packed](./multicodec-packed.md). An example of a _great_ path name is:
## Prefix examples ```
/ipfs/Qmaa4Rw81a3a1VEx4LxB7HADUAXvZFhCoRdBzsMZyZmqHD/ipfs.protocol
/http/w3id.org/ipfs/1.1.0
```
These path names happen to be resolvable -- not just in a "multicodec muxer(e.g [multistream](https://github.com/multiformats/multistream))" but -- in the internet as a whole (provided the program (or OS) knows how to use the `/ipfs` and `/http` protocols).
## Multicodec table
| prefix | codec | desc | type | [packed encoding](https://github.com/multiformats/multicodec/blob/master/multicodec-packed.md)| | prefix | codec | desc | type | [packed encoding](https://github.com/multiformats/multicodec/blob/master/multicodec-packed.md)|
|----------------|-------|-------------|-------|---------------------------------------| |----------------|-------|--------------|--------|---------------------------------------|
|0x052f62696e2f | /bin/ | raw binary | binary | 0x00 | |0x052f62696e2f | /bin/ | raw binary | binary | 0x00 |
|0x042f62322f | /b2/ | ascii base2 | binary | | |0x042f62322f | /b2/ | ascii base2 | binary | |
|0x052f6231362f | /b16/ | ascii base16 | hex | | |0x052f6231362f | /b16/ | ascii base16 | hex | |
@ -99,33 +121,18 @@ See also: [multicodec-packed](./multicodec-packed.md).
|0x052f7a69702f |/zip/ | | archive | | |0x052f7a69702f |/zip/ | | archive | |
|0x052f706e672f | /png/ | | archive | | |0x052f706e672f | /png/ | | archive | |
|0x052f726c702f | /rlp/ | recursive length prefix | ethereum | 0x60 | |0x052f726c702f | /rlp/ | recursive length prefix | ethereum | 0x60 |
## The protocol path
`multicodec` allows us to specify different protocols in a universal namespace, that way being able to recognize, multiplex, and embed them easily. We use the notion of a `path` instead of an `id` because it is meant to be a Unix-friendly URI.
A good path name should be decipherable -- meaning that if some machine or developer -- who has no idea about your protocol -- encounters the path string, they should be able to look it up and resolve how to use it.
An example of a good path name is:
```
/bittorrent.org/1.0
```
An example of a _great_ path name is:
```
/ipfs/Qmaa4Rw81a3a1VEx4LxB7HADUAXvZFhCoRdBzsMZyZmqHD/ipfs.protocol
/http/w3id.org/ipfs/ipfs-1.1.0.json
```
These path names happen to be resolvable -- not just in a "multicodec muxer(e.g [multistream]())" but -- in the internet as a whole (provided the program (or OS) knows how to use the `/ipfs` and `/http` protocols).
## Implementations ## Implementations
- multicodec
- [go-multicodec](https://github.com/multiformats/go-multicodec) - [go-multicodec](https://github.com/multiformats/go-multicodec)
- multicodec-packed
- [go-multicodec-packed](https://github.com/multiformats/go-multicodec-packed)
- [js-multicodec-packed](https://github.com/multiformats/js-multicodec-packed)
- multistream
- [go-multistream](https://github.com/multiformats/go-multistream) - Implements multistream, which uses multicodec for stream negotiation - [go-multistream](https://github.com/multiformats/go-multistream) - Implements multistream, which uses multicodec for stream negotiation
- [js-multistream](https://github.com/multiformats/js-multistream-select) - Implements multistream, which uses multicodec for stream negotiation - [js-multistream](https://github.com/multiformats/js-multistream) - Implements multistream, which uses multicodec for stream negotiation
- [clj-multicodec](https://github.com/multiformats/clj-multicodec) - [clj-multicodec](https://github.com/greglook/clj-multicodec)
## FAQ ## FAQ