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

Commit 6f2dabd

Browse files
committed
Merge pull request #3392 from adobe/jasonsanjose/issue-3372
Fix #3372 check for valid server
2 parents 7095ac1 + 8bb9626 commit 6f2dabd

File tree

4 files changed

+85
-50
lines changed

4 files changed

+85
-50
lines changed

src/LiveDevelopment/Documents/HTMLDocument.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,16 @@ define(function HTMLDocumentModule(require, exports, module) {
6161
return;
6262
}
6363
this.editor = editor;
64-
HTMLInstrumentation._markText(this.editor);
65-
this.onCursorActivity = this.onCursorActivity.bind(this);
66-
$(this.editor).on("cursorActivity", this.onCursorActivity);
67-
this.onCursorActivity();
64+
this._instrumentationEnabled = false;
6865

66+
this.onCursorActivity = this.onCursorActivity.bind(this);
6967
this.onDocumentSaved = this.onDocumentSaved.bind(this);
68+
69+
$(this.editor).on("cursorActivity", this.onCursorActivity);
7070
$(DocumentManager).on("documentSaved", this.onDocumentSaved);
7171

7272
// Experimental code
7373
if (LiveDevelopment.config.experimental) {
74-
7574
// Used by highlight agent to highlight editor text as selected in browser
7675
this.onHighlight = this.onHighlight.bind(this);
7776
$(HighlightAgent).on("highlight", this.onHighlight);
@@ -80,6 +79,32 @@ define(function HTMLDocumentModule(require, exports, module) {
8079
$(this.editor).on("change", this.onChange);
8180
}
8281
};
82+
83+
/**
84+
* Enable instrumented HTML
85+
* @param enabled {boolean}
86+
*/
87+
HTMLDocument.prototype.setInstrumentationEnabled = function setInstrumentationEnabled(enabled) {
88+
if (enabled && !this._instrumentationEnabled) {
89+
HTMLInstrumentation.scanDocument(this.doc);
90+
HTMLInstrumentation._markText(this.editor);
91+
}
92+
93+
this._instrumentationEnabled = enabled;
94+
};
95+
96+
/**
97+
* Returns a JSON object with HTTP response overrides
98+
* @returns {{body: string}}
99+
*/
100+
HTMLDocument.prototype.getResponseData = function getResponseData(enabled) {
101+
var body = (this._instrumentationEnabled) ?
102+
HTMLInstrumentation.generateInstrumentedHTML(this.doc) : this.doc.getText();
103+
104+
return {
105+
body: body
106+
};
107+
};
83108

84109
/** Close the document */
85110
HTMLDocument.prototype.close = function close() {
@@ -92,7 +117,6 @@ define(function HTMLDocumentModule(require, exports, module) {
92117

93118
// Experimental code
94119
if (LiveDevelopment.config.experimental) {
95-
96120
$(HighlightAgent).off("highlight", this.onHighlight);
97121
this.onHighlight();
98122

src/LiveDevelopment/LiveDevelopment.js

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ define(function LiveDevelopment(require, exports, module) {
296296
_liveDocument.close();
297297
_liveDocument = undefined;
298298
}
299+
300+
if (_serverProvider) {
301+
// Remove any "request" listeners that were added previously
302+
$(_serverProvider).off(".livedev");
303+
}
304+
299305
if (_relatedDocuments) {
300306
_relatedDocuments.forEach(function (liveDoc) {
301307
liveDoc.close();
@@ -315,13 +321,43 @@ define(function LiveDevelopment(require, exports, module) {
315321
}
316322
}
317323

318-
/** Open a live document
324+
/**
325+
* @private
326+
* Open a live document
319327
* @param {Document} source document to open
320-
* @return {jQuery.Promise} A promise that is resolved once the live
321-
* document is open, and is never explicitly rejected.
322328
*/
323329
function _openDocument(doc, editor) {
330+
_closeDocument();
331+
_liveDocument = _createDocument(doc, editor);
324332

333+
// Enable instrumentation
334+
if (_liveDocument.setInstrumentationEnabled) {
335+
var enableInstrumentation = false;
336+
337+
if (_serverProvider && _serverProvider.setRequestFilterPaths) {
338+
enableInstrumentation = true;
339+
340+
_serverProvider.setRequestFilterPaths(
341+
["/" + encodeURI(ProjectManager.makeProjectRelativeIfPossible(doc.file.fullPath))]
342+
);
343+
344+
// Send custom HTTP response for the current live document
345+
$(_serverProvider).on("request.livedev", function (event, request) {
346+
// response can be null in which case the StaticServerDomain reverts to simple file serving.
347+
var response = _liveDocument.getResponseData ? _liveDocument.getResponseData() : null;
348+
request.send(response);
349+
});
350+
}
351+
352+
_liveDocument.setInstrumentationEnabled(enableInstrumentation);
353+
}
354+
}
355+
356+
/**
357+
* @private
358+
* Populate array of related documents reported by the browser agent(s)
359+
*/
360+
function _getRelatedDocuments() {
325361
function createLiveStylesheet(url) {
326362
var stylesheetDeferred = $.Deferred();
327363

@@ -348,9 +384,6 @@ define(function LiveDevelopment(require, exports, module) {
348384
return stylesheetDeferred.promise();
349385
}
350386

351-
_closeDocument();
352-
_liveDocument = _createDocument(doc, editor);
353-
354387
// Gather related CSS documents.
355388
// FUTURE: Gather related JS documents as well.
356389
_relatedDocuments = [];
@@ -453,13 +486,12 @@ define(function LiveDevelopment(require, exports, module) {
453486
return;
454487
}
455488

456-
var editor = EditorManager.getCurrentFullEditor(),
457-
status = STATUS_ACTIVE;
489+
var status = STATUS_ACTIVE;
458490

459491
// Note: the following promise is never explicitly rejected, so there
460-
// is no failure handler. If _openDocument is changed so that rejection
492+
// is no failure handler. If _getRelatedDocuments is changed so that rejection
461493
// is possible, failure should be managed accordingly.
462-
_openDocument(doc, editor)
494+
_getRelatedDocuments()
463495
.done(function () {
464496
if (doc.isDirty && _classForDocument(doc) !== CSSDocument) {
465497
status = STATUS_OUT_OF_SYNC;
@@ -572,23 +604,7 @@ define(function LiveDevelopment(require, exports, module) {
572604
function doLaunchAfterServerReady() {
573605
_setStatus(STATUS_CONNECTING);
574606

575-
if (_serverProvider) {
576-
// Install a request filter for the current document. In the future,
577-
// we need to install filters for *all* files that need to be instrumented.
578-
HTMLInstrumentation.scanDocument(doc);
579-
_serverProvider.setRequestFilterPaths(
580-
["/" + ProjectManager.makeProjectRelativeIfPossible(doc.file.fullPath)]
581-
);
582-
583-
// Remove any "request" listeners that were added previously
584-
$(_serverProvider).off(".livedev");
585-
586-
$(_serverProvider).on("request.livedev", function (event, request) {
587-
var html = HTMLInstrumentation.generateInstrumentedHTML(doc);
588-
589-
request.send({ body: html });
590-
});
591-
}
607+
_openDocument(doc, EditorManager.getCurrentFullEditor());
592608

593609
Inspector.connectToURL(launcherUrl).done(result.resolve).fail(function onConnectFail(err) {
594610
if (err === "CANCEL") {
@@ -669,7 +685,6 @@ define(function LiveDevelopment(require, exports, module) {
669685

670686
if (!doc || !doc.root) {
671687
showWrongDocError();
672-
673688
} else {
674689
_serverProvider = LiveDevServerManager.getProvider(doc.file.fullPath);
675690

@@ -841,9 +856,9 @@ define(function LiveDevelopment(require, exports, module) {
841856
if (Inspector.connected()) {
842857
hideHighlight();
843858
if (agents.network && agents.network.wasURLRequested(doc.url)) {
844-
_closeDocument();
845-
var editor = EditorManager.getCurrentFullEditor();
846-
promise = _openDocument(doc, editor);
859+
_openDocument(doc, EditorManager.getCurrentFullEditor());
860+
861+
promise = _getRelatedDocuments();
847862
} else {
848863
if (exports.config.experimental || _isHtmlFileExt(doc.extension)) {
849864
promise = close().done(open);
@@ -895,15 +910,10 @@ define(function LiveDevelopment(require, exports, module) {
895910

896911
/**
897912
* Determines whether we can serve local file.
898-
*
899-
* @param {String} localPath
900-
* A local path to file being served.
901-
*
902-
* @return {Boolean}
903-
* true for yes, otherwise false.
913+
* @param {String} localPath A local path to file being served.
914+
* @return {Boolean} true for yes, otherwise false.
904915
*/
905916
UserServerProvider.prototype.canServe = function (localPath) {
906-
907917
var baseUrl = ProjectManager.getBaseUrl();
908918
if (!baseUrl) {
909919
return false;
@@ -918,9 +928,7 @@ define(function LiveDevelopment(require, exports, module) {
918928

919929
/**
920930
* Returns a base url for current project.
921-
*
922-
* @return {String}
923-
* Base url for current project.
931+
* @return {String} Base url for current project.
924932
*/
925933
UserServerProvider.prototype.getBaseUrl = function () {
926934
return ProjectManager.getBaseUrl();

src/extensions/default/StaticServer/node/StaticServerDomain.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
*
2222
*/
2323

24-
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4,
25-
maxerr: 50, node: true */
24+
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, node: true */
2625
/*global */
2726

2827
(function () {
@@ -292,7 +291,10 @@ maxerr: 50, node: true */
292291
function _cmdSetRequestFilterPaths(root, paths) {
293292
var rootPath = normalizeRootPath(root),
294293
pathKey = getPathKey(root),
295-
rewritePaths = _rewritePaths[pathKey];
294+
rewritePaths = {};
295+
296+
// reset list of filtered paths for each call to setRequestFilterPaths
297+
_rewritePaths[pathKey] = rewritePaths;
296298

297299
paths.forEach(function (path) {
298300
rewritePaths[path] = pathJoin(root, path);

test/spec/LiveDevelopment-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ define(function (require, exports, module) {
725725
instrumentedHtml = HTMLInstrumentationModule.generateInstrumentedHTML(testDocument);
726726
createIdToTagMap(instrumentedHtml);
727727
testHTMLDoc = new HTMLDocumentModule(testDocument, testEditor);
728+
testHTMLDoc.setInstrumentationEnabled(true);
728729
});
729730
});
730731

0 commit comments

Comments
 (0)