Skip to content

feat: support Uint8Array in command arguments#2120

Open
x-at-01 wants to merge 4 commits into
redis:mainfrom
x-at-01:feat/uint8array-support
Open

feat: support Uint8Array in command arguments#2120
x-at-01 wants to merge 4 commits into
redis:mainfrom
x-at-01:feat/uint8array-support

Conversation

@x-at-01

@x-at-01 x-at-01 commented May 27, 2026

Copy link
Copy Markdown

Description

This pull request adds support for raw Uint8Array instances as command arguments and key prefixes.

Since Node's Buffer inherits from Uint8Array, we detect instances of Uint8Array that are not Buffers and convert them to Buffer views. This conversion uses a zero-copy operation:

Buffer.from(val.buffer, val.byteOffset, val.byteLength)

This ensures high performance and zero memory copying overhead.

Usage Example

import Redis from "ioredis";

const redis = new Redis();

// 1. Set using a raw Uint8Array (which is not a Buffer)
const u8 = new Uint8Array([102, 111, 111]); // Represents 'foo'
await redis.set("binary-key", u8);

// 2. Read it back (returns a Buffer)
const result = await redis.getBuffer("binary-key");
console.log(result instanceof Buffer); // true
console.log(result.toString()); // 'foo'

Changes

  • Added helper file lib/utils/buffer.ts to recursively convert Uint8Array inside standard arrays, Maps, and plain object literals.
  • Updated CommandParameter and ArgumentType to support Uint8Array types.
  • Updated Command constructor to map command arguments and convert options.keyPrefix.
  • Updated stringifyArguments to convert any remaining Uint8Array arguments to Buffer.
  • Added unit tests in test/unit/command.ts to cover Uint8Array args, objects, maps, and key prefix inputs.

Note

Low Risk
Localized argument normalization and type extensions; existing Buffer paths unchanged, with unit tests covering encoding and cluster key extraction.

Overview
Adds first-class support for non-Buffer Uint8Array values in Redis command arguments and keyPrefix, converting them to Buffer via zero-copy Buffer.from(buffer, byteOffset, byteLength) so binary payloads work without callers manually wrapping buffers.

New helpers in lib/utils/buffer.ts recursively normalize Uint8Array in arrays, plain objects, and Maps. Command applies this on flatten, in stringifyArguments (including buffer-mode wire encoding), and for keyPrefix. Auto-pipelining key/slot routing now normalizes Uint8Array keys the same way, with getFirstKeyForCommand exported for tests.

Reviewed by Cursor Bugbot for commit 4391f5d. Bugbot is set up for automated code reviews on this repo. Configure here.

Convert raw Uint8Array instances in command arguments and keyPrefixes to Buffer instances using a zero-copy operation. This allows raw Uint8Array data to be set and read seamlessly just like standard Buffers.
@jit-ci

jit-ci Bot commented May 27, 2026

Copy link
Copy Markdown

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 7ec206d. Configure here.

Comment thread lib/autoPipelining.ts Outdated
@PavelPashov

Copy link
Copy Markdown
Contributor

@xtco3o Thanks for the PR. I’ll take a look and review it. In the meantime, please check any Cursor bot comments and address them if needed.

ai and others added 3 commits May 27, 2026 18:40
This prevents Uint8Array.prototype.toString() from returning a comma-separated decimal byte string, which would cause incorrect hash slot calculations in cluster mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants