Skip to content

Commit

Permalink
Merge pull request #30 from ryanrolds/zlib
Browse files Browse the repository at this point in the history
Added zlib/gzip support
  • Loading branch information
mape committed Jan 31, 2012
2 parents 37a9946 + aff6080 commit 2ec66ae
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions lib/assetmanager.js
Expand Up @@ -7,6 +7,11 @@ var fs = require('fs')
, cssmin = require('./../deps/cssmin').minify , cssmin = require('./../deps/cssmin').minify
, crypto = require('crypto'); , crypto = require('crypto');


var zlib;
try {
zlib = require('zlib');
} catch(e) {}

var cache = {} var cache = {}
, settings = {} , settings = {}
, cacheHashes = {} , cacheHashes = {}
Expand Down Expand Up @@ -237,8 +242,25 @@ module.exports = function assetManager (assets) {


cacheHashes[groupName] = crypto.createHash('md5').update(content).digest('hex'); cacheHashes[groupName] = crypto.createHash('md5').update(content).digest('hex');


cache[groupName][match].contentBuffer = new Buffer(content, 'utf8'); cache[groupName][match].encodings = {};
cache[groupName][match].contentLength = cache[groupName][match].contentBuffer.length; var encodings = cache[groupName][match].encodings;

var utf8Buffer = new Buffer(content, 'utf8');
encodings.utf8 = {
'buffer': utf8Buffer,
'length': utf8Buffer.length,
'encoding': false
};

if(zlib) {
var gzipBuffer = zlib.gzip(utf8Buffer, function(error, result) {
encodings.gzip = {
'buffer': result,
'length': result.length,
'encoding': 'gzip'
};
});
}
}); });
}); });
}); });
Expand Down Expand Up @@ -302,6 +324,11 @@ module.exports = function assetManager (assets) {
} }
}; };


this.acceptsGzip = function(req) {
var accept = req.headers["accept-encoding"];
return accept && accept.toLowerCase().indexOf('gzip') !== -1;
}

function assetManager (req, res, next) { function assetManager (req, res, next) {
var self = this; var self = this;
var found = false; var found = false;
Expand All @@ -325,10 +352,18 @@ module.exports = function assetManager (assets) {
Object.keys(cache[groupName]).forEach(function(match) { Object.keys(cache[groupName]).forEach(function(match) {
if (!found && userAgent.match(new RegExp(match, 'i'))) { if (!found && userAgent.match(new RegExp(match, 'i'))) {
found = true; found = true;
var item = cache[groupName][match];

var content = item.encodings.utf8;
if(zlib && item.encodings.gzip && this.acceptsGzip(req)) {
content = item.encodings.gzip;
}

response = { response = {
contentLength: cache[groupName][match].contentLength contentLength: content.length
, modified: cache[groupName][match].modified , modified: item.modified
, contentBuffer: cache[groupName][match].contentBuffer , contentBuffer: content.buffer
, encoding: content.encoding
}; };
} }
}); });
Expand All @@ -347,14 +382,20 @@ module.exports = function assetManager (assets) {
serveContent(response); serveContent(response);
} }
function serveContent(response) { function serveContent(response) {
res.writeHead(200, { var headers = {
'Content-Type': mimeType, 'Content-Type': mimeType,
'Content-Length': response.contentLength, 'Content-Length': response.contentLength,
'Last-Modified': response.modified, 'Last-Modified': response.modified,
'Date': (new Date).toUTCString(), 'Date': (new Date).toUTCString(),
'Cache-Control': 'public max-age=' + 31536000, 'Cache-Control': 'public max-age=' + 31536000,
'Expires': response.expires || (new Date(new Date().getTime()+63113852000)).toUTCString() 'Expires': response.expires || (new Date(new Date().getTime()+63113852000)).toUTCString()
}); };

if(response.encoding) {
headers['Content-Encoding'] = response.encoding
}

res.writeHead(200, headers);
res.end(response.contentBuffer); res.end(response.contentBuffer);
} }
return; return;
Expand Down

0 comments on commit 2ec66ae

Please sign in to comment.