Skip to content

Commit

Permalink
refactored compress()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Oct 12, 2011
1 parent 13a9ff8 commit 715e4f5
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions lib/middleware/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,30 @@ module.exports = function compress(options) {
, stream
, method;

// proxy

res.write = function(chunk){
if (!res.headerSent) this._implicitHeader();
return stream.write(chunk);
};

res.end = function(chunk){
if (chunk) stream.write(chunk);
return stream.end();
};

function revert() {
stream = res;
res.write = write;
res.end = end;
}

res.on('header', function(){
// default request filter
if (!filter(req, res)) return;
if (!filter(req, res)) return revert();

// SHOULD use identity
if (!accept) return;
if (!accept) return revert();

// default to gzip
if ('*' == accept.trim()) method = 'gzip';
Expand All @@ -93,7 +111,7 @@ module.exports = function compress(options) {
}

// compression method
if (!method) return;
if (!method) return revert();

// compression stream
stream = exports.methods[method](options);
Expand All @@ -102,17 +120,6 @@ module.exports = function compress(options) {
res.setHeader('Content-Encoding', method);
res.setHeader('Vary', 'Accept-Encoding');

// proxy

res.write = function(chunk){
return stream.write(chunk);
};

res.end = function(chunk){
if (chunk) stream.write(chunk);
return stream.end();
};

// compression

stream.on('data', function(chunk){
Expand Down

0 comments on commit 715e4f5

Please sign in to comment.