Skip to content

Decoder

In Node.js, most users expect the responses to be in string format unless specified otherwise. Users can select which data type they expect to receive as a response. The user has the option, using DecoderOption as an extension to command option types with Decoder, of converting the response into either a string or bytes for each command. In addition, as part of the client creation process, the user has the option of setting a default for all commands if no decoder argument is passed to the command.

Decoder is an optional parameter for all commands and it has two options

  1. Bytes: decodes the response into a Buffer (byte array).
  2. String: decodes the response into a string.

If Decoder is not configured, Decoder.String will be used as the default decoder. Note: Using the Decoder.String to convert Non-UTF-8 encoded Valkey strings can lead to errors unrecoverable data lost. See: #2221.

Here is an example of command implementation to outline arguments using DecoderOption and response type:

public getdel(
key: GlideString,
options?: DecoderOption,
): Promise<GlideString | null>

Here’s a simple example demonstrating how to use Decoder in command API and Buffer.from() to encode binary data:

expect(await client.set(key, value)).toEqual("OK");
expect(await client.getdel(key)).toEqual(value);
const valueEncoded = Buffer.from(value);
expect(await client.set(key, value)).toEqual("OK");
expect(await client.getdel(key, { decoder: Decoder.Bytes })).toEqual(
valueEncoded,
);