无法使用 Redis for Node.js 处理二进制数据 (gzip)

问题描述 投票:0回答:1

描述

数据无法找回;尽管设置正确,但当我尝试从 Redis 解压缩数据时,我收到一条错误,指出标头无效。然而,Redis 中的数据似乎没有问题,并且解压缩原始压缩数据也没有问题。

如您所见,数据使用 gzip 进行压缩并以正确的编码存储在 Redis 中:

Medis

示例代码:

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 版本

20.11.1

Redis 服务器版本

6.2.6(UpStash)

节点Redis版本

4.6.13

平台

macOS 和 AWS Lambda

node.js redis
1个回答
0
投票

解决了

const key = 'gzip'

await redis.get(commandOptions({ returnBuffers: true }), key)

感谢ChatGPT。

© www.soinside.com 2019 - 2024. All rights reserved.