(hostname: string, pattern: string)
| 51 | } |
| 52 | |
| 53 | export function patternMatchesHostname(hostname: string, pattern: string) { |
| 54 | // Split hostname and pattern to separate hostname and port |
| 55 | const [hostnameWithoutPort, hostnamePort] = hostname.toLowerCase().split(":"); |
| 56 | const [patternWithoutPort, patternPort] = pattern.toLowerCase().split(":"); |
| 57 | |
| 58 | // If pattern specifies a port but hostname doesn't match it, no match |
| 59 | if (patternPort && (!hostnamePort || hostnamePort !== patternPort)) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // Now compare just the hostname parts |
| 64 | |
| 65 | // exact match |
| 66 | if (patternWithoutPort === hostnameWithoutPort) { |
| 67 | return true; |
| 68 | } |
| 69 | // wildcard domain match (*.example.com) |
| 70 | if ( |
| 71 | patternWithoutPort.startsWith("*.") && |
| 72 | hostnameWithoutPort.endsWith(patternWithoutPort.substring(1)) |
| 73 | ) { |
| 74 | return true; |
| 75 | } |
| 76 | // Domain suffix match (.example.com) |
| 77 | if ( |
| 78 | patternWithoutPort.startsWith(".") && |
| 79 | hostnameWithoutPort.endsWith(patternWithoutPort.slice(1)) |
| 80 | ) { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | // TODO IP address ranges |
| 85 | |
| 86 | // TODO CIDR notation |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks if a hostname should bypass proxy based on NO_PROXY environment variable |
no outgoing calls
no test coverage detected