compression
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -0,1 +1,8 @@ | ||
1.0.3 / 2014-05-29 | ||
================== | ||
* use `accepts` for negotiation | ||
* use `on-headers` to handle header checking | ||
* deps: bytes@1.0.0 | ||
1.0.2 / 2014-04-29 | ||
@@ -2,0 +9,0 @@ ================== |
64
index.js
/*! | ||
* Expressjs | Connect - compress | ||
* compression | ||
* Copyright(c) 2010 Sencha Inc. | ||
@@ -13,4 +13,5 @@ * Copyright(c) 2011 TJ Holowaychuk | ||
var zlib = require('zlib'); | ||
var accepts = require('accepts'); | ||
var bytes = require('bytes'); | ||
var Negotiator = require('negotiator'); | ||
var onHeaders = require('on-headers'); | ||
var compressible = require('compressible'); | ||
@@ -45,3 +46,3 @@ | ||
module.exports = function compress(options) { | ||
module.exports = function compression(options) { | ||
options = options || {}; | ||
@@ -59,6 +60,4 @@ var filter = options.filter || exports.filter; | ||
return function compress(req, res, next){ | ||
var accept = req.headers['accept-encoding'] | ||
, writeHead = res.writeHead | ||
, write = res.write | ||
return function compression(req, res, next){ | ||
var write = res.write | ||
, end = res.end | ||
@@ -104,8 +103,5 @@ , compress = true | ||
res.writeHead = function(){ | ||
// set headers from args | ||
var args = setWriteHeadHeaders.apply(this, arguments); | ||
onHeaders(res, function(){ | ||
// default request filter | ||
if (!filter(req, res)) return writeHead.apply(res, args); | ||
if (!filter(req, res)) return; | ||
@@ -120,3 +116,3 @@ // vary | ||
if (!compress) return writeHead.apply(res, args); | ||
if (!compress) return; | ||
@@ -126,14 +122,13 @@ var encoding = res.getHeader('Content-Encoding') || 'identity'; | ||
// already encoded | ||
if ('identity' != encoding) return writeHead.apply(res, args); | ||
if ('identity' != encoding) return; | ||
// SHOULD use identity | ||
if (!accept) return writeHead.apply(res, args); | ||
// head | ||
if ('HEAD' == req.method) return writeHead.apply(res, args); | ||
if ('HEAD' == req.method) return; | ||
// compression method | ||
var method = new Negotiator(req).preferredEncoding(['gzip', 'deflate', 'identity']); | ||
var accept = accepts(req); | ||
var method = accept.encodings(['gzip', 'deflate', 'identity']); | ||
// negotiation failed | ||
if (!method || method === 'identity') return writeHead.apply(res, args); | ||
if (!method || method === 'identity') return; | ||
@@ -164,6 +159,4 @@ // compression stream | ||
}); | ||
}); | ||
writeHead.apply(res, args); | ||
}; | ||
next(); | ||
@@ -180,26 +173,1 @@ }; | ||
function noop(){} | ||
function setWriteHeadHeaders() { | ||
var headerIndex = typeof arguments[1] === 'string' | ||
? 2 | ||
: 1; | ||
var headers = arguments[headerIndex]; | ||
// the following block is from node.js core | ||
if (Array.isArray(headers)) { | ||
// handle array case | ||
for (var i = 0, len = headers.length; i < len; ++i) { | ||
this.setHeader(headers[i][0], headers[i][1]); | ||
} | ||
} else if (headers) { | ||
// handle object case | ||
var keys = Object.keys(headers); | ||
for (var i = 0; i < keys.length; i++) { | ||
var k = keys[i]; | ||
if (k) this.setHeader(k, headers[k]); | ||
} | ||
} | ||
return Array.prototype.slice.call(arguments, 0, headerIndex); | ||
} |
{ | ||
"name": "compression", | ||
"description": "Compression middleware for connect and node.js", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"author": { | ||
@@ -11,2 +11,8 @@ "name": "Jonathan Ong", | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Douglas Christopher Wilson", | ||
"email": "doug@somethingdoug.com" | ||
} | ||
], | ||
"license": "MIT", | ||
@@ -22,18 +28,22 @@ "repository": { | ||
"dependencies": { | ||
"bytes": "0.3.0", | ||
"negotiator": "0.4.3", | ||
"compressible": "1.0.1" | ||
"accepts": "1.0.2", | ||
"bytes": "1.0.0", | ||
"compressible": "1.0.1", | ||
"on-headers": "0.0.0" | ||
}, | ||
"devDependencies": { | ||
"supertest": "*", | ||
"connect": "*", | ||
"mocha": "*", | ||
"should": "*" | ||
"connect": "2", | ||
"istanbul": "0.2.10", | ||
"mocha": "~1.20.0", | ||
"supertest": "~0.13.0", | ||
"should": "~4.0.0" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha --require should --reporter spec" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8" | ||
"test": "mocha --reporter dot", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec" | ||
} | ||
} |
@@ -1,3 +0,7 @@ | ||
# compression [![Build Status](https://travis-ci.org/expressjs/compression.svg)](https://travis-ci.org/expressjs/compression) [![NPM version](https://badge.fury.io/js/compression.svg)](http://badge.fury.io/js/compression) | ||
# compression | ||
[![NPM version](https://badge.fury.io/js/compression.svg)](http://badge.fury.io/js/compression) | ||
[![Build Status](https://travis-ci.org/expressjs/compression.svg?branch=master)](https://travis-ci.org/expressjs/compression) | ||
[![Coverage Status](https://img.shields.io/coveralls/expressjs/compression.svg?branch=master)](https://coveralls.io/r/expressjs/compression) | ||
Node.js compression middleware. | ||
@@ -21,3 +25,3 @@ | ||
app.use(compress({ | ||
threshhold: 512 | ||
threshold: 512 | ||
})) | ||
@@ -24,0 +28,0 @@ ``` |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
59
0
7548
4
5
5
133
+ Addedaccepts@1.0.2
+ Addedon-headers@0.0.0
+ Addedaccepts@1.0.2(transitive)
+ Addedbytes@1.0.0(transitive)
+ Addedmime@1.2.11(transitive)
+ Addednegotiator@0.4.5(transitive)
+ Addedon-headers@0.0.0(transitive)
- Removednegotiator@0.4.3
- Removedbytes@0.3.0(transitive)
- Removednegotiator@0.4.3(transitive)
Updatedbytes@1.0.0