Skip to content

Commit a954da3

Browse files
[pac-resolver] Remove ip dependency (#281)
1 parent e7e0e56 commit a954da3

File tree

5 files changed

+139
-54
lines changed

5 files changed

+139
-54
lines changed

.changeset/heavy-planets-stare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pac-resolver': patch
3+
---
4+
5+
fix [GHSA-78xj-cgh5-2h22](https://github.com/advisories/GHSA-78xj-cgh5-2h22) vulnerability

packages/pac-resolver/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
],
1010
"dependencies": {
1111
"degenerator": "^5.0.0",
12-
"ip": "^1.1.8",
1312
"netmask": "^2.0.2"
1413
},
1514
"devDependencies": {
1615
"@tootallnate/quickjs-emscripten": "^0.23.0",
17-
"@types/ip": "^1.1.0",
1816
"@types/jest": "^29.5.2",
1917
"@types/netmask": "^1.0.30",
2018
"@types/node": "^14.18.52",

packages/pac-resolver/src/ip.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os from 'os';
2+
3+
export const ip = {
4+
address(): string {
5+
const interfaces = os.networkInterfaces();
6+
7+
// Default to `ipv4`
8+
const family = normalizeFamily();
9+
10+
const all = Object.values(interfaces).map((addrs = []) => {
11+
const addresses = addrs.filter((details) => {
12+
const detailsFamily = normalizeFamily(details.family);
13+
if (detailsFamily !== family || ip.isLoopback(details.address)) {
14+
return false;
15+
}
16+
return true;
17+
18+
});
19+
20+
return addresses.length ? addresses[0].address : undefined;
21+
}).filter(Boolean);
22+
23+
return !all.length ? ip.loopback(family) : all[0] as string;
24+
},
25+
26+
isLoopback(addr: string): boolean {
27+
return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/
28+
.test(addr)
29+
|| /^fe80::1$/.test(addr)
30+
|| /^::1$/.test(addr)
31+
|| /^::$/.test(addr);
32+
},
33+
34+
loopback(family: IpFamily): string {
35+
// Default to `ipv4`
36+
family = normalizeFamily(family);
37+
38+
if (family !== 'ipv4' && family !== 'ipv6') {
39+
throw new Error('family must be ipv4 or ipv6');
40+
}
41+
42+
return family === 'ipv4' ? '127.0.0.1' : 'fe80::1';
43+
}
44+
45+
};
46+
47+
function normalizeFamily(family?: unknown): IpFamily {
48+
if (family === 4) {
49+
return 'ipv4';
50+
}
51+
if (family === 6) {
52+
return 'ipv6';
53+
}
54+
return family ? (family as string).toLowerCase() as IpFamily : 'ipv4';
55+
}
56+
57+
type IpFamily = 'ipv4' | 'ipv6'

packages/pac-resolver/src/myIpAddress.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ip from 'ip';
1+
import { ip } from './ip';
22
import net, { AddressInfo } from 'net';
33

44
/**

0 commit comments

Comments
 (0)