# @hazae41/binary

> Zero-copy binary data types for the web

Latest version **2.1.1** (published 2026-06-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hazae41/binary
pnpm add @hazae41/binary
yarn add @hazae41/binary
bun add @hazae41/binary
```

## Health

**Score 50/100 (C)** — status: active.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2026-06-03 |
| First published | 2022-12-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | hazae41 |
| Maintainers | hazae41 |
| Keywords | binary, zero-copy, encoding, decoding, buffer, bytes, protocol, packets, reader, writer, offset, asn1, der, ber, octets, stream, low-level, alloc, allocation, allocator, typescript, esmodules, tested, unit-tested |

## Links

- npm: https://www.npmjs.com/package/@hazae41/binary
- Repository: https://github.com/hazae41/binary
- Homepage: https://github.com/hazae41/binary#readme
- Issues: https://github.com/hazae41/binary/issues
- npm.io page: https://npm.io/package/@hazae41/binary

## Dependencies (1)

- [@hazae41/cursor](https://npm.io/package/@hazae41/cursor.md) ^2.2.0

## 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

- 2.1.1 (latest) — 2026-06-03
- 2.1.0 — 2026-06-03
- 2.0.2 — 2025-10-29
- 2.0.1 — 2025-10-27
- 2.0.0 — 2025-10-25
- 1.3.11 — 2025-08-15
- 1.3.10 — 2025-08-15
- 1.3.9 — 2025-08-13
- 1.3.8 — 2025-07-02
- 1.3.7 — 2025-07-02
- 1.3.6 — 2025-06-30
- 1.3.5 — 2024-08-31
- 1.3.4 — 2023-11-14
- 1.3.3 — 2023-11-13
- 1.3.2 — 2023-11-05
- … 60 more at https://npm.io/package/@hazae41/binary/versions

## README

# Binary

Zero-copy binary data types for the web

```bash
npm install @hazae41/binary
```

[**📦 NPM**](https://www.npmjs.com/package/@hazae41/binary)

## Features

### Current features
- 100% TypeScript and ESM
- No external dependencies
- Zero-copy reading and writing
- Rust-like patterns
- Unit-tested

## Usage

#### Writable

```typescript
class MyObject implements Writable {

  constructor(
    readonly x: number,
    readonly y: number
  ) {}

  size() {
    return 1 + 2
  }

  write(cursor: Cursor) {
    cursor.writeUint8(this.x)
    cursor.writeUint16(this.y)
  }

}
```

```typescript
const myobject = new MyObject(1, 515)
const bytes = Writable.writeToBytes(myobject) // Uint8Array([1, 2, 3])
```

#### Readable

```typescript
class MyObject {

  constructor(
    readonly x: number,
    readonly y: number
  ) {}

  static read(cursor: Cursor): MyObject {
    const x = cursor.readUint8()
    const y = cursor.readUint16()

    return new MyObject(x, y)
  }

}
```

```typescript
const bytes = new Uint8Array([1, 2, 3])
const myobject = Readable.readFromBytes(MyObject, bytes) // MyObject(1, 515)
```

#### Opaque

This is a binary data type that just holds bytes, it can be used when a binary data type is required

```typescript
const bytes = new Uint8Array([1, 2, 3])
const opaque = Readable.readFromBytes(Opaque.Uncopied, bytes) // Opaque(Uint8Array([1, 2, 3]))
const myobject = opaque.readInto(MyObject) // MyObject(1, 515)
```

```typescript
const myobject = new MyObject(1, 515)
const opaque = Opaque.writeFrom(myobject) // Opaque.Copied(Uint8Array([1, 2, 3]))
const bytes = Writable.writeToBytes(opaque) // Uint8Array([1, 2, 3])
```

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