-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up request-handler, github-auth, and analytics; upgrade to Mocha 4 #1142
Clean up request-handler, github-auth, and analytics; upgrade to Mocha 4 #1142
Conversation
- Rename data -> queryParams for readability - Factor out duplicated code - Remove spurious return - Better encapsulate analytics
Upgrade to Mocha 4 which does not forcibly quit the process when tests complete mochajs/mocha#3044
# Conflicts: # package.json
@RedSparr0w I would really like to get this merged. Would you be up for reviewing it? Feel free to ask if any of it is unclear or if you have questions. |
Had a look over it, only noticed 1 thing (will add a note), other than that all looks good to me. |
@@ -109,7 +99,8 @@ function handleRequest (vendorRequestHandler) { | |||
} | |||
options.headers = options.headers || {}; | |||
options.headers['User-Agent'] = options.headers['User-Agent'] || 'Shields.io'; | |||
return request(options, function(err, res, body) { | |||
|
|||
request(options, (err, res, body) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
request(apiUrl, function(err, res, body) {
if (err != null) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
return;
}
try {
...
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'malformed';
sendBadge(format, badgeData);
}
}).on('error', function(e) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
});
It seems to me the .on('error', ...
blocks for request
's streaming API should simply be removed. request
also provides response errors to the callback, and the callbacks are handling them.
@@ -14,16 +14,32 @@ if (process.env.REDISTOGO_URL) { | |||
} | |||
|
|||
let analytics = {}; | |||
let autosaveIntervalId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Call this autoSaveIntervalId
(capital S
) for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed everything to lowercase. AFAIC autosave is one word. Changed autoLoad
to load
which seems to describe it better.
lib/analytics.js
Outdated
setInterval(function analyticsAutoSave() { | ||
|
||
function performAutosave() { | ||
const contents = JSON.stringify(analytics); | ||
if (useRedis) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this should use the adapter pattern rather than hard-coding Redis vs plain text file. That could definitely be refactored separately though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, agreed. We're not even using Redis in production so I don't know if this code works. We could remove it, though it would make more sense to get some tests around it, since it makes our hosting portable to Heroku for people who want to self-host. Definitely agreed it would be nice to refactor it.
}, analyticsAutoSavePeriod); | ||
} | ||
|
||
function scheduleAutosaving() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be consistent about autosave
(lowercase S) vs autoSave
(capital S) in this file.
lib/analytics.js
Outdated
} | ||
|
||
function setRoutes (server) { | ||
server.ajax.on('analytics/v1', function(json, end) { end(analytics); }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arrow function?
server.ajax.on('analytics/v1', (json, end) => { end(analytics); });
@@ -109,7 +99,8 @@ function handleRequest (vendorRequestHandler) { | |||
} | |||
options.headers = options.headers || {}; | |||
options.headers['User-Agent'] = options.headers['User-Agent'] || 'Shields.io'; | |||
return request(options, function(err, res, body) { | |||
|
|||
request(options, (err, res, body) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
'/testing/123.svg?foo=2' | ||
).then(() => { assert.equal(handlerCallCount, 1); }); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I like this.
Looks pretty nice. Just left some minor comments inline. |
Thanks |
setInterval
code. This requires the ability to cancel autosaving.