From 90f5b9dc8dfdc3679e34bf210a364f1891e715db Mon Sep 17 00:00:00 2001 From: Sean Gregory Date: Tue, 6 Feb 2018 16:51:11 -0600 Subject: [PATCH 01/15] pretty file index and page layout --- errors/403.html | 42 ++++++---------------- errors/404.html | 37 +++++-------------- errors/405.html | 40 ++++++--------------- errors/500.html | 42 +++++----------------- errors/shared/layout.html | 76 +++++++++++++++++++++++++++++++++++++++ index.js | 53 ++++++++++++++++++++++++--- package.json | 1 + 7 files changed, 164 insertions(+), 127 deletions(-) create mode 100644 errors/shared/layout.html diff --git a/errors/403.html b/errors/403.html index dc69571..80da810 100644 --- a/errors/403.html +++ b/errors/403.html @@ -1,18 +1,5 @@ - - - Forbidden - - - -
-
+
+
  __ __       __      __     
 /\ \\ \    /'__`\  /'__`\   
 \ \ \\ \  /\ \/\ \/\_\L\ \  
@@ -21,27 +8,18 @@
    \/_/\_\_/\ \____/\ \____/
       \/_/   \/___/  \/___/ 
                             
-            
-
-
-
+
+
 
       __   __   ___  __   __      ___  __   __   __     __   __   ___      
  /\  /  ` /  ` |__  /__` /__`    |__  /  \ |__) |__) | |  \ |  \ |__  |\ | 
 /~~\ \__, \__, |___ .__/ .__/    |    \__/ |  \ |__) | |__/ |__/ |___ | \| 
                        
 
-                                                                           
-			
-
-

We're sorry, but you can't access this information right now.

-

Please contact the webmaster for more information.

- -
- This page is powered by Glance -
-
- - - + + + +
+

We're sorry, but you can't access this information right now.

+

Please contact the webmaster for more information.

diff --git a/errors/404.html b/errors/404.html index b8d9095..75dd76d 100644 --- a/errors/404.html +++ b/errors/404.html @@ -1,18 +1,5 @@ - - - File Not Found - - - -
-
+
+
  __ __       __   __ __      
 /\ \\ \    /'__`\/\ \\ \     
 \ \ \\ \  /\ \/\ \ \ \\ \    
@@ -21,8 +8,8 @@
    \/_/\_\_/\ \____/\/_/\_\_/
       \/_/   \/___/    \/_/  
                            
-			
-
+
+
 
 
  ___         ___          __  ___     ___  __             __  
@@ -31,14 +18,8 @@
 
                                                               
                                                               
-			
-
-

This resource does not appear to exist

-

Please contact the webmaster for more information.

- -
- This page is powered by Glance -
-
- - \ No newline at end of file + + +
+

This resource does not appear to exist

+

Please contact the webmaster for more information.

diff --git a/errors/405.html b/errors/405.html index 06c3a83..ce8df7f 100644 --- a/errors/405.html +++ b/errors/405.html @@ -1,18 +1,5 @@ - - - Method Not Allowed - - - -
-
+
+
  __ __       __   ______    
 /\ \\ \    /'__`\/\  ___\   
 \ \ \\ \  /\ \/\ \ \ \__/   
@@ -21,25 +8,18 @@
    \/_/\_\_/\ \____/\ \____/
       \/_/   \/___/  \/___/ 
                     
-			
-
-
+
+
 
        ___ ___       __   __           __  ___                    __        ___  __  
  |\/| |__   |  |__| /  \ |  \    |\ | /  \  |      /\  |    |    /  \ |  | |__  |  \ 
  |  | |___  |  |  | \__/ |__/    | \| \__/  |     /~~\ |___ |___ \__/ |/\| |___ |__/ 
                                                                                      
 
+
+
+
+

We're sorry, The method you are using to access this resource is not allowed.

+

You may be attempting to run a script that is not supported. +Please contact the webmaster for more information.

-
-
-

We're sorry, The method you are using to access this resource is not allowed.

-

You may be attempting to run a script that is not supported. - Please contact the webmaster for more information.

- -
- This page is powered by Glance -
-
- - diff --git a/errors/500.html b/errors/500.html index 7a3ceee..32b03f3 100644 --- a/errors/500.html +++ b/errors/500.html @@ -1,19 +1,5 @@ - - - Internal Server Error - - - -
-
+
+
  ______     __      __     
 /\  ___\  /'__`\  /'__`\   
 \ \ \__/ /\ \/\ \/\ \/\ \  
@@ -22,27 +8,17 @@
    \ \____/\ \____/\ \____/
     \/___/  \/___/  \/___/ 
                            
-			
-
-
+
+
 
        ___  ___  __                     __   ___  __        ___  __      ___  __   __   __   __  
 | |\ |  |  |__  |__) |\ |  /\  |       /__` |__  |__) \  / |__  |__)    |__  |__) |__) /  \ |__) 
 | | \|  |  |___ |  \ | \| /~~\ |___    .__/ |___ |  \  \/  |___ |  \    |___ |  \ |  \ \__/ |  \ 
     
 
-
-			
-
-

We're sorry, something went wrong.

-

Please contact the webmaster for more information.

- - -
- This page is powered by Glance -
-
- - - + + +
+

We're sorry, something went wrong.

+

Please contact the webmaster for more information.

diff --git a/errors/shared/layout.html b/errors/shared/layout.html new file mode 100644 index 0000000..cb0f3e3 --- /dev/null +++ b/errors/shared/layout.html @@ -0,0 +1,76 @@ + + + + {{ title }} + + + +
+ {{ body }} +
+ + + diff --git a/index.js b/index.js index 31dfe06..9ad68b4 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ var fileExists = require('utils-fs-exists') var htmlls = require('html-ls') var filed = require('filed') var xtend = require('xtend') +var replace = require('stream-replace') var defaults = require('./lib/config') @@ -130,7 +131,19 @@ Glance.prototype.serveRequest = function Glance$serveRequest (req, res) { var listPath = request.fullPath.replace(/\/$/, '') res.writeHead(200, RESPONSE_HEADERS) - htmlls(listPath, {hideDot: self.nodot}).pipe(res) + + let listingHtml = '

Directory Listing

' + + let listing = htmlls(listPath, {hideDot: self.nodot}) + + listing.on('data', function(buf){ + listingHtml += buf.toString(); + }) + + listing.on('end', function(){ + console.log('end'); + renderPage('Directory Listing', listingHtml, res); + }) return self.emit('read', request) } @@ -139,9 +152,41 @@ Glance.prototype.serveRequest = function Glance$serveRequest (req, res) { function showError (errorCode, req, res) { res.writeHead(errorCode, RESPONSE_HEADERS) - fs.createReadStream( - path.join(__dirname, 'errors', errorCode + '.html') - ).pipe(res) + + let errorHtml = '' + + let errorPage = fs.createReadStream( + path.join(__dirname, 'errors', errorCode + '.html') + ) + + errorPage.on('data', function(buf){ + errorHtml += buf.toString(); + }) + + errorPage.on('end', function(){ + let title = errorTitle(errorCode) + renderPage(title, errorHtml, res); + }) +} + +function renderPage (title, body, res) { + let layout = fs.createReadStream( + path.join(__dirname, 'errors/shared/layout.html') + ) + layout + .pipe(replace(/{{\s*title\s*}}/g, title)) + .pipe(replace(/{{\s*body\s*}}/g, body)) + .pipe(res) +} + +function errorTitle (errorCode) { + let mappings = { + '404': 'File Not Found', + '403': 'Forbidden', + '405': 'Method Not Allowed', + '500': 'Internal Server Error' + } + return mappings['' + errorCode]; } function createGlance (options) { diff --git a/package.json b/package.json index 63069a8..7ce4210 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "html-ls": "1.0.0", "mime": "1.2.6", "nopt": "3.0.4", + "stream-replace": "1.0.0", "utils-fs-exists": "1.0.1", "xtend": "4.0.0" }, From 445db02f5b289a57e7b2fbc3622c8e61d86f6c7b Mon Sep 17 00:00:00 2001 From: Sean Gregory Date: Tue, 6 Feb 2018 16:59:15 -0600 Subject: [PATCH 02/15] whoops --- index.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 9ad68b4..78cc7b3 100644 --- a/index.js +++ b/index.js @@ -136,13 +136,12 @@ Glance.prototype.serveRequest = function Glance$serveRequest (req, res) { let listing = htmlls(listPath, {hideDot: self.nodot}) - listing.on('data', function(buf){ - listingHtml += buf.toString(); + listing.on('data', function (buf) { + listingHtml += buf.toString() }) - listing.on('end', function(){ - console.log('end'); - renderPage('Directory Listing', listingHtml, res); + listing.on('end', function () { + renderPage('Directory Listing', listingHtml, res) }) return self.emit('read', request) @@ -159,13 +158,13 @@ function showError (errorCode, req, res) { path.join(__dirname, 'errors', errorCode + '.html') ) - errorPage.on('data', function(buf){ - errorHtml += buf.toString(); + errorPage.on('data', function (buf) { + errorHtml += buf.toString() }) - errorPage.on('end', function(){ + errorPage.on('end', function () { let title = errorTitle(errorCode) - renderPage(title, errorHtml, res); + renderPage(title, errorHtml, res) }) } @@ -182,11 +181,11 @@ function renderPage (title, body, res) { function errorTitle (errorCode) { let mappings = { '404': 'File Not Found', - '403': 'Forbidden', + '403': 'Forbidden', '405': 'Method Not Allowed', '500': 'Internal Server Error' - } - return mappings['' + errorCode]; + } + return mappings['' + errorCode] } function createGlance (options) { From 1fee25e78a98b707c95252227d7671b85b32a966 Mon Sep 17 00:00:00 2001 From: Sean Gregory Date: Tue, 6 Feb 2018 17:02:36 -0600 Subject: [PATCH 03/15] oh cool i can test right from the repo --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 78cc7b3..a1292b2 100644 --- a/index.js +++ b/index.js @@ -142,7 +142,7 @@ Glance.prototype.serveRequest = function Glance$serveRequest (req, res) { listing.on('end', function () { renderPage('Directory Listing', listingHtml, res) - }) + }) return self.emit('read', request) } @@ -184,7 +184,7 @@ function errorTitle (errorCode) { '403': 'Forbidden', '405': 'Method Not Allowed', '500': 'Internal Server Error' - } + } return mappings['' + errorCode] } From 87b1ff6e1440d07a33dc7b5ba33a3200f0f6cad1 Mon Sep 17 00:00:00 2001 From: Sean Gregory Date: Wed, 7 Feb 2018 10:02:17 -0600 Subject: [PATCH 04/15] var instead of let --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index a1292b2..da7c359 100644 --- a/index.js +++ b/index.js @@ -132,9 +132,9 @@ Glance.prototype.serveRequest = function Glance$serveRequest (req, res) { res.writeHead(200, RESPONSE_HEADERS) - let listingHtml = '

Directory Listing

' + var listingHtml = '

Directory Listing

' - let listing = htmlls(listPath, {hideDot: self.nodot}) + var listing = htmlls(listPath, {hideDot: self.nodot}) listing.on('data', function (buf) { listingHtml += buf.toString() @@ -152,9 +152,9 @@ Glance.prototype.serveRequest = function Glance$serveRequest (req, res) { function showError (errorCode, req, res) { res.writeHead(errorCode, RESPONSE_HEADERS) - let errorHtml = '' + var errorHtml = '' - let errorPage = fs.createReadStream( + var errorPage = fs.createReadStream( path.join(__dirname, 'errors', errorCode + '.html') ) @@ -163,13 +163,13 @@ function showError (errorCode, req, res) { }) errorPage.on('end', function () { - let title = errorTitle(errorCode) + var title = errorTitle(errorCode) renderPage(title, errorHtml, res) }) } function renderPage (title, body, res) { - let layout = fs.createReadStream( + var layout = fs.createReadStream( path.join(__dirname, 'errors/shared/layout.html') ) layout @@ -179,7 +179,7 @@ function renderPage (title, body, res) { } function errorTitle (errorCode) { - let mappings = { + var mappings = { '404': 'File Not Found', '403': 'Forbidden', '405': 'Method Not Allowed', From 63968e401426bb6af6057aa76fb93eaad0cd5e86 Mon Sep 17 00:00:00 2001 From: Sean Gregory Date: Wed, 7 Feb 2018 12:51:26 -0600 Subject: [PATCH 05/15] fix long lists --- errors/shared/layout.html | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/errors/shared/layout.html b/errors/shared/layout.html index cb0f3e3..1f010c8 100644 --- a/errors/shared/layout.html +++ b/errors/shared/layout.html @@ -23,13 +23,10 @@ } .main { width: 60%; - height: 60%; font-size: 18px; - margin:0px auto; align-items: flex-start; display:flex; flex-direction:column; - justify-content: center; } .main ul { list-style-type: none; @@ -56,7 +53,8 @@ align-items: center; } footer{ - margin: 3em; + padding: 3em; + margin-bottom: 3em; font-size:11px; color:#6b6b6b; } From 7db334f3ed5766fadb3ca017bff8785be013c1f6 Mon Sep 17 00:00:00 2001 From: Sean Gregory Date: Thu, 8 Feb 2018 07:09:21 -0600 Subject: [PATCH 06/15] remove typecast hack --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index da7c359..0790808 100644 --- a/index.js +++ b/index.js @@ -185,7 +185,7 @@ function errorTitle (errorCode) { '405': 'Method Not Allowed', '500': 'Internal Server Error' } - return mappings['' + errorCode] + return mappings[errorCode.toString()] } function createGlance (options) { From 3778cb27651272afe16defe685dfdfdd21bc8af9 Mon Sep 17 00:00:00 2001 From: Sean Gregory Date: Thu, 8 Feb 2018 10:11:05 -0600 Subject: [PATCH 07/15] better --- errors/shared/layout.html | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/errors/shared/layout.html b/errors/shared/layout.html index 1f010c8..314252a 100644 --- a/errors/shared/layout.html +++ b/errors/shared/layout.html @@ -5,7 +5,7 @@