From 5c3be9b775bc7906367f45c2f52a9b6f2c531c1e Mon Sep 17 00:00:00 2001 From: jhpung Date: Sat, 19 Oct 2024 01:30:22 +0900 Subject: [PATCH 1/6] feat: add zscan's noscores option --- lib/ScanStream.ts | 4 +++ lib/types.ts | 1 + lib/utils/RedisCommander.ts | 64 +++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/lib/ScanStream.ts b/lib/ScanStream.ts index e74a7a2c..c6091b92 100644 --- a/lib/ScanStream.ts +++ b/lib/ScanStream.ts @@ -4,6 +4,7 @@ interface Options extends ReadableOptions { key?: string; match?: string; type?: string; + noscores?: boolean; command: string; redis: any; count?: string | number; @@ -39,6 +40,9 @@ export default class ScanStream extends Readable { if (this.opt.count) { args.push("COUNT", String(this.opt.count)); } + if (this.opt.noscores) { + args.push("noscores"); + } this.opt.redis[this.opt.command](args, (err, res) => { if (err) { diff --git a/lib/types.ts b/lib/types.ts index 8c897a8e..37821ae5 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -30,4 +30,5 @@ export interface ScanStreamOptions { match?: string; type?: string; count?: number; + noscores?: boolean; } diff --git a/lib/utils/RedisCommander.ts b/lib/utils/RedisCommander.ts index 49388d8e..215ed50b 100644 --- a/lib/utils/RedisCommander.ts +++ b/lib/utils/RedisCommander.ts @@ -13798,11 +13798,38 @@ interface RedisCommander { cursor: number | string, callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + noscoresToken: "noscores", + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + noscoresToken: "noscores", + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + countToken: "COUNT", + count: number | string, + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + countToken: "COUNT", + count: number | string, + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; zscan( key: RedisKey, cursor: number | string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", callback?: Callback<[cursor: string, elements: string[]]> ): Result<[cursor: string, elements: string[]], Context>; zscanBuffer( @@ -13810,6 +13837,37 @@ interface RedisCommander { cursor: number | string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + noscoresToken: "noscores", + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + noscoresToken: "noscores", callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; zscan( @@ -13817,6 +13875,8 @@ interface RedisCommander { cursor: number | string, patternToken: "MATCH", pattern: string, + countToken: "COUNT", + count: number | string, callback?: Callback<[cursor: string, elements: string[]]> ): Result<[cursor: string, elements: string[]], Context>; zscanBuffer( @@ -13824,6 +13884,8 @@ interface RedisCommander { cursor: number | string, patternToken: "MATCH", pattern: string, + countToken: "COUNT", + count: number | string, callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; zscan( @@ -13833,6 +13895,7 @@ interface RedisCommander { pattern: string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", callback?: Callback<[cursor: string, elements: string[]]> ): Result<[cursor: string, elements: string[]], Context>; zscanBuffer( @@ -13842,6 +13905,7 @@ interface RedisCommander { pattern: string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; From 32ace0d0bd81b53545c5e14eae3e8aa2a6ba8863 Mon Sep 17 00:00:00 2001 From: jhpung Date: Sat, 19 Oct 2024 01:54:59 +0900 Subject: [PATCH 2/6] test: add zscan unit tests --- test/unit/commander.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/unit/commander.ts b/test/unit/commander.ts index 109cbe6b..f54031fa 100644 --- a/test/unit/commander.ts +++ b/test/unit/commander.ts @@ -1,6 +1,7 @@ import * as sinon from "sinon"; import { expect } from "chai"; import Commander from "../../lib/utils/Commander"; +import Command from "../../lib/Command"; describe("Commander", () => { describe("#getBuiltinCommands()", () => { @@ -63,4 +64,21 @@ describe("Commander", () => { Commander.prototype.sendCommand.restore(); }); + + describe("#zscan", () => { + it("should pass noscores option", async (done) => { + const args: any[] = ["key", "0", "MATCH", "pattern", "COUNT", "10", "noscores"]; + sinon.stub(Commander.prototype, "sendCommand").callsFake((command) => { + if(command.args.every((arg, index) => arg === args[index])) { + return done(); + } + console.error(command.args); + return done(new Error(`args should be ${args.join(", ")}`)); + }); + const c = new Commander(); + + await c.zscan(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); + (Commander.prototype.sendCommand as any).restore(); + }); + }); }); From 8da5618701ca43816adefcf6bf309fe10526724a Mon Sep 17 00:00:00 2001 From: jhpung Date: Sat, 19 Oct 2024 01:55:17 +0900 Subject: [PATCH 3/6] test: fix script command tests --- test/functional/scripting.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/scripting.ts b/test/functional/scripting.ts index ee796bff..60bd86ac 100644 --- a/test/functional/scripting.ts +++ b/test/functional/scripting.ts @@ -251,10 +251,10 @@ describe("scripting", () => { const [a, b] = await redis.multi().test("foo").test("bar").exec(); expect(a[0].message).to.equal( - "NOSCRIPT No matching script. Please use EVAL." + "NOSCRIPT No matching script." ); expect(b[0].message).to.equal( - "NOSCRIPT No matching script. Please use EVAL." + "NOSCRIPT No matching script." ); }); spy.restore(); From 8f976777d8b0ed5feced29ab99b9a499d036b4f2 Mon Sep 17 00:00:00 2001 From: jhpung Date: Sat, 19 Oct 2024 01:30:22 +0900 Subject: [PATCH 4/6] feat: add zscan's noscores option Signed-off-by: jhpung --- lib/ScanStream.ts | 4 +++ lib/types.ts | 1 + lib/utils/RedisCommander.ts | 64 +++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/lib/ScanStream.ts b/lib/ScanStream.ts index e74a7a2c..c6091b92 100644 --- a/lib/ScanStream.ts +++ b/lib/ScanStream.ts @@ -4,6 +4,7 @@ interface Options extends ReadableOptions { key?: string; match?: string; type?: string; + noscores?: boolean; command: string; redis: any; count?: string | number; @@ -39,6 +40,9 @@ export default class ScanStream extends Readable { if (this.opt.count) { args.push("COUNT", String(this.opt.count)); } + if (this.opt.noscores) { + args.push("noscores"); + } this.opt.redis[this.opt.command](args, (err, res) => { if (err) { diff --git a/lib/types.ts b/lib/types.ts index 8c897a8e..37821ae5 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -30,4 +30,5 @@ export interface ScanStreamOptions { match?: string; type?: string; count?: number; + noscores?: boolean; } diff --git a/lib/utils/RedisCommander.ts b/lib/utils/RedisCommander.ts index 49388d8e..215ed50b 100644 --- a/lib/utils/RedisCommander.ts +++ b/lib/utils/RedisCommander.ts @@ -13798,11 +13798,38 @@ interface RedisCommander { cursor: number | string, callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + noscoresToken: "noscores", + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + noscoresToken: "noscores", + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + countToken: "COUNT", + count: number | string, + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + countToken: "COUNT", + count: number | string, + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; zscan( key: RedisKey, cursor: number | string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", callback?: Callback<[cursor: string, elements: string[]]> ): Result<[cursor: string, elements: string[]], Context>; zscanBuffer( @@ -13810,6 +13837,37 @@ interface RedisCommander { cursor: number | string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + callback?: Callback<[cursor: Buffer, elements: Buffer[]]> + ): Result<[cursor: Buffer, elements: Buffer[]], Context>; + zscan( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + noscoresToken: "noscores", + callback?: Callback<[cursor: string, elements: string[]]> + ): Result<[cursor: string, elements: string[]], Context>; + zscanBuffer( + key: RedisKey, + cursor: number | string, + patternToken: "MATCH", + pattern: string, + noscoresToken: "noscores", callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; zscan( @@ -13817,6 +13875,8 @@ interface RedisCommander { cursor: number | string, patternToken: "MATCH", pattern: string, + countToken: "COUNT", + count: number | string, callback?: Callback<[cursor: string, elements: string[]]> ): Result<[cursor: string, elements: string[]], Context>; zscanBuffer( @@ -13824,6 +13884,8 @@ interface RedisCommander { cursor: number | string, patternToken: "MATCH", pattern: string, + countToken: "COUNT", + count: number | string, callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; zscan( @@ -13833,6 +13895,7 @@ interface RedisCommander { pattern: string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", callback?: Callback<[cursor: string, elements: string[]]> ): Result<[cursor: string, elements: string[]], Context>; zscanBuffer( @@ -13842,6 +13905,7 @@ interface RedisCommander { pattern: string, countToken: "COUNT", count: number | string, + noscoresToken: "noscores", callback?: Callback<[cursor: Buffer, elements: Buffer[]]> ): Result<[cursor: Buffer, elements: Buffer[]], Context>; From ef84dd949267bb8e08f78f9082e9a2960ae1da47 Mon Sep 17 00:00:00 2001 From: jhpung Date: Sat, 19 Oct 2024 01:54:59 +0900 Subject: [PATCH 5/6] test: add zscan unit tests Signed-off-by: jhpung --- test/unit/commander.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/unit/commander.ts b/test/unit/commander.ts index 109cbe6b..f54031fa 100644 --- a/test/unit/commander.ts +++ b/test/unit/commander.ts @@ -1,6 +1,7 @@ import * as sinon from "sinon"; import { expect } from "chai"; import Commander from "../../lib/utils/Commander"; +import Command from "../../lib/Command"; describe("Commander", () => { describe("#getBuiltinCommands()", () => { @@ -63,4 +64,21 @@ describe("Commander", () => { Commander.prototype.sendCommand.restore(); }); + + describe("#zscan", () => { + it("should pass noscores option", async (done) => { + const args: any[] = ["key", "0", "MATCH", "pattern", "COUNT", "10", "noscores"]; + sinon.stub(Commander.prototype, "sendCommand").callsFake((command) => { + if(command.args.every((arg, index) => arg === args[index])) { + return done(); + } + console.error(command.args); + return done(new Error(`args should be ${args.join(", ")}`)); + }); + const c = new Commander(); + + await c.zscan(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); + (Commander.prototype.sendCommand as any).restore(); + }); + }); }); From 37ad61e55516465546577733c70cff3e492decec Mon Sep 17 00:00:00 2001 From: jhpung Date: Sat, 19 Oct 2024 01:55:17 +0900 Subject: [PATCH 6/6] test: fix script command tests Signed-off-by: jhpung --- test/functional/scripting.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/scripting.ts b/test/functional/scripting.ts index ee796bff..60bd86ac 100644 --- a/test/functional/scripting.ts +++ b/test/functional/scripting.ts @@ -251,10 +251,10 @@ describe("scripting", () => { const [a, b] = await redis.multi().test("foo").test("bar").exec(); expect(a[0].message).to.equal( - "NOSCRIPT No matching script. Please use EVAL." + "NOSCRIPT No matching script." ); expect(b[0].message).to.equal( - "NOSCRIPT No matching script. Please use EVAL." + "NOSCRIPT No matching script." ); }); spy.restore();