Skip to content

Commit 02657f4

Browse files
Arunabose
authored andcommitted
fix: live preview in safari/mac
1 parent 27e47f4 commit 02657f4

File tree

6 files changed

+496
-491
lines changed

6 files changed

+496
-491
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/phoenix/virtualServer/config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ if(!self.Config){
3333
*
3434
* `debug`: if present (i.e., `Boolean`), enable workbox debug logging
3535
*/
36-
const url = new URL(location);
3736

3837
/**
3938
* Given a route string, make sure it follows the pattern we expect:
@@ -47,7 +46,7 @@ if(!self.Config){
4746
* @param {String} route
4847
*/
4948
function getNormalizeRoute() {
50-
let route = url.searchParams.get('route') || 'fs';
49+
let route = (new URL(location)).searchParams.get('route') || 'fs';
5150

5251
// Only a single / at the front of the route
5352
route = route.replace(/^\/*/, '');
@@ -59,7 +58,7 @@ if(!self.Config){
5958

6059
self.Config = {
6160
route: getNormalizeRoute(),
62-
disableIndexes: url.searchParams.get('disableIndexes') !== null,
61+
disableIndexes: (new URL(location)).searchParams.get('disableIndexes') !== null,
6362
debug: false // this is set via sw messages from phoenix
6463
};
6564
}

src/phoenix/virtualServer/content-type.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,48 @@
1717
*
1818
*/
1919

20-
/* global lookup, importScripts*/
20+
/* global mime, importScripts*/
2121

2222
importScripts('phoenix/virtualServer/mime-types.js');
2323

2424
if(!self.ContentType){
25-
function getMimeType(path) {
26-
return lookup(path) || 'application/octet-stream';
27-
}
28-
29-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#Audio_and_video_types
30-
function isMedia(path) {
31-
let mimeType = lookup(path);
32-
if (!mimeType) {
33-
return false;
25+
(function () {
26+
function getMimeType(path) {
27+
return mime.lookup(path) || 'application/octet-stream';
3428
}
3529

36-
mimeType = mimeType.toLowerCase();
30+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#Audio_and_video_types
31+
function isMedia(path) {
32+
let mimeType = mime.lookup(path);
33+
if (!mimeType) {
34+
return false;
35+
}
3736

38-
// Deal with OGG special case
39-
if (mimeType === 'application/ogg') {
40-
return true;
41-
}
37+
mimeType = mimeType.toLowerCase();
4238

43-
// Anything else with `audio/*` or `video/*` is "media"
44-
return mimeType.startsWith('audio/') || mimeType.startsWith('video/');
45-
}
39+
// Deal with OGG special case
40+
if (mimeType === 'application/ogg') {
41+
return true;
42+
}
4643

47-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#Image_types
48-
function isImage(path) {
49-
const mimeType = lookup(path);
50-
if (!mimeType) {
51-
return false;
44+
// Anything else with `audio/*` or `video/*` is "media"
45+
return mimeType.startsWith('audio/') || mimeType.startsWith('video/');
5246
}
5347

54-
return mimeType.toLowerCase().startsWith('image/');
55-
}
48+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#Image_types
49+
function isImage(path) {
50+
const mimeType = mime.lookup(path);
51+
if (!mimeType) {
52+
return false;
53+
}
54+
55+
return mimeType.toLowerCase().startsWith('image/');
56+
}
5657

57-
self.ContentType = {
58-
isMedia,
59-
isImage,
60-
getMimeType
61-
};
58+
self.ContentType = {
59+
isMedia,
60+
isImage,
61+
getMimeType
62+
};
63+
}());
6264
}

src/phoenix/virtualServer/html-formatter.js

Lines changed: 119 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,45 @@ importScripts('phoenix/virtualServer/content-type.js');
2323
importScripts('phoenix/virtualServer/icons.js');
2424

2525
if(!self.HtmlFormatter){
26-
// 20-Apr-2004 17:14
27-
const formatDate = d => {
28-
const day = d.getDate();
29-
const month = d.toLocaleString('en-us', {month: 'short'});
30-
const year = d.getFullYear();
31-
const hours = d.getHours();
32-
const mins = d.getMinutes();
33-
return `${day}-${month}-${year} ${hours}:${mins}`;
34-
};
35-
36-
const formatSize = s => {
37-
const units = ['', 'K', 'M'];
38-
if (!s) {
39-
return '-';
40-
}
41-
const i = Math.floor(Math.log(s) / Math.log(1024));
42-
return Math.round(s / Math.pow(1024, i), 2) + units[i];
43-
};
44-
45-
const formatRow = (
46-
icon,
47-
alt = '[ ]',
48-
href,
49-
name,
50-
modified,
51-
size
52-
) => `<tr><td valign='top'><img src='${icon || icons.unknown}' alt='${alt}'></td><td>
26+
(function () {
27+
// 20-Apr-2004 17:14
28+
const formatDate = d => {
29+
const day = d.getDate();
30+
const month = d.toLocaleString('en-us', {month: 'short'});
31+
const year = d.getFullYear();
32+
const hours = d.getHours();
33+
const mins = d.getMinutes();
34+
return `${day}-${month}-${year} ${hours}:${mins}`;
35+
};
36+
37+
const formatSize = s => {
38+
const units = ['', 'K', 'M'];
39+
if (!s) {
40+
return '-';
41+
}
42+
const i = Math.floor(Math.log(s) / Math.log(1024));
43+
return Math.round(s / Math.pow(1024, i), 2) + units[i];
44+
};
45+
46+
const formatRow = (
47+
icon,
48+
alt = '[ ]',
49+
href,
50+
name,
51+
modified,
52+
size
53+
) => `<tr><td valign='top'><img src='${icon || icons.unknown}' alt='${alt}'></td><td>
5354
<a href='${href}'>${name}</a></td>
5455
<td align='right'>${formatDate(new Date(modified))}</td>
5556
<td align='right'>${formatSize(size)}</td><td>&nbsp;</td></tr>`;
5657

57-
const footerClose = '<address>nohost (Web Browser Server)</address></body></html>';
58+
const footerClose = '<address>nohost (Web Browser Server)</address></body></html>';
5859

59-
/**
60-
* Send an Apache-style 404
61-
*/
62-
function format404(url) {
63-
const body = `
60+
/**
61+
* Send an Apache-style 404
62+
*/
63+
function format404(url) {
64+
const body = `
6465
<!DOCTYPE html>
6566
<html><head>
6667
<title>404 Not Found</title>
@@ -69,21 +70,21 @@ if(!self.HtmlFormatter){
6970
<p>The requested URL ${url} was not found on this server.</p>
7071
<hr>${footerClose}`;
7172

72-
return {
73-
body,
74-
config: {
75-
status: 404,
76-
statusText: 'Not Found',
77-
headers: {'Content-Type': 'text/html'}
78-
}
79-
};
80-
}
73+
return {
74+
body,
75+
config: {
76+
status: 404,
77+
statusText: 'Not Found',
78+
headers: {'Content-Type': 'text/html'}
79+
}
80+
};
81+
}
8182

82-
/**
83-
* Send an Apache-style 500
84-
*/
85-
function format500(path, err) {
86-
const body = `
83+
/**
84+
* Send an Apache-style 500
85+
*/
86+
function format500(path, err) {
87+
const body = `
8788
<!DOCTYPE html>
8889
<html><head>
8990
<title>500 Internal Server Error</title>
@@ -93,24 +94,24 @@ if(!self.HtmlFormatter){
9394
<p>The error was: ${err.message}.</p>
9495
<hr>${footerClose}`;
9596

96-
return {
97-
body,
98-
config: {
99-
status: 500,
100-
statusText: 'Internal Error',
101-
headers: {'Content-Type': 'text/html'}
102-
}
103-
};
104-
}
105-
106-
/**
107-
* Send an Apache-style directory listing
108-
*/
109-
function formatDir(route, dirPath, entries) {
110-
const parent = self.path.dirname(dirPath) || '/';
111-
// Maintain path sep, but deal with things like spaces in filenames
112-
const url = encodeURI(route + parent);
113-
const header = `
97+
return {
98+
body,
99+
config: {
100+
status: 500,
101+
statusText: 'Internal Error',
102+
headers: {'Content-Type': 'text/html'}
103+
}
104+
};
105+
}
106+
107+
/**
108+
* Send an Apache-style directory listing
109+
*/
110+
function formatDir(route, dirPath, entries) {
111+
const parent = self.path.dirname(dirPath) || '/';
112+
// Maintain path sep, but deal with things like spaces in filenames
113+
const url = encodeURI(route + parent);
114+
const header = `
114115
<!DOCTYPE html>
115116
<html><head><title>Index of ${dirPath}</title></head>
116117
<body><h1>Index of ${dirPath}</h1>
@@ -121,59 +122,60 @@ if(!self.HtmlFormatter){
121122
<tr><td valign='top'><img src='${icons.back}' alt='[DIR]'></td>
122123
<td><a href='${url}'>Parent Directory</a></td><td>&nbsp;</td>
123124
<td align='right'> - </td><td>&nbsp;</td></tr>`;
124-
const footer = `<tr><th colspan='5'><hr></th></tr></table>${footerClose}`;
125-
126-
const rows = entries.map(entry => {
127-
let entryName = entry.name || entry;
128-
const ext = self.path.extname(entryName);
129-
// Maintain path sep, but deal with things like spaces in filenames
130-
const href = encodeURI(`${route}${self.path.join(dirPath, entryName)}`);
131-
let icon;
132-
let alt;
133-
134-
// TODO: switch this to entry.isDirectory() if possible
135-
if (ContentType.isImage(ext)) {
136-
icon = icons.image2;
137-
alt = '[IMG]';
138-
} else if (ContentType.isMedia(ext)) {
139-
icon = icons.movie;
140-
alt = '[MOV]';
141-
} else if (!ext) {
142-
icon = icons.folder;
143-
alt = '[DIR]';
144-
} else {
145-
icon = icons.text;
146-
alt = '[TXT]';
147-
}
125+
const footer = `<tr><th colspan='5'><hr></th></tr></table>${footerClose}`;
126+
127+
const rows = entries.map(entry => {
128+
let entryName = entry.name || entry;
129+
const ext = self.path.extname(entryName);
130+
// Maintain path sep, but deal with things like spaces in filenames
131+
const href = encodeURI(`${route}${self.path.join(dirPath, entryName)}`);
132+
let icon;
133+
let alt;
134+
135+
// TODO: switch this to entry.isDirectory() if possible
136+
if (ContentType.isImage(ext)) {
137+
icon = icons.image2;
138+
alt = '[IMG]';
139+
} else if (ContentType.isMedia(ext)) {
140+
icon = icons.movie;
141+
alt = '[MOV]';
142+
} else if (!ext) {
143+
icon = icons.folder;
144+
alt = '[DIR]';
145+
} else {
146+
icon = icons.text;
147+
alt = '[TXT]';
148+
}
149+
150+
return formatRow(icon, alt, href, entryName, entry.mtime, entry.size);
151+
}).join('\n');
152+
153+
return {
154+
body: header + rows + footer,
155+
config: {
156+
status: 200,
157+
statusText: 'OK',
158+
headers: {'Content-Type': 'text/html'}
159+
}
160+
};
161+
}
148162

149-
return formatRow(icon, alt, href, entryName, entry.mtime, entry.size);
150-
}).join('\n');
163+
function formatFile(path, content) {
164+
return {
165+
body: content,
166+
config: {
167+
status: 200,
168+
statusText: 'OK',
169+
headers: {'Content-Type': ContentType.getMimeType(path)}
170+
}
171+
};
172+
}
151173

152-
return {
153-
body: header + rows + footer,
154-
config: {
155-
status: 200,
156-
statusText: 'OK',
157-
headers: {'Content-Type': 'text/html'}
158-
}
159-
};
160-
}
161-
162-
function formatFile(path, content) {
163-
return {
164-
body: content,
165-
config: {
166-
status: 200,
167-
statusText: 'OK',
168-
headers: {'Content-Type': ContentType.getMimeType(path)}
169-
}
174+
self.HtmlFormatter = {
175+
format404,
176+
format500,
177+
formatDir,
178+
formatFile
170179
};
171-
}
172-
173-
self.HtmlFormatter = {
174-
format404,
175-
format500,
176-
formatDir,
177-
formatFile
178-
};
180+
}());
179181
}

0 commit comments

Comments
 (0)