Comparing version 0.1.2 to 0.2.0
45
index.js
let __tcpCheck = require('./build/Release/tcpCheck'); | ||
let net = require('net'); | ||
let dns = require('dns'); | ||
class InvalidAddressError extends Error { | ||
constructor() { | ||
super('invalid ip address, must be a ipv4 string'); | ||
this.name = 'INVALID_IP_ADDRESS'; | ||
} | ||
} | ||
class InvalidPortError extends Error { | ||
@@ -18,28 +12,31 @@ constructor() { | ||
let tcpCheck = (ip, port, timeout) => new Promise((resolve, reject) => { | ||
let tcpCheck = (host, port, timeout) => new Promise((resolve, reject) => { | ||
// Type converting | ||
ip += ''; | ||
let timer = setTimeout(reject, timeout * 1 || 1000, { name: 'TIMEOUT' }); | ||
host += ''; | ||
port |= 0; | ||
timeout = timeout * 1 || 1000; | ||
// Value checking | ||
if (!net.isIPv4(ip)) return reject(new InvalidAddressError()); | ||
if (port < 1 || port > 65535) return reject(new InvalidPortError()); | ||
// Set timer | ||
let timer = setTimeout(reject, timeout, { name: 'TIMEOUT' }); | ||
const onLookup = (error, ip) => { | ||
if (error) return reject({ name: error.code }); | ||
actualCheck(ip, port); | ||
}; | ||
// Actual check | ||
__tcpCheck(ip, port, status => { | ||
clearTimeout(timer); | ||
if (status) { | ||
reject(status); | ||
} else { | ||
resolve(status); | ||
} | ||
}); | ||
const actualCheck = (ip, port) => { | ||
__tcpCheck(ip, port, result => { | ||
clearTimeout(timer); | ||
if (result.name === 'OK') { | ||
resolve(result); | ||
} else { | ||
reject(result); | ||
} | ||
}); | ||
}; | ||
net.isIPv4(host) ? actualCheck(host, port) : dns.lookup(host, { family: 4, timeout: 100 }, onLookup); | ||
}); | ||
module.exports = tcpCheck; |
{ | ||
"name": "tcp-check", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "TCP LISTENING CHECKER", | ||
@@ -15,2 +15,5 @@ "main": "index.js", | ||
], | ||
"bin": { | ||
"tcp-check": "./bin/tcp-check.js" | ||
}, | ||
"devDependencies": { | ||
@@ -22,3 +25,24 @@ "mocha": "^3.1.0", | ||
"nan": "^2.4.0" | ||
}, | ||
"reciperConfig": { | ||
"name": "tcp-check", | ||
"darkColor": "#333", | ||
"normalColor": "#666", | ||
"primaryColor": "#000", | ||
"languages": [ | ||
"javascript" | ||
], | ||
"home": "/tcp-check/", | ||
"items": [ | ||
{ | ||
"text": "Introduction", | ||
"href": "/tcp-check/docs/intro/" | ||
}, | ||
{ | ||
"text": "Github", | ||
"href": "//github.com/YanagiEiichi/tcp-check", | ||
"target": "_blank" | ||
} | ||
] | ||
} | ||
} |
@@ -1,25 +0,1 @@ | ||
## tcp-check | ||
### install | ||
```bash | ||
npm install tcp-check | ||
``` | ||
### usage | ||
```js | ||
var promise = tcpCheck(ip, port); | ||
``` | ||
### demo | ||
```js | ||
const tcpCheck = require('tcp-check'); | ||
tcpCheck('127.0.0.1', 80).then(() => { | ||
console.log('ok, your 80 port is listening.'); | ||
}, error => { | ||
console.log(':joy:, your 80 port is not listening.'); | ||
}); | ||
``` | ||
docs <- https://yanagieiichi.github.io/tcp-check/ |
@@ -12,7 +12,11 @@ const mocha = require('mocha'); | ||
it(`tcpCheck('127.0.0.1', ${port})`, () => { | ||
it(`tcpCheck('127.0.0.1', ${port}) OK`, () => { | ||
return tcpCheck('127.0.0.1', port); | ||
}); | ||
it(`tcpCheck('127.0.0.1', 1)`, () => { | ||
it(`tcpCheck('github.com', 80) OK`, () => { | ||
return tcpCheck('github.com', 80); | ||
}); | ||
it(`tcpCheck('127.0.0.1', 1) ECONNREFUSED`, () => { | ||
return tcpCheck('127.0.0.1', 1).then(() => { | ||
@@ -25,6 +29,30 @@ throw new Error('must throw'); | ||
it(`tcpCheck('10.0.0.0', 80)`, () => { | ||
return tcpCheck('10.0.0.0', 80).then(() => { | ||
it(`tcpCheck('127.0.0.1', 0) INVALID_PORT`, () => { | ||
return tcpCheck('127.0.0.1', 0).then(() => { | ||
throw new Error('must throw'); | ||
}, error => { | ||
error.name.should.equal('INVALID_PORT'); | ||
}); | ||
}); | ||
it(`tcpCheck('0.0.0.1', 80) EHOSTUNREACH`, () => { | ||
return tcpCheck('0.0.0.1', 80).then(() => { | ||
throw new Error('must throw'); | ||
}, error => { | ||
error.name.should.equal('EHOSTUNREACH'); | ||
}); | ||
}); | ||
it(`tcpCheck('x.x', 80) ENOTFOUND`, () => { | ||
return tcpCheck('x.x', 80).then(() => { | ||
throw new Error('must throw'); | ||
}, error => { | ||
error.name.should.equal('ENOTFOUND'); | ||
}); | ||
}); | ||
it(`tcpCheck('x.com', 80) TIMEOUT`, () => { | ||
return tcpCheck('x.com', 80).then(() => { | ||
throw new Error('must throw'); | ||
}, error => { | ||
error.name.should.equal('TIMEOUT'); | ||
@@ -31,0 +59,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13679
13
89
2
4