@@ -23,44 +23,45 @@ importScripts('phoenix/virtualServer/content-type.js');
2323importScripts ( 'phoenix/virtualServer/icons.js' ) ;
2424
2525if ( ! 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> </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> </td>
123124 <td align='right'> - </td><td> </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