bukkit-stats
Advanced tools
Comparing version 1.0.1 to 1.1.0
// module dependencies | ||
var events = require("events") | ||
var colors = require("./colors") | ||
, sanitize = require("sanitize-arguments") | ||
, events = require("events") | ||
, net = require("net") | ||
@@ -17,21 +19,11 @@ , util = require("util"); | ||
// constructor | ||
function Stats(host, port, cb) { | ||
function Stats(host, port, colorize, cb) { | ||
// ensure that the object is instanciated with the new operator | ||
if(!(this instanceof Stats)) return new Stats(host, port, cb); | ||
if(!(this instanceof Stats)) return new Stats(host, port, colorize, cb); | ||
// parse the arguments | ||
if(typeof host == "string" && ~host.indexOf(":")) { | ||
host = host.split(":"); | ||
this.config = { host: host[0], port: host[1] }; | ||
} else { | ||
this.config = { | ||
host: typeof host == "string" ? host : CONNECTION.defaultHost, | ||
port: typeof port == "number" ? port : CONNECTION.defaultPort | ||
}; | ||
} | ||
this.config = sanitize(arguments, Stats, [ | ||
[CONNECTION.defaultHost], [CONNECTION.defaultPort], [false], [function() {}] | ||
]); | ||
if(typeof host == "function") cb = host; | ||
else if(typeof port == "function") cb = port; | ||
else if(typeof cb != "function") cb = function() {}; | ||
// start the event listener | ||
@@ -41,3 +33,3 @@ events.EventEmitter.call(this); | ||
// register the callback | ||
this.on("stats", cb); | ||
this.on("stats", this.config.cb); | ||
@@ -60,8 +52,8 @@ // fire the request | ||
// listen on the response | ||
socket.once("data", wrap(this.handleResponse, this, socket, startTime)); | ||
socket.once("data", this.handleResponse.bind(this, socket, startTime)); | ||
// listen on errors | ||
socket.once("error", wrap(this.handleError, this, "error")); | ||
socket.once("timeout", wrap(this.handleError, this, "timeout")); | ||
socket.once("close", wrap(this.handleError, this, "close")); | ||
socket.once("error", this.handleError.bind(this, "error")); | ||
socket.once("timeout", this.handleError.bind(this, "timeout")); | ||
socket.once("close", this.handleError.bind(this, "close")); | ||
@@ -75,9 +67,10 @@ // send the magic packet | ||
switch(type) { | ||
case "timeout": this.emit("error", new Error(ERRORS.timeout)); break; | ||
case "close": this.emit("error", new Error(ERRORS.close)); break; | ||
case "format": this.emit("error", new Error(ERRORS.format)); break; | ||
case "error": | ||
default: | ||
error.message = ERRORS.unknown + error.message; | ||
this.emit("error", error); | ||
break; | ||
case "timeout": this.emit("error", new Error(ERRORS.timeout)); break; | ||
case "close": this.emit("error", new Error(ERRORS.close)); break; | ||
case "format": this.emit("error", new Error(ERRORS.format)); break; | ||
} | ||
@@ -110,3 +103,3 @@ }; | ||
// append a 0x00 to the buffer, because Bukkit forget it | ||
// append a 0x00 to the buffer, because Bukkit forgot it | ||
buffer = Buffer.concat([buffer, new Buffer([CTRL.missingByte])]); | ||
@@ -117,15 +110,12 @@ | ||
// find the subparts | ||
var maxPlayers = parseInt(res.pop()) | ||
, onlinePlayers = parseInt(res.pop()) | ||
, motd = res.join(String.fromCharCode(CTRL.color)); | ||
return { | ||
motd: res[0], | ||
onlinePlayers: parseInt(res[1]), | ||
maxPlayers: parseInt(res[2]) | ||
motd: this.config.colorize ? colors(motd) : motd, | ||
onlinePlayers: onlinePlayers, | ||
maxPlayers: maxPlayers | ||
}; | ||
}; | ||
// helper function | ||
function wrap(fn, ref) { | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
return function() { | ||
fn.apply(ref, args.concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
} | ||
}; |
@@ -83,2 +83,3 @@ exports.PACKETS = { | ||
delimeter: 0xA7, | ||
color: 0xA7, | ||
missingByte: 0x00 | ||
@@ -97,2 +98,28 @@ }; | ||
format: "Server returned an invalid response!" | ||
}; | ||
exports.COLORS = { | ||
"0": "black", | ||
"1": "blue", | ||
"2": "green", | ||
"3": "cyan", | ||
"4": "red", | ||
"5": "magenta", | ||
"5": "yellow", | ||
"7": "grey", | ||
"8": "grey", | ||
"9": "blue", | ||
"a": "green", | ||
"b": "cyan", | ||
"c": "red", | ||
"d": "magenta", | ||
"e": "yellow", | ||
"f": "white", | ||
"k": "inverse", | ||
"l": "bold", | ||
"m": "inverse", | ||
"n": "underline", | ||
"o": "italic", | ||
"r": "reset", | ||
"reset": "r" | ||
}; |
{ | ||
"name": "bukkit-stats", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Gets a server's stats displayed in the client serverlist", | ||
@@ -19,3 +19,7 @@ "main": "index.js", | ||
"author": "Jan Buschtöns <buschtoens@gmail.com>", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"colors": "*", | ||
"sanitize-arguments": "*" | ||
} | ||
} |
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 2 instances 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
7367
7
241
2
3
+ Addedcolors@*
+ Addedsanitize-arguments@*
+ Addedcolors@1.4.0(transitive)
+ Addedsanitize-arguments@2.0.3(transitive)