-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Validate serverURL on Start #4204
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
Conversation
src/ParseServer.js
Outdated
@@ -373,6 +373,7 @@ class ParseServer { | |||
if (process.env.PARSE_SERVER_ENABLE_EXPERIMENTAL_DIRECT_ACCESS === '1') { | |||
Parse.CoreManager.setRESTController(ParseServerRESTController(appId, appRouter)); | |||
} | |||
this.verifyServerUrl(_ref2.publicServerURL); |
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.
_ref2 ?
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.
Also? You probably need to add a delay, at least a process.nextTick as the app is not mounted/ not listening yet on the port.
Not sure if we can get that event from Express, i’ll Have a look!
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.
That would be ideal to have it execute after an event! As for _ref2
, yeah that's gonna get changed before this can be seriously considered, I can't remember where I pulled that from...
Beyond that is this acceptable to put in ParseServer.js or do you think it would be more appropriate elsewhere?
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.
That’s an excellent place to put it, makes total sense. About the event, I may have been mistaken, perhaps we should wait for the ‘mount’ event, not sure if we can have access to the server that listens in the end. Best chance may be a check after 2s no?
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 would have to catch when express was listening, but that object is a bit high up. We could do a timing check but I wish we could just get the event. I'll look around a bit more and see if there's anything else, if not we can always do the delay.
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 do think we can just listen on the api constructed there, it's later returned, and comes back to be listened under whatever the mount path is, under another express instance. I believe when the first one has listen
called on it, it will chain down, and we'll get our listening callback as expected; without being too far ahead.
Looks like it's used in ParseWebSocketServer as such,
...
wss.on('listening', () => {
logger.info('Parse LiveQuery Server starts running');
});
Perhaps check the listening event: https://nodejs.org/api/net.html#net_event_listening Should be available/ fired on the app :) app.on(‘listening’, () => { verifyServerURL() }) |
The event sounds much better than sleeping/waiting for a bit, I'll rework this to fire from that, and address that improper var reference. |
Doh! I wrote in |
src/ParseServer.js
Outdated
this.verifyServerUrl(_ref2.publicServerURL); | ||
|
||
const AppCache = require('./cache'); | ||
var serverUrl = AppCache.AppCache.cache[appId]['value']['publicServerURL']; |
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.
or just Parse.serverURL
;)
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.
That would be much better
src/ParseServer.js
Outdated
setTimeout(function() { | ||
if(serverUrl) { | ||
const request = require('request'); | ||
request(serverUrl.replace(/\/$/, "") + "/health", function (error, response, 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.
you could use Parse._request probably this way you don't even need those replacements.
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.
And I'll look to see if that'll swap out ok.
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.
a) love this. thank you
b) ugh, I've become THAT guy™
spec/ParseServer.spec.js
Outdated
@@ -0,0 +1,32 @@ | |||
"use strict"; |
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.
Please use ' instead of " there are a few places.
spec/ParseServer.spec.js
Outdated
/* Tests for ParseServer.js */ | ||
var express = require('express'); | ||
|
||
import ParseServer from '../src/ParseServer'; |
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.
remove extra white space
spec/ParseServer.spec.js
Outdated
import ParseServer from '../src/ParseServer'; | ||
|
||
var app = express(); | ||
//app.use(bodyParser.json({ 'type': '*/*' })); |
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.
remove commented out code.
spec/ParseServer.spec.js
Outdated
it("validate good publicServerUrl", (done) => { | ||
ParseServer.verifyServerUrl('http://localhost:12345', function(result) { | ||
if(!result) { | ||
jfail('Did not pass valid url'); |
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.
using jfail is fine. we've moved to using done.fail() just for future reference.
src/ParseServer.js
Outdated
@@ -373,6 +373,11 @@ class ParseServer { | |||
if (process.env.PARSE_SERVER_ENABLE_EXPERIMENTAL_DIRECT_ACCESS === '1') { | |||
Parse.CoreManager.setRESTController(ParseServerRESTController(appId, appRouter)); | |||
} | |||
|
|||
const AppCache = require('./cache'); |
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.
requires at the top please.
src/ParseServer.js
Outdated
request(serverUrl.replace(/\/$/, "") + "/health", function (error, response, body) { | ||
if (error || response.statusCode !== 200 || body !== "OK") { | ||
// eslint-disable-next-line | ||
console.warn("\nWARNING, Unable to connect to publicServerURL '" + serverUrl + "'. Cloud code and push notifications may be unavailable!\n"); |
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.
use template
`${a-var} blah`
break the code for the comment onto two lines (not the comment).
console.warn(`\nWARNING, Unable to ... \'${serverUrl}\'. Cloud code and ....` +
'blah blah blah');
remove the // eslint-disable
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.
Totally valid nits. I've made the changes locally but I'm still fiddling with something else before I push changes up.
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.
@acinader as for the disable on lint, console statements don't look to be allowed. Ending up with Unexpected console statement no-console
. If you take a look you'll see that others have run into the same issue and ignored their console calls as well.
If there's some alternative approach I'm not aware of let me know, but that seems about right.
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.
Following up on that something like /* eslint-disable no-console */
may be more appropriate than just a flat out disable.
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.
ah, yeah. the logger isn't available here. so yes, adding the error your targeting (no-console) is the right call see around line 225 where console is used.
Codecov Report
@@ Coverage Diff @@
## master #4204 +/- ##
==========================================
- Coverage 92.22% 92.21% -0.02%
==========================================
Files 116 116
Lines 8092 8103 +11
==========================================
+ Hits 7463 7472 +9
- Misses 629 631 +2
Continue to review full report at Codecov.
|
Just need to adjust the port I'm listening to for testing. I'm verifying the entire suite locally before I push this up again. |
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.
lgtm
https://hackernoon.com/why-you-shouldnt-use-var-anymore-f109a58b9b70
is a good write-up on why to use let (and ideally const) instead of var.
Thanks @acinader , you'll have to forgive me I'm a bit rusty with my js on node at the moment. Using let/const makes sense over var. I'll go back over those and make the appropriate changes. @flovilmart I believe things are almost good on my end here (minus the var changes I'll be putting up) . Let me know if there's anything in particular that still requires attention. As for the failure I'm seeing quite a few of these 'open connection...' errors. At first I thought it had something to do with my using |
…ication call into app.on('mount') callback, removed setTimeout from verification.
So I believe I have found the issue, but we'll see for sure once these tests are done. It appears that running the url verification actually breaks quite a few tests, but only if the connection that is made is still open when a given tests completes, particularly those that use To fix this I changed a couple points, one to move the verification call under a conditional that is only executed when not in a testing environment, and two to wrap said call within a Although the parent is not listening yet, it is assumed it will be before we have the chance to time out (likely to be milliseconds behind). Checking this locally it looks to work just fine. If for some reason it misses this won't cause anything to not run, just would give an eroneous warning. If that ends up being the case in some production instances we could always safely modify this further to be a bit more patient in waiting for a response post-mount. Those changes looks like so: // run the following when not testing
if (!process.env.TESTING) {
...
...
...
// verify the server url after a 'mount' event is received
api.on('mount', function() {
ParseServer.verifyServerUrl();
});
} Since the verification is called separately during testing it is still covered, but now it's out of the way of interfering with the expected behavior of other tests. The only thing not covered being the bit that's inside the conditional above of course, I'm thinking the coverage diff will still be a positive. Also changed a couple declarations using ::edit:: |
Specifically this adds validation for
publicServerUrl
, if the value is present. This will attempt to grab the/health
endpoint, expecting a 200 code and OK body, if it doesn't receive this it will spit out something like.This does not add tests yet as I wanted to field the positioning (and wording) of this before I proceed. There may be other systems that would be affected besides cloud code and push.
If this looks ok as is I'll rework it so we can get test coverage on this.