fix: replace regex with charCodeAt-based check

Use character code comparisons instead of RegExp for link-local
IPv6 detection. This avoids the overhead of regex compilation and
matching while still correctly detecting the fe80::/10 range.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Claude Bot
2026-01-12 20:40:32 +00:00
parent 9a36086593
commit d025e0f92a

View File

@@ -502,8 +502,21 @@ function ClientRequest(input, options, cb) {
// so we deprioritize them to avoid connection timeouts on VPN networks.
// See: https://github.com/oven-sh/bun/issues/25619
const isLinkLocalIPv6 = (addr: string) => {
// fe80::/10 covers fe80:: through febf::
return /^fe[89ab][0-9a-f]:/i.test(addr);
// fe80::/10 covers fe80:: through febf:: (first 10 bits are 1111111010)
// Check if address starts with fe8, fe9, fea, or feb (case-insensitive)
if (addr.length < 4) return false;
const c0 = addr.charCodeAt(0) | 0x20; // lowercase
const c1 = addr.charCodeAt(1) | 0x20;
const c2 = addr.charCodeAt(2) | 0x20;
const c3 = addr.charCodeAt(3);
// Check for 'fe[89ab]:'
return (
c0 === 0x66 &&
c1 === 0x65 && // 'fe'
(c2 === 0x38 || c2 === 0x39 || c2 === 0x61 || c2 === 0x62) && // '8', '9', 'a', 'b'
((c3 >= 0x30 && c3 <= 0x39) || ((c3 | 0x20) >= 0x61 && (c3 | 0x20) <= 0x66)) && // hex digit
addr.charCodeAt(4) === 0x3a
); // ':'
};
let candidates = results.sort((a, b) => {
const aIsLinkLocal = a.family === 6 && isLinkLocalIPv6(a.address);