Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 8349821

Browse files
author
Ian Wehrman
committed
Add NodeDomain wrapper and refactor StaticServer to use it.
1 parent 39bac0b commit 8349821

6 files changed

Lines changed: 238 additions & 112 deletions

File tree

src/brackets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ define(function (require, exports, module) {
8181
Resizer = require("utils/Resizer"),
8282
LiveDevelopmentMain = require("LiveDevelopment/main"),
8383
NodeConnection = require("utils/NodeConnection"),
84+
NodeDomain = require("utils/NodeDomain"),
8485
ExtensionUtils = require("utils/ExtensionUtils"),
8586
DragAndDrop = require("utils/DragAndDrop"),
8687
ColorUtils = require("utils/ColorUtils"),

src/extensions/default/StaticServer/StaticServer.js

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ define(function (require, exports, module) {
4343
* baseUrl - Optional base URL (populated by the current project)
4444
* pathResolver - Function to covert absolute native paths to project relative paths
4545
* root - Native path to the project root (and base URL)
46-
* nodeConnection - An active NodeConnection
46+
* nodeDomain - An initialized NodeDomain
4747
*/
4848
function StaticServer(config) {
49-
this._nodeConnection = config.nodeConnection;
49+
this._nodeDomain = config.nodeDomain;
5050
this._onRequestFilter = this._onRequestFilter.bind(this);
5151

5252
BaseServer.call(this, config);
@@ -61,7 +61,7 @@ define(function (require, exports, module) {
6161
* @return {boolean} true for yes, otherwise false.
6262
*/
6363
StaticServer.prototype.canServe = function (localPath) {
64-
if (!this._nodeConnection.connected()) {
64+
if (!this._nodeDomain.ready()) {
6565
return false;
6666
}
6767

@@ -87,17 +87,9 @@ define(function (require, exports, module) {
8787
* @return {jQuery.Promise} Resolved by the StaticServer domain when the message is acknowledged.
8888
*/
8989
StaticServer.prototype._updateRequestFilterPaths = function () {
90-
if (!this._nodeConnection.connected()) {
91-
return;
92-
}
93-
94-
var paths = [];
90+
var paths = Object.keys(this._liveDocuments);
9591

96-
Object.keys(this._liveDocuments).forEach(function (path) {
97-
paths.push(path);
98-
});
99-
100-
return this._nodeConnection.domains.staticServer.setRequestFilterPaths(this._root, paths);
92+
return this._nodeDomain.exec("setRequestFilterPaths", this._root, paths);
10193
};
10294

10395
/**
@@ -109,37 +101,14 @@ define(function (require, exports, module) {
109101
* the server is ready/failed.
110102
*/
111103
StaticServer.prototype.readyToServe = function () {
112-
var readyToServeDeferred = $.Deferred(),
113-
self = this;
114-
115-
if (this._nodeConnection.connected()) {
116-
this._nodeConnection.domains.staticServer.getServer(self._root).done(function (address) {
104+
var self = this;
105+
return this._nodeDomain.exec("getServer", self._root)
106+
.done(function (address) {
117107
self._baseUrl = "http://" + address.address + ":" + address.port + "/";
118-
readyToServeDeferred.resolve();
119-
}).fail(function () {
108+
})
109+
.fail(function () {
120110
self._baseUrl = "";
121-
readyToServeDeferred.reject();
122111
});
123-
} else {
124-
// nodeConnection has been connected once (because the deferred
125-
// resolved, but is not currently connected).
126-
//
127-
// If we are in this case, then the node process has crashed
128-
// and is in the process of restarting. Once that happens, the
129-
// node connection will automatically reconnect and reload the
130-
// domain. Unfortunately, we don't have any promise to wait on
131-
// to know when that happens. The best we can do is reject this
132-
// readyToServe so that the user gets an error message to try
133-
// again later.
134-
//
135-
// The user will get the error immediately in this state, and
136-
// the new node process should start up in a matter of seconds
137-
// (assuming there isn't a more widespread error). So, asking
138-
// them to retry in a second is reasonable.
139-
readyToServeDeferred.reject();
140-
}
141-
142-
return readyToServeDeferred.promise();
143112
};
144113

145114
/**
@@ -181,9 +150,7 @@ define(function (require, exports, module) {
181150
* Send HTTP response data back to the StaticServerSomain
182151
*/
183152
StaticServer.prototype._send = function (location, response) {
184-
if (this._nodeConnection.connected()) {
185-
this._nodeConnection.domains.staticServer.writeFilteredResponse(location.root, location.pathname, response);
186-
}
153+
this._nodeDomain.exec("writeFilteredResponse", location.root, location.pathname, response);
187154
};
188155

189156
/**
@@ -209,15 +176,15 @@ define(function (require, exports, module) {
209176
* See BaseServer#start. Starts listenting to StaticServerDomain events.
210177
*/
211178
StaticServer.prototype.start = function () {
212-
$(this._nodeConnection).on("staticServer.requestFilter", this._onRequestFilter);
179+
$(this._nodeDomain).on("requestFilter", this._onRequestFilter);
213180
};
214181

215182
/**
216183
* See BaseServer#stop. Remove event handlers from StaticServerDomain.
217184
*/
218185
StaticServer.prototype.stop = function () {
219-
$(this._nodeConnection).off("staticServer.requestFilter", this._onRequestFilter);
186+
$(this._nodeDomain).off("requestFilter", this._onRequestFilter);
220187
};
221188

222-
exports.StaticServer = StaticServer;
189+
module.exports = StaticServer;
223190
});

src/extensions/default/StaticServer/main.js

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,15 @@ define(function (require, exports, module) {
3434
FileUtils = brackets.getModule("file/FileUtils"),
3535
LiveDevServerManager = brackets.getModule("LiveDevelopment/LiveDevServerManager"),
3636
BaseServer = brackets.getModule("LiveDevelopment/Servers/BaseServer").BaseServer,
37-
NodeConnection = brackets.getModule("utils/NodeConnection"),
37+
NodeDomain = brackets.getModule("utils/NodeDomain"),
3838
ProjectManager = brackets.getModule("project/ProjectManager"),
39-
StaticServer = require("StaticServer").StaticServer;
40-
41-
/**
42-
* @const
43-
* Amount of time to wait before automatically rejecting the connection
44-
* deferred. If we hit this timeout, we'll never have a node connection
45-
* for the static server in this run of Brackets.
46-
*/
47-
var NODE_CONNECTION_TIMEOUT = 5000; // 5 seconds
48-
49-
/**
50-
* @private
51-
* @type{jQuery.Deferred.<NodeConnection>}
52-
* A deferred which is resolved with a NodeConnection or rejected if
53-
* we are unable to connect to Node.
54-
*/
55-
var _nodeConnectionDeferred = new $.Deferred();
39+
StaticServer = require("StaticServer");
5640

5741
/**
5842
* @private
59-
* @type {NodeConnection}
43+
* @type {NodeDomain}
6044
*/
61-
var _nodeConnection = new NodeConnection();
45+
var _nodeDomain;
6246

6347
/**
6448
* @private
@@ -67,61 +51,26 @@ define(function (require, exports, module) {
6751
*/
6852
function _createStaticServer() {
6953
var config = {
70-
nodeConnection : _nodeConnection,
54+
nodeDomain : _nodeDomain,
7155
pathResolver : ProjectManager.makeProjectRelativeIfPossible,
7256
root : ProjectManager.getProjectRoot().fullPath
7357
};
7458

7559
return new StaticServer(config);
7660
}
77-
78-
/**
79-
* Allows access to the deferred that manages the node connection. This
80-
* is *only* for unit tests. Messing with this not in testing will
81-
* potentially break everything.
82-
*
83-
* @private
84-
* @return {jQuery.Deferred} The deferred that manages the node connection
85-
*/
86-
function _getNodeConnectionDeferred() {
87-
return _nodeConnectionDeferred;
88-
}
8961

9062
function initExtension() {
91-
// Start up the node connection, which is held in the
92-
// _nodeConnectionDeferred module variable. (Use
93-
// _nodeConnectionDeferred.done() to access it.
94-
var connectionTimeout = setTimeout(function () {
95-
console.error("[StaticServer] Timed out while trying to connect to node");
96-
_nodeConnectionDeferred.reject();
97-
}, NODE_CONNECTION_TIMEOUT);
98-
99-
_nodeConnection.connect(true).then(function () {
100-
_nodeConnection.loadDomains(
101-
[ExtensionUtils.getModulePath(module, "node/StaticServerDomain")],
102-
true
103-
).then(
104-
function () {
105-
clearTimeout(connectionTimeout);
106-
107-
// Register as a Live Development server provider
108-
LiveDevServerManager.registerServer({ create: _createStaticServer }, 5);
63+
var modulePath = ExtensionUtils.getModulePath(module, "node/StaticServerDomain");
64+
_nodeDomain = new NodeDomain("staticServer", modulePath);
10965

110-
_nodeConnectionDeferred.resolveWith(null, [_nodeConnection]);
111-
},
112-
function () { // Failed to connect
113-
console.error("[StaticServer] Failed to connect to node", arguments);
114-
_nodeConnectionDeferred.reject();
115-
}
116-
);
66+
return _nodeDomain.promise().then(function () {
67+
LiveDevServerManager.registerServer({ create: _createStaticServer }, 5);
68+
return _nodeDomain.connection;
11769
});
118-
119-
return _nodeConnectionDeferred.promise();
12070
}
12171

12272
exports.initExtension = initExtension;
12373

12474
// For unit tests only
12575
exports._getStaticServerProvider = _createStaticServer;
126-
exports._getNodeConnectionDeferred = _getNodeConnectionDeferred;
12776
});

src/extensions/default/StaticServer/unittests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ define(function (require, exports, module) {
3232
NodeConnection = brackets.getModule("utils/NodeConnection"),
3333
FileUtils = brackets.getModule("file/FileUtils"),
3434
SpecRunnerUtils = brackets.getModule("spec/SpecRunnerUtils"),
35-
StaticServer = require("StaticServer").StaticServer;
35+
StaticServer = require("StaticServer");
3636

3737
var testFolder = FileUtils.getNativeModuleDirectoryPath(module) + "/unittest-files";
3838

@@ -517,7 +517,7 @@ define(function (require, exports, module) {
517517
// Unit tests for the StaticServerProvider that wraps the underlying node server.
518518
describe("StaticServer", function () {
519519
var projectPath = testFolder + "/",
520-
mockNodeConnection = { connected: function () { return true; } },
520+
mockNodeDomain = { ready: function () { return true; } },
521521
pathResolver = function (path) {
522522
if (path.indexOf(projectPath) === 0) {
523523
return path.slice(projectPath.length);
@@ -527,7 +527,7 @@ define(function (require, exports, module) {
527527
},
528528
config = {
529529
baseUrl: "http://localhost/",
530-
nodeConnection: mockNodeConnection,
530+
nodeDomain: mockNodeDomain,
531531
pathResolver: pathResolver,
532532
root: projectPath
533533
};
@@ -576,7 +576,7 @@ define(function (require, exports, module) {
576576

577577
it("should decline serving if not connected to node", function () {
578578
// mock NodeConnection state to be disconnected
579-
config.nodeConnection = { connected: function () { return false; } };
579+
config.nodeDomain = { ready: function () { return false; } };
580580

581581
var server = new StaticServer(config);
582582

0 commit comments

Comments
 (0)