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
ip
The 'ip' package provides utilities for handling and manipulating IPv4 and IPv6 addresses in Node.js. Unlike @leichtgewicht/ip-codec, it focuses more on IP address manipulation (e.g., subnet calculations, address validation) rather than encoding and decoding.
ip-address
The 'ip-address' package offers a comprehensive suite for working with both IPv4 and IPv6 addresses, including parsing, validating, and manipulating IP addresses. It provides a more object-oriented approach compared to @leichtgewicht/ip-codec, which is focused on encoding and decoding.
@leichtgewicht/ip-codec
Small package to encode or decode IP addresses from buffers to strings.
Supports IPV4 and IPV6.
Usage
The basics are straigthforward
const { encode, decode, sizeOf, familyOf } = require('@leichtgewicht/ip-codec')
const uint8Array = encode("127.0.0.1")
const str = decode(uint8Array)
try {
switch sizeOf(str) {
case 4:
case 16:
}
switch familyOf(str) {
case: 1:
case: 2:
}
} catch (err) {
}
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:
const { v4, v6 } = require('@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