This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
NodeDomain #6193
Merged
Merged
NodeDomain #6193
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8349821
Add NodeDomain wrapper and refactor StaticServer to use it.
e320244
Fix a race condition that arose with concurrent requests for the same…
93b1187
Respond to review comments
5adeb59
Fix `_requests` JSDoc
iwehrman 434b0db
Use one return instead of many.
iwehrman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,10 +43,10 @@ define(function (require, exports, module) { | |
* baseUrl - Optional base URL (populated by the current project) | ||
* pathResolver - Function to covert absolute native paths to project relative paths | ||
* root - Native path to the project root (and base URL) | ||
* nodeConnection - An active NodeConnection | ||
* nodeDomain - An initialized NodeDomain | ||
*/ | ||
function StaticServer(config) { | ||
this._nodeConnection = config.nodeConnection; | ||
this._nodeDomain = config.nodeDomain; | ||
this._onRequestFilter = this._onRequestFilter.bind(this); | ||
|
||
BaseServer.call(this, config); | ||
|
@@ -61,7 +61,7 @@ define(function (require, exports, module) { | |
* @return {boolean} true for yes, otherwise false. | ||
*/ | ||
StaticServer.prototype.canServe = function (localPath) { | ||
if (!this._nodeConnection.connected()) { | ||
if (!this._nodeDomain.ready()) { | ||
return false; | ||
} | ||
|
||
|
@@ -87,17 +87,9 @@ define(function (require, exports, module) { | |
* @return {jQuery.Promise} Resolved by the StaticServer domain when the message is acknowledged. | ||
*/ | ||
StaticServer.prototype._updateRequestFilterPaths = function () { | ||
if (!this._nodeConnection.connected()) { | ||
return; | ||
} | ||
|
||
var paths = []; | ||
|
||
Object.keys(this._liveDocuments).forEach(function (path) { | ||
paths.push(path); | ||
}); | ||
var paths = Object.keys(this._liveDocuments); | ||
|
||
return this._nodeConnection.domains.staticServer.setRequestFilterPaths(this._root, paths); | ||
return this._nodeDomain.exec("setRequestFilterPaths", this._root, paths); | ||
}; | ||
|
||
/** | ||
|
@@ -109,37 +101,14 @@ define(function (require, exports, module) { | |
* the server is ready/failed. | ||
*/ | ||
StaticServer.prototype.readyToServe = function () { | ||
var readyToServeDeferred = $.Deferred(), | ||
self = this; | ||
|
||
if (this._nodeConnection.connected()) { | ||
this._nodeConnection.domains.staticServer.getServer(self._root).done(function (address) { | ||
var self = this; | ||
return this._nodeDomain.exec("getServer", self._root) | ||
.done(function (address) { | ||
self._baseUrl = "http://" + address.address + ":" + address.port + "/"; | ||
readyToServeDeferred.resolve(); | ||
}).fail(function () { | ||
}) | ||
.fail(function () { | ||
self._baseUrl = ""; | ||
readyToServeDeferred.reject(); | ||
}); | ||
} else { | ||
// nodeConnection has been connected once (because the deferred | ||
// resolved, but is not currently connected). | ||
// | ||
// If we are in this case, then the node process has crashed | ||
// and is in the process of restarting. Once that happens, the | ||
// node connection will automatically reconnect and reload the | ||
// domain. Unfortunately, we don't have any promise to wait on | ||
// to know when that happens. The best we can do is reject this | ||
// readyToServe so that the user gets an error message to try | ||
// again later. | ||
// | ||
// The user will get the error immediately in this state, and | ||
// the new node process should start up in a matter of seconds | ||
// (assuming there isn't a more widespread error). So, asking | ||
// them to retry in a second is reasonable. | ||
readyToServeDeferred.reject(); | ||
} | ||
|
||
return readyToServeDeferred.promise(); | ||
}; | ||
|
||
/** | ||
|
@@ -181,26 +150,27 @@ define(function (require, exports, module) { | |
* Send HTTP response data back to the StaticServerSomain | ||
*/ | ||
StaticServer.prototype._send = function (location, response) { | ||
if (this._nodeConnection.connected()) { | ||
this._nodeConnection.domains.staticServer.writeFilteredResponse(location.root, location.pathname, response); | ||
} | ||
this._nodeDomain.exec("writeFilteredResponse", location.root, location.pathname, response); | ||
}; | ||
|
||
/** | ||
* @private | ||
* Event handler for StaticServerDomain requestFilter event | ||
* @param {jQuery.Event} event | ||
* @param {{hostname: string, pathname: string, port: number, root: string}} request | ||
* @param {{hostname: string, pathname: string, port: number, root: string, id: number}} request | ||
*/ | ||
StaticServer.prototype._onRequestFilter = function (event, request) { | ||
var key = request.location.pathname, | ||
liveDocument = this._liveDocuments[key], | ||
response = null; | ||
|
||
response; | ||
// send instrumented response or null to fallback to static file | ||
if (liveDocument && liveDocument.getResponseData) { | ||
response = liveDocument.getResponseData(); | ||
} else { | ||
response = {}; | ||
} | ||
response.id = request.id; | ||
|
||
this._send(request.location, response); | ||
}; | ||
|
@@ -209,15 +179,15 @@ define(function (require, exports, module) { | |
* See BaseServer#start. Starts listenting to StaticServerDomain events. | ||
*/ | ||
StaticServer.prototype.start = function () { | ||
$(this._nodeConnection).on("staticServer.requestFilter", this._onRequestFilter); | ||
$(this._nodeDomain).on("requestFilter", this._onRequestFilter); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better. The domainevent notation is a bit confusing when you're used to namespaces. |
||
}; | ||
|
||
/** | ||
* See BaseServer#stop. Remove event handlers from StaticServerDomain. | ||
*/ | ||
StaticServer.prototype.stop = function () { | ||
$(this._nodeConnection).off("staticServer.requestFilter", this._onRequestFilter); | ||
$(this._nodeDomain).off("requestFilter", this._onRequestFilter); | ||
}; | ||
|
||
exports.StaticServer = StaticServer; | ||
module.exports = StaticServer; | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 actually like the old way better where the command is a method on the domain. Is there a reason we can't do this with
NodeDomain
?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.
The problem is that I wanted to make a wrapper that doesn't require you to wait on a promise before you start firing off commands. But we have to wait until the domain is loaded before we know what the commands are. If we do it this way, we can just wait for the connection to come up and the domains to be loaded before rejecting the exec promise if the command isn't there. In a previous version, I actually went ahead and bound all the loaded commands to the domain object itself at domain load time, but then if you tried to execute a command before the domain had been loaded you'd get a
TypeError
. I also tried to find a way to override arbitrary property lookups for an object, but there doesn't seem to be a way to do this in ES5. I also would prefer to not have to pass strings around, but I personally think it's mostly just a style issue. (It's not like a static analysis could ever correctly predict the the properties on these domains anyway for the sake of, say, code hinting.)I only added the
promise
andready
methods for backwards compatibility with the basic structure ofStaticServer
. I don't consider them to really be core features ofNodeDomain
.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 points. Thanks for the clarification. I think you've made the right trade offs.