Skip to content

Commit

Permalink
🐛 fix for PERCY_GZIP flag (#1842)
Browse files Browse the repository at this point in the history
* :bug fix for gzip

* :bug fix for gzip

* test fix

* test fix
  • Loading branch information
prklm10 authored Jan 10, 2025
1 parent 4d589ce commit 20b4d94
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/core/src/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
snapshotLogName,
waitForTimeout,
withRetries,
waitForSelectorInsideBrowser
waitForSelectorInsideBrowser,
isGzipped
} from './utils.js';
import {
sha256hash
Expand Down Expand Up @@ -213,8 +214,12 @@ function processSnapshotResources({ domSnapshot, resources, ...snapshot }) {

if (process.env.PERCY_GZIP) {
for (let index = 0; index < resources.length; index++) {
resources[index].content = Pako.gzip(resources[index].content);
resources[index].sha = sha256hash(resources[index].content);
const alreadyZipped = isGzipped(resources[index].content);
/* istanbul ignore next: very hard to mock true */
if (!alreadyZipped) {
resources[index].content = Pako.gzip(resources[index].content);
resources[index].sha = sha256hash(resources[index].content);
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,21 @@ export function base64encode(content) {
.toString('base64');
}

// It checks if content is already gzipped or not.
// We don't want to gzip already gzipped content.
export function isGzipped(content) {
if (!(content instanceof Uint8Array || content instanceof ArrayBuffer)) {
return false;
}

// Ensure content is a Uint8Array
const data =
content instanceof ArrayBuffer ? new Uint8Array(content) : content;

// Gzip magic number: 0x1f8b
return data.length > 2 && data[0] === 0x1f && data[1] === 0x8b;
}

const RESERVED_CHARACTERS = {
'%3A': ':',
'%23': '#',
Expand Down
45 changes: 44 additions & 1 deletion packages/core/test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { decodeAndEncodeURLWithLogging, waitForSelectorInsideBrowser, compareObjectTypes } from '../src/utils.js';
import { decodeAndEncodeURLWithLogging, waitForSelectorInsideBrowser, compareObjectTypes, isGzipped } from '../src/utils.js';
import { logger, setupTest } from './helpers/index.js';
import percyLogger from '@percy/logger';
import Percy from '@percy/core';
import Pako from 'pako';

describe('utils', () => {
let log;
Expand Down Expand Up @@ -179,4 +180,46 @@ describe('utils', () => {
});
});
});
describe('isGzipped', () => {
it('returns true for gzipped content', () => {
const compressed = Pako.gzip('Hello, world!');
expect(isGzipped(compressed)).toBe(true);
});

it('returns false for plain uncompressed content', () => {
const uncompressed = new TextEncoder().encode('Hello, world!');
expect(isGzipped(uncompressed)).toBe(false);
});

it('returns false for a plain string input', () => {
const invalidInput = 'Not a Uint8Array';
expect(isGzipped(invalidInput)).toBe(false);
});

it('returns false for an empty Uint8Array', () => {
const emptyArray = new Uint8Array([]);
expect(isGzipped(emptyArray)).toBe(false);
});

it('returns false for an undefined', () => {
const empty = undefined;
expect(isGzipped(empty)).toBe(false);
});

it('returns false for an ArrayBuffer without Gzip magic number', () => {
const buffer = new ArrayBuffer(10);
const view = new Uint8Array(buffer);
view[0] = 0x00;
view[1] = 0x01;
expect(isGzipped(buffer)).toBe(false);
});

it('returns true for an ArrayBuffer with Gzip magic number', () => {
const buffer = new ArrayBuffer(10);
const view = new Uint8Array(buffer);
view[0] = 0x1f; // Gzip magic number
view[1] = 0x8b; // Gzip magic number
expect(isGzipped(buffer)).toBe(true);
});
});
});

0 comments on commit 20b4d94

Please sign in to comment.