Repository navigation
fix(types): accept binary keys in scan streams - #2296
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@ragib-mrb Thank you for the PR, I'll review it and get back to you |
| 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(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Dropped the runtime unit test cases and kept only the tsd test in \ est/typing/scan-stream.test-d.ts.
| 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`. | ||
|
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Dropped this paragraph from \README.md.
417c441 to
77b9319
Compare
## [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))
|
🎉 This PR is included in version 6.1.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
hscanStream,sscanStream,zscanStreamand their BufferStream counterparts accept binary keys at runtime, but their standalone and cluster signatures only accept strings. A binary key such asBuffer.from([0xff, 0x00, 0x80])therefore cannot be scanned in TypeScript without an assertion; converting it to a string changes its bytes.Use the existing
RedisKeytype 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:
npm run buildpasses. Library ESLint passes with existing warnings and no errors; scoped source/test formatting, whitespace and content checks pass.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*BufferStreamvariants) to acceptRedisKey(string | Buffer) instead ofstringonly.ScanStreamandcreateScanStreamhelpers 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.tsto lock in compile-time acceptance of binary keys on bothRedisandClusterand 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.