(host)
| 39 | }; |
| 40 | |
| 41 | const isIPv6Loopback = (host) => { |
| 42 | // Collapse all-zero groups: any form of ::1 / 0:0:...:0:1 |
| 43 | // First, strip any leading "::" by normalising with Set lookup of common forms, |
| 44 | // then fall back to structural check. |
| 45 | if (host === '::1') return true; |
| 46 | |
| 47 | // Check IPv4-mapped IPv6 loopback: ::ffff:<v4-loopback> or ::ffff:<hex-v4-loopback> |
| 48 | // Node's URL parser normalises ::ffff:127.0.0.1 → ::ffff:7f00:1 |
| 49 | const v4MappedDotted = host.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/i); |
| 50 | if (v4MappedDotted) return isIPv4Loopback(v4MappedDotted[1]); |
| 51 | |
| 52 | const v4MappedHex = host.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i); |
| 53 | if (v4MappedHex) { |
| 54 | const high = parseInt(v4MappedHex[1], 16); |
| 55 | // High 16 bits must start with 127 (0x7f) — i.e. 0x7f00..0x7fff |
| 56 | return high >= 0x7f00 && high <= 0x7fff; |
| 57 | } |
| 58 | |
| 59 | // Full-form ::1 variants: any number of zero groups followed by trailing 1 |
| 60 | // e.g. 0:0:0:0:0:0:0:1, 0000:...:0001 |
| 61 | const groups = host.split(':'); |
| 62 | if (groups.length === 8) { |
| 63 | for (let i = 0; i < 7; i++) { |
| 64 | if (!/^0+$/.test(groups[i])) return false; |
| 65 | } |
| 66 | return /^0*1$/.test(groups[7]); |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | }; |
| 71 | |
| 72 | const isLoopback = (host) => { |
| 73 | if (!host) return false; |
no test coverage detected