From f85a0f32ca0fcd90bb950823ae57c10fb83c7622 Mon Sep 17 00:00:00 2001 From: Boris Dolgov Date: Wed, 6 Nov 2024 00:14:03 +0100 Subject: [PATCH] Serve the index file only if the the original URL has trailing slash. This change makes the behaviour consistent with serve-static when redirects are enabled and non-root mount is used. --- index.js | 3 ++- package-lock.json | 2 ++ package.json | 1 + test/e2e.spec.js | 33 +++++++++++++++++++++++++++++---- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a6f0368..d8e8e94 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ let fs = require("fs"); +var parseUrl = require('parseurl') let serveStatic = require("serve-static"); let sanitizeOptions = require("./util/options").sanitizeOptions; let findEncoding = require("./util/encoding-selection").findEncoding; @@ -91,7 +92,7 @@ function expressStaticGzipMiddleware(root, options) { function changeUrlFromDirectoryToIndexFile(req) { const parts = req.url.split('?'); - if (opts.index && parts[0].endsWith("/")) { + if (opts.index && parts[0].endsWith("/") && parseUrl.original(req).pathname.endsWith("/")) { parts[0] += opts.index; req.url = parts.length > 1 ? parts.join('?') : parts[0]; } diff --git a/package-lock.json b/package-lock.json index cae5284..41f756a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "2.1.8", "license": "MIT", "dependencies": { + "parseurl": "^1.3.3", "serve-static": "^1.16.2" }, "devDependencies": { @@ -2471,6 +2472,7 @@ "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", "engines": { "node": ">= 0.8" } diff --git a/package.json b/package.json index 06ede93..a11a817 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ }, "license": "MIT", "dependencies": { + "parseurl": "^1.3.3", "serve-static": "^1.16.2" }, "devDependencies": { diff --git a/test/e2e.spec.js b/test/e2e.spec.js index 279c7c6..647e31c 100644 --- a/test/e2e.spec.js +++ b/test/e2e.spec.js @@ -1,7 +1,7 @@ const expect = require('chai').expect; const express = require('express'); -const request = require('request'); +const request = require('request').defaults({ followRedirect: false }); const serveStaticGzip = require('../index'); describe('End to end', function () { @@ -74,6 +74,30 @@ describe('End to end', function () { }); }); + it('should redirect to a directory with trailing slash with non-root mount', function() { + setupServer(null, null, '/custom'); + return requestFile('/custom', { 'accept-encoding': 'br' }).then(resp => { + expect(resp.statusCode).to.equal(301); + expect(resp.headers['location']).to.equal('/custom/'); + }); + }); + + it('should serve compressed index with non-root mount', function() { + setupServer(null, null, '/custom'); + return requestFile('/custom/', { 'accept-encoding': 'gzip' }).then(resp => { + expect(resp.statusCode).to.equal(200); + expect(resp.body).to.equal('index.html.gz'); + }); + }); + + it('should serve compressed file with non-root mount', function() { + setupServer({ enableBrotli: true }, null, '/custom'); + return requestFile('/custom/main.js', { 'accept-encoding': 'br ' }).then(resp => { + expect(resp.statusCode).to.equal(200); + expect(resp.body).to.equal('main.js.br'); + }); + }); + it('should not serve brotli if not enabled', function () { setupServer(); @@ -186,7 +210,7 @@ describe('End to end', function () { }); }); - it('should handle subforlders', function () { + it('should handle subfolders', function () { setupServer(null, 'wwwroot.gzipped'); return requestFile("/css/style.css", { 'accept-encoding': 'gzip' }).then(resp => { @@ -257,10 +281,11 @@ describe('End to end', function () { * * @param {expressStaticGzip.ExpressStaticGzipOptions} options */ - function setupServer(options, dir) { + function setupServer(options, dir, mount) { dir = dir || 'wwwroot'; + mount = mount || '/'; const app = express(); - app.use(serveStaticGzip(__dirname + '/' + dir, options)); + app.use(mount, serveStaticGzip(__dirname + '/' + dir, options)); server = app.listen(8181); } });