Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead authored Oct 17, 2016
1 parent d61a9b8 commit 9fabe33
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/node-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Server.prototype.serve = function (req, res, callback) {
/* Check if we should consider sending a gzip version of the file based on the
* file content type and client's Accept-Encoding header value.
*/
Server.prototype.gzipOk = function(req, contentType) {
Server.prototype.gzipOk = function (req, contentType) {
var enable = this.options.gzip;
if(enable &&
(typeof enable === 'boolean' ||
Expand All @@ -206,20 +206,17 @@ Server.prototype.gzipOk = function(req, contentType) {
/* Send a gzipped version of the file if the options and the client indicate gzip is enabled and
* we find a .gz file mathing the static resource requested.
*/
Server.prototype.respondGzip = function(pathname, status, contentType, _headers, files, stat, req, res, finish) {
Server.prototype.respondGzip = function (pathname, status, contentType, _headers, files, stat, req, res, finish) {
var that = this;
if(files.length == 1 && this.gzipOk(req, contentType)) {
if (files.length == 1 && this.gzipOk(req, contentType)) {
var gzFile = files[0] + ".gz";
fs.stat(gzFile, function(e, gzStat) {
if(!e && gzStat.isFile()) {
//console.log('Serving', gzFile, 'to gzip-capable client instead of', files[0], 'new size is', gzStat.size, 'uncompressed size', stat.size);
fs.stat(gzFile, function (e, gzStat) {
if (!e && gzStat.isFile()) {
var vary = _headers['Vary'];
_headers['Vary'] = (vary && vary != 'Accept-Encoding'?vary+', ':'')+'Accept-Encoding';
_headers['Vary'] = (vary && vary != 'Accept-Encoding' ? vary + ', ' : '') + 'Accept-Encoding';
_headers['Content-Encoding'] = 'gzip';
stat.size = gzStat.size;
files = [gzFile];
} else {
//console.log('gzip file not found or error finding it', gzFile, String(e), stat.isFile());
}
that.respondNoGzip(pathname, status, contentType, _headers, files, stat, req, res, finish);
});
Expand All @@ -229,7 +226,7 @@ Server.prototype.respondGzip = function(pathname, status, contentType, _headers,
}
}

Server.prototype.parseByteRange = function(req, stat) {
Server.prototype.parseByteRange = function (req, stat) {
var byteRange = {
from: 0,
to: 0,
Expand Down Expand Up @@ -328,12 +325,11 @@ Server.prototype.respondNoGzip = function (pathname, status, contentType, _heade
'Content-Range',
'Content-Type',
'Expires',
'Last-Modified'].forEach(function(entityHeader) {
'Last-Modified'].forEach(function (entityHeader) {
delete headers[entityHeader];
});
finish(304, headers);
} else {

res.writeHead(status, headers);

this.stream(key, files, length, startByte, res, function (e) {
Expand Down

0 comments on commit 9fabe33

Please sign in to comment.