Millwork codecs for IP addresses and CIDR ranges with ipaddr.js.
pnpm add @millwork-ts/ip @millwork-ts/core ipaddr.js
| Codec | Value | Raw |
|---|---|---|
ip |
a frozen ipaddr.js IPv4 or IPv6 |
"192.168.0.1", "::1" |
cidr |
a frozen { address, prefix, contains } |
"10.0.0.0/8" |
"127.1", octal or hex
forms, because different tools read these forms differently.ip codec writes IPv6 in the short form of RFC 5952, such as "2001:db8::1".cidr codec keeps host bits as the server sent them: "10.1.2.3/8" stays
"10.1.2.3/8".range.contains(address) takes an address value or a string. It gives false for a bad
address or for an address of a different kind. It does not throw.ipaddr, the ipaddr.js module.| Codec | Option | Type | Use |
|---|---|---|---|
ip |
unmapIPv4 |
boolean |
Read an IPv4-mapped IPv6 address, such as "::ffff:10.0.0.1", as IPv4, and write it as IPv4: "10.0.0.1". The default is false. |
ip: equals compares the kind and the normalized form. "2001:DB8::1" equals
"2001:db8:0:0:0:0:0:1". An IPv4 address does not equal its IPv4-mapped IPv6 form.cidr: equals compares the address and the prefix.Register the codecs before the store calls transformations(), because transformations()
reads the registry when it runs. See the
@millwork-ts/warp-drive README.
import { codecs } from '@millwork-ts/ip'
import { registerCodecs, valueField } from '@millwork-ts/warp-drive'
import { withDefaults } from '@warp-drive/core/reactive'
registerCodecs(codecs)
export const FirewallRuleSchema = withDefaults({
type: 'firewall-rule',
fields: [valueField('source', 'cidr'), valueField('lastClient', 'ip', { unmapIPv4: true })],
})