How are Buffers converted to string internally?

Aperta
#1,553 1 commento 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
25/100
Tipo di issue
Documentazione
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
node.js, redis
Ambito
backend, database

Direzione di ricerca

Inizia con il codice stream-write indicato in index.js e con l'esempio node-redis-test collegato, quindi traccia il modo in cui il client gestisce un Buffer rispetto a Buffer.toString(). Confronta i valori Redis osservati con le versioni di Node.js e Redis elencate. Il lavoro è completato quando vengono documentati il comportamento della conversione e il motivo per cui i due valori memorizzati differiscono.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I apologize in advance for asking the question here - I've originally posted my question on stackoverflow, but after 10 days I still didn't manage to get the answer that I was looking for... I thought I might try my luck here 😄. If it's not ok to ask the question here, let me know and I'll remove it...

I'm not having any issues with the library, but I'm trying to figure out how Buffer is being handled in node-redis library. I have my nodejs script that fetches some data from database, serializes and gzips the result and then stores that result in redis. Later I read this value in php script, so I need to match what php script expects

In node gzip library returns Buffer and what I'm wondering is how does node-redis convert this Buffer to string internally?

When I pass Buffer (from gzip) to redis, it will be saved in the format that my php script can read and decode properly

await client.setex('compressed', 3600, compressed);

Value in redis (result is from redis desktop manager terminal)

redis1:0>get "compressed'
"\x1F\x8B\x08\x00\x00\x00\x00\x00\x02\x03\xABVJI,IT\xB2R*I-.Q\xAA\x05\x00\xC0\xE3/\xAC\x0F\x00\x00\x00"

But when I pass Buffer.toString() to redis, php won't be able to read and decode

await client.setex('compressed', 3600, compressed.toString());

Value in redis

redis1:0>get "compressed-toString"
"\x1F\xEF\xBF\xBD\x08\x00\x00\x00\x00\x00\x02\x03\xEF\xBF\xBDVJI,IT\xEF\xBF\xBDR*I-.Q\xEF\xBF\xBD\x05\x00\xEF\xBF\xBD\xEF\xBF\xBD/\xEF\xBF\xBD\x0F\x00\x00\x00"

Now I'm trying to figure out how exactly is node-redis converting this buffer to string internally? Why are these 2 values different? Best that I could find is this stream write, but I'm still not clear on how exactly it works and why are the redis values different...

Any help is appreciated!


Test example

I've created simple example


Environment
  • Node.js Version: v12.18.3

  • Redis Version: 5.0.5 (redis:5.0.5-alpine)

  • Platform: Ubuntu 20.04

Lingua principale
TypeScript
Stelle
17.6k
Fork
2k
Merge medio
1g 22h
PR unite (30g)
62

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di redis/node-redis

Tutte le issue di redis/node-redis

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.