# ebinary

> Binary encoder

Latest version **0.3.2** (published 2023-11-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install ebinary
pnpm add ebinary
yarn add ebinary
bun add ebinary
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2023-11-03 |
| First published | 2022-11-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 106.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | yourconst |
| Maintainers | yourconst |
| Keywords | binary, encode, pack, buffer |

## Links

- npm: https://www.npmjs.com/package/ebinary
- Repository: https://github.com/yourconst/ebinary
- Homepage: https://github.com/yourconst/ebinary#readme
- Issues: https://github.com/yourconst/ebinary/issues
- npm.io page: https://npm.io/package/ebinary

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M 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
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 0.3.2 (latest) — 2023-11-03
- 0.3.1 — 2023-11-02
- 0.3.0 — 2023-11-02
- 0.2.2 — 2022-12-02
- 0.2.1 — 2022-12-02
- 0.2.0 — 2022-12-02
- 0.1.1 — 2022-12-01
- 0.1.0 — 2022-11-30

## README

# ebinary

## Description
Statically typed data binary encoding library.
<br />
Dependency free.
<br />
Good types supporting.

<br />

## Installation

```bash
npm i ebinary
```

## Usage

```typescript
import { Encoder, Type } from 'ebinary';

const encoder = new Encoder(Type.Array(Type.Struct({
    id: Type.UInt32(),
    count: Type.UInt16(),
    enabled: Type.Nullable(Type.Bool()),
})));

const buffer = encoder.encode([
    { id: 1, count: 123, enabled: null },
    { id: 2, count: 756, enabled: true },
    { id: 3, count: 435, enabled: false },
]);

const value = encoder.decode(buffer);
```

<br />

<!-- ## Motivation
This library is ~2 times faster than `protobufjs` library data encoding (for different from `string` data types). Decoding has the same performance.
<br />
Also it supports `ascii` string encoding (it's faster and smaller than `utf8`) and any top-level type. -->

<br />

## Supported types

| Name | Byte length | JS type | Description |
|---|---|---|---|
| `Struct<T>` | ... | `{...T}` | - |
| `OneOf<T>` | [1,2]+... | `{$key:T[$key]}` | Only one of properties provided |
| `Array<T,L>` | sizeof(L)+... | `T[]` | `L` - typeof `length` |
| `String<L>` | sizeof(L)+... | `string` | utf8 / ascii. `L` - typeof `length` |
| `Buffer<L>` | sizeof(L)+... | `Uint8Array` | `L` - typeof `length` |
| `Enum<T>` | [1,2] | `T` | - |
| `Const<T>` | 0 | `T` | Not encodes into buffer |
| `Nullable<T>` | 1+... | `T\|null` | `undefined` encodes as `null` |
| `Aligned<T,A>` | [0,7]+... | `T` | Add trailing empty bytes for desired bytes align (`A`) (eg. for `C` struct compatibility) |
| `UVarInt32` | [1, 5] | `number` | variable length unsigned int |
| `VarInt32` | [1, 5] | `number` | variable length signed int |
| `Bool` | 1 | `boolean` | - |
| `Float64` | 8 | `number` | little / big endian |
| `Float32` | 4 | `number` | little / big endian |
| `UInt64` | 8 | `bigint` | little / big endian |
| `Int64` | 8 | `bigint` | little / big endian |
| `UInt32` | 4 | `number` | little / big endian |
| `Int32` | 4 | `number` | little / big endian |
| `UInt16` | 2 | `number` | little / big endian |
| `Int16` | 2 | `number` | little / big endian |
| `UInt8` | 1 | `number` | - |
| `Int8` | 1 | `number` | - |

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