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

🐛 Accounting base64 encoding for resources size validation #1803

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 3 additions & 6 deletions packages/client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs';
import PercyEnv from '@percy/env';
import { git } from '@percy/env/utils';
import logger from '@percy/logger';
import Pako from 'pako';

import {
pool,
Expand All @@ -14,7 +13,8 @@ import {
waitForTimeout,
validateTiles,
formatLogErrors,
tagsList
tagsList,
getEncodedContent
} from './utils.js';

// Default client API URL can be set with an env var for API development
Expand Down Expand Up @@ -342,11 +342,8 @@ export class PercyClient {
validateId('build', buildId);
if (filepath) {
content = await fs.promises.readFile(filepath);
if (process.env.PERCY_GZIP) {
content = Pako.gzip(content);
}
}
let encodedContent = base64encode(content);
let encodedContent = getEncodedContent(content);
rishigupta1599 marked this conversation as resolved.
Show resolved Hide resolved

this.log.debug(`Uploading ${formatBytes(encodedContent.length)} resource: ${url}`, meta);
this.mayBeLogUploadSize(encodedContent.length, meta);
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import url from 'url';
import path from 'path';
import crypto from 'crypto';
import logger from '@percy/logger';
import Pako from 'pako';

// Formats a raw byte integer as a string
export function formatBytes(int) {
Expand Down Expand Up @@ -260,6 +261,13 @@ export function tagsList(tags) {
return tagsArr;
}

export function getEncodedContent(content) {
if (process.env.PERCY_GZIP) {
content = Pako.gzip(content);
}
return base64encode(content);
}

export {
hostnameMatches,
getProxy,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/network.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { request as makeRequest } from '@percy/client/utils';
import { request as makeRequest, getEncodedContent } from '@percy/client/utils';
import logger from '@percy/logger';
import mime from 'mime-types';
import { DefaultMap, createResource, hostnameMatches, normalizeURL, waitFor, decodeAndEncodeURLWithLogging } from './utils.js';
Expand Down Expand Up @@ -471,7 +471,7 @@ async function saveResponseResource(network, request, session) {
return log.debug('- Skipping remote resource', meta);
} else if (!body.length) {
return log.debug('- Skipping empty response', meta);
} else if (body.length > MAX_RESOURCE_SIZE) {
} else if (getEncodedContent(body).length > MAX_RESOURCE_SIZE) {
return log.debug('- Skipping resource larger than 25MB', meta);
} else if (!ALLOWED_STATUSES.includes(response.status)) {
return log.debug(`- Skipping disallowed status [${response.status}]`, meta);
Expand Down