# @leichtgewicht/ip-codec

> Small package to encode or decode IP addresses from buffers to strings.

Latest version **2.0.5** (published 2024-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @leichtgewicht/ip-codec
pnpm add @leichtgewicht/ip-codec
yarn add @leichtgewicht/ip-codec
bun add @leichtgewicht/ip-codec
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2024-03-27 |
| First published | 2021-07-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Martin Heidegger |
| Maintainers | leichtgewicht |
| Keywords | ip, ipv4, ipv6, codec, codecs, buffer, conversion |

## Links

- npm: https://www.npmjs.com/package/@leichtgewicht/ip-codec
- Repository: https://github.com/martinheidegger/ip-codec
- Homepage: https://github.com/martinheidegger/ip-codec#readme
- Issues: https://github.com/martinheidegger/ip-codec/issues
- npm.io page: https://npm.io/package/@leichtgewicht/ip-codec

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 2.0.5 (latest) — 2024-03-27
- 2.0.4 — 2022-05-09
- 2.0.3 — 2021-07-17
- 2.0.2 — 2021-07-01
- 2.0.1 — 2021-07-01
- 2.0.0 — 2021-07-01
- 1.0.0 — 2021-07-01

## README

# @leichtgewicht/ip-codec

Small package to encode or decode IP addresses from buffers to strings.
Supports IPV4 and IPV6.

## Usage

The basics are straigthforward

```js
import { encode, decode, sizeOf, familyOf } from '@leichtgewicht/ip-codec'

const uint8Array = encode("127.0.0.1")
const str = decode(uint8Array)

try {
  switch sizeOf(str) {
    case 4: // IPv4
    case 16: // IPv6
  }
  switch familyOf(str) {
    case: 1: // IPv4
    case: 2: // IPv6
  }
} catch (err) {
  // Invalid IP
}
```

By default the library will work with Uint8Array's but you can bring your own buffer:

```js
const buf = Buffer.alloc(4)
encode('127.0.0.1', buf)
```

It is also possible to de-encode at a location inside a given buffer

```js
const buf = Buffer.alloc(10)
encode('127.0.0.1', buf, 4)
```

Allocation of a buffer may be difficult if you don't know what type the buffer:
you can pass in a generator to allocate it for you:

```js
encode('127.0.0.1', Buffer.alloc)
```

You can also de/encode ipv4 or ipv6 specifically:

```js
import { v4, v6 } from '@leichtgewicht/ip-codec'

v4.decode(v4.encode('127.0.0.1'))
v6.decode(v6.encode('::'))
```

## History

The code in this package was originally extracted from [node-ip](https://github.com/indutny/node-ip) and since improved.

Notable changes are the removal of the `Buffer` dependency and better support for detection of
formats and allocation of buffers.

## License

[MIT](./LICENSE)

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