Skip to content

Commit

Permalink
test: achieve 100% code coverage (#196)
Browse files Browse the repository at this point in the history
* test: removing duplicates

* test: make sure `encodings` and `requestEncodings` are sorted

* test: `deflate`/`gzip` compressed Buffers/Streams decompression

* test: `deflate`/`gzip` compressed payloads decompression on 'x-no-compression' header

* test: add `content-encoding` for a Stream when `inflateIfDeflated` is true and `encoding` is undefined

* test: remove `content-length` for a Stream when `inflateIfDeflated` is true on `x-no-compression` header

* test: handle case where serialize doesn't return a string or Buffer

* test: make sure `fastify-compress` hooks are correctly added

* test: decompress `zip` compressed payloads on `x-no-compression` header

* fix (test): linting

* fix: remove superfluous code

* test: make sure `Content-Encoding` header is actually added

* test: no compression when reply `Content-Type` header is not set

* fix: remove the check preventing multiple onSend triggers as it was fixed in fastify upstream

* fix: restore code

* test: `compressStream` and `uncompressStream` zlib fallbacks

* fix (test): skip coverage on unreachable paths

* chore (test): enable 100% code coverage check

* chore: upgrade package dependencies

* ci: add Node.js 17
  • Loading branch information
darkgl0w committed Nov 24, 2021
1 parent d5f0ab0 commit 321cdd0
Show file tree
Hide file tree
Showing 5 changed files with 810 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [10, 12, 14, 16]
node-version: [10, 12, 14, 16, 17]
os: [macos-latest, ubuntu-latest, windows-latest]

steps:
Expand Down
6 changes: 6 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ts: false
jsx: false
flow: false
coverage: true
check-coverage: true
100: true
26 changes: 9 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ const { inherits, format } = require('util')
const InvalidRequestEncodingError = createError('FST_CP_ERR_INVALID_CONTENT_ENCODING', 'Unsupported Content-Encoding: %s', 415)
const InvalidRequestCompressedPayloadError = createError('FST_CP_ERR_INVALID_CONTENT', 'Could not decompress the request payload using the provided encoding', 400)

const compressAdded = Symbol('fastify-compress.added')

function compressPlugin (fastify, opts, next) {
const globalCompressParams = processCompressParams(opts)
const globalDecompressParams = processDecompressParams(opts)
Expand Down Expand Up @@ -69,7 +67,7 @@ function compressPlugin (fastify, opts, next) {
throw new Error('Unknown value for route compress configuration')
}
} else if (globalCompressParams.global) {
// if the plugin is set globally ( meaning that all the routes will be compressed )
// if the plugin is set globally (meaning that all the routes will be compressed)
// As the endpoint, does not have a custom rateLimit configuration, use the global one.
buildRouteCompress(fastify, globalCompressParams, routeOptions)
} else {
Expand All @@ -93,7 +91,7 @@ function compressPlugin (fastify, opts, next) {
throw new Error('Unknown value for route decompress configuration')
}
} else if (globalDecompressParams.global) {
// if the plugin is set globally ( meaning that all the routes will be decompressed )
// if the plugin is set globally (meaning that all the routes will be decompressed)
// As the endpoint, does not have a custom rateLimit configuration, use the global one.
buildRouteDecompress(fastify, globalDecompressParams, routeOptions)
}
Expand Down Expand Up @@ -124,7 +122,9 @@ function processCompressParams (opts) {
deflate: () => ((opts.zlib || zlib).createDeflate || zlib.createDeflate)(params.zlibOptions)
}
params.uncompressStream = {
br: () => ((opts.zlib || zlib).createBrotliDecompress || zlib.createBrotliDecompress)(params.brotliOptions),
// Currently params.uncompressStream.br() is never called as we do not have any way to autodetect brotli compression in `fastify-compress`
// Brotli documentation reference: [RFC 7932](https://www.rfc-editor.org/rfc/rfc7932)
br: /* istanbul ignore next */ () => ((opts.zlib || zlib).createBrotliDecompress || zlib.createBrotliDecompress)(params.brotliOptions),
gzip: () => ((opts.zlib || zlib).createGunzip || zlib.createGunzip)(params.zlibOptions),
deflate: () => ((opts.zlib || zlib).createInflate || zlib.createInflate)(params.zlibOptions)
}
Expand Down Expand Up @@ -181,18 +181,8 @@ function processDecompressParams (opts) {
}

function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
// This methods works by altering the routeOptions, it has side effects.
// There is the possibility that the same options are set for more than
// one route, so we just need to make sure that the hook is addded only
// once.
if (routeOptions[compressAdded]) {
return
}

routeOptions[compressAdded] = true

// In order to provide a compress method with the same parameter set as the route itself has
// we do the decorate the reply at the start of the request
// In order to provide a compress method with the same parameter set as the route itself,
// we decorate the reply at the start of the request
if (Array.isArray(routeOptions.onRequest)) {
routeOptions.onRequest.push(onRequest)
} else if (typeof routeOptions.onRequest === 'function') {
Expand Down Expand Up @@ -504,6 +494,8 @@ function zipStream (deflate, encoding) {
function unzipStream (inflate, maxRecursion) {
if (!(maxRecursion >= 0)) maxRecursion = 3
return peek({ newline: false, maxBuffer: 10 }, function (data, swap) {
/* istanbul ignore if */
// This path is never taken, when `maxRecursion` < 0 it is automatically set back to 3
if (maxRecursion < 0) return swap(new Error('Maximum recursion reached'))
switch (isCompressed(data)) {
case 1: return swap(null, pumpify(inflate.gzip(), unzipStream(inflate, maxRecursion - 1)))
Expand Down
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@
"into-stream": "^6.0.0",
"is-deflate": "^1.0.0",
"is-gzip": "^2.0.0",
"is-stream": "^2.0.0",
"is-stream": "^2.0.1",
"is-zip": "^1.0.0",
"mime-db": "^1.45.0",
"minipass": "^3.1.1",
"peek-stream": "^1.1.0",
"mime-db": "^1.51.0",
"minipass": "^3.1.5",
"peek-stream": "^1.1.3",
"pump": "^3.0.0",
"pumpify": "^2.0.1",
"string-to-stream": "^3.0.0",
"unzipper": "^0.10.8"
"string-to-stream": "^3.0.1",
"unzipper": "^0.10.11"
},
"devDependencies": {
"@types/node": "^16.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@types/node": "^16.11.10",
"@typescript-eslint/parser": "^5.4.0",
"adm-zip": "^0.5.9",
"eslint-plugin-typescript": "^0.14.0",
"fastify": "^3.7.0",
"fastify": "^3.24.0",
"jsonstream": "^1.0.3",
"pre-commit": "^1.2.2",
"standard": "^16.0.0",
"tap": "^15.0.2",
"standard": "^16.0.4",
"tap": "^15.1.2",
"tsd": "^0.19.0",
"typescript": "^4.0.2"
"typescript": "^4.5.2"
},
"scripts": {
"coverage": "npm run unit -- --cov",
"coverage-report": "npm run coverage && tap --coverage-report=lcov",
"lint:typescript": "standard --fix --parser @typescript-eslint/parser --plugin typescript test/types/*.ts",
"test": "standard && npm run unit && npm run typescript",
"typescript": "tsd",
"unit": "tap test/*.js --no-check-coverage"
"unit": "tap -J test/*.js"
},
"keywords": [
"fastify",
Expand Down

0 comments on commit 321cdd0

Please sign in to comment.