Description
It’s impossible to retrieve the data; although it’s set correctly, when I attempt to decompress the data from Redis, I receive an error saying that the header is invalid. However, the data within Redis appears to be fine, and decompressing the original compressed data works without issues.
As you can see, the data is compressed using gzip and stored in Redis with the correct encoding:
Sample code:
<code>import { promisify } from 'node:util'
import { gzip } from 'node:zlib'
import { gunzip } from 'node:zlib'
import { redis } from '~/library/provider'
const asyncGzip = promisify(gzip)
const asyncGunzip = promisify(gunzip)
export async function handler() {
try {
await redis.connect()
} catch (/* c8 ignore next */ error) {}
const compressed = await asyncGzip(Buffer.from(JSON.stringify({ ok: true })))
await redis.set('gzip', compressed) // OK
console.log('>>> compressed', compressed)
const data = (await redis.get('gzip')) || Buffer.from('{}')
console.log('>>> data', data)
const payload = await asyncGunzip(compressed) // OK!
console.log('>>> payload', payload)
const decompressed = await asyncGunzip(data) // FAIL
console.log('>>> decompressed', decompressed.toString())
await redis.disconnect()
return {
statusCode: 200,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
body: JSON.stringify({ result: JSON.parse(decompressed.toString()) }),
}
}
</code>
<code>import { promisify } from 'node:util'
import { gzip } from 'node:zlib'
import { gunzip } from 'node:zlib'
import { redis } from '~/library/provider'
const asyncGzip = promisify(gzip)
const asyncGunzip = promisify(gunzip)
export async function handler() {
try {
await redis.connect()
} catch (/* c8 ignore next */ error) {}
const compressed = await asyncGzip(Buffer.from(JSON.stringify({ ok: true })))
await redis.set('gzip', compressed) // OK
console.log('>>> compressed', compressed)
const data = (await redis.get('gzip')) || Buffer.from('{}')
console.log('>>> data', data)
const payload = await asyncGunzip(compressed) // OK!
console.log('>>> payload', payload)
const decompressed = await asyncGunzip(data) // FAIL
console.log('>>> decompressed', decompressed.toString())
await redis.disconnect()
return {
statusCode: 200,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
body: JSON.stringify({ result: JSON.parse(decompressed.toString()) }),
}
}
</code>
import { promisify } from 'node:util'
import { gzip } from 'node:zlib'
import { gunzip } from 'node:zlib'
import { redis } from '~/library/provider'
const asyncGzip = promisify(gzip)
const asyncGunzip = promisify(gunzip)
export async function handler() {
try {
await redis.connect()
} catch (/* c8 ignore next */ error) {}
const compressed = await asyncGzip(Buffer.from(JSON.stringify({ ok: true })))
await redis.set('gzip', compressed) // OK
console.log('>>> compressed', compressed)
const data = (await redis.get('gzip')) || Buffer.from('{}')
console.log('>>> data', data)
const payload = await asyncGunzip(compressed) // OK!
console.log('>>> payload', payload)
const decompressed = await asyncGunzip(data) // FAIL
console.log('>>> decompressed', decompressed.toString())
await redis.disconnect()
return {
statusCode: 200,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
body: JSON.stringify({ result: JSON.parse(decompressed.toString()) }),
}
}
Node.js Version
20.11.1
Redis Server Version
6.2.6 (UpStash)
Node Redis Version
4.6.13
Platform
macOS & AWS Lambda