Skip to content

Commit

Permalink
fix: keyPrefix should work with Buffer
Browse files Browse the repository at this point in the history
Closes #1486
  • Loading branch information
luin committed Mar 19, 2022
1 parent bb166cc commit 6942cec
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,24 @@ export default class Command implements Respondable {
this.initPromise();

if (options.keyPrefix) {
this._iterateKeys((key) => options.keyPrefix + key);
// @ts-expect-error
const isBufferKeyPrefix = options.keyPrefix instanceof Buffer;
// @ts-expect-error
let keyPrefixBuffer: Buffer | null = isBufferKeyPrefix
? options.keyPrefix
: null;
this._iterateKeys((key) => {
if (key instanceof Buffer) {
if (keyPrefixBuffer === null) {
keyPrefixBuffer = Buffer.from(options.keyPrefix);
}
return Buffer.concat([keyPrefixBuffer, key]);
} else if (isBufferKeyPrefix) {
// @ts-expect-error
return Buffer.concat([options.keyPrefix, Buffer.from(String(key))]);
}
return options.keyPrefix + key;
});
}

if (options.readOnly) {
Expand Down Expand Up @@ -327,8 +344,8 @@ export default class Command implements Respondable {
* Iterate through the command arguments that are considered keys.
*/
private _iterateKeys(
transform: Function = (key) => key
): Array<string | Buffer> {
transform: (key: CommandParameter) => CommandParameter = (key) => key
): (string | Buffer)[] {
if (typeof this.keys === "undefined") {
this.keys = [];
if (exists(this.name)) {
Expand Down
1 change: 0 additions & 1 deletion test/functional/cluster/scripting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("cluster:scripting", () => {
[12182, 16383, ["127.0.0.1", 30002]],
];
}
console.log(argv);
if (argv[0] === "eval" && argv[1] === lua && argv[2] === "2") {
return argv.slice(3);
}
Expand Down
35 changes: 35 additions & 0 deletions test/functional/send_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe("send command", () => {
expect(res).to.eql("OK");
}
);
// @ts-expect-error
redis.callBuffer("get", Buffer.from("foo"), function (err, result) {
expect(result).to.be.instanceof(Buffer);
expect(result.toString()).to.eql("bar");
Expand Down Expand Up @@ -149,6 +150,40 @@ describe("send command", () => {
});
});

it("should support prefixing buffer keys", async () => {
const redis = new Redis({ keyPrefix: "foo:" });
await redis.mset(
Buffer.from("bar"),
Buffer.from("baz"),
Buffer.from("foo"),
Buffer.from("baz")
);
await redis.set(Buffer.from([0xff]), Buffer.from("baz"));

const redisWOPrefix = new Redis();
expect(await redisWOPrefix.get("foo:bar")).to.eql("baz");
expect(await redisWOPrefix.get("foo:foo")).to.eql("baz");
expect(
await redisWOPrefix.get(Buffer.from([0x66, 0x6f, 0x6f, 0x3a, 0xff]))
).to.eql("baz");
});

it("should support buffer as keyPrefix", async () => {
// @ts-expect-error
const redis = new Redis({ keyPrefix: Buffer.from([0xff]) });
await redis.mset("bar", Buffer.from("baz"), "foo", Buffer.from("bar"));
await redis.set(Buffer.from([0xff]), Buffer.from("baz"));

const redisWOPrefix = new Redis();
expect(
await redisWOPrefix.get(Buffer.from([0xff, 0x62, 0x61, 0x72]))
).to.eql("baz");
expect(
await redisWOPrefix.get(Buffer.from([0xff, 0x66, 0x6f, 0x6f]))
).to.eql("bar");
expect(await redisWOPrefix.get(Buffer.from([0xff, 0xff]))).to.eql("baz");
});

it("should support key prefixing for zunionstore", (done) => {
const redis = new Redis({ keyPrefix: "foo:" });
redis.zadd("zset1", 1, "one");
Expand Down

0 comments on commit 6942cec

Please sign in to comment.