compression
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -0,1 +1,8 @@ | ||
1.0.4 / 2014-06-03 | ||
================== | ||
* fix adding `Vary` when value stored as array | ||
* fix back-pressure behavior | ||
* fix length check for `res.end` | ||
1.0.3 / 2014-05-29 | ||
@@ -2,0 +9,0 @@ ================== |
75
index.js
@@ -60,3 +60,4 @@ /*! | ||
var write = res.write | ||
, end = res.end | ||
var on = res.on | ||
var end = res.end | ||
, compress = true | ||
@@ -89,9 +90,18 @@ , stream; | ||
res.end = function(chunk, encoding){ | ||
var len | ||
if (chunk) { | ||
if (!this._header && getSize(chunk) < threshold) compress = false; | ||
len = Buffer.isBuffer(chunk) | ||
? chunk.length | ||
: Buffer.byteLength(chunk, encoding) | ||
} | ||
if (!this._header) { | ||
compress = len && len >= threshold | ||
} | ||
if (chunk) { | ||
this.write(chunk, encoding); | ||
} else if (!this._header) { | ||
// response size === 0 | ||
compress = false; | ||
} | ||
return stream | ||
@@ -102,2 +112,10 @@ ? stream.end() | ||
res.on = function(type, listener){ | ||
if (!stream || type !== 'drain') { | ||
return on.call(this, type, listener) | ||
} | ||
return stream.on(type, listener) | ||
} | ||
onHeaders(res, function(){ | ||
@@ -108,8 +126,3 @@ // default request filter | ||
// vary | ||
var vary = res.getHeader('Vary'); | ||
if (!vary) { | ||
res.setHeader('Vary', 'Accept-Encoding'); | ||
} else if (!~vary.indexOf('Accept-Encoding')) { | ||
res.setHeader('Vary', vary + ', Accept-Encoding'); | ||
} | ||
vary(res, 'Accept-Encoding') | ||
@@ -147,3 +160,5 @@ if (!compress) return; | ||
stream.on('data', function(chunk){ | ||
write.call(res, chunk); | ||
if (write.call(res, chunk) === false) { | ||
stream.pause() | ||
} | ||
}); | ||
@@ -155,4 +170,4 @@ | ||
stream.on('drain', function() { | ||
res.emit('drain'); | ||
on.call(res, 'drain', function() { | ||
stream.resume() | ||
}); | ||
@@ -165,8 +180,28 @@ }); | ||
function getSize(chunk) { | ||
return Buffer.isBuffer(chunk) | ||
? chunk.length | ||
: Buffer.byteLength(chunk); | ||
function noop(){} | ||
/** | ||
* Add val to Vary header | ||
*/ | ||
function vary(res, val) { | ||
var header = res.getHeader('Vary') || '' | ||
var headers = Array.isArray(header) | ||
? header.join(', ') | ||
: header | ||
// enumerate current values | ||
var vals = headers.toLowerCase().split(/ *, */) | ||
if (vals.indexOf(val.toLowerCase()) !== -1) { | ||
// already set | ||
return | ||
} | ||
// append value (in existing format) | ||
header = headers | ||
? headers + ', ' + val | ||
: val | ||
res.setHeader('Vary', header) | ||
} | ||
function noop(){} |
{ | ||
"name": "compression", | ||
"description": "Compression middleware for connect and node.js", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"author": { | ||
@@ -33,3 +33,2 @@ "name": "Jonathan Ong", | ||
"devDependencies": { | ||
"connect": "2", | ||
"istanbul": "0.2.10", | ||
@@ -36,0 +35,0 @@ "mocha": "~1.20.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8165
4
157