|
1 | | -# ip |
2 | | -Lite IP tools |
| 1 | +# @webpod/ip |
| 2 | + |
| 3 | +> Lite IP tools |
| 4 | +
|
| 5 | +This lib is an alternative for the [`ip`](https://www.npmjs.com/package/ip) package which resolves [many upstream issues](https://github.com/indutny/node-ip/issues). |
| 6 | +* Rewritten in TypeScript |
| 7 | +* Provides both ESM and CJS entries |
| 8 | +* Eliminates annoying vulnerability [CVE-2024-29415](https://github.com/advisories/GHSA-2p57-rm9w-gvfp) |
| 9 | +* Brings various fixes and improvements |
| 10 | + |
| 11 | +## Install |
| 12 | +```shell |
| 13 | +npm i @webpod/ip |
| 14 | +``` |
| 15 | + |
| 16 | +## Drop-in |
| 17 | +```shell |
| 18 | +- const ip = require('ip') |
| 19 | ++ const ip = require('@webpod/ip') |
| 20 | +``` |
| 21 | +Temporary workaround to avoid refactoring is using `overrides` / `resolutions` in your `package.json`: |
| 22 | +```json |
| 23 | +{ |
| 24 | + "overrides": { |
| 25 | + "ip": "@webpod/ip" |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | +## Usage |
| 32 | +The API is fully compatible with the latest `ip@2.0.1` |
| 33 | + |
| 34 | +```ts |
| 35 | +import ip from '@webpod/ip' |
| 36 | + |
| 37 | +// ------------------------------------------------------- |
| 38 | +// Addresses & formats |
| 39 | +// ------------------------------------------------------- |
| 40 | + |
| 41 | +ip.address() // '192.168.1.50' (example local address) |
| 42 | +ip.isPrivate('127.0.0.1') // true |
| 43 | +ip.isV4Format('127.0.0.1') // true |
| 44 | +ip.isV6Format('::ffff:127.0.0.1') // true |
| 45 | +ip.isEqual('::1', '::0:1') // true |
| 46 | + |
| 47 | +// ------------------------------------------------------- |
| 48 | +// Conversions |
| 49 | +// ------------------------------------------------------- |
| 50 | + |
| 51 | +ip.toBuffer('127.0.0.1') // <Buffer 7f 00 00 01> |
| 52 | +ip.toString(Buffer.from([127, 0, 0, 1])) // '127.0.0.1' |
| 53 | + |
| 54 | +ip.toLong('127.0.0.1') // 2130706433 |
| 55 | +ip.fromLong(2130706433) // '127.0.0.1' |
| 56 | + |
| 57 | +ip.fromPrefixLen(24) // '255.255.255.0' |
| 58 | + |
| 59 | +// in-place buffer usage |
| 60 | +const buf = Buffer.alloc(128) |
| 61 | +const offset = 64 |
| 62 | +ip.toBuffer('127.0.0.1', buf, offset) // writes [127, 0, 0, 1] at offset 64 |
| 63 | +ip.toString(buf, offset, 4) // '127.0.0.1' |
| 64 | + |
| 65 | +// ------------------------------------------------------- |
| 66 | +// Masking, bitwise, and ranges |
| 67 | +// ------------------------------------------------------- |
| 68 | + |
| 69 | +ip.mask('192.168.1.134', '255.255.255.0') // '192.168.1.0' |
| 70 | +ip.cidr('192.168.1.134/26') // '192.168.1.128' |
| 71 | +ip.not('255.255.255.0') // '0.0.0.255' |
| 72 | +ip.or('192.168.1.134', '0.0.0.255') // '192.168.1.255' |
| 73 | + |
| 74 | +// ------------------------------------------------------- |
| 75 | +// Subnets |
| 76 | +// ------------------------------------------------------- |
| 77 | + |
| 78 | +ip.subnet('192.168.1.134', '255.255.255.192') |
| 79 | +/* |
| 80 | +{ |
| 81 | + networkAddress: '192.168.1.128', |
| 82 | + firstAddress: '192.168.1.129', |
| 83 | + lastAddress: '192.168.1.190', |
| 84 | + broadcastAddress: '192.168.1.191', |
| 85 | + subnetMask: '255.255.255.192', |
| 86 | + subnetMaskLength: 26, |
| 87 | + numHosts: 62, |
| 88 | + length: 64, |
| 89 | + contains: [Function: contains] |
| 90 | +} |
| 91 | +*/ |
| 92 | + |
| 93 | +ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true |
| 94 | +``` |
| 95 | + |
| 96 | +Additional features: |
| 97 | +```ts |
| 98 | +ip.isV4Format('255.255.255.256') // true |
| 99 | +ip.isV4('255.255.255.256') // false |
| 100 | +``` |
| 101 | + |
| 102 | +## License |
| 103 | +MIT |
0 commit comments