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

Fix: Drop mime-db dependency for http-compression rule #1126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/rule-http-compression/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"timeout": "1m"
},
"dependencies": {
"brotli": "^1.3.2",
"mime-db": "^1.34.0"
"brotli": "^1.3.2"
},
"description": "sonarwhal rule that checks for HTTP compression related best practices",
"devDependencies": {
Expand Down
55 changes: 15 additions & 40 deletions packages/rule-http-compression/src/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* ------------------------------------------------------------------------------
*/
import * as decompressBrotli from 'brotli/decompress';
import * as mimeDB from 'mime-db';

import { Category } from 'sonarwhal/dist/src/lib/enums/category';
import { RuleScope } from 'sonarwhal/dist/src/lib/enums/rulescope';
Expand Down Expand Up @@ -573,30 +572,24 @@ export default class HttpCompressionRule implements IRule {
};

const isCompressibleAccordingToMediaType = (mediaType: string): boolean => {
const COMMON_MEDIA_TYPES_THAT_SHOULD_BE_COMPRESSED = [
'image/x-icon',
'image/bmp'
];

const COMMON_MEDIA_TYPES_THAT_SHOULD_NOT_BE_COMPRESSED = [
'image/jpeg',
'image/png',
'image/gif',
'font/woff2',
'font/woff',
'font/otf',
'font/ttf'
];

if (!mediaType) {
return false;
}

/*
* The reason for doing the following is because
* `mime-db` is quite big, so querying it for
* eveything is expensive.
*/
const OTHER_COMMON_MEDIA_TYPES_THAT_SHOULD_BE_COMPRESSED = [
'application/rtf',
'application/wasm',
'font/collection',
'font/eot',
'font/otf',
'font/sfnt',
'font/ttf',
'image/bmp',
'image/x-icon',
'x-shader/x-fragment',
'x-shader/x-vertex'
];

/*
* Check if the media type is one of the common
Expand All @@ -605,29 +598,11 @@ export default class HttpCompressionRule implements IRule {
*/

if (isTextMediaType(mediaType) ||
COMMON_MEDIA_TYPES_THAT_SHOULD_BE_COMPRESSED.includes(mediaType)) {
OTHER_COMMON_MEDIA_TYPES_THAT_SHOULD_BE_COMPRESSED.includes(mediaType)) {
return true;
}

/*
* Check if the media type is one of the common
* ones for which it is known the response should
* not be compressed.
*/

if (COMMON_MEDIA_TYPES_THAT_SHOULD_NOT_BE_COMPRESSED.includes(mediaType)) {
return false;
}

/*
* If the media type is not included in any of the
* above, check `mime-db`.
*/

const typeInfo = mimeDB[mediaType];

return typeInfo && typeInfo.compressible;

return false;
};

const isSpecialCase = async (resource: string, element: IAsyncHTMLElement, response: Response): Promise<boolean> => {
Expand Down