Skip to content

Commit fe91caf

Browse files
authored
Merge pull request #230 from jfhbrook/revert-228-brotli-encoding
Revert "Add support for brotli encoding"
2 parents a3f9359 + 63b6e11 commit fe91caf

12 files changed

+12
-264
lines changed

README.md

-11
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ const opts = {
9797
cache: 'max-age=3600',
9898
cors: false,
9999
gzip: true,
100-
brotli: false,
101100
defaultExt: 'html',
102101
handleError: true,
103102
serverHeader: true,
@@ -210,16 +209,6 @@ that the behavior is appropriate. If `./public/some-file.js.gz` is not valid
210209
gzip, this will fall back to `./public/some-file.js`. You can turn this off
211210
with `opts.gzip === false`.
212211

213-
### `opts.brotli`
214-
### `--brotli`
215-
216-
Serve `./public/some-file.js.br` in place of `./public/some-file.js` when the
217-
[brotli encoded](https://github.com/google/brotli) version exists and ecstatic
218-
determines that the behavior is appropriate. If the request does not contain
219-
`br` in the HTTP `accept-encoding` header, ecstatic will instead attempt to
220-
serve a gzipped version (if `opts.gzip` is `true`), or fall back to
221-
`./public.some-file.js`. Defaults to **false**.
222-
223212
### `opts.serverHeader`
224213
### `--no-server-header`
225214

lib/ecstatic.js

+12-50
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function decodePathname(pathname) {
3232

3333

3434
// Check to see if we should try to compress a file with gzip.
35-
function shouldCompressGzip(req) {
35+
function shouldCompress(req) {
3636
const headers = req.headers;
3737

3838
return headers && headers['accept-encoding'] &&
@@ -42,16 +42,6 @@ function shouldCompressGzip(req) {
4242
;
4343
}
4444

45-
function shouldCompressBrotli(req) {
46-
const headers = req.headers;
47-
48-
return headers && headers['accept-encoding'] &&
49-
headers['accept-encoding']
50-
.split(',')
51-
.some(el => ['*', 'br'].indexOf(el.trim()) !== -1)
52-
;
53-
}
54-
5545
function hasGzipId12(gzipped, cb) {
5646
const stream = fs.createReadStream(gzipped, { start: 0, end: 1 });
5747
let buffer = Buffer('');
@@ -176,8 +166,7 @@ module.exports = function createMiddleware(_dir, _options) {
176166
const parsed = url.parse(req.url);
177167
let pathname = null;
178168
let file = null;
179-
let gzippedFile = null;
180-
let brotliFile = null;
169+
let gzipped = null;
181170

182171
// Strip any null bytes from the url
183172
// This was at one point necessary because of an old bug in url.parse
@@ -209,9 +198,7 @@ module.exports = function createMiddleware(_dir, _options) {
209198
path.relative(path.join('/', baseDir), pathname)
210199
)
211200
);
212-
// determine compressed forms if they were to exist
213-
gzippedFile = `${file}.gz`;
214-
brotliFile = `${file}.br`;
201+
gzipped = `${file}.gz`;
215202

216203
if (serverHeader !== false) {
217204
// Set common headers.
@@ -242,7 +229,7 @@ module.exports = function createMiddleware(_dir, _options) {
242229

243230
function serve(stat) {
244231
// Do a MIME lookup, fall back to octet-stream and handle gzip
245-
// and brotli special case.
232+
// special case.
246233
const defaultType = opts.contentType || 'application/octet-stream';
247234
let contentType = mime.lookup(file, defaultType);
248235
let charSet;
@@ -251,21 +238,19 @@ module.exports = function createMiddleware(_dir, _options) {
251238
const etag = generateEtag(stat, weakEtags);
252239
let cacheControl = cache;
253240
let stream = null;
241+
254242
if (contentType) {
255243
charSet = mime.charsets.lookup(contentType, 'utf-8');
256244
if (charSet) {
257245
contentType += `; charset=${charSet}`;
258246
}
259247
}
260248

261-
if (file === gzippedFile) { // is .gz picked up
249+
if (file === gzipped) { // is .gz picked up
262250
res.setHeader('Content-Encoding', 'gzip');
251+
263252
// strip gz ending and lookup mime type
264253
contentType = mime.lookup(path.basename(file, '.gz'), defaultType);
265-
} else if (file === brotliFile) { // is .br picked up
266-
res.setHeader('Content-Encoding', 'br');
267-
// strip br ending and lookup mime type
268-
contentType = mime.lookup(path.basename(file, '.br'), defaultType);
269254
}
270255

271256
if (typeof cacheControl === 'function') {
@@ -416,13 +401,13 @@ module.exports = function createMiddleware(_dir, _options) {
416401
});
417402
}
418403

419-
// serve gzip file if exists and is valid
420-
function tryServeWithGzip() {
421-
fs.stat(gzippedFile, (err, stat) => {
404+
// Look for a gzipped file if this is turned on
405+
if (opts.gzip && shouldCompress(req)) {
406+
fs.stat(gzipped, (err, stat) => {
422407
if (!err && stat.isFile()) {
423-
hasGzipId12(gzippedFile, (gzipErr, isGzip) => {
408+
hasGzipId12(gzipped, (gzipErr, isGzip) => {
424409
if (!gzipErr && isGzip) {
425-
file = gzippedFile;
410+
file = gzipped;
426411
serve(stat);
427412
} else {
428413
statFile();
@@ -432,29 +417,6 @@ module.exports = function createMiddleware(_dir, _options) {
432417
statFile();
433418
}
434419
});
435-
}
436-
437-
// serve brotli file if exists, otherwise try gzip
438-
function tryServeWithBrotli(shouldTryGzip) {
439-
fs.stat(brotliFile, (err, stat) => {
440-
if (!err && stat.isFile()) {
441-
file = brotliFile;
442-
serve(stat);
443-
} else if (shouldTryGzip) {
444-
tryServeWithGzip();
445-
} else {
446-
statFile();
447-
}
448-
});
449-
}
450-
451-
const shouldTryBrotli = opts.brotli && shouldCompressBrotli(req);
452-
const shouldTryGzip = opts.gzip && shouldCompressGzip(req);
453-
// always try brotli first, next try gzip, finally serve without compression
454-
if (shouldTryBrotli) {
455-
tryServeWithBrotli(shouldTryGzip);
456-
} else if (shouldTryGzip) {
457-
tryServeWithGzip();
458420
} else {
459421
statFile();
460422
}

lib/ecstatic/defaults.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"cache": "max-age=3600",
99
"cors": false,
1010
"gzip": true,
11-
"brotli": false,
1211
"defaultExt": ".html",
1312
"handleError": true,
1413
"serverHeader": true,

lib/ecstatic/opts.js

-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module.exports = (opts) => {
1414
let si = defaults.si;
1515
let cache = defaults.cache;
1616
let gzip = defaults.gzip;
17-
let brotli = defaults.brotli;
1817
let defaultExt = defaults.defaultExt;
1918
let handleError = defaults.handleError;
2019
const headers = {};
@@ -106,10 +105,6 @@ module.exports = (opts) => {
106105
gzip = opts.gzip;
107106
}
108107

109-
if (typeof opts.brotli !== 'undefined' && opts.brotli !== null) {
110-
brotli = opts.brotli;
111-
}
112-
113108
aliases.handleError.some((k) => {
114109
if (isDeclared(k)) {
115110
handleError = opts[k];
@@ -200,7 +195,6 @@ module.exports = (opts) => {
200195
defaultExt,
201196
baseDir: (opts && opts.baseDir) || '/',
202197
gzip,
203-
brotli,
204198
handleError,
205199
headers,
206200
serverHeader,

test/compression.js

-187
This file was deleted.

test/public/brotli/fake_ecstatic

-1
This file was deleted.

test/public/brotli/fake_ecstatic.br

-11 Bytes
Binary file not shown.

test/public/brotli/index.html

-1
This file was deleted.

test/public/brotli/index.html.br

-3
This file was deleted.

test/public/brotli/not_actually_brotli.br

-1
This file was deleted.

test/public/brotli/real_ecstatic

-1
This file was deleted.

test/public/brotli/real_ecstatic.br

-2
This file was deleted.

0 commit comments

Comments
 (0)