mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-21 23:41:18 +01:00
Merge
This commit is contained in:
commit
01e95f7f19
@ -14,7 +14,8 @@ Please [visit the website](https://crafatar.com) for details.
|
|||||||
|
|
||||||
## Contact
|
## Contact
|
||||||
|
|
||||||
You can [join us](https://webchat.esper.net/?channels=spongy) in #spongy on irc.esper.net.
|
* You can follow us on [@crafatar](https://twitter.com/crafatar)
|
||||||
|
* You can [join us](https://webchat.esper.net/?channels=spongy) in #spongy on irc.esper.net.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
|||||||
@ -59,6 +59,8 @@ exp.get_redis = function() {
|
|||||||
exp.update_timestamp = function(uuid, hash) {
|
exp.update_timestamp = function(uuid, hash) {
|
||||||
logging.log(uuid + " cache: updating timestamp");
|
logging.log(uuid + " cache: updating timestamp");
|
||||||
var time = new Date().getTime();
|
var time = new Date().getTime();
|
||||||
|
// store uuid in lower case if not null
|
||||||
|
uuid = uuid && uuid.toLowerCase();
|
||||||
redis.hmset(uuid, "t", time);
|
redis.hmset(uuid, "t", time);
|
||||||
update_file_date(hash);
|
update_file_date(hash);
|
||||||
};
|
};
|
||||||
@ -67,6 +69,10 @@ exp.update_timestamp = function(uuid, hash) {
|
|||||||
exp.save_hash = function(uuid, hash) {
|
exp.save_hash = function(uuid, hash) {
|
||||||
logging.log(uuid + " cache: saving hash");
|
logging.log(uuid + " cache: saving hash");
|
||||||
var time = new Date().getTime();
|
var time = new Date().getTime();
|
||||||
|
// store shorter null byte instead of "null"
|
||||||
|
hash = hash || ".";
|
||||||
|
// store uuid in lower case if not null
|
||||||
|
uuid = uuid && uuid.toLowerCase();
|
||||||
redis.hmset(uuid, "h", hash, "t", time);
|
redis.hmset(uuid, "h", hash, "t", time);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -74,11 +80,13 @@ exp.save_hash = function(uuid, hash) {
|
|||||||
// {hash: "0123456789abcdef", time: 1414881524512}
|
// {hash: "0123456789abcdef", time: 1414881524512}
|
||||||
// null when uuid unkown
|
// null when uuid unkown
|
||||||
exp.get_details = function(uuid, callback) {
|
exp.get_details = function(uuid, callback) {
|
||||||
|
// get uuid in lower case if not null
|
||||||
|
uuid = uuid && uuid.toLowerCase();
|
||||||
redis.hgetall(uuid, function(err, data) {
|
redis.hgetall(uuid, function(err, data) {
|
||||||
var details = null;
|
var details = null;
|
||||||
if (data) {
|
if (data) {
|
||||||
details = {
|
details = {
|
||||||
hash: (data.h == "null" ? null : data.h),
|
hash: (data.h == "." ? null : data.h),
|
||||||
time: Number(data.t)
|
time: Number(data.t)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -112,10 +112,11 @@ exp.get_image_hash = function(uuid, callback) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
callback(err, -1, details && details.hash);
|
callback(err, -1, details && details.hash);
|
||||||
} else {
|
} else {
|
||||||
var oldhash = details && details.hash || "none";
|
// skin is only checked (3) when uuid known AND hash didn't change
|
||||||
logging.debug(uuid + " old hash: " + oldhash);
|
// in all other cases the skin is downloaded (2)
|
||||||
|
var status = details && (details.hash == hash) ? 3 : 2;
|
||||||
|
logging.debug(uuid + " old hash: " + (details && details.hash));
|
||||||
logging.log(uuid + " hash: " + hash);
|
logging.log(uuid + " hash: " + hash);
|
||||||
var status = hash == oldhash ? 3 : 2;
|
|
||||||
callback(null, status, hash);
|
callback(null, status, hash);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"author": "Jake0oo0",
|
"author": "Jake0oo0",
|
||||||
"description": "A NodeJS application to server Minecraft avatars.",
|
"description": "A Minecraft avatar service with support for avatars, skins, and even renders!",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
{
|
{
|
||||||
"name": "jomo"
|
"name": "jomo"
|
||||||
|
|||||||
@ -15,7 +15,7 @@ var human_status = {
|
|||||||
|
|
||||||
/* GET avatar request. */
|
/* GET avatar request. */
|
||||||
router.get("/:uuid.:ext?", function(req, res) {
|
router.get("/:uuid.:ext?", function(req, res) {
|
||||||
var uuid = req.params.uuid;
|
var uuid = (req.params.uuid || "");
|
||||||
var size = parseInt(req.query.size) || config.default_size;
|
var size = parseInt(req.query.size) || config.default_size;
|
||||||
var def = req.query.default;
|
var def = req.query.default;
|
||||||
var helm = req.query.hasOwnProperty("helm");
|
var helm = req.query.hasOwnProperty("helm");
|
||||||
@ -33,8 +33,8 @@ router.get("/:uuid.:ext?", function(req, res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip dashes, to lower case
|
// strip dashes
|
||||||
uuid = uuid.replace(/-/g, "").toLowerCase();
|
uuid = uuid.replace(/-/g, "");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) {
|
helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) {
|
||||||
|
|||||||
@ -8,7 +8,7 @@ var lwip = require("lwip");
|
|||||||
|
|
||||||
/* GET skin request. */
|
/* GET skin request. */
|
||||||
router.get("/:uuid.:ext?", function(req, res) {
|
router.get("/:uuid.:ext?", function(req, res) {
|
||||||
var uuid = req.params.uuid;
|
var uuid = (req.params.uuid || "");
|
||||||
var def = req.query.default;
|
var def = req.query.default;
|
||||||
var start = new Date();
|
var start = new Date();
|
||||||
var etag = null;
|
var etag = null;
|
||||||
@ -18,8 +18,8 @@ router.get("/:uuid.:ext?", function(req, res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip dashes, to lower case
|
// strip dashes
|
||||||
uuid = uuid.replace(/-/g, "").toLowerCase();
|
uuid = uuid.replace(/-/g, "");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
helpers.get_skin(uuid, function(err, hash, image) {
|
helpers.get_skin(uuid, function(err, hash, image) {
|
||||||
|
|||||||
234
test/test.js
234
test/test.js
@ -16,12 +16,18 @@ config.http_timeout *= 3;
|
|||||||
logging.log = function(){};
|
logging.log = function(){};
|
||||||
|
|
||||||
var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
|
var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
|
||||||
var usernames = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
|
var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
|
||||||
// Get a random UUID + username in order to prevent rate limiting
|
|
||||||
|
// Get a random UUID + name in order to prevent rate limiting
|
||||||
var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
|
var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
|
||||||
console.log("using uuid '" + uuid + "'");
|
var name = names[Math.round(Math.random() * (names.length - 1))];
|
||||||
var username = usernames[Math.round(Math.random() * (usernames.length - 1))];
|
|
||||||
console.log("using username '" + username + "'");
|
var ids = [
|
||||||
|
uuid.toLowerCase(),
|
||||||
|
uuid.toUpperCase(),
|
||||||
|
name.toLowerCase(),
|
||||||
|
name.toUpperCase()
|
||||||
|
];
|
||||||
|
|
||||||
describe("Crafatar", function() {
|
describe("Crafatar", function() {
|
||||||
// we might have to make 2 HTTP requests
|
// we might have to make 2 HTTP requests
|
||||||
@ -32,43 +38,43 @@ describe("Crafatar", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("UUID/username", function() {
|
describe("UUID/username", function() {
|
||||||
it("should be an invalid uuid", function(done) {
|
it("non-hex uuid is invalid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
|
assert.strictEqual(helpers.uuid_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be an invalid uuid", function(done) {
|
it("empty id is invalid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid(""), false);
|
assert.strictEqual(helpers.uuid_valid(""), false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be an invalid username", function(done) {
|
it("non-alphanumeric username is invalid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("usernäme"), false);
|
assert.strictEqual(helpers.uuid_valid("usernäme"), false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be an invalid username", function(done) {
|
it("dashed username is invalid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("user-name"), false);
|
assert.strictEqual(helpers.uuid_valid("user-name"), false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be an invalid username", function(done) {
|
it(">16 length username is invalid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("ThisNameIsTooLong"), false);
|
assert.strictEqual(helpers.uuid_valid("ThisNameIsTooLong"), false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be a valid uuid", function(done) {
|
it("lowercase uuid is valid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
|
assert.strictEqual(helpers.uuid_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be a valid uuid", function(done) {
|
it("uppercase uuid is valid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
|
assert.strictEqual(helpers.uuid_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be a valid uuid", function(done) {
|
it("dashed uuid is valid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
|
assert.strictEqual(helpers.uuid_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be a valid username", function(done) {
|
it("16 chars, underscored, capital, numbered username is valid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("__niceUs3rname__"), true);
|
assert.strictEqual(helpers.uuid_valid("__niceUs3rname__"), true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
it("should be a valid username", function(done) {
|
it("1 char username is valid", function(done) {
|
||||||
assert.strictEqual(helpers.uuid_valid("a"), true);
|
assert.strictEqual(helpers.uuid_valid("a"), true);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -86,128 +92,28 @@ describe("Crafatar", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Networking: Avatar", function() {
|
describe("Avatar", function() {
|
||||||
it("should be downloaded (uuid)", function(done) {
|
// profile "Alex" - hoping it'll never have a skin
|
||||||
helpers.get_avatar(uuid, false, 160, function(err, status, image) {
|
var alex_uuid = "ec561538f3fd461daff5086b22154bce";
|
||||||
assert.strictEqual(status, 2);
|
// profile "Steven" (Steve doesn't exist) - hoping it'll never have a skin
|
||||||
done();
|
var steven_uuid = "b8ffc3d37dbf48278f69475f6690aabd";
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should be cached (uuid)", function(done) {
|
|
||||||
helpers.get_avatar(uuid, false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(status === 0 || status === 1, true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
/* We can't test this because of mojang's rate limits :(
|
|
||||||
it("should be checked (uuid)", function(done) {
|
|
||||||
var original_cache_time = config.local_cache_time;
|
|
||||||
config.local_cache_time = 0;
|
|
||||||
helpers.get_avatar(uuid, false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(status, 3);
|
|
||||||
config.local_cache_time = original_cache_time;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
it("should be downloaded (username)", function(done) {
|
|
||||||
helpers.get_avatar(username, false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(status, 2);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should be cached (username)", function(done) {
|
|
||||||
helpers.get_avatar(username, false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(status === 0 || status === 1, true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should be checked (username)", function(done) {
|
|
||||||
var original_cache_time = config.local_cache_time;
|
|
||||||
config.local_cache_time = 0;
|
|
||||||
helpers.get_avatar(username, false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(status, 3);
|
|
||||||
config.local_cache_time = original_cache_time;
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should not exist (but account does)", function(done) {
|
|
||||||
// profile "Alex"
|
|
||||||
helpers.get_avatar("ec561538f3fd461daff5086b22154bce", false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(status, 2);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should default to Alex", function(done) {
|
|
||||||
assert.strictEqual(skins.default_skin("ec561538f3fd461daff5086b22154bce"), "alex");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
it("should default to Steve", function(done) {
|
|
||||||
assert.strictEqual(skins.default_skin("b8ffc3d37dbf48278f69475f6690aabd"), "steve");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Networking: Skin", function() {
|
it("uuid's account should exist, but skin should not", function(done) {
|
||||||
it("should not fail (uuid)", function(done) {
|
helpers.get_avatar(alex_uuid, false, 160, function(err, status, image) {
|
||||||
helpers.get_skin(uuid, function(err, hash, img) {
|
assert.strictEqual(status, 2);
|
||||||
assert.strictEqual(err, null);
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("should not fail (username)", function(done) {
|
it("odd UUID should default to Alex", function(done) {
|
||||||
helpers.get_skin(username, function(err, hash, img) {
|
assert.strictEqual(skins.default_skin(alex_uuid), "alex");
|
||||||
assert.strictEqual(err, null);
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
it("even UUID should default to Steve", function(done) {
|
||||||
});
|
assert.strictEqual(skins.default_skin(steven_uuid), "steve");
|
||||||
describe("Networking: Renders", function() {
|
|
||||||
describe("Head", function() {
|
|
||||||
it("should not fail (username)", function(done) {
|
|
||||||
helpers.get_render(username, 6, true, false, function(err, status, hash, img) {
|
|
||||||
assert.strictEqual(err, null);
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("should not fail (uuid)", function(done) {
|
|
||||||
helpers.get_render(username, 6, true, false, function(err, status, hash, img) {
|
|
||||||
assert.strictEqual(err, null);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe("Body", function() {
|
|
||||||
it("should not fail (username)", function(done) {
|
|
||||||
helpers.get_render(username, 6, true, true, function(err, status, hash, img) {
|
|
||||||
assert.strictEqual(err, null);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should not fail (uuid)", function(done) {
|
|
||||||
helpers.get_render(username, 6, true, true, function(err, status, hash, img) {
|
|
||||||
assert.strictEqual(err, null);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe("Errors", function() {
|
describe("Errors", function() {
|
||||||
before(function() {
|
|
||||||
cache.get_redis().flushall();
|
|
||||||
});
|
|
||||||
it("should be rate limited (uuid)", function(done) {
|
|
||||||
helpers.get_avatar(uuid, false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(JSON.parse(err).error, "TooManyRequestsException");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should NOT be rate limited (username)", function(done) {
|
|
||||||
helpers.get_avatar(username, false, 160, function(err, status, image) {
|
|
||||||
assert.strictEqual(err, null);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should time out on uuid info download", function(done) {
|
it("should time out on uuid info download", function(done) {
|
||||||
var original_timeout = config.http_timeout;
|
var original_timeout = config.http_timeout;
|
||||||
config.http_timeout = 1;
|
config.http_timeout = 1;
|
||||||
@ -243,11 +149,83 @@ describe("Crafatar", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("should handle file updates on invalid files", function(done) {
|
it("should ignore file updates on invalid files", function(done) {
|
||||||
assert.doesNotThrow(function() {
|
assert.doesNotThrow(function() {
|
||||||
cache.update_timestamp("0123456789abcdef0123456789abcdef", "invalid-file.png");
|
cache.update_timestamp("0123456789abcdef0123456789abcdef", "invalid-file.png");
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// DRY with uuid and username tests
|
||||||
|
for (var i in ids) {
|
||||||
|
var id = ids[i];
|
||||||
|
var id_type = id.length > 16 ? "uuid" : "name";
|
||||||
|
// needs an anonymous function because id and id_type aren't constant
|
||||||
|
(function(id, id_type) {
|
||||||
|
describe("Networking: Avatar", function() {
|
||||||
|
before(function() {
|
||||||
|
cache.get_redis().flushall();
|
||||||
|
console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should be downloaded", function(done) {
|
||||||
|
helpers.get_avatar(id, false, 160, function(err, status, image) {
|
||||||
|
assert.strictEqual(status, 2);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("should be cached", function(done) {
|
||||||
|
helpers.get_avatar(id, false, 160, function(err, status, image) {
|
||||||
|
assert.strictEqual(status === 0 || status === 1, true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
if (id.length > 16) {
|
||||||
|
console.log("can't run 'checked' test due to Mojang's rate limits :(");
|
||||||
|
} else {
|
||||||
|
it("should be checked", function(done) {
|
||||||
|
var original_cache_time = config.local_cache_time;
|
||||||
|
config.local_cache_time = 0;
|
||||||
|
helpers.get_avatar(id, false, 160, function(err, status, image) {
|
||||||
|
assert.strictEqual(status, 3);
|
||||||
|
config.local_cache_time = original_cache_time;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Networking: Skin", function() {
|
||||||
|
it("should not fail (uuid)", function(done) {
|
||||||
|
helpers.get_skin(id, function(err, hash, img) {
|
||||||
|
assert.strictEqual(err, null);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Errors", function() {
|
||||||
|
before(function() {
|
||||||
|
cache.get_redis().flushall();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (id_type == "uuid") {
|
||||||
|
it("uuid should be rate limited", function(done) {
|
||||||
|
helpers.get_avatar(id, false, 160, function(err, status, image) {
|
||||||
|
assert.strictEqual(JSON.parse(err).error, "TooManyRequestsException");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
it("username should NOT be rate limited (username)", function(done) {
|
||||||
|
helpers.get_avatar(id, false, 160, function(err, status, image) {
|
||||||
|
assert.strictEqual(err, null);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})(id, id_type);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user