Skip to content

Commit

Permalink
Abort zip upload if larger than individual files, and log size reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Oct 20, 2023
1 parent 5b0de27 commit ad78d15
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 3 additions & 3 deletions node-src/lib/compress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ afterEach(() => {
});

const testContext = { sourceDir: '/chromatic-tmp', log: new TestLogger() } as any;
const fileInfo = { paths: ['file1'] };
const files = [{ localPath: '/chromatic-tmp/file1', targetPath: 'file1', contentLength: 1 }];

describe('makeZipFile', () => {
it('adds files to an archive', async () => {
Expand All @@ -24,14 +24,14 @@ describe('makeZipFile', () => {
},
});

const result = await makeZipFile(testContext, fileInfo);
const result = await makeZipFile(testContext, files);

expect(existsSync(result.path)).toBeTruthy();
expect(result.size).toBeGreaterThan(0);
});

it('rejects on error signals', () => {
return expect(makeZipFile(testContext, fileInfo)).rejects.toThrow(
return expect(makeZipFile(testContext, files)).rejects.toThrow(
`ENOENT: no such file or directory, open '/chromatic-tmp/file1'`
);
});
Expand Down
11 changes: 8 additions & 3 deletions node-src/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ export async function uploadAsZipFile(
onError?: (error: Error, path?: string) => void;
} = {}
) {
const originalSize = files.reduce((acc, { contentLength }) => acc + contentLength, 0);
const zipped = await makeZipFile(ctx, files);
const { path, size: total } = zipped;
const { path, size } = zipped;

if (size > originalSize) throw new Error('Zip file is larger than individual files');
ctx.log.debug(`Compression reduced upload size by ${originalSize - size} bytes`);

const { getZipUploadUrl } = await ctx.client.runQuery<GetZipUploadUrlMutationResult>(
GetZipUploadUrlMutation,
{ buildId: ctx.announcedBuild.id }
Expand All @@ -96,12 +101,12 @@ export async function uploadAsZipFile(
options.onStart?.();

try {
await uploadZip(ctx, path, url, total, (progress) => options.onProgress?.(progress, total));
await uploadZip(ctx, path, url, size, (progress) => options.onProgress?.(progress, size));
} catch (e) {
return options.onError?.(e, path);
}

await waitForUnpack(ctx, sentinelUrl);

options.onComplete?.(total, domain);
options.onComplete?.(size, domain);
}
5 changes: 4 additions & 1 deletion node-src/tasks/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ export const uploadStorybook = async (ctx: Context, task: Task) => {
try {
await uploadAsZipFile(ctx, files, options);
} catch (err) {
ctx.log.debug({ err }, 'Error uploading zip file');
ctx.log.debug(
{ err },
'Error uploading zip file, falling back to uploading individual files'
);
await uploadAsIndividualFiles(ctx, files, options);
}
} else {
Expand Down

0 comments on commit ad78d15

Please sign in to comment.