Skip to content

Commit

Permalink
fix: remove ip module, hand-check loopback
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Mar 2, 2020
1 parent 3ff9cad commit d80e89a
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 37 deletions.
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"execa": "^4.0.0",
"glob-stream": "^6.1.0",
"inversify": "^5.0.1",
"ip": "^1.1.5",
"js-beautify": "^1.10.3",
"jsonc-parser": "^2.2.0",
"micromatch": "^4.0.2",
Expand Down Expand Up @@ -84,7 +83,6 @@
"@types/glob": "^7.1.1",
"@types/glob-stream": "^6.1.0",
"@types/http-server": "^0.10.0",
"@types/ip": "^1.1.0",
"@types/js-beautify": "^1.8.1",
"@types/json-schema": "^7.0.3",
"@types/lodash": "^4.14.144",
Expand Down
23 changes: 4 additions & 19 deletions src/common/urlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { AnyChromiumConfiguration } from '../configuration';
import { escapeRegexSpecialChars } from './stringUtils';
import { promises as dns } from 'dns';
import { memoize } from './objUtils';
import ipModule from 'ip';
import { exists } from './fsUtils';
import { IDisposable } from './disposable';
import { TaskCancelledError, NeverCancelled } from './cancellation';
Expand Down Expand Up @@ -69,30 +68,16 @@ export const nearestDirectoryWhere = async (
export const nearestDirectoryContaining = (rootDir: string, file: string) =>
nearestDirectoryWhere(rootDir, p => exists(path.join(p, file)));

const localv4 = ipModule.toBuffer('127.0.0.1');
const localv6 = ipModule.toBuffer('::1');
// todo: not super correct, and most node libraries don't handle this accurately
const knownLoopbacks = new Set<string>(['localhost', '127.0.0.1', '::1']);

/**
* Checks if the given address, well-formed loopback IPs. We don't need exotic
* variations like `127.1` because `dns.lookup()` will resolve the proper
* version for us. The "right" way would be to parse the IP to an integer
* like Go does (https://golang.org/pkg/net/#IP.IsLoopback), but this
* is lightweight and works.
* like Go does (https://golang.org/pkg/net/#IP.IsLoopback).
*/
const isLoopbackIp = (ipOrLocalhost: string) => {
if (ipOrLocalhost.toLowerCase() === 'localhost') {
return true;
}

let buf: Buffer;
try {
buf = ipModule.toBuffer(ipOrLocalhost);
} catch {
return false;
}

return buf.equals(localv4) || buf.equals(localv6);
};
const isLoopbackIp = (ipOrLocalhost: string) => knownLoopbacks.has(ipOrLocalhost.toLowerCase());

/**
* Gets whether the IP is a loopback address.
Expand Down
2 changes: 0 additions & 2 deletions src/test/common/urlUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ describe('urlUtils', () => {
'totes invalid': false,
'1.1.1.1': false,
'::1': true,
':0:1': true,
'0:0:0:0:0:0:0:1': true,
':1:1': false,
'http://[::1]/foo': true,
'http://[:1:1]/foo': false,
Expand Down

0 comments on commit d80e89a

Please sign in to comment.