mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-08 19:14:06 +08:00
Autofix on save
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
// https://github.com/ben-bradley/ping-lite/blob/master/ping-lite.js
|
||||
// Fixed on Windows
|
||||
|
||||
var spawn = require('child_process').spawn,
|
||||
events = require('events'),
|
||||
fs = require('fs'),
|
||||
let spawn = require("child_process").spawn,
|
||||
events = require("events"),
|
||||
fs = require("fs"),
|
||||
WIN = /^win/.test(process.platform),
|
||||
LIN = /^linux/.test(process.platform),
|
||||
MAC = /^darwin/.test(process.platform);
|
||||
@@ -11,8 +11,9 @@ var spawn = require('child_process').spawn,
|
||||
module.exports = Ping;
|
||||
|
||||
function Ping(host, options) {
|
||||
if (!host)
|
||||
throw new Error('You must specify a host to ping!');
|
||||
if (!host) {
|
||||
throw new Error("You must specify a host to ping!");
|
||||
}
|
||||
|
||||
this._host = host;
|
||||
this._options = options = (options || {});
|
||||
@@ -20,26 +21,24 @@ function Ping(host, options) {
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
if (WIN) {
|
||||
this._bin = 'c:/windows/system32/ping.exe';
|
||||
this._args = (options.args) ? options.args : [ '-n', '1', '-w', '5000', host ];
|
||||
this._bin = "c:/windows/system32/ping.exe";
|
||||
this._args = (options.args) ? options.args : [ "-n", "1", "-w", "5000", host ];
|
||||
this._regmatch = /[><=]([0-9.]+?)ms/;
|
||||
}
|
||||
else if (LIN) {
|
||||
this._bin = '/bin/ping';
|
||||
this._args = (options.args) ? options.args : [ '-n', '-w', '2', '-c', '1', host ];
|
||||
} else if (LIN) {
|
||||
this._bin = "/bin/ping";
|
||||
this._args = (options.args) ? options.args : [ "-n", "-w", "2", "-c", "1", host ];
|
||||
this._regmatch = /=([0-9.]+?) ms/; // need to verify this
|
||||
}
|
||||
else if (MAC) {
|
||||
this._bin = '/sbin/ping';
|
||||
this._args = (options.args) ? options.args : [ '-n', '-t', '2', '-c', '1', host ];
|
||||
} else if (MAC) {
|
||||
this._bin = "/sbin/ping";
|
||||
this._args = (options.args) ? options.args : [ "-n", "-t", "2", "-c", "1", host ];
|
||||
this._regmatch = /=([0-9.]+?) ms/;
|
||||
}
|
||||
else {
|
||||
throw new Error('Could not detect your ping binary.');
|
||||
} else {
|
||||
throw new Error("Could not detect your ping binary.");
|
||||
}
|
||||
|
||||
if (!fs.existsSync(this._bin))
|
||||
throw new Error('Could not detect '+this._bin+' on your system');
|
||||
if (!fs.existsSync(this._bin)) {
|
||||
throw new Error("Could not detect " + this._bin + " on your system");
|
||||
}
|
||||
|
||||
this._i = 0;
|
||||
|
||||
@@ -51,48 +50,56 @@ Ping.prototype.__proto__ = events.EventEmitter.prototype;
|
||||
// SEND A PING
|
||||
// ===========
|
||||
Ping.prototype.send = function(callback) {
|
||||
var self = this;
|
||||
let self = this;
|
||||
callback = callback || function(err, ms) {
|
||||
if (err) return self.emit('error', err);
|
||||
else return self.emit('result', ms);
|
||||
if (err) {
|
||||
return self.emit("error", err);
|
||||
}
|
||||
return self.emit("result", ms);
|
||||
};
|
||||
|
||||
var _ended, _exited, _errored;
|
||||
let _ended, _exited, _errored;
|
||||
|
||||
this._ping = spawn(this._bin, this._args); // spawn the binary
|
||||
|
||||
this._ping.on('error', function(err) { // handle binary errors
|
||||
this._ping.on("error", function(err) { // handle binary errors
|
||||
_errored = true;
|
||||
callback(err);
|
||||
});
|
||||
|
||||
this._ping.stdout.on('data', function(data) { // log stdout
|
||||
this._stdout = (this._stdout || '') + data;
|
||||
this._ping.stdout.on("data", function(data) { // log stdout
|
||||
this._stdout = (this._stdout || "") + data;
|
||||
});
|
||||
|
||||
this._ping.stdout.on('end', function() {
|
||||
this._ping.stdout.on("end", function() {
|
||||
_ended = true;
|
||||
if (_exited && !_errored) onEnd.call(self._ping);
|
||||
if (_exited && !_errored) {
|
||||
onEnd.call(self._ping);
|
||||
}
|
||||
});
|
||||
|
||||
this._ping.stderr.on('data', function(data) { // log stderr
|
||||
this._stderr = (this._stderr || '') + data;
|
||||
this._ping.stderr.on("data", function(data) { // log stderr
|
||||
this._stderr = (this._stderr || "") + data;
|
||||
});
|
||||
|
||||
this._ping.on('exit', function(code) { // handle complete
|
||||
this._ping.on("exit", function(code) { // handle complete
|
||||
_exited = true;
|
||||
if (_ended && !_errored) onEnd.call(self._ping);
|
||||
if (_ended && !_errored) {
|
||||
onEnd.call(self._ping);
|
||||
}
|
||||
});
|
||||
|
||||
function onEnd() {
|
||||
var stdout = this.stdout._stdout,
|
||||
let stdout = this.stdout._stdout,
|
||||
stderr = this.stderr._stderr,
|
||||
ms;
|
||||
|
||||
if (stderr)
|
||||
if (stderr) {
|
||||
return callback(new Error(stderr));
|
||||
else if (!stdout)
|
||||
return callback(new Error('No stdout detected'));
|
||||
}
|
||||
if (!stdout) {
|
||||
return callback(new Error("No stdout detected"));
|
||||
}
|
||||
|
||||
ms = stdout.match(self._regmatch); // parse out the ##ms response
|
||||
ms = (ms && ms[1]) ? Number(ms[1]) : ms;
|
||||
@@ -104,7 +111,7 @@ Ping.prototype.send = function(callback) {
|
||||
// CALL Ping#send(callback) ON A TIMER
|
||||
// ===================================
|
||||
Ping.prototype.start = function(callback) {
|
||||
var self = this;
|
||||
let self = this;
|
||||
this._i = setInterval(function() {
|
||||
self.send(callback);
|
||||
}, (self._options.interval || 5000));
|
||||
|
Reference in New Issue
Block a user