From e726fb9d5fd800f8c720d86cab0abf8e165524c8 Mon Sep 17 00:00:00 2001 From: jomo Date: Mon, 8 Dec 2014 21:57:37 +0100 Subject: [PATCH 1/9] convert uuid params to lower case, store only lower case uuids --- modules/cache.js | 2 ++ routes/avatars.js | 2 +- routes/skins.js | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/cache.js b/modules/cache.js index 39f9aff..5fb045f 100644 --- a/modules/cache.js +++ b/modules/cache.js @@ -67,6 +67,8 @@ exp.update_timestamp = function(uuid, hash) { exp.save_hash = function(uuid, hash) { logging.log(uuid + " cache: saving hash"); var time = new Date().getTime(); + // store uuid in lower case if not null + uuid = uuid && uuid.toLowerCase(); redis.hmset(uuid, "h", hash, "t", time); }; diff --git a/routes/avatars.js b/routes/avatars.js index 241e046..93f0d65 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -15,7 +15,7 @@ var human_status = { /* GET avatar request. */ router.get("/:uuid.:ext?", function(req, res) { - var uuid = req.params.uuid; + var uuid = (req.params.uuid || "").toLowerCase(); var size = parseInt(req.query.size) || config.default_size; var def = req.query.default; var helm = req.query.hasOwnProperty("helm"); diff --git a/routes/skins.js b/routes/skins.js index 76f6340..1680cfa 100644 --- a/routes/skins.js +++ b/routes/skins.js @@ -8,7 +8,7 @@ var lwip = require("lwip"); /* GET skin request. */ router.get("/:uuid.:ext?", function(req, res) { - var uuid = req.params.uuid; + var uuid = (req.params.uuid || "").toLowerCase(); var def = req.query.default; var start = new Date(); var etag = null; From 04c167f5aad57d20f8e27dfa06dd75647969864b Mon Sep 17 00:00:00 2001 From: jomo Date: Mon, 8 Dec 2014 21:59:21 +0100 Subject: [PATCH 2/9] fix false 'downloaded' status being returned when null hash is 'checked' --- modules/helpers.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/helpers.js b/modules/helpers.js index f97f77b..5bd4926 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -111,10 +111,11 @@ exp.get_image_hash = function(uuid, callback) { if (err) { callback(err, -1, details && details.hash); } else { - var oldhash = details && details.hash || "none"; - logging.debug(uuid + " old hash: " + oldhash); + // skin is only checked (3) when uuid known AND hash didn't change + // 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); - var status = hash == oldhash ? 3 : 2; callback(null, status, hash); } }); From 0c73d39c79f2fa0dc730901f6bcb27ca93bf5273 Mon Sep 17 00:00:00 2001 From: jomo Date: Mon, 8 Dec 2014 22:38:06 +0100 Subject: [PATCH 3/9] high scalability!!! using '.' instead of 'null' string --- modules/cache.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/cache.js b/modules/cache.js index 5fb045f..1537c71 100644 --- a/modules/cache.js +++ b/modules/cache.js @@ -67,6 +67,8 @@ exp.update_timestamp = function(uuid, hash) { exp.save_hash = function(uuid, hash) { logging.log(uuid + " cache: saving hash"); 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); @@ -80,7 +82,7 @@ exp.get_details = function(uuid, callback) { var details = null; if (data) { details = { - hash: (data.h == "null" ? null : data.h), + hash: (data.h == "." ? null : data.h), time: Number(data.t) }; } From c8d28ed2df94cdf97f4154bfe0cda2a596cb64e3 Mon Sep 17 00:00:00 2001 From: jomo Date: Mon, 8 Dec 2014 22:39:38 +0100 Subject: [PATCH 4/9] Revert "Lower case UUIDs - skins/avatars" I already commited this ;) This reverts commit e4bdecfbb756f38a85310a205a5693b7c5055f1b. --- routes/avatars.js | 4 ++-- routes/skins.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/routes/avatars.js b/routes/avatars.js index b0b2f3a..93f0d65 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -33,8 +33,8 @@ router.get("/:uuid.:ext?", function(req, res) { return; } - // strip dashes, to lower case - uuid = uuid.replace(/-/g, "").toLowerCase(); + // strip dashes + uuid = uuid.replace(/-/g, ""); try { helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) { diff --git a/routes/skins.js b/routes/skins.js index 7ac2c91..1680cfa 100644 --- a/routes/skins.js +++ b/routes/skins.js @@ -18,8 +18,8 @@ router.get("/:uuid.:ext?", function(req, res) { return; } - // strip dashes, to lower case - uuid = uuid.replace(/-/g, "").toLowerCase(); + // strip dashes + uuid = uuid.replace(/-/g, ""); try { helpers.get_skin(uuid, function(err, hash, image) { From 9faeedc03f19a0b784430ed2b2259e85db3b6d3d Mon Sep 17 00:00:00 2001 From: jomo Date: Wed, 10 Dec 2014 18:42:19 +0100 Subject: [PATCH 5/9] convert keys to lower case at cache level --- modules/cache.js | 4 ++++ routes/avatars.js | 2 +- routes/skins.js | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/cache.js b/modules/cache.js index 1537c71..885c078 100644 --- a/modules/cache.js +++ b/modules/cache.js @@ -59,6 +59,8 @@ exp.get_redis = function() { exp.update_timestamp = function(uuid, hash) { logging.log(uuid + " cache: updating timestamp"); var time = new Date().getTime(); + // store uuid in lower case if not null + uuid = uuid && uuid.toLowerCase(); redis.hmset(uuid, "t", time); update_file_date(hash); }; @@ -78,6 +80,8 @@ exp.save_hash = function(uuid, hash) { // {hash: "0123456789abcdef", time: 1414881524512} // null when uuid unkown exp.get_details = function(uuid, callback) { + // get uuid in lower case if not null + uuid = uuid && uuid.toLowerCase(); redis.hgetall(uuid, function(err, data) { var details = null; if (data) { diff --git a/routes/avatars.js b/routes/avatars.js index 93f0d65..6537b59 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -15,7 +15,7 @@ var human_status = { /* GET avatar request. */ router.get("/:uuid.:ext?", function(req, res) { - var uuid = (req.params.uuid || "").toLowerCase(); + var uuid = (req.params.uuid || ""); var size = parseInt(req.query.size) || config.default_size; var def = req.query.default; var helm = req.query.hasOwnProperty("helm"); diff --git a/routes/skins.js b/routes/skins.js index 1680cfa..4493847 100644 --- a/routes/skins.js +++ b/routes/skins.js @@ -8,7 +8,7 @@ var lwip = require("lwip"); /* GET skin request. */ router.get("/:uuid.:ext?", function(req, res) { - var uuid = (req.params.uuid || "").toLowerCase(); + var uuid = (req.params.uuid || ""); var def = req.query.default; var start = new Date(); var etag = null; From 9aede69c9d63e81624261deb3e3843cd986a9244 Mon Sep 17 00:00:00 2001 From: jomo Date: Wed, 10 Dec 2014 21:25:52 +0100 Subject: [PATCH 6/9] clean up tests - renamed a bunch of tests to be more descriptive - added tests for lower/upper case uuid/name - looping over the 4 IDs (see above) instead of redefining tests fixes #40 --- package.json | 2 +- test/test.js | 210 ++++++++++++++++++++++++++------------------------- 2 files changed, 110 insertions(+), 102 deletions(-) diff --git a/package.json b/package.json index f10a536..e2c8a7f 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ], "scripts": { "start": "node server.js", - "test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" + "test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R list && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "dependencies": { "body-parser": "~1.8.1", diff --git a/test/test.js b/test/test.js index 3070452..9b4fbb4 100644 --- a/test/test.js +++ b/test/test.js @@ -15,12 +15,18 @@ config.http_timeout *= 3; logging.log = function(){}; var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/); -var usernames = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/); -// Get a random UUID + username in order to prevent rate limiting +var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/); + +// Get a random UUID + name in order to prevent rate limiting var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))]; -console.log("using uuid '" + uuid + "'"); -var username = usernames[Math.round(Math.random() * (usernames.length - 1))]; -console.log("using username '" + username + "'"); +var name = names[Math.round(Math.random() * (names.length - 1))]; + +var ids = [ + uuid.toLowerCase(), + uuid.toUpperCase(), + name.toLowerCase(), + name.toUpperCase() +]; describe("Crafatar", function() { // we might have to make 2 HTTP requests @@ -31,43 +37,43 @@ describe("Crafatar", 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); done(); }); - it("should be an invalid uuid", function(done) { + it("empty id is invalid", function(done) { assert.strictEqual(helpers.uuid_valid(""), false); 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); done(); }); - it("should be an invalid username", function(done) { + it("dashed username is invalid", function(done) { assert.strictEqual(helpers.uuid_valid("user-name"), false); done(); }); - it("should be an invalid username", function(done) { + it(">16 length username is invalid", function(done) { assert.strictEqual(helpers.uuid_valid("ThisNameIsTooLong"), false); done(); }); - it("should be a valid uuid", function(done) { + it("lowercase uuid is valid", function(done) { assert.strictEqual(helpers.uuid_valid("0098cb60fa8e427cb299793cbd302c9a"), true); done(); }); - it("should be a valid uuid", function(done) { + it("uppercase uuid is valid", function(done) { assert.strictEqual(helpers.uuid_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true); 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); 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); done(); }); - it("should be a valid username", function(done) { + it("1 char username is valid", function(done) { assert.strictEqual(helpers.uuid_valid("a"), true); done(); }); @@ -85,99 +91,29 @@ describe("Crafatar", function() { }); }); - describe("Networking: Avatar", function() { - it("should be downloaded (uuid)", function(done) { - helpers.get_avatar(uuid, false, 160, function(err, status, image) { - assert.strictEqual(status, 2); - done(); - }); - }); - 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("Avatar", function() { + // profile "Alex" - hoping it'll never have a skin + var alex_uuid = "ec561538f3fd461daff5086b22154bce"; + // profile "Steven" (Steve doesn't exist) - hoping it'll never have a skin + var steven_uuid = "b8ffc3d37dbf48278f69475f6690aabd"; - describe("Networking: Skin", function() { - it("should not fail (uuid)", function(done) { - helpers.get_skin(uuid, function(err, hash, img) { - assert.strictEqual(err, null); + it("uuid's account should exist, but skin should not", function(done) { + helpers.get_avatar(alex_uuid, false, 160, function(err, status, image) { + assert.strictEqual(status, 2); done(); }); }); - it("should not fail (username)", function(done) { - helpers.get_skin(username, function(err, hash, img) { - assert.strictEqual(err, null); - done(); - }); + it("odd UUID should default to Alex", function(done) { + assert.strictEqual(skins.default_skin(alex_uuid), "alex"); + done(); + }); + it("even UUID should default to Steve", function(done) { + assert.strictEqual(skins.default_skin(steven_uuid), "steve"); + done(); }); }); 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) { var original_timeout = config.http_timeout; config.http_timeout = 1; @@ -213,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() { cache.update_timestamp("0123456789abcdef0123456789abcdef", "invalid-file.png"); }); 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); + } }); \ No newline at end of file From 9140f15f112d763379fa4daf8d57aed3f925571f Mon Sep 17 00:00:00 2001 From: jomo Date: Wed, 10 Dec 2014 22:17:57 +0100 Subject: [PATCH 7/9] I guess coveralls doesn't like the 'list' reporter --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e2c8a7f..f10a536 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ], "scripts": { "start": "node server.js", - "test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R list && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" + "test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "dependencies": { "body-parser": "~1.8.1", From b322ecab7b05fb04c670e0f87c5b09d81ed64c1f Mon Sep 17 00:00:00 2001 From: jomo Date: Thu, 11 Dec 2014 00:16:02 +0100 Subject: [PATCH 8/9] get rid of typo'd package description --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f10a536..f0e7f18 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.1", "private": true, "author": "Jake0oo0", - "description": "A NodeJS application to server Minecraft avatars.", + "description": "A Minecraft avatar service with support for avatars, skins, and even renders!", "contributors": [ { "name": "jomo" From 686b09eeb58e916ff01a851809ee225f431e3861 Mon Sep 17 00:00:00 2001 From: jomo Date: Fri, 26 Dec 2014 04:53:50 +0100 Subject: [PATCH 9/9] add twitter account to readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e51cf86..fc4ed8c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ Please [visit the website](https://crafatar.com) for details. ## Contact -You can [join us](https://webchat.esper.net/?channels=spongy) in #spongy on irc.esper.net. +* You can follow us on [![t](https://favicons.githubusercontent.com/twitter.com)@crafatar](https://twitter.com/crafatar) +* You can [join us](https://webchat.esper.net/?channels=spongy) in #spongy on irc.esper.net. ## Install