# Enbox Common

> **Research Preview** — Enbox is under active development. APIs may change without notice.

[![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/LiranCohen/02d15f39a46173a612a8862ec6d7cfcf/raw/common.json)](https://github.com/enboxorg/enbox/actions/workflows/ci.yml)

Shared utilities used across the Enbox monorepo — data conversion, key-value stores, caching, streams, and type helpers.

## Installation

```bash
bun add @enbox/common
```

## Exports

### `Convert`

Fluent data-format conversion between ArrayBuffer, Base32Z, Base58Btc, Base64Url, Hex, Multibase, JSON objects, strings, and Uint8Array.

```typescript
import { Convert } from '@enbox/common';

const bytes = Convert.string('hello').toUint8Array();
const hex = Convert.uint8Array(bytes).toHex();
const b64 = Convert.hex(hex).toBase64Url();
```

### `MemoryStore`

Map-backed implementation of the `KeyValueStore<K, V>` interface.

```typescript
import { MemoryStore } from '@enbox/common';

const ephemeral = new MemoryStore();
```

### `LevelStore`

LevelDB-backed implementation of the `KeyValueStore<K, V>` interface. It lives behind a dedicated subpath so default `@enbox/common` imports do not require LevelDB.

```typescript
import { LevelStore } from '@enbox/common/level-store';

const persistent = new LevelStore({ location: './data' });
await persistent.set('key', 'value');
const value = await persistent.get('key');
```

### `TtlCache`

Time-to-live in-memory cache with bounded size support. Expired entries are removed by an unref'd background timer and
are also checked on `get()` / `has()`, so stale values are not returned if the timer has not fired yet. `cancelTimer()`
only stops the background timer; lazy expiry checks still run on access. Disposal callbacks are provided with the
constructor's `dispose` option.

### `Stream`

Utilities for Web Streams API — create streams from blobs/bytes, consume to various formats, type guards.

### `Multicodec`

Multicodec-prefixed binary data utilities. Pre-registers Ed25519, X25519, and secp256k1 key codecs.

### Type Utilities

- `universalTypeOf()` — Cross-context type detection
- `isDefined()` / `isAsyncIterable()` / `isArrayBufferSlice()` — Type guards
- `isEmptyObject()` / `removeEmptyObjects()` / `removeUndefinedProperties()` — Object helpers

## Development

```bash
bun run build
bun run test:node
bun run lint
```

## License

Apache-2.0
