Skip to content

Commit

Permalink
fix(cache): Fix compression and decompression format mismatch (#27021)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Feb 2, 2024
1 parent 5d7770c commit 68d5faa
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/util/cache/package/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function set(
getKey(namespace, key),
JSON.stringify({
compress: true,
value: await compressToBase64(value),
value: await compressToBase64(JSON.stringify(value)),
expiry: DateTime.local().plus({ minutes: ttlMinutes }),
}),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/util/cache/package/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function set(
getKey(namespace, key),
JSON.stringify({
compress: true,
value: await compressToBase64(value),
value: await compressToBase64(JSON.stringify(value)),
expiry: DateTime.local().plus({ minutes: ttlMinutes }),
}),
{ EX: redisTTL },
Expand Down
10 changes: 0 additions & 10 deletions lib/util/compress.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,4 @@ describe('util/compress', () => {
const decompressed = await decompressFromBase64(compressed);
expect(decompressed).toBe(input);
});

it('compresses objects', async () => {
const input = { foo: 'bar' };

const compressed = await compressToBase64(input);
expect(compressed).toBe('CwaAeyJmb28iOiJiYXIifQM=');

const decompressed = await decompressFromBase64(compressed);
expect(JSON.parse(decompressed)).toEqual(input);
});
});
6 changes: 2 additions & 4 deletions lib/util/compress.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { promisify } from 'node:util';
import zlib, { constants } from 'node:zlib';
import is from '@sindresorhus/is';

const brotliCompress = promisify(zlib.brotliCompress);
const brotliDecompress = promisify(zlib.brotliDecompress);

/**
* @deprecated
*/
export async function compressToBase64(input: unknown): Promise<string> {
const jsonStr = is.string(input) ? input : JSON.stringify(input);
const buf = await brotliCompress(jsonStr, {
export async function compressToBase64(input: string): Promise<string> {
const buf = await brotliCompress(input, {
params: {
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,
[constants.BROTLI_PARAM_QUALITY]: 8,
Expand Down

0 comments on commit 68d5faa

Please sign in to comment.