a flexible and powerful network library written in TypeScript
npm install mb-networkbash
$ npm i mb-network
`
Documentation
$3
- Subnet
- SubnetIter
$3
- Ip
$3
- areSubnetsIntersecting
- broadcast
- firstHost
- hostCountToSuffix
- ipToString
- isHostAddress
- isIpValid
- isSubnetValid
- lastHost
- networkMask
- networkMaskToSuffix
- newIp
- newSubnet
- newSubnetHostIter
- newSubnetIter
- possibleHostCount
- subnetToString
Examples
$3
It is recommended to always use the functions newIp and newSubnet for creating new Ip's and Subnet's. Although you don't need to.
`ts
import { newIp, newSubnet, subnetToString } from 'mb-network'
const ip = newIp('192.168.0.11')
const subnet = newSubnet(ip, 24)
console.log(subnetToString(subnet)) // logs -> 192.168.0.0/24
`
$3
In mb-network Ip's are stored as a single number. To convert them to the well known readable format e.g. 192.168.0.1 you need to use the function ipToString.
It was a design decision to have the subnet functions, such as broadcast, also return the ip as a number (type Ip).
This approach opens up more options for further working with the results.
`ts
import { newSubnet, broadcast, ipToString } from 'mb-network'
// You can also create a subnet without first creating an ip
const subnet = newSubnet('192.168.0.1', 24)
const broadcastOfSubnet = broadcast(subnet)
console.log(broadcastOfSubnet) // logs -> 3232235775
console.log(ipToString(broadcastOfSubnet)) // logs -> 192.168.0.255
// For example, this allows us to perform the following, offering maximum flexibility.
console.log(broadcastOfSubnet - 10) // logs -> 3232235765
console.log(ipToString(broadcastOfSubnet - 10)) // logs -> 192.168.0.245
`
$3
A subnet object consists solely of a network address (Ip) and a suffix (number). But what if you need to work with the addresses within the subnet?
For this purpose mb-network provides two iterators. A general address iterator newSubnetIter(): SubnetIter and a host address iterator newSubnetHostIter(): SubnetIter
This iterators sit on top of a subnet and are not a direct part of it as you may know it from Arrays.
#### Spreading a subnet
`ts
import { newSubnet, newSubnetIter }
const subnet = newSubnet('192.168.0.5', 28)
const iter = newSubnetIter(subnet) // Iterator over all addresses including network address and broadcast
const addresses = [...iter] // Array
`
#### Classic for..of loop
``ts
import { newSubnet, newSubnetHostIter }
const subnet = newSubnet('192.168.0.5', 28)
const iter = newSubnetHostIter(subnet) // Iterator over all host addresses
for (const host of iter) {
// do something with host address (Ip)
}
``
#### Manual consumption
``ts
import { newSubnet, newSubnetIter, ipToString }
const subnet = newSubnet('192.168.0.5', 28)
const iter = newSubnetIter(subnet)
// skip first three addresses (for some reason)
iter.next()
iter.next()
iter.next()
// do something with the rest
let result = iter.next();
while (!result.done) {
const address = result.value
console.log(ipToString(address))
result = iter.next();
}
```