-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pac-resolver] Remove
ip
dependency (#281)
- Loading branch information
1 parent
e7e0e56
commit a954da3
Showing
5 changed files
with
139 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'pac-resolver': patch | ||
--- | ||
|
||
fix [GHSA-78xj-cgh5-2h22](https://github.com/advisories/GHSA-78xj-cgh5-2h22) vulnerability |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os from 'os'; | ||
|
||
export const ip = { | ||
address(): string { | ||
const interfaces = os.networkInterfaces(); | ||
|
||
// Default to `ipv4` | ||
const family = normalizeFamily(); | ||
|
||
const all = Object.values(interfaces).map((addrs = []) => { | ||
const addresses = addrs.filter((details) => { | ||
const detailsFamily = normalizeFamily(details.family); | ||
if (detailsFamily !== family || ip.isLoopback(details.address)) { | ||
return false; | ||
} | ||
return true; | ||
|
||
}); | ||
|
||
return addresses.length ? addresses[0].address : undefined; | ||
}).filter(Boolean); | ||
|
||
return !all.length ? ip.loopback(family) : all[0] as string; | ||
}, | ||
|
||
isLoopback(addr: string): boolean { | ||
return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/ | ||
.test(addr) | ||
|| /^fe80::1$/.test(addr) | ||
|| /^::1$/.test(addr) | ||
|| /^::$/.test(addr); | ||
}, | ||
|
||
loopback(family: IpFamily): string { | ||
// Default to `ipv4` | ||
family = normalizeFamily(family); | ||
|
||
if (family !== 'ipv4' && family !== 'ipv6') { | ||
throw new Error('family must be ipv4 or ipv6'); | ||
} | ||
|
||
return family === 'ipv4' ? '127.0.0.1' : 'fe80::1'; | ||
} | ||
|
||
}; | ||
|
||
function normalizeFamily(family?: unknown): IpFamily { | ||
if (family === 4) { | ||
return 'ipv4'; | ||
} | ||
if (family === 6) { | ||
return 'ipv6'; | ||
} | ||
return family ? (family as string).toLowerCase() as IpFamily : 'ipv4'; | ||
} | ||
|
||
type IpFamily = 'ipv4' | 'ipv6' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import ip from 'ip'; | ||
import { ip } from './ip'; | ||
import net, { AddressInfo } from 'net'; | ||
|
||
/** | ||
|
Oops, something went wrong.