[Pkg-javascript-commits] [node-compression] 02/09: Imported Upstream version 1.1.0
Leo Iannacone
l3on-guest at moszumanska.debian.org
Sat Oct 11 13:53:13 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-compression.
commit abd78f28cf1ec77f2d61e555cf87e4b2563a52e6
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Fri Oct 10 13:28:57 2014 +0200
Imported Upstream version 1.1.0
---
.npmignore | 3 --
HISTORY.md | 32 ++++++++++++++++++
LICENSE | 23 +++++++++++++
README.md | 107 +++++++++++++++++++++++++++++++++++++++++++++--------------
index.js | 60 +++++++++++++++++++++++++--------
package.json | 22 +++++++-----
test/test.js | 59 ++++++++++++++++++++++++++++++++
7 files changed, 256 insertions(+), 50 deletions(-)
diff --git a/.npmignore b/.npmignore
deleted file mode 100644
index cd39b77..0000000
--- a/.npmignore
+++ /dev/null
@@ -1,3 +0,0 @@
-coverage/
-test/
-.travis.yml
diff --git a/HISTORY.md b/HISTORY.md
index f9b7667..56fd2c6 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -1,3 +1,35 @@
+1.1.0 / 2014-09-07
+==================
+
+ * deps: accepts@~1.1.0
+ * deps: compressible@~2.0.0
+ * deps: debug@~2.0.0
+
+1.0.11 / 2014-08-10
+===================
+
+ * deps: on-headers@~1.0.0
+ * deps: vary@~1.0.0
+
+1.0.10 / 2014-08-05
+===================
+
+ * deps: compressible@~1.1.1
+ - Fix upper-case Content-Type characters prevent compression
+
+1.0.9 / 2014-07-20
+==================
+
+ * Add `debug` messages
+ * deps: accepts@~1.0.7
+ - deps: negotiator at 0.4.7
+
+1.0.8 / 2014-06-20
+==================
+
+ * deps: accepts@~1.0.5
+ - use `mime-types`
+
1.0.7 / 2014-06-11
==================
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..53e49a3
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,23 @@
+(The MIT License)
+
+Copyright (c) 2014 Jonathan Ong <me at jongleberry.com>
+Copyright (c) 2014 Douglas Christopher Wilson <doug at somethingdoug.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
index 5a1b6bb..2efc10f 100644
--- a/README.md
+++ b/README.md
@@ -1,19 +1,23 @@
# 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)
+[![NPM Version][npm-image]][npm-url]
+[![NPM Downloads][downloads-image]][downloads-url]
+[![Build Status][travis-image]][travis-url]
+[![Test Coverage][coveralls-image]][coveralls-url]
+[![Gratipay][gratipay-image]][gratipay-url]
Node.js compression middleware.
+## Install
+
+```bash
+$ npm install compression
+```
+
## API
```js
-var express = require('express')
var compression = require('compression')
-
-var app = express()
-app.use(compression())
```
### compression(options)
@@ -33,26 +37,79 @@ app.use(compression({
In addition to these, [zlib](http://nodejs.org/api/zlib.html) options may be passed in to the options object.
-## License
+### res.flush
+
+This module adds a `res.flush()` method to force the partially-compressed
+response to be flushed to the client.
-The MIT License (MIT)
+## Examples
-Copyright (c) 2014 Jonathan Ong me at jongleberry.com
+### express/connect
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
+When using this module with express or connect, simply `app.use` the module as
+high as you like. Requests that pass through the middleware will be compressed.
+
+```js
+var compression = require('compression')
+var express = require('express')
+
+var app = express()
+
+// compress all requests
+app.use(compression())
+
+// add alll routes
+```
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
+### Server-Sent Events
+
+Because of the nature of compression this module does not work out of the box
+with server-sent events. To compress content, a window of the output needs to
+be buffered up in order to get good compression. Typically when using server-sent
+events, there are certain block of data that need to reach the client.
+
+You can achieve this by calling `res.flush()` when you need the data written to
+actually make it to the client.
+
+```js
+var compression = require('compression')
+var express = require('express')
+
+var app = express()
+
+// compress responses
+app.use(compression())
+
+// server-sent event stream
+app.get('/events', function (req, res) {
+ res.setHeader('Content-Type', 'text/event-stream')
+ res.setHeader('Cache-Control', 'no-cache')
+
+ // send a ping approx eveny 2 seconds
+ var timer = setInterval(function () {
+ res.write('data: ping\n\n')
+
+ // !!! this is the important part
+ res.flush()
+ }, 2000)
+
+ res.on('close', function () {
+ clearInterval(timer)
+ })
+})
+```
+
+## License
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
+[MIT](LICENSE)
+
+[npm-image]: https://img.shields.io/npm/v/compression.svg?style=flat
+[npm-url]: https://npmjs.org/package/compression
+[travis-image]: https://img.shields.io/travis/expressjs/compression.svg?style=flat
+[travis-url]: https://travis-ci.org/expressjs/compression
+[coveralls-image]: https://img.shields.io/coveralls/expressjs/compression.svg?style=flat
+[coveralls-url]: https://coveralls.io/r/expressjs/compression?branch=master
+[downloads-image]: http://img.shields.io/npm/dm/compression.svg?style=flat
+[downloads-url]: https://npmjs.org/package/compression
+[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
+[gratipay-url]: https://www.gratipay.com/dougwilson/
diff --git a/index.js b/index.js
index 5106de6..ab75477 100644
--- a/index.js
+++ b/index.js
@@ -2,6 +2,8 @@
* compression
* Copyright(c) 2010 Sencha Inc.
* Copyright(c) 2011 TJ Holowaychuk
+ * Copyright(c) 2014 Jonathan Ong
+ * Copyright(c) 2014 Douglas Christopher Wilson
* MIT Licensed
*/
@@ -12,6 +14,7 @@
var zlib = require('zlib');
var accepts = require('accepts');
var bytes = require('bytes');
+var debug = require('debug')('compression')
var onHeaders = require('on-headers');
var compressible = require('compressible');
var vary = require('vary');
@@ -29,15 +32,20 @@ exports.methods = {
* Default filter function.
*/
-exports.filter = function(req, res){
- return compressible(res.getHeader('Content-Type'));
+exports.filter = function filter(req, res) {
+ var type = res.getHeader('Content-Type')
+
+ if (type === undefined || !compressible(type)) {
+ debug('%s not compressible', type)
+ return false
+ }
+
+ return true
};
/**
* Compress response data with gzip / deflate.
*
- * See README.md for documentation of options.
- *
* @param {Object} options
* @return {Function} middleware
* @api public
@@ -78,8 +86,8 @@ module.exports = function compression(options) {
if (!this._header) {
// if content-length is set and is lower
// than the threshold, don't compress
- var length = res.getHeader('content-length');
- if (!isNaN(length) && length < threshold) compress = false;
+ var len = Number(res.getHeader('Content-Length'))
+ checkthreshold(len)
this._implicitHeader();
}
return stream
@@ -97,7 +105,7 @@ module.exports = function compression(options) {
}
if (!this._header) {
- compress = len && len >= threshold
+ checkthreshold(len)
}
if (chunk) {
@@ -124,36 +132,60 @@ module.exports = function compression(options) {
return this
}
- function nocompress(){
+ function checkthreshold(len) {
+ if (compress && len < threshold) {
+ debug('size below threshold')
+ compress = false
+ }
+ }
+
+ function nocompress(msg) {
+ debug('no compression' + (msg ? ': ' + msg : ''))
addListeners(res, on, listeners)
listeners = null
}
onHeaders(res, function(){
- // default request filter
- if (!filter(req, res)) return nocompress()
+ // determine if request is filtered
+ if (!filter(req, res)) {
+ nocompress('filtered')
+ return
+ }
// vary
vary(res, 'Accept-Encoding')
- if (!compress) return nocompress()
+ if (!compress) {
+ nocompress()
+ return
+ }
var encoding = res.getHeader('Content-Encoding') || 'identity';
// already encoded
- if ('identity' !== encoding) return nocompress()
+ if ('identity' !== encoding) {
+ nocompress('already encoded')
+ return
+ }
// head
- if ('HEAD' === req.method) return nocompress()
+ if ('HEAD' === req.method) {
+ nocompress('HEAD request')
+ return
+ }
// compression method
var accept = accepts(req);
var method = accept.encodings(['gzip', 'deflate', 'identity']);
// negotiation failed
- if (!method || method === 'identity') return nocompress()
+ if (!method || method === 'identity') {
+ nocompress('not acceptable')
+ return
+ }
// compression stream
+ debug('%s compression', method)
stream = exports.methods[method](options);
addListeners(stream, stream.on, listeners)
diff --git a/package.json b/package.json
index 2e4e81a..6e7794d 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "compression",
"description": "Compression middleware for connect and node.js",
- "version": "1.0.7",
+ "version": "1.1.0",
"author": "Jonathan Ong <me at jongleberry.com> (http://jongleberry.com)",
"contributors": [
"Douglas Christopher Wilson <doug at somethingdoug.com>"
@@ -9,23 +9,29 @@
"license": "MIT",
"repository": "expressjs/compression",
"dependencies": {
- "accepts": "1.0.3",
+ "accepts": "~1.1.0",
"bytes": "1.0.0",
- "compressible": "1.1.0",
- "on-headers": "0.0.0",
- "vary": "0.1.0"
+ "compressible": "~2.0.0",
+ "debug": "~2.0.0",
+ "on-headers": "~1.0.0",
+ "vary": "~1.0.0"
},
"devDependencies": {
- "istanbul": "0.2.10",
- "mocha": "~1.20.1",
+ "istanbul": "0.3.2",
+ "mocha": "~1.21.3",
"supertest": "~0.13.0",
"should": "~4.0.1"
},
+ "files": [
+ "LICENSE",
+ "HISTORY.md",
+ "index.js"
+ ],
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
- "test": "mocha --check-leaks --reporter dot",
+ "test": "mocha --check-leaks --reporter spec --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec"
}
diff --git a/test/test.js b/test/test.js
index 0aab512..bef2567 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,3 +1,4 @@
+var bytes = require('bytes');
var crypto = require('crypto');
var http = require('http');
var request = require('supertest');
@@ -108,6 +109,18 @@ describe('compress()', function(){
})
})
+ it('should set Vary for HEAD request', function(done){
+ var server = createServer({ threshold: 0 }, function (req, res) {
+ res.setHeader('Content-Type', 'text/plain')
+ res.end('hello, world')
+ })
+
+ request(server)
+ .head('/')
+ .set('Accept-Encoding', 'gzip')
+ .expect('Vary', 'Accept-Encoding', done)
+ })
+
it('should transfer chunked', function(done){
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
@@ -263,6 +276,52 @@ describe('compress()', function(){
.end()
})
+ it('should transfer large bodies', function (done) {
+ var len = bytes('1mb')
+ var buf = new Buffer(len)
+ var server = createServer({ threshold: 0 }, function (req, res) {
+ res.setHeader('Content-Type', 'text/plain')
+ res.end(buf)
+ })
+
+ buf.fill('.')
+
+ request(server)
+ .get('/')
+ .set('Accept-Encoding', 'gzip')
+ .expect('Transfer-Encoding', 'chunked')
+ .expect('Content-Encoding', 'gzip', function (err, res) {
+ if (err) return done(err)
+ should(res.text).equal(buf.toString())
+ res.text.length.should.equal(len)
+ done()
+ })
+ })
+
+ it('should transfer large bodies with multiple writes', function (done) {
+ var len = bytes('40kb')
+ var buf = new Buffer(len)
+ var server = createServer({ threshold: 0 }, function (req, res) {
+ res.setHeader('Content-Type', 'text/plain')
+ res.write(buf)
+ res.write(buf)
+ res.write(buf)
+ res.end(buf)
+ })
+
+ buf.fill('.')
+
+ request(server)
+ .get('/')
+ .set('Accept-Encoding', 'gzip')
+ .expect('Transfer-Encoding', 'chunked')
+ .expect('Content-Encoding', 'gzip', function (err, res) {
+ if (err) return done(err)
+ res.text.length.should.equal(len * 4)
+ done()
+ })
+ })
+
describe('threshold', function(){
it('should not compress responses below the threshold size', function(done){
var server = createServer({ threshold: '1kb' }, function (req, res) {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-compression.git
More information about the Pkg-javascript-commits
mailing list