MCPcopy
hub / github.com/axios/axios / isIPv6Loopback

Function isIPv6Loopback

lib/helpers/shouldBypassProxy.js:41–70  ·  view source on GitHub ↗
(host)

Source from the content-addressed store, hash-verified

39};
40
41const 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
72const isLoopback = (host) => {
73 if (!host) return false;

Callers 1

isLoopbackFunction · 0.85

Calls 1

isIPv4LoopbackFunction · 0.85

Tested by

no test coverage detected