core(dbw response compression): Exclude non binary files from auditing#4144
Conversation
| if (!isTextBasedResource || !record.resourceSize || !record.finished || | ||
| isChromeExtensionResource || !record.transferSize || record.statusCode === 304) { | ||
| isChromeExtensionResource || !record.transferSize || record.statusCode === 304 | ||
| ||isMediaResource) { |
There was a problem hiding this comment.
nit: add a space between ||isMediaResource => || isMediaResource
|
|
||
| networkRecords.forEach(record => { | ||
| const isTextBasedResource = record.resourceType() && record.resourceType().isTextType(); | ||
| const url = record.url.toLowerCase(); |
There was a problem hiding this comment.
const isMediaResource = (record.resourceType() === WebInspector.resourceTypes.Media || record.resourceType() === WebInspector.resourceTypes.Image; is probably a better approach.
Although I think we want svgs to be compressed at they are text based and not binary based.
There was a problem hiding this comment.
in this case we're already being fooled by what Chrome thinks the resource type is unfortunately, let's try to add a mime type sanity check instead :)
patrickhulce
left a comment
There was a problem hiding this comment.
thanks so much for picking this one up @FadySamirSadek! great thinking for an exhaustive list of binary extensions!
I left a few hints as to what the issue was referring to with a mime type sanity check that should help make this a little more robust.
| const gzip = require('zlib').gzip; | ||
|
|
||
| const compressionTypes = ['gzip', 'br', 'deflate']; | ||
| const mediaTypes = ['mp3', 'wav', 'wma', 'webm', 'jpg', 'gif', 'png', 'webp', |
There was a problem hiding this comment.
how about we changes this to a set of banned mime types instead of extensions
const binaryMimeTypes = ['image', 'audio', 'video'];| * @param {!NetworkRecords} networkRecords | ||
| * @return {!Array<{url: string, isBase64DataUri: boolean, mimeType: string, resourceSize: number}>} | ||
| */ | ||
| * @param {!NetworkRecords} networkRecords |
There was a problem hiding this comment.
nit: let's revert these changes to keep it as it was :)
|
|
||
| networkRecords.forEach(record => { | ||
| const isTextBasedResource = record.resourceType() && record.resourceType().isTextType(); | ||
| const url = record.url.toLowerCase(); |
There was a problem hiding this comment.
in this case we're already being fooled by what Chrome thinks the resource type is unfortunately, let's try to add a mime type sanity check instead :)
| networkRecords.forEach(record => { | ||
| const isTextBasedResource = record.resourceType() && record.resourceType().isTextType(); | ||
| const url = record.url.toLowerCase(); | ||
| let isMediaResource; |
There was a problem hiding this comment.
once we have a set of mime types we can combine them into the check for isTextBasedResource since that's what we're really getting at
const isBinaryResource = record.mimeType && binaryMimeTypes.some(mimeType => record.mimeType.startsWith(mimeType));
const isTextBasedResource = !isBinaryResource && record.resourceType() && ...| const isContentEncoded = record.responseHeaders.find(header => | ||
| header.name.toLowerCase() === 'content-encoding' && | ||
| compressionTypes.includes(header.value) | ||
| header.name.toLowerCase() === 'content-encoding' && |
There was a problem hiding this comment.
nit: let's revert this change too
|
hey @FadySamirSadek thanks for starting this! do you think you have time to carry it across the finish line? 🐎 🏁 if not no worries, we have another contributor interested in picking this up :) |
|
@patrickhulce yes will make progress on it today |
|
@FadySamirSadek any progress? Sorry to keep nagging but it would be awesome if we could land this! 💪 |
|
@wardpeet @patrickhulce Thank you for keeping me on this , sorry for making you wait that long for a simple issue. Please provide me with any feedback or changes you request. |
patrickhulce
left a comment
There was a problem hiding this comment.
awesome! mind adding a quick test case to
and then I think we're good here!| networkRecords.forEach(record => { | ||
| const isTextBasedResource = record.resourceType() && record.resourceType().isTextType(); | ||
| const isBinaryResource = record.mimeType && | ||
| binaryMimeTypes.some(mimeType => record.mimeType.startsWith(mimeType)); |
There was a problem hiding this comment.
nit: let's indent this line
| const isBinaryResource = record.mimeType && | ||
| binaryMimeTypes.some(mimeType => record.mimeType.startsWith(mimeType)); | ||
| const isTextBasedResource = !isBinaryResource && record.resourceType() && | ||
| record.resourceType().isTextType(); |
There was a problem hiding this comment.
nit: this one too :)
|
feedback addressed. @patrickhulce ptal. |
I have followed the recommended approach but I am wondering if there is a better way to know the type of the file and if the extensions I added are enough and if not what can I refer to in order to get a full list of extensions.
#4039