Comparing version 1.4.0 to 2.0.0
@@ -0,0 +0,0 @@ const icmp = require('icmp'); |
@@ -0,0 +0,0 @@ const icmp = require('icmp'); |
@@ -0,0 +0,0 @@ const icmp = require('icmp'); |
51
index.js
@@ -29,3 +29,3 @@ const raw = require('raw-socket'); | ||
_resolveIP() { | ||
return new Promise(resolve => { | ||
return new Promise((resolve, reject) => { | ||
if (!this.ip) { | ||
@@ -37,6 +37,6 @@ if (net.isIPv4(this.host)) { | ||
dns.resolve4(this.host, (err, [ip]) => { | ||
dns.resolve4(this.host, (err, ips) => { | ||
if (err) return reject(err); | ||
this.ip = ip; | ||
this.ip = ips[0]; | ||
@@ -73,16 +73,18 @@ resolve(); | ||
this.socket.on('message', (buffer, source) => { | ||
const NS_PER_SEC = 1e9; | ||
if (source === this.ip) { | ||
const NS_PER_SEC = 1e9; | ||
this.diff = process.hrtime(this.start); | ||
this.elapsed = (this.diff[0] + this.diff[1] / NS_PER_SEC) * 1000; | ||
this.diff = process.hrtime(this.start); | ||
this.elapsed = (this.diff[0] + this.diff[1] / NS_PER_SEC) * 1000; | ||
const offset = 20; | ||
const type = buffer.readUInt8(offset); | ||
const code = buffer.readUInt8(offset + 1); | ||
const offset = 20; | ||
const type = buffer.readUInt8(offset); | ||
const code = buffer.readUInt8(offset + 1); | ||
this.parse(type, code); | ||
this.parse(type, code); | ||
this.close(); | ||
this.close(); | ||
resolve(); | ||
resolve(); | ||
} | ||
}); | ||
@@ -122,17 +124,12 @@ | ||
send(data = "", timeout = 5000) { | ||
return new Promise((resolve, reject) => { | ||
const header = this.createHeader(data); | ||
this._queue(header, timeout).then(resolve, reject); | ||
}); | ||
async send(data = "", timeout = 5000) { | ||
const header = this.createHeader(data); | ||
return await this._queue(header, timeout); | ||
} | ||
static send(host, data = "", timeout = 5000) { | ||
const obj = new this(host); | ||
static async send(host, data = "", timeout = 5000) { | ||
const obj = new ICMP(host); | ||
return new Promise((resolve, reject) => obj.send(data, timeout) | ||
.then(() => resolve(obj)) | ||
.catch(err => reject(err)) | ||
); | ||
await obj.send(data, timeout); | ||
return obj; | ||
} | ||
@@ -148,8 +145,8 @@ | ||
listen(cb = (buffer, source) => {}) { | ||
listen(cb = (buffer, source) => { }) { | ||
return this.socket.on('message', cb); | ||
} | ||
static listen(cb = (buffer, source) => {}) { | ||
const obj = new this(null); | ||
static listen(cb = (buffer, source) => { }) { | ||
const obj = new ICMP(null); | ||
return obj.listen(cb); | ||
@@ -156,0 +153,0 @@ } |
{ | ||
"name": "icmp", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"description": "Internet Control Message Protocol in Node", | ||
@@ -37,4 +37,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "^5.2.0" | ||
"mocha": "^8.2.1" | ||
} | ||
} |
@@ -0,0 +0,0 @@ <h1 align="center">Internet Control Message Protocol in Node</h1> |
73
test.js
@@ -7,27 +7,6 @@ const assert = require('assert'); | ||
describe('Offline ICMP', () => { | ||
describe('ping localhost', () => { | ||
const ping = icmp.ping('localhost', 1900); | ||
try { | ||
it('ip property is 127.0.0.1', async () => { | ||
const { ip } = await ping; | ||
assert(ip, '127.0.0.1'); | ||
}); | ||
it('open property is true', async () => { | ||
const { open } = await ping; | ||
assert(open, true); | ||
}); | ||
} catch (e) { assert.fail(e) } | ||
}); | ||
}); | ||
const check = new Promise(resolve => { | ||
dns.lookup('google.com', (err) => { | ||
if (err && err.code == "ENOTFOUND") { | ||
assert.fail(); | ||
resolve(false); | ||
fail(); | ||
} else { | ||
@@ -41,9 +20,30 @@ assert.ok(true); | ||
// describe('Offline ICMP', () => { | ||
// describe('ping 127.0.0.1', () => { | ||
// const ping = icmp.ping('127.0.0.1'); | ||
// try { | ||
// it('ip property is 127.0.0.1', async () => { | ||
// const { ip } = await ping; | ||
// console.log(ip) | ||
// assert.strictEqual(ip, '127.0.0.1'); | ||
// }); | ||
// it('open property is true', async () => { | ||
// const { open } = await ping; | ||
// assert.strictEqual(open, true); | ||
// }); | ||
// } catch (e) { fail(e) } | ||
// }); | ||
// }); | ||
describe('Online ICMP', () => { | ||
it('internet connection', () => { | ||
check.then(res => res ? assert.ok(true) : assert.fail()); | ||
check.then(res => res ? assert.ok(true) : fail()); | ||
}); | ||
check.then(res => { | ||
if(!res) return; | ||
if (!res) return; | ||
@@ -54,6 +54,6 @@ describe('ping www.google.com', () => { | ||
try { | ||
it('ip property is an IP', async (done) => { | ||
it('ip property is an IP', async () => { | ||
const { ip } = await ping; | ||
assert(net.isIP(ip) >= 0, true); | ||
assert.notStrictEqual(net.isIP(ip), 0); | ||
}); | ||
@@ -64,7 +64,24 @@ | ||
assert(open, true); | ||
assert(open); | ||
}); | ||
} catch (e) { assert.fail(e) } | ||
} catch (e) { fail(e) } | ||
}); | ||
describe('ping a non-existing domain', async () => { | ||
try { | ||
await icmp.ping('this-domain-does.not.exist'); | ||
} catch (error) { | ||
try { | ||
it('rejects with the error object', (done) => { | ||
ping.catch(error => { | ||
assert(error.message.indexOf('ENOTFOUND this-dns-does.not.exist')); | ||
assert(error.code === 'ENOTFOUND'); | ||
assert(error.hostname === 'this-dns-does.not.exist'); | ||
done(); | ||
}) | ||
}); | ||
} catch (e) { fail(e) } | ||
} | ||
}); | ||
}); | ||
}); |
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
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
11183
224
0