Skip to content

core(dbw response compression): Exclude non binary files from auditing#4144

Merged
paulirish merged 6 commits intoGoogleChrome:masterfrom
FadySamirSadek:exclude-media-response-compression
Feb 7, 2018
Merged

core(dbw response compression): Exclude non binary files from auditing#4144
paulirish merged 6 commits intoGoogleChrome:masterfrom
FadySamirSadek:exclude-media-response-compression

Conversation

@FadySamirSadek
Copy link
Contributor

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

if (!isTextBasedResource || !record.resourceSize || !record.finished ||
isChromeExtensionResource || !record.transferSize || record.statusCode === 304) {
isChromeExtensionResource || !record.transferSize || record.statusCode === 304
||isMediaResource) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a space between ||isMediaResource => || isMediaResource


networkRecords.forEach(record => {
const isTextBasedResource = record.resourceType() && record.resourceType().isTextType();
const url = record.url.toLowerCase();
Copy link
Collaborator

@wardpeet wardpeet Jan 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

@GoogleChrome GoogleChrome deleted a comment Jan 1, 2018
@GoogleChrome GoogleChrome deleted a comment Jan 1, 2018
@GoogleChrome GoogleChrome deleted a comment Jan 1, 2018
@GoogleChrome GoogleChrome deleted a comment Jan 1, 2018
Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's revert this change too

@patrickhulce
Copy link
Collaborator

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 :)

@FadySamirSadek
Copy link
Contributor Author

@patrickhulce yes will make progress on it today

@wardpeet
Copy link
Collaborator

@FadySamirSadek any progress? Sorry to keep nagging but it would be awesome if we could land this! 💪

@FadySamirSadek
Copy link
Contributor Author

@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.

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome! mind adding a quick test case to

it('returns only text and non encoded responses', () => {
return responseCompression.afterPass(options, createNetworkRequests(traceData))
.then(artifact => {
assert.equal(artifact.length, 2);
assert.ok(/index\.css$/.test(artifact[0].url));
assert.ok(/index\.json$/.test(artifact[1].url));
});
});
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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this one too :)

@paulirish
Copy link
Member

feedback addressed. @patrickhulce ptal.

@paulirish paulirish merged commit 3131106 into GoogleChrome:master Feb 7, 2018
@FadySamirSadek FadySamirSadek deleted the exclude-media-response-compression branch February 8, 2018 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants