From 559e28424fe71166087f5c51b605fd5b3edc0b09 Mon Sep 17 00:00:00 2001
From: flakey5 <73616808+flakey5@users.noreply.github.com>
Date: Mon, 13 Nov 2023 11:39:51 -0800
Subject: [PATCH] fix: use same directory listing format as nginx
---
scripts/compile-handlebars.js | 4 +--
src/handlers/strategies/directoryListing.ts | 14 ++------
src/templates/directoryListing.hbs | 36 ++++-----------------
src/templates/directoryListing.out.js | 2 +-
4 files changed, 12 insertions(+), 44 deletions(-)
diff --git a/scripts/compile-handlebars.js b/scripts/compile-handlebars.js
index 076ccd0..16248bd 100644
--- a/scripts/compile-handlebars.js
+++ b/scripts/compile-handlebars.js
@@ -36,9 +36,9 @@ templatesToParse.then(files => {
// The Handlebars.precompile returns a JavaScript object
// and then we make a default export of the object
- const javascriptTemplate = `export default ${removeStringIndents(
+ const javascriptTemplate = `export default ${
compiledTemplate
- )}`;
+ }`;
const outputFilename = filePath.replace('.hbs', '.out.js');
diff --git a/src/handlers/strategies/directoryListing.ts b/src/handlers/strategies/directoryListing.ts
index 34a7933..3d5b80a 100644
--- a/src/handlers/strategies/directoryListing.ts
+++ b/src/handlers/strategies/directoryListing.ts
@@ -35,17 +35,7 @@ export function renderDirectoryListing(
env: Env
): Response {
// Holds the contents of the listing (directories and files)
- const tableElements = [];
-
- // There shouldn't really be a case where we're listing the root
- // directory (/) when this is deployed, so always add the option
- // to go up a directory
- tableElements.push({
- href: '../',
- name: '../',
- lastModified: '-',
- size: '-',
- });
+ const tableElements: object[] = [];
const urlPathname = `${url.pathname}${url.pathname.endsWith('/') ? '' : '/'}`;
@@ -54,7 +44,7 @@ export function renderDirectoryListing(
const extra = encodeURIComponent(name.substring(0, name.length - 1));
tableElements.push({
- href: `${urlPathname}${extra}/`,
+ href: `${extra}/`,
name,
lastModified: '-',
size: '-',
diff --git a/src/templates/directoryListing.hbs b/src/templates/directoryListing.hbs
index b9a7e4c..87d6356 100644
--- a/src/templates/directoryListing.hbs
+++ b/src/templates/directoryListing.hbs
@@ -1,31 +1,9 @@
-
- Index of {{pathname}}
-
-
-
-
-
- Index of {{pathname}}
-
-
- Filename |
- Modified |
- Size |
-
- {{#each entries}}
-
- {{name}} |
- {{lastModified}} |
- {{size}} |
-
- {{/each}}
-
-
+Index of {{pathname}}
+
+Index of {{pathname}}
../
+{{#each entries}}
+{{name}} {{lastModified}} {{size}}
+{{/each}}
+
\ No newline at end of file
diff --git a/src/templates/directoryListing.out.js b/src/templates/directoryListing.out.js
index ec461bf..97f7d41 100644
--- a/src/templates/directoryListing.out.js
+++ b/src/templates/directoryListing.out.js
@@ -1 +1 @@
-export default{1:function(t,e,n,l,a){var o,i=t.strict,r=t.lambda;return""+(null!=(o=r(i(e,"name",{start:{line:24,column:35},end:{line:24,column:39}}),e))?o:"")+" | "+(null!=(o=r(i(e,"lastModified",{start:{line:25,column:16},end:{line:25,column:28}}),e))?o:"")+" | "+(null!=(o=r(i(e,"size",{start:{line:26,column:16},end:{line:26,column:20}}),e))?o:"")+" |
"},compiler:[8,">= 4.3.0"],main:function(t,e,n,l,a){var o,i=t.strict,r=t.lambda,c=t.lookupProperty||function(t,e){if(Object.prototype.hasOwnProperty.call(t,e))return t[e]};return"Index of "+(null!=(o=r(i(e,"pathname",{start:{line:3,column:22},end:{line:3,column:30}}),e))?o:"")+"Index of "+(null!=(o=r(i(e,"pathname",{start:{line:15,column:19},end:{line:15,column:27}}),e))?o:"")+"
Filename | Modified | Size |
"+(null!=(o=c(n,"each").call(null!=e?e:t.nullContext||{},c(e,"entries"),{name:"each",hash:{},fn:t.program(1,a,0),inverse:t.noop,data:a,loc:{start:{line:22,column:6},end:{line:28,column:15}}}))?o:"")+"
"},useData:!0};
+export default{1:function(n,l,e,t,a){var r,o=n.strict,u=n.lambda;return''+(null!=(r=u(o(l,"name",{start:{line:6,column:21},end:{line:6,column:25}}),l))?r:"")+" "+(null!=(r=u(o(l,"lastModified",{start:{line:6,column:34},end:{line:6,column:46}}),l))?r:"")+" "+(null!=(r=u(o(l,"size",{start:{line:6,column:51},end:{line:6,column:55}}),l))?r:"")+"
\n"},compiler:[8,">= 4.3.0"],main:function(n,l,e,t,a){var r,o=n.strict,u=n.lambda,i=n.lookupProperty||function(n,l){if(Object.prototype.hasOwnProperty.call(n,l))return n[l]};return"\nIndex of "+(null!=(r=u(o(l,"pathname",{start:{line:2,column:24},end:{line:2,column:32}}),l))?r:"")+"\n\nIndex of "+(null!=(r=u(o(l,"pathname",{start:{line:4,column:15},end:{line:4,column:23}}),l))?r:"")+'
../
\n'+(null!=(r=i(e,"each").call(null!=l?l:n.nullContext||{},i(l,"entries"),{name:"each",hash:{},fn:n.program(1,a,0),inverse:n.noop,data:a,loc:{start:{line:5,column:0},end:{line:7,column:9}}}))?r:"")+"
\n"},useData:!0};