Socket
Socket
Sign inDemoInstall

@leichtgewicht/ip-codec

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @leichtgewicht/ip-codec

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


Version published
Weekly downloads
8.4M
decreased by-2.72%
Maintainers
1
Install size
17.3 kB
Created
Weekly downloads
 

Package description

What is @leichtgewicht/ip-codec?

The @leichtgewicht/ip-codec package is designed for encoding and decoding IP addresses. It supports both IPv4 and IPv6 formats, providing a straightforward way to handle IP address transformations in Node.js applications. This package can be particularly useful for applications that need to store or transmit IP addresses in a compact format, perform IP address manipulations, or validate IP address formats.

What are @leichtgewicht/ip-codec's main functionalities?

Encoding an IPv4 address

This feature allows you to encode an IPv4 address into a compact, easily storable or transmittable format.

"const ipCodec = require('@leichtgewicht/ip-codec');\nconst encoded = ipCodec.encode('192.168.1.1');\nconsole.log(encoded); // Outputs the encoded IP address"

Decoding an IPv4 address

This feature enables the decoding of an encoded IPv4 address back to its standard dot notation.

"const ipCodec = require('@leichtgewicht/ip-codec');\nconst decoded = ipCodec.decode('c0a80101');\nconsole.log(decoded); // Outputs '192.168.1.1'"

Encoding and decoding IPv6 addresses

Support for IPv6 addresses, allowing them to be encoded and decoded just like IPv4 addresses, facilitating operations on modern IP addresses.

"const ipCodec = require('@leichtgewicht/ip-codec');\nconst encodedIPv6 = ipCodec.encode('2001:0db8:85a3:0000:0000:8a2e:0370:7334');\nconst decodedIPv6 = ipCodec.decode(encodedIPv6);\nconsole.log(decodedIPv6); // Outputs the original IPv6 address"

Other packages similar to @leichtgewicht/ip-codec

Readme

Source

@leichtgewicht/ip-codec

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

Usage

The basics are straigthforward

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:

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

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:

encode('127.0.0.1', Buffer.alloc)

You can also de/encode ipv4 or ipv6 specifically:

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

Keywords

FAQs

Last updated on 27 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc