https://github.com/sjinks/node-inet-xtoy
inet_ntop() and inet_pton() bindings for Node.js
https://github.com/sjinks/node-inet-xtoy
Last synced: 4 months ago
JSON representation
inet_ntop() and inet_pton() bindings for Node.js
- Host: GitHub
- URL: https://github.com/sjinks/node-inet-xtoy
- Owner: sjinks
- License: mit
- Created: 2019-06-05T08:24:58.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2026-02-23T01:59:50.000Z (4 months ago)
- Last Synced: 2026-02-23T08:16:14.261Z (4 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/inet_xtoy
- Size: 1.13 MB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-inet-xtoy
[](https://github.com/sjinks/node-inet-xtoy/actions/workflows/build-and-test.yml)
[](https://github.com/sjinks/node-inet-xtoy/actions/workflows/codeql.yml)
`inet_ntop()` and `inet_pton()` bindings for Node.js
# Installation
```sh
npm install inet_xtoy
```
# Example
```js
const { inet_ntop, inet_pton } = require('inet_xtoy');
console.log(inet_pton('127.0.0.1'));
console.log(inet_pton('::1'));
console.log(inet_ntop(inet_pton('127.0.0.1')));
console.log(inet_ntop(inet_pton('::1')));
```
# API
**inet_ntop(buf)** converts an IPv4 or IPv6 address `buf` from binary to text form.
`buf` is expected to be a 4 (IPv4) or 16 (IPv6) bytes long String or Buffer.
Returns the IP address as a string or throws an Error in case the call to [`inet_ntop(3)`](http://man7.org/linux/man-pages/man3/inet_ntop.3.html) fails.
Throws `TypeError` when the number of arguments is not 1, when `buf` is neither String nor Buffer or when the length of `buf` is neither 4 nor 16.
**inet_pton(ip)** converts an IPv4 or IPv6 address `buf` from text to binary form.
`ip` is expected to be a string. The function returns `null` if `ip` is not a valid IP address.
Returns `Buffer` with the binary form of the given `ip`; the length of the buffer is either 4 or 16 bytes.
Throws `TypeError` when the number of arguments is not 1 or when `ip` is not a string.
Throws `Error` when a call to [`inet_pton(3)`](http://man7.org/linux/man-pages/man3/inet_pton.3.html) fails.