Skip to content
Browse files

Merge pull request #30 from ryanrolds/zlib

Added zlib/gzip support
  • Loading branch information...
2 parents 37a9946 + aff6080 commit 2ec66aedd6cc304e497e445a203f0412ee128d4b @mape committed Jan 31, 2012
Showing with 48 additions and 7 deletions.
  1. +48 −7 lib/assetmanager.js
View
55 lib/assetmanager.js
@@ -7,6 +7,11 @@ var fs = require('fs')
, cssmin = require('./../deps/cssmin').minify
, crypto = require('crypto');
+var zlib;
+try {
+ zlib = require('zlib');
+} catch(e) {}
+
var cache = {}
, settings = {}
, cacheHashes = {}
@@ -237,8 +242,25 @@ module.exports = function assetManager (assets) {
cacheHashes[groupName] = crypto.createHash('md5').update(content).digest('hex');
- cache[groupName][match].contentBuffer = new Buffer(content, 'utf8');
- cache[groupName][match].contentLength = cache[groupName][match].contentBuffer.length;
+ cache[groupName][match].encodings = {};
+ 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'
+ };
+ });
+ }
});
});
});
@@ -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) {
var self = this;
var found = false;
@@ -325,10 +352,18 @@ module.exports = function assetManager (assets) {
Object.keys(cache[groupName]).forEach(function(match) {
if (!found && userAgent.match(new RegExp(match, 'i'))) {
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 = {
- contentLength: cache[groupName][match].contentLength
- , modified: cache[groupName][match].modified
- , contentBuffer: cache[groupName][match].contentBuffer
+ contentLength: content.length
+ , modified: item.modified
+ , contentBuffer: content.buffer
+ , encoding: content.encoding
};
}
});
@@ -347,14 +382,20 @@ module.exports = function assetManager (assets) {
serveContent(response);
}
function serveContent(response) {
- res.writeHead(200, {
+ var headers = {
'Content-Type': mimeType,
'Content-Length': response.contentLength,
'Last-Modified': response.modified,
'Date': (new Date).toUTCString(),
'Cache-Control': 'public max-age=' + 31536000,
'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);
}
return;

0 comments on commit 2ec66ae

Please sign in to comment.
Something went wrong with that request. Please try again.