fix(cache): Fix compression and decompression format mismatch (#27021)

This commit is contained in:
Sergei Zharinov 2024-02-02 13:22:16 -03:00 committed by GitHub
parent 5d7770c5b5
commit 68d5faa96e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 4 additions and 16 deletions

View file

@ -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 }),
}),
);

View file

@ -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 },

View file

@ -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);
});
});

View file

@ -1,6 +1,5 @@
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);
@ -8,9 +7,8 @@ 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,