express-static-gzip
Advanced tools
Comparing version
30
index.js
@@ -1,3 +0,5 @@ | ||
var serveStatic = require("serve-static"); | ||
var mime = serveStatic.mime; | ||
let serveStatic = require('serve-static'); | ||
let parseOptions = require('./util/options').parseOptions; | ||
let findEncoding = require('./util/encoding-selection').findEncoding; | ||
let mime = serveStatic.mime; | ||
@@ -11,13 +13,13 @@ module.exports = expressStaticGzip; | ||
* @param {string} rootFolder: folder to staticly serve files from | ||
* @param {{enableBrotli:boolean, customCompressions:[{encodingName:string,fileExtension:string}], indexFromEmptyFile:boolean}} options: options to change module behaviour | ||
* @param {{enableBrotli?:boolean, customCompressions?:[{encodingName:string,fileExtension:string}], index?: boolean}} options: options to change module behaviour | ||
* @returns express middleware function | ||
*/ | ||
function expressStaticGzip(rootFolder, options) { | ||
options = options || {}; | ||
if (typeof (options.indexFromEmptyFile) === "undefined") options.indexFromEmptyFile = true; | ||
// strip away unnecessary options | ||
let opts = parseOptions(options); | ||
//create a express.static middleware to handle serving files | ||
var defaultStatic = serveStatic(rootFolder, options), | ||
compressions = [], | ||
files = {}; | ||
let defaultStatic = serveStatic(rootFolder, options); | ||
let compressions = []; | ||
let files = {}; | ||
@@ -45,3 +47,3 @@ //read compressions from options | ||
//use the first matching compression to serve a compresed file | ||
var compression = findAvailableCompressionForFile(matchedFile.compressions, acceptEncoding); | ||
var compression = findEncoding(acceptEncoding, matchedFile.compressions); | ||
if (compression) { | ||
@@ -61,5 +63,5 @@ convertToCompressedRequest(req, res, compression); | ||
//register all provided compressions | ||
if (options.customCompressions && options.customCompressions.length > 0) { | ||
for (var i = 0; i < options.customCompressions.length; i++) { | ||
var customCompression = options.customCompressions[i]; | ||
if (opts.customCompressions && opts.customCompressions.length > 0) { | ||
for (var i = 0; i < opts.customCompressions.length; i++) { | ||
var customCompression = opts.customCompressions[i]; | ||
registerCompression(customCompression.encodingName, customCompression.fileExtension); | ||
@@ -70,3 +72,3 @@ } | ||
//enable brotli compression | ||
if (options.enableBrotli) { | ||
if (opts.enableBrotli) { | ||
registerCompression("br", "br"); | ||
@@ -103,3 +105,3 @@ } | ||
function changeUrlFromEmptyToIndexHtml(req) { | ||
if (options.indexFromEmptyFile && req.url.endsWith("/")) { | ||
if (opts.index && req.url.endsWith("/")) { | ||
req.url += "index.html"; | ||
@@ -106,0 +108,0 @@ } |
{ | ||
"name": "express-static-gzip", | ||
"version": "0.3.2", | ||
"version": "1.0.0", | ||
"description": "simple wrapper on top of express.static, that allows serving pre-gziped files", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test/*.spec.js" | ||
}, | ||
"keywords": [ | ||
@@ -20,3 +23,9 @@ "express", | ||
"serve-static": "^1.12.3" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"express": "^4.16.3", | ||
"mocha": "^5.2.0", | ||
"request": "^2.87.0" | ||
} | ||
} |
@@ -46,17 +46,36 @@ # express-static-gzip | ||
Compressions are selected in the following order if a file is requested from the middleware: | ||
* any custom compression in the order they are provided to *options.customCompressions* | ||
* brotli (if enabled via *options.enableBrotli*) | ||
* gzip | ||
* plain file (in case no compression exists or none is matching the browsers accepted encodings header) | ||
* in order of the requests 'accept-encoding' header content (if no quality if provided) | ||
* in order of their respective quality (if provided) | ||
* in case of a wildcard '*', the compression is selected in alphabetical order (for now) | ||
* plain file (in case no compression exists or none is matching the browsers accept-encoding header) | ||
When the middleware is created it will check the given root folder and all subfolders for files matching the registered compression. Adding files later to the folder will not be recognized by the middleware. | ||
For more details see [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding), but not all of it is implemented at the moment. | ||
When the middleware is created it will check the given root folder and all subfolders for files matching the registered compression. **Adding files later to the folder will not be recognized by the middleware.** | ||
# Available options | ||
* **`enableBrotli`**: boolean (default: **false**) | ||
This will enable brotli compression in addition to gzip | ||
* **`index`**: boolean (default: **true**) | ||
If not set to false, any request to '/' or 'somepath/' will be answered with the file '/index.html' or 'somepath/index.html' in an accepted compression | ||
* **`indexFromEmptyFile`** (**deprecated**, see `index` option) | ||
* **`customCompressions`**: [{encodingName: string, fileExtension: string}] | ||
Using this option, you can add any other compressions you would like. `encodingName` will be checked against the `Accept`-Header. `fileExtension` is used to find files using this compression. `fileExtension` does not require a dot (not ~~'.gz'~~, but `'gz'`). | ||
# Behavior warning | ||
In default mode a request for "/" or "\<somepath\>/" will now serve index.html as compressed version. This could lead to **complications if you are serving a REST API** from the same path, when the *express-server-static* is registered before your API. | ||
In default mode a request for "/" or "\<somepath\>/" will serve index.html as compressed version. This could lead to **complications if you are serving a REST API** from the same path, when the *express-server-static* is registered before your API. | ||
One solution would be to register *express-server-static* last. Otherweise you can set **options.indexFromEmptyFile** to false: | ||
One solution would be to register *express-server-static* last. Otherweise you can set **options.index** to false: | ||
```javascript | ||
app.use("/", expressStaticGzip("/my/rootFolder/", { indexFromEmptyFile: false })); | ||
app.use("/", expressStaticGzip("/my/rootFolder/", { index: false })); | ||
``` | ||
@@ -69,7 +88,7 @@ | ||
* rootFolder | ||
* index.html | ||
* index.html.gz | ||
* index.html.br | ||
* test.html.gz | ||
* main.js | ||
* index.html | ||
* index.html.gz | ||
* index.html.br | ||
* test.html.gz | ||
* main.js | ||
@@ -76,0 +95,0 @@ and you use set the *enableBrotli* flag to true, express-static-gzip will answer GET requests like this: |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
26060
111.23%15
275%485
166.48%1
-50%102
22.89%4
Infinity%2
100%1
Infinity%