Skip to content

fix(types): accept binary keys in scan streams - #2296

Merged
PavelPashov merged 2 commits into
redis:mainfrom
ragib-mrb:fix/binary-scan-stream-keys
Nov 24, 2026
Merged

PavelPashov merged 2 commits into
redis:mainfrom
ragib-mrb:fix/binary-scan-stream-keys

Conversation

@ragib-mrb

@ragib-mrb ragib-mrb commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

hscanStream, sscanStream, zscanStream and their BufferStream counterparts accept binary keys at runtime, but their standalone and cluster signatures only accept strings. A binary key such as Buffer.from([0xff, 0x00, 0x80]) therefore cannot be scanned in TypeScript without an assertion; converting it to a string changes its bytes.

Use the existing RedisKey type for all twelve public methods and the shared stream helpers, and allow the scan argument array to retain Buffer keys. The README now clarifies that BufferStream chooses the reply encoding independently of the key's type. No command-routing or runtime behavior change is intended.

Validation:

  • The new tsd test fails before the fix with six Buffer-to-string errors. The full tsd suite passes after rebuilding, including positive standalone/cluster calls and rejection of numeric keys.
  • All 20 scan-stream unit tests pass. Twelve new cases verify Buffer identity and byte preservation across an empty intermediate page and the terminating cursor, for all six methods on standalone and cluster clients with command replies stubbed.
  • npm run build passes. Library ESLint passes with existing warnings and no errors; scoped source/test formatting, whitespace and content checks pass.
  • Live standalone/cluster Redis integration suites were not run.

Note

Low Risk
Type-only and argument typing adjustments for scan streams; no intended routing or runtime behavior change.

Overview
Aligns standalone and cluster scan-stream APIs with runtime behavior by typing sscanStream, hscanStream, zscanStream (and their *BufferStream variants) to accept RedisKey (string | Buffer) instead of string only. ScanStream and createScanStream helpers use the same key type, and the internal scan argument list is typed as (string | Buffer)[] so Buffer keys are passed through without coercing bytes.

Adds test/typing/scan-stream.test-d.ts to lock in compile-time acceptance of binary keys on both Redis and Cluster and rejection of invalid key types.

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T16:28:38.793764Z 77b9319 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@PavelPashov

Copy link
Copy Markdown
Contributor

@ragib-mrb Thank you for the PR, I'll review it and get back to you

Comment thread test/unit/scan_stream.ts Outdated
Comment on lines +26 to +60
for (const command of ["hscan", "sscan", "zscan"]) {
for (const suffix of ["", "Buffer"]) {
for (const mode of ["standalone", "cluster"]) {
it(`preserves binary keys across ${command}${suffix}Stream pages in ${mode}`, async () => {
const client =
mode === "standalone"
? new Redis({ lazyConnect: true })
: new Cluster([], { lazyConnect: true });
const key = Buffer.from([0xff, 0x00, 0x80]);
const scan = sinon.stub(client, `${command}${suffix}` as any);
scan
.onFirstCall()
.callsFake((_args, callback) => callback(null, ["42", []]));
scan
.onSecondCall()
.callsFake((_args, callback) =>
callback(null, [Buffer.from("0"), []])
);

try {
for await (const _ of client[`${command}${suffix}Stream`](key)) {
// Consume both pages, including the empty intermediate one.
}
expect(scan.calledTwice).to.eql(true);
expect(scan.firstCall.args[0]).to.eql([key, "0"]);
expect(scan.secondCall.args[0]).to.eql([key, "42"]);
expect(scan.firstCall.args[0][0]).to.equal(key);
expect(scan.secondCall.args[0][0]).to.equal(key);
} finally {
client.disconnect();
}
});
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we drop these unit cases and keep only the tsd test? They pass on main without this change because Buffer keys already worked at runtime. That means they don't cover the fix, and test/typing/scan-stream.test-d.ts already fails on main as expected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dropped the runtime unit test cases and kept only the tsd test in \ est/typing/scan-stream.test-d.ts.

Comment thread README.md Outdated
Comment on lines +838 to +839
The key passed to `hscanStream`, `sscanStream`, `zscanStream` and their `BufferStream` variants can be a string or a `Buffer`. Use a `Buffer` for binary keys; the `BufferStream` suffix controls the reply encoding, independently of the key type. This also applies to these methods on a `Cluster`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we drop this paragraph? The Binary data section already says you can pass Buffers anywhere and that the Buffer suffix only changes the reply type, and the new RedisKey signature makes this clear in editors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dropped this paragraph from \README.md.

@ragib-mrb
ragib-mrb force-pushed the fix/binary-scan-stream-keys branch from 417c441 to 77b9319 Compare November 23, 2026 16:26

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, LGTM

@PavelPashov
PavelPashov merged commit 1c1fd3c into redis:main Nov 24, 2026
22 checks passed
github-actions Bot pushed a commit that referenced this pull request Dec 2, 2026
## [6.1.1](v6.1.0...v6.1.1) (2026-12-02)

### Bug Fixes

* exclude all subscriber-mode commands from auto pipelines ([#2311](#2311)) ([466f6be](466f6be))
* keep the current db when Redis rejects a SELECT ([#2308](#2308)) ([0516749](0516749)), closes [#2307](#2307)
* reject length-prefixed strings that are not followed by CRLF ([#2300](#2300)) ([77f88ee](77f88ee)), closes [#1172](#1172)
* **types:** accept binary keys in scan streams ([#2296](#2296)) ([1c1fd3c](1c1fd3c))
* **types:** keep redis-errors out of public error declarations ([#2322](#2322)) ([754d967](754d967))
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 6.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants