pax_global_header00006660000000000000000000000064134437257140014524gustar00rootroot0000000000000052 comment=dd5055dc92fdeacad706972c4fcf3a7ff10066ef compression-1.7.4/000077500000000000000000000000001344372571400140765ustar00rootroot00000000000000compression-1.7.4/.eslintignore000066400000000000000000000000261344372571400165770ustar00rootroot00000000000000coverage node_modules compression-1.7.4/.eslintrc.yml000066400000000000000000000000351344372571400165200ustar00rootroot00000000000000root: true extends: standard compression-1.7.4/.gitignore000066400000000000000000000000441344372571400160640ustar00rootroot00000000000000coverage node_modules npm-debug.log compression-1.7.4/.travis.yml000066400000000000000000000055141344372571400162140ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.9" - "5.12" - "6.16" - "7.10" - "8.15" - "9.11" - "10.15" - "11.10" sudo: false cache: directories: - node_modules before_install: - | # Setup utility functions function node_version_lt () { [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v "${1}")" ]] } function npm_module_installed () { npm -lsp ls | grep -Fq "$(pwd)/node_modules/${1}:${1}@" } function npm_remove_module_re () { node -e ' fs = require("fs"); p = JSON.parse(fs.readFileSync("package.json", "utf8")); r = RegExp(process.argv[1]); for (k in p.devDependencies) { if (r.test(k)) delete p.devDependencies[k]; } fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n"); ' "$@" } function npm_use_module () { node -e ' fs = require("fs"); p = JSON.parse(fs.readFileSync("package.json", "utf8")); p.devDependencies[process.argv[1]] = process.argv[2]; fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n"); ' "$@" } function v () { tr '.' '\n' <<< "${1}" \ | awk '{ printf "%03d", $0 }' \ | sed 's/^0*//' } # Configure npm - | # Skip updating shrinkwrap / lock npm config set shrinkwrap false # Setup Node.js version-specific dependencies - | # Configure istanbul for coverage if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$' fi - | # Configure eslint for linting if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)' fi - | # Configure mocha for testing if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3' elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3' elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0' fi - | # Configure supertest for http calls if node_version_lt '0.10'; then npm_use_module 'supertest' '1.1.0' elif node_version_lt '4.0' ; then npm_use_module 'supertest' '2.0.0' elif node_version_lt '6.0' ; then npm_use_module 'supertest' '3.4.2' fi # Update Node.js modules - | # Prune & rebuild node_modules if [[ -d node_modules ]]; then npm prune npm rebuild fi before_scrpt: - | # Contents of node_modules npm -s ls ||: script: - | # Run test script, depending on istanbul install if npm_module_installed 'istanbul'; then npm run-script test-travis else npm test fi - | # Run linting, if eslint exists if npm_module_installed 'eslint'; then npm run-script lint fi after_script: - | # Upload coverage to coveralls if exists if [[ -e ./coverage/lcov.info ]]; then npm install --save-dev coveralls@2 coveralls < ./coverage/lcov.info fi compression-1.7.4/HISTORY.md000066400000000000000000000157031344372571400155670ustar00rootroot000000000000001.7.4 / 2019-03-18 ================== * deps: compressible@~2.0.16 - Mark `text/less` as compressible - deps: mime-db@'>= 1.38.0 < 2' * deps: on-headers@~1.0.2 - Fix `res.writeHead` patch missing return value * perf: prevent unnecessary buffer copy 1.7.3 / 2018-07-15 ================== * deps: accepts@~1.3.5 - deps: mime-types@~2.1.18 * deps: compressible@~2.0.14 - Mark all XML-derived types as compressible - deps: mime-db@'>= 1.34.0 < 2' * deps: safe-buffer@5.1.2 1.7.2 / 2018-02-18 ================== * deps: compressible@~2.0.13 - deps: mime-db@'>= 1.33.0 < 2' 1.7.1 / 2017-09-26 ================== * deps: accepts@~1.3.4 - deps: mime-types@~2.1.16 * deps: bytes@3.0.0 * deps: compressible@~2.0.11 - deps: mime-db@'>= 1.29.0 < 2' * deps: debug@2.6.9 * deps: vary@~1.1.2 - perf: improve header token parsing speed 1.7.0 / 2017-07-10 ================== * Use `safe-buffer` for improved Buffer API * deps: bytes@2.5.0 * deps: compressible@~2.0.10 - Fix regex fallback to not override `compressible: false` in db - deps: mime-db@'>= 1.27.0 < 2' * deps: debug@2.6.8 - Allow colors in workers - Deprecated `DEBUG_FD` environment variable set to `3` or higher - Fix error when running under React Native - Fix `DEBUG_MAX_ARRAY_LENGTH` - Use same color for same namespace - deps: ms@2.0.0 * deps: vary@~1.1.1 - perf: hoist regular expression 1.6.2 / 2016-05-12 ================== * deps: accepts@~1.3.3 - deps: mime-types@~2.1.11 - deps: negotiator@0.6.1 * deps: bytes@2.3.0 - Drop partial bytes on all parsed units - Fix parsing byte string that looks like hex - perf: hoist regular expressions * deps: compressible@~2.0.8 - deps: mime-db@'>= 1.23.0 < 2' 1.6.1 / 2016-01-19 ================== * deps: bytes@2.2.0 * deps: compressible@~2.0.7 - deps: mime-db@'>= 1.21.0 < 2' * deps: accepts@~1.3.1 - deps: mime-types@~2.1.9 1.6.0 / 2015-09-29 ================== * Skip compression when response has `Cache-Control: no-transform` * deps: accepts@~1.3.0 - deps: mime-types@~2.1.7 - deps: negotiator@0.6.0 * deps: compressible@~2.0.6 - deps: mime-db@'>= 1.19.0 < 2' * deps: on-headers@~1.0.1 - perf: enable strict mode * deps: vary@~1.1.0 - Only accept valid field names in the `field` argument 1.5.2 / 2015-07-30 ================== * deps: accepts@~1.2.12 - deps: mime-types@~2.1.4 * deps: compressible@~2.0.5 - deps: mime-db@'>= 1.16.0 < 2' * deps: vary@~1.0.1 - Fix setting empty header from empty `field` - perf: enable strict mode - perf: remove argument reassignments 1.5.1 / 2015-07-05 ================== * deps: accepts@~1.2.10 - deps: mime-types@~2.1.2 * deps: compressible@~2.0.4 - deps: mime-db@'>= 1.14.0 < 2' - perf: enable strict mode 1.5.0 / 2015-06-09 ================== * Fix return value from `.end` and `.write` after end * Improve detection of zero-length body without `Content-Length` * deps: accepts@~1.2.9 - deps: mime-types@~2.1.1 - perf: avoid argument reassignment & argument slice - perf: avoid negotiator recursive construction - perf: enable strict mode - perf: remove unnecessary bitwise operator * deps: bytes@2.1.0 - Slight optimizations - Units no longer case sensitive when parsing * deps: compressible@~2.0.3 - Fix regex fallback to work if type exists, but is undefined - deps: mime-db@'>= 1.13.0 < 2' - perf: hoist regex declaration - perf: use regex to extract mime * perf: enable strict mode * perf: remove flush reassignment * perf: simplify threshold detection 1.4.4 / 2015-05-11 ================== * deps: accepts@~1.2.7 - deps: mime-types@~2.0.11 - deps: negotiator@0.5.3 * deps: debug@~2.2.0 - deps: ms@0.7.1 1.4.3 / 2015-03-14 ================== * deps: accepts@~1.2.5 - deps: mime-types@~2.0.10 * deps: debug@~2.1.3 - Fix high intensity foreground color for bold - deps: ms@0.7.0 1.4.2 / 2015-03-11 ================== * Fix error when code calls `res.end(str, encoding)` - Specific to Node.js 0.8 * deps: debug@~2.1.2 - deps: ms@0.7.0 1.4.1 / 2015-02-15 ================== * deps: accepts@~1.2.4 - deps: mime-types@~2.0.9 - deps: negotiator@0.5.1 1.4.0 / 2015-02-01 ================== * Prefer `gzip` over `deflate` on the server - Not all clients agree on what "deflate" coding means 1.3.1 / 2015-01-31 ================== * deps: accepts@~1.2.3 - deps: mime-types@~2.0.8 * deps: compressible@~2.0.2 - deps: mime-db@'>= 1.1.2 < 2' 1.3.0 / 2014-12-30 ================== * Export the default `filter` function for wrapping * deps: accepts@~1.2.2 - deps: mime-types@~2.0.7 - deps: negotiator@0.5.0 * deps: debug@~2.1.1 1.2.2 / 2014-12-10 ================== * Fix `.end` to only proxy to `.end` - Fixes an issue with Node.js 0.11.14 * deps: accepts@~1.1.4 - deps: mime-types@~2.0.4 1.2.1 / 2014-11-23 ================== * deps: accepts@~1.1.3 - deps: mime-types@~2.0.3 1.2.0 / 2014-10-16 ================== * deps: debug@~2.1.0 - Implement `DEBUG_FD` env variable support 1.1.2 / 2014-10-15 ================== * deps: accepts@~1.1.2 - Fix error when media type has invalid parameter - deps: negotiator@0.4.9 1.1.1 / 2014-10-12 ================== * deps: accepts@~1.1.1 - deps: mime-types@~2.0.2 - deps: negotiator@0.4.8 * deps: compressible@~2.0.1 - deps: mime-db@1.x 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@0.4.7 1.0.8 / 2014-06-20 ================== * deps: accepts@~1.0.5 - use `mime-types` 1.0.7 / 2014-06-11 ================== * use vary module for better `Vary` behavior * deps: accepts@1.0.3 * deps: compressible@1.1.0 1.0.6 / 2014-06-03 ================== * fix regression when negotiation fails 1.0.5 / 2014-06-03 ================== * fix listeners for delayed stream creation - fixes regression for certain `stream.pipe(res)` situations 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 ================== * use `accepts` for negotiation * use `on-headers` to handle header checking * deps: bytes@1.0.0 1.0.2 / 2014-04-29 ================== * only version compatible with node.js 0.8 * support headers given to `res.writeHead` * deps: bytes@0.3.0 * deps: negotiator@0.4.3 1.0.1 / 2014-03-08 ================== * bump negotiator * use compressible * use .headersSent (drops 0.8 support) * handle identity;q=0 case compression-1.7.4/LICENSE000066400000000000000000000022241344372571400151030ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 Jonathan Ong Copyright (c) 2014-2015 Douglas Christopher Wilson 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. compression-1.7.4/README.md000066400000000000000000000170351344372571400153630ustar00rootroot00000000000000# 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] Node.js compression middleware. The following compression codings are supported: - deflate - gzip ## Install This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): ```bash $ npm install compression ``` ## API ```js var compression = require('compression') ``` ### compression([options]) Returns the compression middleware using the given `options`. The middleware will attempt to compress response bodies for all request that traverse through the middleware, based on the given `options`. This middleware will never compress responses that include a `Cache-Control` header with the [`no-transform` directive](https://tools.ietf.org/html/rfc7234#section-5.2.2.4), as compressing will transform the body. #### Options `compression()` accepts these properties in the options object. In addition to those listed below, [zlib](http://nodejs.org/api/zlib.html) options may be passed in to the options object. ##### chunkSize The default value is `zlib.Z_DEFAULT_CHUNK`, or `16384`. See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning) regarding the usage. ##### filter A function to decide if the response should be considered for compression. This function is called as `filter(req, res)` and is expected to return `true` to consider the response for compression, or `false` to not compress the response. The default filter function uses the [compressible](https://www.npmjs.com/package/compressible) module to determine if `res.getHeader('Content-Type')` is compressible. ##### level The level of zlib compression to apply to responses. A higher level will result in better compression, but will take longer to complete. A lower level will result in less compression, but will be much faster. This is an integer in the range of `0` (no compression) to `9` (maximum compression). The special value `-1` can be used to mean the "default compression level", which is a default compromise between speed and compression (currently equivalent to level 6). - `-1` Default compression level (also `zlib.Z_DEFAULT_COMPRESSION`). - `0` No compression (also `zlib.Z_NO_COMPRESSION`). - `1` Fastest compression (also `zlib.Z_BEST_SPEED`). - `2` - `3` - `4` - `5` - `6` (currently what `zlib.Z_DEFAULT_COMPRESSION` points to). - `7` - `8` - `9` Best compression (also `zlib.Z_BEST_COMPRESSION`). The default value is `zlib.Z_DEFAULT_COMPRESSION`, or `-1`. **Note** in the list above, `zlib` is from `zlib = require('zlib')`. ##### memLevel This specifies how much memory should be allocated for the internal compression state and is an integer in the range of `1` (minimum level) and `9` (maximum level). The default value is `zlib.Z_DEFAULT_MEMLEVEL`, or `8`. See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning) regarding the usage. ##### strategy This is used to tune the compression algorithm. This value only affects the compression ratio, not the correctness of the compressed output, even if it is not set appropriately. - `zlib.Z_DEFAULT_STRATEGY` Use for normal data. - `zlib.Z_FILTERED` Use for data produced by a filter (or predictor). Filtered data consists mostly of small values with a somewhat random distribution. In this case, the compression algorithm is tuned to compress them better. The effect is to force more Huffman coding and less string matching; it is somewhat intermediate between `zlib.Z_DEFAULT_STRATEGY` and `zlib.Z_HUFFMAN_ONLY`. - `zlib.Z_FIXED` Use to prevent the use of dynamic Huffman codes, allowing for a simpler decoder for special applications. - `zlib.Z_HUFFMAN_ONLY` Use to force Huffman encoding only (no string match). - `zlib.Z_RLE` Use to limit match distances to one (run-length encoding). This is designed to be almost as fast as `zlib.Z_HUFFMAN_ONLY`, but give better compression for PNG image data. **Note** in the list above, `zlib` is from `zlib = require('zlib')`. ##### threshold The byte threshold for the response body size before compression is considered for the response, defaults to `1kb`. This is a number of bytes or any string accepted by the [bytes](https://www.npmjs.com/package/bytes) module. **Note** this is only an advisory setting; if the response size cannot be determined at the time the response headers are written, then it is assumed the response is _over_ the threshold. To guarantee the response size can be determined, be sure set a `Content-Length` response header. ##### windowBits The default value is `zlib.Z_DEFAULT_WINDOWBITS`, or `15`. See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning) regarding the usage. #### .filter The default `filter` function. This is used to construct a custom filter function that is an extension of the default function. ```js var compression = require('compression') var express = require('express') var app = express() app.use(compression({ filter: shouldCompress })) function shouldCompress (req, res) { if (req.headers['x-no-compression']) { // don't compress responses with this request header return false } // fallback to standard filter function return compression.filter(req, res) } ``` ### res.flush This module adds a `res.flush()` method to force the partially-compressed response to be flushed to the client. ## Examples ### express/connect 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 responses app.use(compression()) // add all routes ``` ### 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 every 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 [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/compression.svg [npm-url]: https://npmjs.org/package/compression [travis-image]: https://img.shields.io/travis/expressjs/compression/master.svg [travis-url]: https://travis-ci.org/expressjs/compression [coveralls-image]: https://img.shields.io/coveralls/expressjs/compression/master.svg [coveralls-url]: https://coveralls.io/r/expressjs/compression?branch=master [downloads-image]: https://img.shields.io/npm/dm/compression.svg [downloads-url]: https://npmjs.org/package/compression compression-1.7.4/index.js000066400000000000000000000135301344372571400155450ustar00rootroot00000000000000/*! * compression * Copyright(c) 2010 Sencha Inc. * Copyright(c) 2011 TJ Holowaychuk * Copyright(c) 2014 Jonathan Ong * Copyright(c) 2014-2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var accepts = require('accepts') var Buffer = require('safe-buffer').Buffer var bytes = require('bytes') var compressible = require('compressible') var debug = require('debug')('compression') var onHeaders = require('on-headers') var vary = require('vary') var zlib = require('zlib') /** * Module exports. */ module.exports = compression module.exports.filter = shouldCompress /** * Module variables. * @private */ var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/ /** * Compress response data with gzip / deflate. * * @param {Object} [options] * @return {Function} middleware * @public */ function compression (options) { var opts = options || {} // options var filter = opts.filter || shouldCompress var threshold = bytes.parse(opts.threshold) if (threshold == null) { threshold = 1024 } return function compression (req, res, next) { var ended = false var length var listeners = [] var stream var _end = res.end var _on = res.on var _write = res.write // flush res.flush = function flush () { if (stream) { stream.flush() } } // proxy res.write = function write (chunk, encoding) { if (ended) { return false } if (!this._header) { this._implicitHeader() } return stream ? stream.write(toBuffer(chunk, encoding)) : _write.call(this, chunk, encoding) } res.end = function end (chunk, encoding) { if (ended) { return false } if (!this._header) { // estimate the length if (!this.getHeader('Content-Length')) { length = chunkLength(chunk, encoding) } this._implicitHeader() } if (!stream) { return _end.call(this, chunk, encoding) } // mark ended ended = true // write Buffer for Node.js 0.8 return chunk ? stream.end(toBuffer(chunk, encoding)) : stream.end() } res.on = function on (type, listener) { if (!listeners || type !== 'drain') { return _on.call(this, type, listener) } if (stream) { return stream.on(type, listener) } // buffer listeners for future stream listeners.push([type, listener]) return this } function nocompress (msg) { debug('no compression: %s', msg) addListeners(res, _on, listeners) listeners = null } onHeaders(res, function onResponseHeaders () { // determine if request is filtered if (!filter(req, res)) { nocompress('filtered') return } // determine if the entity should be transformed if (!shouldTransform(req, res)) { nocompress('no transform') return } // vary vary(res, 'Accept-Encoding') // content-length below threshold if (Number(res.getHeader('Content-Length')) < threshold || length < threshold) { nocompress('size below threshold') return } var encoding = res.getHeader('Content-Encoding') || 'identity' // already encoded if (encoding !== 'identity') { nocompress('already encoded') return } // head if (req.method === 'HEAD') { nocompress('HEAD request') return } // compression method var accept = accepts(req) var method = accept.encoding(['gzip', 'deflate', 'identity']) // we really don't prefer deflate if (method === 'deflate' && accept.encoding(['gzip'])) { method = accept.encoding(['gzip', 'identity']) } // negotiation failed if (!method || method === 'identity') { nocompress('not acceptable') return } // compression stream debug('%s compression', method) stream = method === 'gzip' ? zlib.createGzip(opts) : zlib.createDeflate(opts) // add buffered listeners to stream addListeners(stream, stream.on, listeners) // header fields res.setHeader('Content-Encoding', method) res.removeHeader('Content-Length') // compression stream.on('data', function onStreamData (chunk) { if (_write.call(res, chunk) === false) { stream.pause() } }) stream.on('end', function onStreamEnd () { _end.call(res) }) _on.call(res, 'drain', function onResponseDrain () { stream.resume() }) }) next() } } /** * Add bufferred listeners to stream * @private */ function addListeners (stream, on, listeners) { for (var i = 0; i < listeners.length; i++) { on.apply(stream, listeners[i]) } } /** * Get the length of a given chunk */ function chunkLength (chunk, encoding) { if (!chunk) { return 0 } return !Buffer.isBuffer(chunk) ? Buffer.byteLength(chunk, encoding) : chunk.length } /** * Default filter function. * @private */ function shouldCompress (req, res) { var type = res.getHeader('Content-Type') if (type === undefined || !compressible(type)) { debug('%s not compressible', type) return false } return true } /** * Determine if the entity should be transformed. * @private */ function shouldTransform (req, res) { var cacheControl = res.getHeader('Cache-Control') // Don't compress for Cache-Control: no-transform // https://tools.ietf.org/html/rfc7234#section-5.2.2.4 return !cacheControl || !cacheControlNoTransformRegExp.test(cacheControl) } /** * Coerce arguments to Buffer * @private */ function toBuffer (chunk, encoding) { return !Buffer.isBuffer(chunk) ? Buffer.from(chunk, encoding) : chunk } compression-1.7.4/package.json000066400000000000000000000024761344372571400163750ustar00rootroot00000000000000{ "name": "compression", "description": "Node.js compression middleware", "version": "1.7.4", "contributors": [ "Douglas Christopher Wilson ", "Jonathan Ong (http://jongleberry.com)" ], "license": "MIT", "repository": "expressjs/compression", "dependencies": { "accepts": "~1.3.5", "bytes": "3.0.0", "compressible": "~2.0.16", "debug": "2.6.9", "on-headers": "~1.0.2", "safe-buffer": "5.1.2", "vary": "~1.1.2" }, "devDependencies": { "after": "0.8.2", "eslint": "5.15.1", "eslint-config-standard": "12.0.0", "eslint-plugin-import": "2.16.0", "eslint-plugin-markdown": "1.0.0", "eslint-plugin-node": "7.0.1", "eslint-plugin-promise": "4.0.1", "eslint-plugin-standard": "4.0.0", "istanbul": "0.4.5", "mocha": "6.0.2", "supertest": "4.0.0" }, "files": [ "LICENSE", "HISTORY.md", "index.js" ], "engines": { "node": ">= 0.8.0" }, "scripts": { "lint": "eslint --plugin markdown --ext js,md .", "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" } } compression-1.7.4/test/000077500000000000000000000000001344372571400150555ustar00rootroot00000000000000compression-1.7.4/test/.eslintrc.yml000066400000000000000000000000231344372571400174740ustar00rootroot00000000000000env: mocha: true compression-1.7.4/test/compression.js000066400000000000000000000471221344372571400177620ustar00rootroot00000000000000var after = require('after') var assert = require('assert') var Buffer = require('safe-buffer').Buffer var bytes = require('bytes') var crypto = require('crypto') var http = require('http') var request = require('supertest') var zlib = require('zlib') var compression = require('..') describe('compression()', function () { it('should skip HEAD', 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(shouldNotHaveHeader('Content-Encoding')) .expect(200, done) }) it('should skip unknown accept-encoding', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'bogus') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, done) }) it('should skip if content-encoding already set', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Encoding', 'x-custom') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'x-custom') .expect(200, 'hello, world', done) }) it('should set Vary', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip') .expect('Vary', 'Accept-Encoding', done) }) it('should set Vary even if Accept-Encoding is not set', function (done) { var server = createServer({ threshold: 1000 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .expect('Vary', 'Accept-Encoding') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, done) }) it('should not set Vary if Content-Type does not pass filter', function (done) { var server = createServer(null, function (req, res) { res.setHeader('Content-Type', 'image/jpeg') res.end() }) request(server) .get('/') .expect(shouldNotHaveHeader('Vary')) .expect(200, done) }) 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') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Transfer-Encoding', 'chunked', done) }) it('should remove Content-Length for chunked', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .expect('Content-Encoding', 'gzip') .expect(shouldNotHaveHeader('Content-Length')) .expect(200, done) }) it('should work with encoding arguments', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('hello, ', 'utf8') res.end('world', 'utf8') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Transfer-Encoding', 'chunked') .expect(200, 'hello, world', done) }) it('should allow writing after close', function (done) { // UGH var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.once('close', function () { res.write('hello, ') res.end('world') done() }) res.destroy() }) request(server) .get('/') .end(function () {}) }) it('should back-pressure when compressed', function (done) { var buf var cb = after(2, done) var client var drained = false var resp var server = createServer({ threshold: 0 }, function (req, res) { resp = res res.on('drain', function () { drained = true }) res.setHeader('Content-Type', 'text/plain') res.write('start') pressure() }) crypto.pseudoRandomBytes(1024 * 128, function (err, chunk) { if (err) return done(err) buf = chunk pressure() }) function pressure () { if (!buf || !resp || !client) return assert.ok(!drained) while (resp.write(buf) !== false) { resp.flush() } resp.on('drain', function () { assert.ok(resp.write('end')) resp.end() }) resp.on('finish', cb) client.resume() } request(server) .get('/') .request() .on('response', function (res) { client = res assert.strictEqual(res.headers['content-encoding'], 'gzip') res.pause() res.on('end', function () { server.close(cb) }) pressure() }) .end() }) it('should back-pressure when uncompressed', function (done) { var buf var cb = after(2, done) var client var drained = false var resp var server = createServer({ filter: function () { return false } }, function (req, res) { resp = res res.on('drain', function () { drained = true }) res.setHeader('Content-Type', 'text/plain') res.write('start') pressure() }) crypto.pseudoRandomBytes(1024 * 128, function (err, chunk) { if (err) return done(err) buf = chunk pressure() }) function pressure () { if (!buf || !resp || !client) return while (resp.write(buf) !== false) { resp.flush() } resp.on('drain', function () { assert.ok(drained) assert.ok(resp.write('end')) resp.end() }) resp.on('finish', cb) client.resume() } request(server) .get('/') .request() .on('response', function (res) { client = res shouldNotHaveHeader('Content-Encoding')(res) res.pause() res.on('end', function () { server.close(cb) }) pressure() }) .end() }) it('should transfer large bodies', function (done) { var len = bytes('1mb') var buf = Buffer.alloc(len, '.') var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end(buf) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Transfer-Encoding', 'chunked') .expect('Content-Encoding', 'gzip') .expect(shouldHaveBodyLength(len)) .expect(200, buf.toString(), done) }) it('should transfer large bodies with multiple writes', function (done) { var len = bytes('40kb') var buf = Buffer.alloc(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) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Transfer-Encoding', 'chunked') .expect('Content-Encoding', 'gzip') .expect(shouldHaveBodyLength(len * 4)) .expect(200, done) }) describe('threshold', function () { it('should not compress responses below the threshold size', function (done) { var server = createServer({ threshold: '1kb' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Length', '12') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, done) }) it('should compress responses above the threshold size', function (done) { var server = createServer({ threshold: '1kb' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Length', '2048') res.end(Buffer.alloc(2048)) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip', done) }) it('should compress when streaming without a content-length', function (done) { var server = createServer({ threshold: '1kb' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('hello, ') setTimeout(function () { res.end('world') }, 10) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip', done) }) it('should not compress when streaming and content-length is lower than threshold', function (done) { var server = createServer({ threshold: '1kb' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Length', '12') res.write('hello, ') setTimeout(function () { res.end('world') }, 10) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, done) }) it('should compress when streaming and content-length is larger than threshold', function (done) { var server = createServer({ threshold: '1kb' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Length', '2048') res.write(Buffer.alloc(1024)) setTimeout(function () { res.end(Buffer.alloc(1024)) }, 10) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip', done) }) // res.end(str, encoding) broken in node.js 0.8 var run = /^v0\.8\./.test(process.version) ? it.skip : it run('should handle writing hex data', function (done) { var server = createServer({ threshold: 6 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('2e2e2e2e', 'hex') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, '....', done) }) it('should consider res.end() as 0 length', function (done) { var server = createServer({ threshold: 1 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end() }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, '', done) }) it('should work with res.end(null)', function (done) { var server = createServer({ threshold: 1000 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end(null) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, '', done) }) }) describe('when "Accept-Encoding: gzip"', function () { it('should respond with gzip', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip', done) }) it('should return false writing after end', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') assert.ok(res.write() === false) assert.ok(res.end() === false) }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Content-Encoding', 'gzip', done) }) }) describe('when "Accept-Encoding: deflate"', function () { it('should respond with deflate', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'deflate') .expect('Content-Encoding', 'deflate', done) }) }) describe('when "Accept-Encoding: gzip, deflate"', function () { it('should respond with gzip', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip, deflate') .expect('Content-Encoding', 'gzip', done) }) }) describe('when "Accept-Encoding: deflate, gzip"', function () { it('should respond with gzip', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'deflate, gzip') .expect('Content-Encoding', 'gzip', done) }) }) describe('when "Cache-Control: no-transform" response header', function () { it('should not compress response', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Cache-Control', 'no-transform') res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Cache-Control', 'no-transform') .expect(shouldNotHaveHeader('Content-Encoding')) .expect(200, 'hello, world', done) }) it('should not set Vary headerh', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Cache-Control', 'no-transform') res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) request(server) .get('/') .set('Accept-Encoding', 'gzip') .expect('Cache-Control', 'no-transform') .expect(shouldNotHaveHeader('Vary')) .expect(200, done) }) }) describe('.filter', function () { it('should be a function', function () { assert.strictEqual(typeof compression.filter, 'function') }) it('should return false on empty response', function (done) { var server = http.createServer(function (req, res) { res.end(String(compression.filter(req, res))) }) request(server) .get('/') .expect(200, 'false', done) }) it('should return true for "text/plain"', function (done) { var server = http.createServer(function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end(String(compression.filter(req, res))) }) request(server) .get('/') .expect(200, 'true', done) }) it('should return false for "application/x-bogus"', function (done) { var server = http.createServer(function (req, res) { res.setHeader('Content-Type', 'application/x-bogus') res.end(String(compression.filter(req, res))) }) request(server) .get('/') .expect(200, 'false', done) }) }) describe('res.flush()', function () { it('should always be present', function (done) { var server = createServer(null, function (req, res) { res.statusCode = typeof res.flush === 'function' ? 200 : 500 res.flush() res.end() }) request(server) .get('/') .expect(200, done) }) it('should flush the response', function (done) { var chunks = 0 var next var server = createServer({ threshold: 0 }, function (req, res) { next = writeAndFlush(res, 2, Buffer.alloc(1024)) res.setHeader('Content-Type', 'text/plain') res.setHeader('Content-Length', '2048') next() }) function onchunk (chunk) { assert.ok(chunks++ < 2) assert.strictEqual(chunk.length, 1024) next() } request(server) .get('/') .set('Accept-Encoding', 'gzip') .request() .on('response', unchunk('gzip', onchunk, function (err) { if (err) return done(err) server.close(done) })) .end() }) it('should flush small chunks for gzip', function (done) { var chunks = 0 var next var server = createServer({ threshold: 0 }, function (req, res) { next = writeAndFlush(res, 2, Buffer.from('..')) res.setHeader('Content-Type', 'text/plain') next() }) function onchunk (chunk) { assert.ok(chunks++ < 20) assert.strictEqual(chunk.toString(), '..') next() } request(server) .get('/') .set('Accept-Encoding', 'gzip') .request() .on('response', unchunk('gzip', onchunk, function (err) { if (err) return done(err) server.close(done) })) .end() }) it('should flush small chunks for deflate', function (done) { var chunks = 0 var next var server = createServer({ threshold: 0 }, function (req, res) { next = writeAndFlush(res, 2, Buffer.from('..')) res.setHeader('Content-Type', 'text/plain') next() }) function onchunk (chunk) { assert.ok(chunks++ < 20) assert.strictEqual(chunk.toString(), '..') next() } request(server) .get('/') .set('Accept-Encoding', 'deflate') .request() .on('response', unchunk('deflate', onchunk, function (err) { if (err) return done(err) server.close(done) })) .end() }) }) }) function createServer (opts, fn) { var _compression = compression(opts) return http.createServer(function (req, res) { _compression(req, res, function (err) { if (err) { res.statusCode = err.status || 500 res.end(err.message) return } fn(req, res) }) }) } function shouldHaveBodyLength (length) { return function (res) { assert.strictEqual(res.text.length, length, 'should have body length of ' + length) } } function shouldNotHaveHeader (header) { return function (res) { assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header) } } function writeAndFlush (stream, count, buf) { var writes = 0 return function () { if (writes++ >= count) return if (writes === count) return stream.end(buf) stream.write(buf) stream.flush() } } function unchunk (encoding, onchunk, onend) { return function (res) { var stream assert.strictEqual(res.headers['content-encoding'], encoding) switch (encoding) { case 'deflate': stream = res.pipe(zlib.createInflate()) break case 'gzip': stream = res.pipe(zlib.createGunzip()) break } stream.on('data', onchunk) stream.on('end', onend) } }