Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: achieve 100% code coverage #196

Merged
merged 20 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b12d42f
test: removing duplicates
darkgl0w Nov 22, 2021
a842c95
test: make sure `encodings` and `requestEncodings` are sorted
darkgl0w Nov 22, 2021
21c2369
test: `deflate`/`gzip` compressed Buffers/Streams decompression
darkgl0w Nov 22, 2021
2834adf
test: `deflate`/`gzip` compressed payloads decompression on 'x-no-com…
darkgl0w Nov 22, 2021
318e6ab
test: add `content-encoding` for a Stream when `inflateIfDeflated` is…
darkgl0w Nov 22, 2021
a07e6ee
test: remove `content-length` for a Stream when `inflateIfDeflated` i…
darkgl0w Nov 22, 2021
b34ea6e
test: handle case where serialize doesn't return a string or Buffer
darkgl0w Nov 22, 2021
d70f2ea
test: make sure `fastify-compress` hooks are correctly added
darkgl0w Nov 23, 2021
8f12963
test: decompress `zip` compressed payloads on `x-no-compression` header
darkgl0w Nov 23, 2021
f846491
fix (test): linting
darkgl0w Nov 23, 2021
47b8887
fix: remove superfluous code
darkgl0w Nov 23, 2021
5cc14f6
test: make sure `Content-Encoding` header is actually added
darkgl0w Nov 23, 2021
6a44bf4
test: no compression when reply `Content-Type` header is not set
darkgl0w Nov 23, 2021
cf7276e
fix: remove the check preventing multiple onSend triggers as it was f…
darkgl0w Nov 23, 2021
270466b
fix: restore code
darkgl0w Nov 23, 2021
f0c2d06
test: `compressStream` and `uncompressStream` zlib fallbacks
darkgl0w Nov 23, 2021
0f4eaef
fix (test): skip coverage on unreachable paths
darkgl0w Nov 24, 2021
03e526e
chore (test): enable 100% code coverage check
darkgl0w Nov 24, 2021
3f54844
chore: upgrade package dependencies
darkgl0w Nov 24, 2021
aac4ea6
ci: add Node.js 17
darkgl0w Nov 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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