|
1 | 1 | # msgpack_elixir |
2 | 2 |
|
| 3 | +[](https://hex.pm/packages/msgpack_elixir) |
| 4 | + |
| 5 | +A [MessagePack](https://msgpack.org/) serialization library for Elixir. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Full Specification Compliance:** Adheres to the MessagePack specification to |
| 10 | + ensure compatibility with other MessagePack implementations |
| 11 | + - Includes support for types such as `Booleans`, `Integers`, `Floats`, `Tuples`, |
| 12 | + `Lists`, `Maps`, `Strings`, `Binaries`, `Extensions`, and `Timestamps` |
| 13 | + - Encodes and decodes Elixir's `DateTime` and `NaiveDateTime` structs using |
| 14 | + the MessagePack `Timestamp` extension |
| 15 | +- **Performant Encoding:** Implements efficient encoding for collections and |
| 16 | + provides a `:string_validation` option to bypass UTF-8 validation in |
| 17 | + performance-sensitive applications |
| 18 | +- **Exception-raising Variants:** Includes bang (`!`) variants like `encode!/2` |
| 19 | + and `decode!/2` for contexts where raising an exception is preferred over |
| 20 | + error tuples |
| 21 | +- **Telemetry Integration:** Emits standard `:telemetry` events for all encode |
| 22 | + and decode operations, allowing for easy integration into monitoring and |
| 23 | + observability tools |
| 24 | + |
3 | 25 | ## Installation |
4 | 26 |
|
5 | | -The package can be installed by adding `msgpack_elixir` to your list of |
6 | | -dependencies in `mix.exs`: |
| 27 | +Add `msgpack_elixir` to your list of dependencies in `mix.exs`: |
7 | 28 |
|
8 | 29 | ```elixir |
9 | 30 | def deps do |
10 | | - [ |
11 | | - {:msgpack_elixir, "~> 0.1.0"} |
12 | | - ] |
| 31 | + [{:msgpack_elixir, "~> 1.0.0"}] |
13 | 32 | end |
14 | 33 | ``` |
| 34 | + |
| 35 | +Then, run `mix deps.get`. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### Basic Operations |
| 40 | + |
| 41 | +The library returns `{:ok, value}` tuples for successful operations and |
| 42 | +`{:error, reason}` tuples for failures. |
| 43 | + |
| 44 | +```elixir |
| 45 | +# Encode a map |
| 46 | +iex> data = %{"id" => 1, "name" => "Elixir"} |
| 47 | +iex> {:ok, encoded} = Msgpack.encode(data) |
| 48 | +<<130, 162, 105, 100, 1, 164, 110, 97, 109, 101, 166, 69, 108, 105, 120, 105, 114>> |
| 49 | + |
| 50 | +# Decode a binary |
| 51 | +iex> Msgpack.decode(encoded) |
| 52 | +{:ok, %{"id" => 1, "name" => "Elixir"}} |
| 53 | +``` |
| 54 | + |
| 55 | +### Exception-raising Operations |
| 56 | + |
| 57 | +If you prefer an exception to be raised on failure, use the bang (`!`) variants. |
| 58 | + |
| 59 | +```elixir |
| 60 | +iex> encoded = Msgpack.encode!(%{id: 1}) |
| 61 | +<<129, 162, 105, 100, 1>> |
| 62 | + |
| 63 | +iex> Msgpack.decode!(<<192, 42>>) |
| 64 | +** (Msgpack.DecodeError) Failed to decode MessagePack binary. Reason = {:trailing_bytes, <<42>>} |
| 65 | +``` |
| 66 | + |
| 67 | +## Options |
| 68 | + |
| 69 | +The following options can be passed as a second argument to the `encode` and |
| 70 | +`decode` functions. |
| 71 | + |
| 72 | +### For `encode/2` |
| 73 | + |
| 74 | +- `:atoms` |
| 75 | + - Controls how atoms are encoded. |
| 76 | + - `:string` (default) - Encodes atoms as MessagePack strings |
| 77 | + - `:error` - Returns an `{:error, {:unsupported_atom, atom}}` tuple if an atom |
| 78 | + is encountered |
| 79 | +- `:string_validation` |
| 80 | + - Controls whether to perform UTF-8 validation on binaries |
| 81 | + - `true` (default) - Validates binaries; encodes as `str` type if valid UTF-8, |
| 82 | + `bin` type otherwise |
| 83 | + - `false` - Skips validation and encodes all binaries as the `str` type. |
| 84 | + Improves performance but should only be used if you are certain your data is |
| 85 | + valid |
| 86 | + |
| 87 | +### For `decode/2` |
| 88 | + |
| 89 | +- `:max_depth` |
| 90 | + - Sets a limit on the nesting level of arrays and maps to prevent stack |
| 91 | + exhaustion attacks |
| 92 | + - Defaults to `100` |
| 93 | +- `:max_byte_size` |
| 94 | + - Sets a limit on the declared byte size of any single string, binary, array, |
| 95 | + or map to prevent memory exhaustion attacks |
| 96 | + - Defaults to `10_000_000` (10MB) |
| 97 | + |
| 98 | +## Telemetry |
| 99 | + |
| 100 | +The library emits `:telemetry` events which can be used for monitoring or |
| 101 | +logging. |
| 102 | + |
| 103 | +- `[:msgpack, :encode]` - Dispatched when `Msgpack.encode/2` is called |
| 104 | +- `[:msgpack, :decode]` - Dispatched when `Msgpack.decode/2` is called |
| 105 | + |
| 106 | +Example of attaching a logger: |
| 107 | + |
| 108 | +```elixir |
| 109 | +defmodule MyTelemetryHandler do |
| 110 | + require Logger |
| 111 | + |
| 112 | + def attach do |
| 113 | + :telemetry.attach( |
| 114 | + "msgpack-logger", |
| 115 | + [:msgpack, :encode], |
| 116 | + &__MODULE__.handle_event/4, |
| 117 | + nil |
| 118 | + ) |
| 119 | + end |
| 120 | + |
| 121 | + def handle_event(event_name, measurements, metadata, _config) do |
| 122 | + Logger.info("Telemetry Event: #{inspect(event_name)}", |
| 123 | + measurements: measurements, |
| 124 | + metadata: metadata |
| 125 | + ) |
| 126 | + end |
| 127 | +end |
| 128 | +``` |
| 129 | + |
| 130 | +## Development |
| 131 | + |
| 132 | +This section explains how to setup the project locally for development. |
| 133 | + |
| 134 | +### Dependencies |
| 135 | + |
| 136 | +- Elixir `~> 1.7` (OTP 21+) |
| 137 | + |
| 138 | +### Get the Source |
| 139 | + |
| 140 | +Clone the project locally: |
| 141 | + |
| 142 | +```bash |
| 143 | +# via HTTPS |
| 144 | +git clone https://github.com/nrednav/msgpack_elixir.git |
| 145 | + |
| 146 | +# via SSH |
| 147 | +git clone git@github.com:nrednav/msgpack_elixir.git |
| 148 | +``` |
| 149 | + |
| 150 | +### Install |
| 151 | + |
| 152 | +Install the project's dependencies: |
| 153 | + |
| 154 | +```bash |
| 155 | +cd msgpack_elixir/ |
| 156 | +mix deps.get |
| 157 | +``` |
| 158 | + |
| 159 | +### Test |
| 160 | + |
| 161 | +Run the test suite: |
| 162 | + |
| 163 | +```bash |
| 164 | +mix test |
| 165 | +``` |
| 166 | + |
| 167 | +### Benchmark |
| 168 | + |
| 169 | +Run the benchmarks: |
| 170 | + |
| 171 | +```bash |
| 172 | +mix run bench/run.exs |
| 173 | +``` |
| 174 | + |
| 175 | +## Versioning |
| 176 | + |
| 177 | +This project uses [Semantic Versioning](https://semver.org/). |
| 178 | +For a list of available versions, see the [repository tag list](https://github.com/nrednav/msgpack_elixir/tags). |
| 179 | + |
| 180 | +## Issues & Requests |
| 181 | + |
| 182 | +If you encounter a bug or have a feature request, please [open an |
| 183 | +issue](https://github.com/nrednav/msgpack_elixir/issues) on the GitHub |
| 184 | +repository. |
| 185 | + |
| 186 | +## Contributing |
| 187 | + |
| 188 | +Public contributions are welcome! If you would like to contribute, please fork |
| 189 | +the repository and create a pull request. |
| 190 | + |
| 191 | +## License |
| 192 | + |
| 193 | +This project is licensed under the MIT License - see the [LICENSE](./LICENSE) |
| 194 | +file for details. |
0 commit comments