Skip to content

Commit

Permalink
feat: replace validator
Browse files Browse the repository at this point in the history
  • Loading branch information
gucheen committed May 7, 2024
1 parent e9fb0bb commit fb69b73
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
"traceroute"
],
"dependencies": {
"validator": "^13.7.0"
"is-fqdn": "^2.0.1"
},
"devDependencies": {
"@types/jest": "^29.2.3",
"@types/node": "^18.11.9",
"@types/validator": "^13.7.10",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"eslint": "^8.27.0",
Expand Down
6 changes: 4 additions & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import events from 'events';
import readline from 'readline';
import validator from 'validator';
import net from 'net';
import isFQDN from 'is-fqdn';

import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { Readable } from 'stream';
Expand Down Expand Up @@ -72,7 +73,8 @@ export abstract class Process extends events.EventEmitter {
}

private isValidDomainName(domainName: string): boolean {
return validator.isFQDN(domainName + '') || validator.isIP(domainName + '');
const ipTestResult = net.isIP(domainName + '')
return isFQDN(domainName + '') || ipTestResult === 6 || ipTestResult === 4;
}

abstract parseDestination(data: string): string | null;
Expand Down
8 changes: 4 additions & 4 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import validator from 'validator';
import net from 'net';

import Traceroute from '../src/index';

Expand All @@ -11,18 +11,19 @@ describe('Traceroute', () => {
expect(Number.isInteger(pid)).toBeTruthy();
})
.on('destination', (destination) => {
expect(validator.isIP(destination)).toBeTruthy();
expect(net.isIP(destination)).toBeTruthy();
})
.on('hop', (hopObj) => {
const { hop, ip, rtt1 } = hopObj;

expect(Number.isInteger(hop)).toBeTruthy();
expect(validator.isIP(ip) || ip === '*').toBeTruthy();
expect(net.isIP(ip) || ip === '*').toBeTruthy();
expect(/^\d+\.\d+\sms$/.test(rtt1) || rtt1 === '*').toBeTruthy();
})
.on('close', (code) => {
expect(Number.isInteger(code)).toBeTruthy();

tracer.process?.kill()
wait();
});

Expand All @@ -36,7 +37,6 @@ describe('Traceroute', () => {

setTimeout(() => {
const killResult = tracer.kill()
expect(tracer.process?.killed).toBeTruthy()
expect(killResult).toBeTruthy()
done()
}, 5000)
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["src"]
"include": ["src", "typings"]
}
3 changes: 3 additions & 0 deletions typings/is-fqdn.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'is-fqdn' {
export default function isFQDN(input: string): boolean;
}
15 changes: 5 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -746,11 +746,6 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==

"@types/validator@^13.7.10":
version "13.7.10"
resolved "https://registry.yarnpkg.com/@types/validator/-/validator-13.7.10.tgz#f9763dc0933f8324920afa9c0790308eedf55ca7"
integrity sha512-t1yxFAR2n0+VO6hd/FJ9F2uezAZVWHLmpmlJzm1eX03+H7+HsuTAp7L8QJs+2pQCfWkP1+EXsGK9Z9v7o/qPVQ==

"@types/yargs-parser@*":
version "21.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b"
Expand Down Expand Up @@ -1671,6 +1666,11 @@ is-extglob@^2.1.1:
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
integrity sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==

is-fqdn@^2.0.1:
version "2.0.1"
resolved "https://registry.npmmirror.com/is-fqdn/-/is-fqdn-2.0.1.tgz#2c47b17007b780cd034922ef86a4b3aad0901352"
integrity sha512-oQgLvyS2gWO5ZT3EbgdpkIN5fORg5Sadn0SQ4seTwt/fuKxQHrg8k/BaI02dGgcek+Gc2zxrd1tBo7h5HyBdNA==

is-fullwidth-code-point@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d"
Expand Down Expand Up @@ -2782,11 +2782,6 @@ v8-to-istanbul@^9.0.1:
"@types/istanbul-lib-coverage" "^2.0.1"
convert-source-map "^1.6.0"

validator@^13.7.0:
version "13.7.0"
resolved "https://registry.yarnpkg.com/validator/-/validator-13.7.0.tgz#4f9658ba13ba8f3d82ee881d3516489ea85c0857"
integrity sha512-nYXQLCBkpJ8X6ltALua9dRrZDHVYxjJ1wgskNt1lH9fzGjs3tgojGSCBjmEPwkWS1y29+DrizMTW19Pr9uB2nw==

walker@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.8.tgz#bd498db477afe573dc04185f011d3ab8a8d7653f"
Expand Down

0 comments on commit fb69b73

Please sign in to comment.