-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathBrowserStaticServer.js
More file actions
788 lines (721 loc) · 34.8 KB
/
BrowserStaticServer.js
File metadata and controls
788 lines (721 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*
* Copyright (c) 2012 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*global Phoenix, logger, fs, path */
define(function (require, exports, module) {
const BaseServer = require("LiveDevelopment/Servers/BaseServer").BaseServer,
LiveDevelopmentUtils = require("LiveDevelopment/LiveDevelopmentUtils"),
LiveDevelopment = require("LiveDevelopment/main"),
LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol"),
marked = require('thirdparty/marked.min'),
DocumentManager = require("document/DocumentManager"),
Mustache = require("thirdparty/mustache/mustache"),
FileSystem = require("filesystem/FileSystem"),
EventDispatcher = require("utils/EventDispatcher"),
StringUtils = require("utils/StringUtils"),
EventManager = require("utils/EventManager"),
LivePreviewSettings = require("./LivePreviewSettings"),
ProjectManager = require("project/ProjectManager"),
Strings = require("strings"),
utils = require('./utils'),
BootstrapCSSText = require("text!thirdparty/bootstrap/bootstrap.min.css"),
GithubCSSText = require("text!thirdparty/highlight.js/styles/github.min.css"),
HilightJSText = require("text!thirdparty/highlight.js/highlight.min.js"),
GFMCSSText = require("text!thirdparty/gfm.min.css"),
markdownHTMLTemplate = require("text!./markdown.html"),
redirectionHTMLTemplate = require("text!./redirectPage.html");
const EVENT_GET_PHOENIX_INSTANCE_ID = 'GET_PHOENIX_INSTANCE_ID';
const EVENT_GET_CONTENT = 'GET_CONTENT';
const EVENT_TAB_ONLINE = 'TAB_ONLINE';
const EVENT_REPORT_ERROR = 'REPORT_ERROR';
const EVENT_UPDATE_TITLE_ICON = 'UPDATE_TITLE_AND_ICON';
const EVENT_EMBEDDED_IFRAME_ESCAPE_PRESS = 'embeddedEscapeKeyPressed';
// In browser the SERVER_READY event is raised by the phcode.live virtual server page. That is why you wouldnt see
// this triggered in the phcode.dev codebase. It comes from the embedded iframe. Do not remove as unused.
const EVENT_SERVER_READY = 'SERVER_READY';
EventDispatcher.makeEventDispatcher(exports);
const livePreviewTabs = new Map();
const PHCODE_LIVE_PREVIEW_QUERY_PARAM = "phcodeLivePreview";
// Communication Channels for PHCode.dev Editor and Live Preview
// -------------------------------------------------------------
//
// NAVIGATOR_CHANNEL:
// - Purpose: To handle navigation messages between the PHCode.dev editor and multiple tabs open
// at phcode.dev/live-preview-loader.html.
// - Function: Mainly used to redirect pages in response to user actions, such as clicking on different
// files in the files panel or through other navigational inputs.
// - Channel ID: `live-preview-loader-${Phoenix.PHOENIX_INSTANCE_ID}` uniquely identifies this channel
// for a specific phoenix instance. This allows multiple live previews to exist if the user opens the
// same project in multiple phoenix editor instances.
let navigatorChannel;
const NAVIGATOR_CHANNEL_ID = `live-preview-loader-${Phoenix.PHOENIX_INSTANCE_ID}`;
// LIVE_PREVIEW_MESSENGER_CHANNEL:
// - Purpose: To facilitate communication of live preview transport messages between the PHCode.dev
// editor and tabs open at phcode.dev/live-preview-loader.html.
// - Function: Acts as a relay channel. Messages received here are forwarded to the LIVE_PREVIEW_MAIN_CHANNEL
// in the phcode.live domain.
// - Note: This setup ensures that messages are securely relayed within the constraints of the same origin
// communication policy.
let livePreviewChannel;
const LIVE_PREVIEW_MESSENGER_CHANNEL = `live-preview-messenger-${Phoenix.PHOENIX_INSTANCE_ID}`;
// LIVE_PREVIEW_MAIN_CHANNEL:
// - Purpose: The primary channel for receiving live preview messages in the phcode.live preview iframe.
// - Function: Listens to messages forwarded from LIVE_PREVIEW_MESSENGER_CHANNEL and updates the
// live preview accordingly in phcode.live domain.
// - Note: This channel is crucial for the real-time update and synchronization of the live preview
// with user actions in the PHCode.dev editor.
const LIVE_PREVIEW_BROADCAST_CHANNEL_ID = `${Phoenix.PHOENIX_INSTANCE_ID}_livePreview`;
let _staticServerInstance, $livepreviewServerIframe;
const LIVE_PREVIEW_STATIC_SERVER_BASE_URL = "https://phcode.live/";
// #LIVE_PREVIEW_STATIC_SERVER_BASE_URL_OVERRIDE uncomment below line if you are developing -
// live preview server for browser.
// const LIVE_PREVIEW_STATIC_SERVER_BASE_URL = "http://localhost:8001/";
// const LIVE_PREVIEW_STATIC_SERVER_ORIGIN = "http://localhost:8001";
const PREVIEW_BASE_URL = `${LIVE_PREVIEW_STATIC_SERVER_BASE_URL}vfs/PHOENIX_LIVE_PREVIEW_${Phoenix.PHOENIX_INSTANCE_ID}`;
const BASE_URL_PATH_PREFIX = `/vfs/PHOENIX_LIVE_PREVIEW_${Phoenix.PHOENIX_INSTANCE_ID}`;
function getLivePreviewNotSupportedURL() {
return `${window.Phoenix.baseURL}assets/phoenix-splash/live-preview-error.html?mainHeading=`+
encodeURIComponent(`${Strings.DESCRIPTION_LIVEDEV_MAIN_HEADING}`) + "&mainSpan="+
encodeURIComponent(`${Strings.DESCRIPTION_LIVEDEV_MAIN_SPAN}`);
}
function getNoPreviewURL(
heading = Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW,
message = Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW_DETAILS
){
return `${window.Phoenix.baseURL}assets/phoenix-splash/no-preview.html?jsonInput=`+
encodeURIComponent(`{"heading":"${heading}",`
+`"details":"${message}"}`);
}
function _isLivePreviewSupported() {
// in safari, service workers are disabled in third party iframes. We use phcode.live for secure sandboxing
// live previews into its own domain apart from phcode.dev. Since safari doesn't support this, we are left
// with using phcode.dev domain directly for live previews. That is a large attack surface for untrusted
// code execution. so we will disable live previews in safari instead of shipping a security vulnerability.
return Phoenix.isNativeApp || !(Phoenix.browser.desktop.isSafari || Phoenix.browser.mobile.isIos);
}
let _lastPreviewedFilePath;
/**
* Finds out a {URL,filePath} to live preview from the project. Will return and empty object if the current
* file is not previewable.
* @return {Promise<*>}
*/
async function getPreviewDetails() {
return new Promise(async (resolve, reject)=>{ // eslint-disable-line
// async is explicitly caught
try {
if(!_isLivePreviewSupported()){
resolve({
URL: getLivePreviewNotSupportedURL(),
isNoPreview: true
});
return;
}
const projectRoot = ProjectManager.getProjectRoot().fullPath;
const projectRootUrl = `${PREVIEW_BASE_URL}${projectRoot}`;
const currentDocument = DocumentManager.getCurrentDocument();
const currentFile = currentDocument? currentDocument.file : ProjectManager.getSelectedItem();
if(currentFile){
let fullPath = currentFile.fullPath;
let httpFilePath = null;
if(fullPath.startsWith("http://") || fullPath.startsWith("https://")){
httpFilePath = fullPath;
}
const customServeURL = LivePreviewSettings.getCustomServerConfig(fullPath);
const shouldUseInbuiltPreview = utils.isMarkdownFile(fullPath) || utils.isSVG(fullPath);
if(customServeURL){
const relativePath = path.relative(projectRoot, fullPath);
resolve({
URL: customServeURL,
filePath: relativePath,
fullPath: fullPath,
isMarkdownFile: utils.isMarkdownFile(fullPath),
isHTMLFile: utils.isHTMLFile(fullPath),
isCustomServer: true,
serverSupportsHotReload: LivePreviewSettings.serverSupportsHotReload()
});
return;
} else if(LivePreviewSettings.isUsingCustomServer() && !customServeURL && !shouldUseInbuiltPreview){
// this is the case where the file is outside of a custom configured server root (E. `www/`)
// like `notServed/Path.html`. For markdown and SVG, we will still use the inbuilt live preview.
resolve({
URL: getNoPreviewURL(Strings.DESCRIPTION_LIVEDEV_EXCLUDED,
StringUtils.format(Strings.DESCRIPTION_LIVEDEV_NO_PREVIEW_EXCLUDED,
LivePreviewSettings.getCustomServeRoot())),
isNoPreview: true
});
return;
} else if(utils.isPreviewableFile(fullPath)){
// this is the case where the user has html/svg/any previewable file as the active document
_lastPreviewedFilePath = fullPath;
}
let fileExists = await FileSystem.existsAsync(_lastPreviewedFilePath);
if(_lastPreviewedFilePath && fileExists){
// user either has active document as a previewable file or this is the case where
// user switched to a css/js/other file that is not previewable, but we have on old previewable
// file we will just take the _lastPreviewedFilePath as active
const filePath = httpFilePath || path.relative(projectRoot, _lastPreviewedFilePath);
let URL = httpFilePath || `${projectRootUrl}${filePath}`;
resolve({
URL,
filePath: filePath,
fullPath: _lastPreviewedFilePath,
isMarkdownFile: utils.isMarkdownFile(_lastPreviewedFilePath),
isHTMLFile: utils.isHTMLFile(_lastPreviewedFilePath)
});
return;
}
}
resolve({
URL: getNoPreviewURL(),
isNoPreview: true
});
}catch (e) {
reject(e);
}
});
}
function _initNavigatorChannel() {
navigatorChannel = new BroadcastChannel(NAVIGATOR_CHANNEL_ID);
navigatorChannel.onmessage = (event) => {
window.logger.livePreview.log("Live Preview navigator channel: Phoenix received event from tab: ", event);
const type = event.data.type;
switch (type) {
case 'GET_INITIAL_URL':
_sendInitialURL(event.data.pageLoaderID);
return;
case 'TAB_LOADER_ONLINE':
livePreviewTabs.set(event.data.pageLoaderID, {
lastSeen: new Date(),
URL: event.data.URL,
navigationTab: true
});
return;
default: return; // ignore messages not intended for us.
}
};
}
// this is the server tabs located at "src/live-preview.html" which embeds the `phcode.live` server and
// preview iframes.
function _sendToLivePreviewServerTabs(data, pageLoaderID=null) {
livePreviewChannel.postMessage({
pageLoaderID,
data
});
}
function _initLivePreviewChannel() {
livePreviewChannel = new BroadcastChannel(LIVE_PREVIEW_MESSENGER_CHANNEL);
livePreviewChannel.onmessage = (event) => {
window.logger.livePreview.log("StaticServer: Live Preview message channel Phoenix recvd:", event);
const pageLoaderID = event.data.pageLoaderID;
const data = event.data.data;
const eventName = data.eventName;
const message = data.message;
switch (eventName) {
case EVENT_GET_PHOENIX_INSTANCE_ID:
_sendToLivePreviewServerTabs({
type: 'PHOENIX_INSTANCE_ID',
PHOENIX_INSTANCE_ID: Phoenix.PHOENIX_INSTANCE_ID
}, pageLoaderID);
return;
case EVENT_GET_CONTENT:
getContent(message.path, message.url)
.then(response =>{
// response has the following attributes set
// response.contents: <text or arrayBuffer content>,
// response.path
// headers: {'Content-Type': 'text/html'} // optional headers
response.type = 'REQUEST_RESPONSE';
response.requestID = message.requestID;
_sendToLivePreviewServerTabs(response, pageLoaderID);
})
.catch(console.error);
return;
case EVENT_TAB_ONLINE:
livePreviewTabs.set(message.clientID, {
lastSeen: new Date(),
URL: message.URL
});
return;
case EVENT_REPORT_ERROR:
logger.reportError(new Error(message));
return;
default:
exports.trigger(eventName, {
data
});
}
};
}
// see markdown advanced rendering options at https://marked.js.org/using_advanced
marked.setOptions({
renderer: new marked.Renderer(),
pedantic: false,
gfm: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false
});
/**
* @constructor
* @extends {BaseServer}
* Live preview server that uses a built-in HTTP server to serve static
* and instrumented files.
*
* @param {!{baseUrl: string, root: string, pathResolver: function(string), nodeDomain: NodeDomain}} config
* Configuration parameters for this server:
* 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)
*/
function StaticServer(config) {
this._baseUrl = PREVIEW_BASE_URL;
this._getInstrumentedContent = this._getInstrumentedContent.bind(this);
BaseServer.call(this, config);
}
StaticServer.prototype = Object.create(BaseServer.prototype);
StaticServer.prototype.constructor = StaticServer;
/**
* Returns a base url for current project.
*
* @return {string}
* Base url for current project.
*/
StaticServer.prototype.getBaseUrl = function () {
return this._baseUrl;
};
/**
* Returns a URL for a given path
* @param {string} path Absolute path to covert to a URL
* @return {?string} Converts a path within the project root to a URL.
* Returns null if the path is not a descendant of the project root.
*/
StaticServer.prototype.pathToUrl = function (path) {
const baseUrl = this.getBaseUrl(),
relativePath = this._pathResolver(path);
// See if base url has been specified and path is within project
if (relativePath !== path) {
// Map to server url. Base url is already encoded, so don't encode again.
return `${baseUrl}${encodeURI(path)}`;
}
return null;
};
/**
* Convert a URL to a local full file path
* @param {string} url
* @return {?string} The absolute path for given URL or null if the path is
* not a descendant of the project.
*/
StaticServer.prototype.urlToPath = function (url) {
let baseUrl = this.getBaseUrl();
if (baseUrl !== "" && url.indexOf(baseUrl) === 0) {
const urlObj = new URL(url);
const filePath = decodeURI(urlObj.pathname)
.replace(BASE_URL_PATH_PREFIX, "");
return decodeURI(filePath);
}
return null;
};
/**
* Determines whether we can serve local file.
* @param {string} localPath A local path to file being served.
* @return {boolean} true for yes, otherwise false.
*/
StaticServer.prototype.canServe = function (localPath) {
// If we can't transform the local path to a project relative path,
// the path cannot be served
if (localPath === this._pathResolver(localPath)) {
return false;
}
// Url ending in "/" implies default file, which is usually index.html.
// Return true to indicate that we can serve it.
if (localPath.match(/\/$/)) {
return true;
}
// FUTURE: do a MIME Type lookup on file extension
return LiveDevelopmentUtils.isStaticHtmlFileExt(localPath);
};
/**
* Gets the server details from the StaticServerDomain in node.
* The domain itself handles starting a server if necessary (when
* the staticServer.getServer command is called).
*
* @return {jQuery.Promise} A promise that resolves/rejects when
* the server is ready/failed.
*/
StaticServer.prototype.readyToServe = function () {
return $.Deferred().resolve().promise(); // virtual server is always assumed present in phoenix
};
/**
* This will add the given text to be served when the path is hit in server. use this to either serve a file
* that doesn't exist in project, or to override a given path to the contents you give.
*/
StaticServer.prototype.addVirtualContentAtPath = function (path, docText) {
BaseServer.prototype.addVirtualContentAtPath.call(this, path, docText);
};
/**
* See BaseServer#add. StaticServer ignores documents that do not have
* a setInstrumentationEnabled method. Updates request filters.
*/
StaticServer.prototype.add = function (liveDocument) {
if (liveDocument.setInstrumentationEnabled) {
// enable instrumentation
liveDocument.setInstrumentationEnabled(true);
}
BaseServer.prototype.add.call(this, liveDocument);
};
/**
* See BaseServer#remove. Updates request filters.
*/
StaticServer.prototype.remove = function (liveDocument) {
BaseServer.prototype.remove.call(this, liveDocument);
};
/**
* removes path added by addVirtualContentAtPath()
*/
StaticServer.prototype.removeVirtualContentAtPath = function (path) {
BaseServer.prototype.removeVirtualContentAtPath.call(this, path);
};
/**
* See BaseServer#clear. Updates request filters.
*/
StaticServer.prototype.clear = function () {
BaseServer.prototype.clear.call(this);
};
function _getMarkdown(fullPath) {
return new Promise((resolve, reject)=>{
DocumentManager.getDocumentForPath(fullPath)
.done(function (doc) {
let text = doc.getText();
// Input: special ZERO WIDTH unicode characters (for example \uFEFF) might interfere with parsing.
// Some text editors add them at the start of the file. See
// https://github.com/markedjs/marked/issues/2139
text = text.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, "");
let markdownHtml = marked.parse(text);
let templateVars = {
markdownContent: markdownHtml,
BOOTSTRAP_LIB_CSS: BootstrapCSSText,
HIGHLIGHT_JS_CSS: GithubCSSText,
TRUSTED_ORIGINS_EMBED:
`const TRUSTED_ORIGINS_EMBED = ${JSON.stringify(Phoenix.TRUSTED_ORIGINS)};`,
HIGHLIGHT_JS: HilightJSText,
GFM_CSS: GFMCSSText,
PARENT_ORIGIN: location.origin
};
let html = Mustache.render(markdownHTMLTemplate, templateVars);
resolve({
contents: html,
headers: {'Content-Type': 'text/html'},
path: fullPath
});
})
.fail(function (err) {
reject(new Error(`Markdown rendering failed for ${fullPath}: ` + err));
});
});
}
/**
* return a page loader html with redirect script tag that just redirects the page to the given redirectURL.
* Strips the PHCODE_LIVE_PREVIEW_QUERY_PARAM in redirectURL also, indicating this is not a live previewed url.
*
* @param redirectURL
* @return {string}
* @private
*/
function _getRedirectionPage(redirectURL) {
let url = new URL(redirectURL);
// strip this query param as the redirection will be done by the page loader and not the content iframe.
url.searchParams.delete(PHCODE_LIVE_PREVIEW_QUERY_PARAM);
let templateVars = {
redirectURL: url.href
};
return Mustache.render(redirectionHTMLTemplate, templateVars);
}
/**
* @private
* Events raised by broadcast channel from the service worker will be captured here. The service worker will ask
* all phoenix instances if the url to be served should be replaced with instrumented content here or served
* as static file from disk.
*/
StaticServer.prototype._getInstrumentedContent = function (requestedPath, url) {
return new Promise((resolve, reject)=>{
let path = this._documentKey(requestedPath),
liveDocument = this._liveDocuments[path],
virtualDocument = this._virtualServingDocuments[path];
let contents;
if(!ProjectManager.isWithinProject(requestedPath)) {
console.error("Security issue prevented: Live preview tried to access non project resource!!!", path);
resolve({
path,
contents: null // 404. the user doesnt need to know this, might be a mistake too
});
return;
}
url = new URL(url);
let isLivePreviewPopoutPage = false;
if(url.searchParams.get(PHCODE_LIVE_PREVIEW_QUERY_PARAM)) {
isLivePreviewPopoutPage = true;
}
if (virtualDocument) {
// virtual document overrides takes precedence over live preview docs
contents = virtualDocument;
} else if (liveDocument && liveDocument.getResponseData) {
contents = liveDocument.getResponseData().body;
if(isLivePreviewPopoutPage && contents.indexOf(LiveDevProtocol.getRemoteScript()) === -1){
// #LIVE_PREVIEW_TAB_NAVIGATION_RACE_FIX
// check if this is a live preview html. If so, then if you are here, it means that users switched
// live preview to a different page while we are just about to serve an old live preview page that is
// no longer in live preview. If we just serve the raw html here, it will not have any tab navigation
// instrumentation on popped out tabs and live preview navigation will stop on this page. So we will
// use a page loader url to continue navigation.
console.log("serving stale live preview with navigable url", url);
contents = _getRedirectionPage(url);
}
} else {
const file = FileSystem.getFileForPath(requestedPath);
let doc = DocumentManager.getOpenDocumentForPath(file.fullPath);
if (doc) {
// this file is open in some editor, so we sent the edited contents.
contents = doc.getText();
} else {
fs.readFile(requestedPath, fs.BYTE_ARRAY_ENCODING, function (error, binContent) {
if(error){
binContent = null;
}
resolve({
path,
contents: binContent
});
});
return;
}
}
let headers;
if(path.endsWith(".htm") || path.endsWith(".html") || path.endsWith(".xhtml") || path.endsWith(".php")) {
headers = {
'Content-Type': 'text/html;charset=UTF-8'
};
}
resolve({
path,
contents: contents,
headers
});
});
};
function getContent(path, url) {
const currentDocument = DocumentManager.getCurrentDocument();
const currentFile = currentDocument? currentDocument.file : ProjectManager.getSelectedItem();
if(!_staticServerInstance){
return Promise.reject("Static serve not started!");
}
if(!url.startsWith(_staticServerInstance.getBaseUrl())) {
return Promise.reject("Not serving content as url belongs to another phcode instance: " + url);
}
const shouldServeRendered = ((path === _lastPreviewedFilePath)
|| (currentFile && currentFile.fullPath === path));
if(utils.isMarkdownFile(path) && shouldServeRendered){
return _getMarkdown(path);
}
if(_staticServerInstance){
return _staticServerInstance._getInstrumentedContent(path, url);
}
return Promise.reject("Cannot get content");
}
/**
* See BaseServer#start. Starts listenting to StaticServerDomain events.
*/
StaticServer.prototype.start = async function () {
_staticServerInstance = this;
// in browsers, the virtual server is always loaded permanently in iframe.
};
StaticServer.prototype.isActive = function () {
return _staticServerInstance === this;
};
/**
* See BaseServer#stop. Remove event handlers from StaticServerDomain.
*/
StaticServer.prototype.stop = function () {
_staticServerInstance = undefined;
};
exports.on(EVENT_REPORT_ERROR, function(_ev, event){
logger.reportError(new Error(event.data.message));
});
exports.on(EVENT_GET_CONTENT, function(_ev, event){
window.logger.livePreview.log("Static Server GET_CONTENT", event);
if(event.data.message && event.data.message.phoenixInstanceID === Phoenix.PHOENIX_INSTANCE_ID) {
const requestPath = event.data.message.path,
requestID = event.data.message.requestID,
url = event.data.message.url;
getContent(requestPath, url)
.then(response =>{
// response has the following attributes set
// response.contents: <text or arrayBuffer content>,
// response.path
// headers: {'Content-Type': 'text/html'} // optional headers
response.type = 'REQUEST_RESPONSE';
response.requestID = requestID;
messageToLivePreviewTabs(response);
})
.catch(console.error);
}
});
exports.on(EVENT_GET_PHOENIX_INSTANCE_ID, function(_ev){
messageToLivePreviewTabs({
type: 'PHOENIX_INSTANCE_ID',
PHOENIX_INSTANCE_ID: Phoenix.PHOENIX_INSTANCE_ID
});
});
exports.on(EVENT_TAB_ONLINE, function(_ev, event){
livePreviewTabs.set(event.data.message.clientID, {
lastSeen: new Date(),
URL: event.data.message.URL
});
});
function _startHeartBeatListeners() {
// If we didn't receive heartbeat message from a tab for 10 seconds, we assume tab closed
const TAB_HEARTBEAT_TIMEOUT = 10000; // in millis secs
setInterval(()=>{
let endTime = new Date();
for(let tab of livePreviewTabs.keys()){
const tabInfo = livePreviewTabs.get(tab);
let timeDiff = endTime - tabInfo.lastSeen; // in ms
if(timeDiff > TAB_HEARTBEAT_TIMEOUT){
livePreviewTabs.delete(tab);
// the parent navigationTab `phcode.dev/live-preview-loader.html` which loads the live preview tab
// is in the list too. We should not raise browser close for a live-preview-loader tab.
if(!tabInfo.navigationTab) {
exports.trigger('BROWSER_CLOSE', { data: { message: {clientID: tab}}});
}
}
}
}, 1000);
}
/**
* The message should be and object of the form: {type, ...}. a type attribute is mandatory
* @param message
*/
function messageToLivePreviewTabs(message) {
if(!message.type){
throw new Error('Missing type attribute to send live preview message to tabs');
}
// The embedded iframe is a trusted origin and hence we use '*'. We can alternatively use
// getStaticServerBaseURLs().origin, but there seems to be a single error on startup
// Most likely as we switch frequently between about:blank and the live preview server host page.
// Error message in console:
// `Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:8001')
// does not match the recipient window's origin ('http://localhost:8000').`
$livepreviewServerIframe && $livepreviewServerIframe[0].contentWindow.postMessage(message, '*');
_sendToLivePreviewServerTabs(message);
}
let currentPopoutURL;
function _sendInitialURL(pageLoaderID) {
if(!currentPopoutURL){
return;
}
navigatorChannel.postMessage({
type: 'INITIAL_URL_NAVIGATE',
URL: currentPopoutURL,
pageLoaderID: pageLoaderID
});
}
function redirectAllTabs(newURL, force) {
currentPopoutURL = newURL;
navigatorChannel.postMessage({
type: 'REDIRECT_PAGE',
URL: newURL,
force
});
}
function _projectOpened(_evt, projectRoot) {
_lastPreviewedFilePath = null;
navigatorChannel.postMessage({
type: 'PROJECT_SWITCH',
projectRoot: projectRoot.fullPath
});
}
exports.on(EVENT_UPDATE_TITLE_ICON, function(_ev, event){
const title = event.data.message.title;
const faviconBase64 = event.data.message.faviconBase64;
navigatorChannel.postMessage({
type: 'UPDATE_TITLE_ICON',
title,
faviconBase64
});
});
exports.on(EVENT_EMBEDDED_IFRAME_ESCAPE_PRESS, function () {
if(LiveDevelopment.isInPreviewMode()){
return;
}
utils.focusActiveEditorIfFocusInLivePreview();
});
function getPageLoaderURL(url) {
return `${Phoenix.baseURL}live-preview-loader.html?`
+`virtualServerURL=${encodeURIComponent(LIVE_PREVIEW_STATIC_SERVER_BASE_URL)}`
+`&phoenixInstanceID=${Phoenix.PHOENIX_INSTANCE_ID}&initialURL=${encodeURIComponent(url)}`
+`&localMessage=${encodeURIComponent(Strings.DESCRIPTION_LIVEDEV_SECURITY_POPOUT_MESSAGE)}`
+`&appName=${encodeURIComponent(Strings.APP_NAME)}`
+`&initialProjectRoot=${encodeURIComponent(ProjectManager.getProjectRoot().fullPath)}`
+`&okMessage=${encodeURIComponent(Strings.TRUST_PROJECT)}`;
}
function getTabPopoutURL(url) {
let openURL = new URL(url);
// we tag all externally opened urls with query string parameter phcodeLivePreview="true" to address
// #LIVE_PREVIEW_TAB_NAVIGATION_RACE_FIX
openURL.searchParams.set(PHCODE_LIVE_PREVIEW_QUERY_PARAM, "true");
return getPageLoaderURL(openURL.href);
}
function hasActiveLivePreviews() {
return livePreviewTabs.size > 0;
}
function getRemoteTransportScript() {
return `TRANSPORT_CONFIG.LIVE_PREVIEW_BROADCAST_CHANNEL_ID = "${LIVE_PREVIEW_BROADCAST_CHANNEL_ID}";\n`;
}
function init() {
LiveDevelopment.setLivePreviewTransportBridge(exports);
// load the hidden iframe that loads the service worker server page once. we will reuse the same server
// as this is a cross-origin server phcode.live, the browser will identify it as a security issue
// if we continuously reload the service worker loader page frequently and it will stop working.
$livepreviewServerIframe = $("#live-preview-server-iframe");
let url = LIVE_PREVIEW_STATIC_SERVER_BASE_URL +
`?parentOrigin=${location.origin}`;
$livepreviewServerIframe.attr("src", url);
_initNavigatorChannel();
_initLivePreviewChannel();
EventManager.registerEventHandler("ph-liveServer", exports);
ProjectManager.on(ProjectManager.EVENT_PROJECT_OPEN, _projectOpened);
_startHeartBeatListeners();
}
exports.init = init;
exports.StaticServer = StaticServer;
exports.messageToLivePreviewTabs = messageToLivePreviewTabs;
exports.getPreviewDetails = getPreviewDetails;
exports.livePreviewTabs = livePreviewTabs;
exports.redirectAllTabs = redirectAllTabs;
exports.getTabPopoutURL = getTabPopoutURL;
exports.hasActiveLivePreviews = hasActiveLivePreviews;
exports.getNoPreviewURL = getNoPreviewURL;
exports.getRemoteTransportScript = getRemoteTransportScript;
exports.PHCODE_LIVE_PREVIEW_QUERY_PARAM = PHCODE_LIVE_PREVIEW_QUERY_PARAM;
exports.EVENT_SERVER_READY = EVENT_SERVER_READY;
});