(value: unknown, isCidr: boolean)
| 775 | const PGSQL_AF_INET6 = 3 |
| 776 | |
| 777 | const encodeInet = (value: unknown, isCidr: boolean): Uint8Array => { |
| 778 | const type = isCidr ? "cidr" : "inet" |
| 779 | const text = requireString(value, type) |
| 780 | const parsed = IpInterface.fromString(text) |
| 781 | if (Result.isFailure(parsed)) return fail(`Invalid ${type} value ${JSON.stringify(text)}: ${parsed.failure.message}`) |
| 782 | const address = parsed.success.address |
| 783 | const bits = parsed.success.prefixLength |
| 784 | if (isCidr) { |
| 785 | const network = IpNetwork.make(address, bits) |
| 786 | if (Result.isFailure(network)) { |
| 787 | return fail(`Invalid ${type} value ${JSON.stringify(text)}: ${network.failure.message}`) |
| 788 | } |
| 789 | } |
| 790 | const octets = NetAddress.isIpv4Address(address) |
| 791 | ? NetAddress.ipv4ToOctets(address) |
| 792 | : NetAddress.ipv6ToOctets(address) |
| 793 | const result = new Uint8Array(4 + octets.length) |
| 794 | result[0] = NetAddress.isIpv4Address(address) ? PGSQL_AF_INET : PGSQL_AF_INET6 |
| 795 | result[1] = bits |
| 796 | result[2] = isCidr ? 1 : 0 |
| 797 | result[3] = octets.length |
| 798 | result.set(octets, 4) |
| 799 | return result |
| 800 | } |
| 801 | |
| 802 | const decodeInet = (bytes: Uint8Array, offset: number, size: number): string => { |
| 803 | if (size < 4) return fail("Truncated inet value") |
no test coverage detected