diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index f052cdc369..5848362b2c 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -2189,7 +2189,7 @@ export class BaseClient { * console.log(result); // Output: 3 - Indicates that there are 3 elements in the list. * ``` */ - public async llen(key: string): Promise { + public async llen(key: GlideString): Promise { return this.createWritePromise(createLLen(key)); } @@ -2205,6 +2205,8 @@ export class BaseClient { * @param destination - The key to the destination list. * @param whereFrom - The {@link ListDirection} to remove the element from. * @param whereTo - The {@link ListDirection} to add the element to. + * @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. + * If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used. * @returns The popped element, or `null` if `source` does not exist. * * @example @@ -2223,13 +2225,15 @@ export class BaseClient { * ``` */ public async lmove( - source: string, - destination: string, + source: GlideString, + destination: GlideString, whereFrom: ListDirection, whereTo: ListDirection, - ): Promise { + decoder?: Decoder, + ): Promise { return this.createWritePromise( createLMove(source, destination, whereFrom, whereTo), + { decoder: decoder }, ); } @@ -2249,6 +2253,8 @@ export class BaseClient { * @param whereFrom - The {@link ListDirection} to remove the element from. * @param whereTo - The {@link ListDirection} to add the element to. * @param timeout - The number of seconds to wait for a blocking operation to complete. A value of `0` will block indefinitely. + * @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. + * If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used. * @returns The popped element, or `null` if `source` does not exist or if the operation timed-out. * * @example @@ -2266,14 +2272,16 @@ export class BaseClient { * ``` */ public async blmove( - source: string, - destination: string, + source: GlideString, + destination: GlideString, whereFrom: ListDirection, whereTo: ListDirection, timeout: number, - ): Promise { + decoder?: Decoder, + ): Promise { return this.createWritePromise( createBLMove(source, destination, whereFrom, whereTo, timeout), + { decoder: decoder }, ); } @@ -5258,6 +5266,8 @@ export class BaseClient { * * @param key - The `key` of the list. * @param index - The `index` of the element in the list to retrieve. + * @param decoder - (Optional) {@link Decoder} type which defines how to handle the response. + * If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used. * @returns - The element at `index` in the list stored at `key`. * If `index` is out of range or if `key` does not exist, null is returned. * @@ -5275,8 +5285,14 @@ export class BaseClient { * console.log(result); // Output: 'value3' - Returns the last element in the list stored at 'my_list'. * ``` */ - public async lindex(key: string, index: number): Promise { - return this.createWritePromise(createLIndex(key, index)); + public async lindex( + key: GlideString, + index: number, + decoder?: Decoder, + ): Promise { + return this.createWritePromise(createLIndex(key, index), { + decoder: decoder, + }); } /** @@ -5300,10 +5316,10 @@ export class BaseClient { * ``` */ public async linsert( - key: string, + key: GlideString, position: InsertPosition, - pivot: string, - element: string, + pivot: GlideString, + element: GlideString, ): Promise { return this.createWritePromise( createLInsert(key, position, pivot, element), diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 09bb359ecf..73255ef14f 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -884,7 +884,7 @@ export function createLRange( /** * @internal */ -export function createLLen(key: string): command_request.Command { +export function createLLen(key: GlideString): command_request.Command { return createCommand(RequestType.LLen, [key]); } @@ -906,8 +906,8 @@ export enum ListDirection { * @internal */ export function createLMove( - source: string, - destination: string, + source: GlideString, + destination: GlideString, whereFrom: ListDirection, whereTo: ListDirection, ): command_request.Command { @@ -923,8 +923,8 @@ export function createLMove( * @internal */ export function createBLMove( - source: string, - destination: string, + source: GlideString, + destination: GlideString, whereFrom: ListDirection, whereTo: ListDirection, timeout: number, @@ -1857,7 +1857,7 @@ export function createStrlen(key: string): command_request.Command { * @internal */ export function createLIndex( - key: string, + key: GlideString, index: number, ): command_request.Command { return createCommand(RequestType.LIndex, [key, index.toString()]); @@ -1881,10 +1881,10 @@ export enum InsertPosition { * @internal */ export function createLInsert( - key: string, + key: GlideString, position: InsertPosition, - pivot: string, - element: string, + pivot: GlideString, + element: GlideString, ): command_request.Command { return createCommand(RequestType.LInsert, [key, position, pivot, element]); } diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 19a8f09493..8230b483f2 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -2081,6 +2081,7 @@ export function runBaseTests(config: { const valueList = ["value4", "value3", "value2", "value1"]; expect(await client.lpush(key1, valueList)).toEqual(4); expect(await client.llen(key1)).toEqual(4); + expect(await client.llen(Buffer.from(key1))).toEqual(4); expect(await client.llen("nonExistingKey")).toEqual(0); @@ -2108,12 +2109,16 @@ export function runBaseTests(config: { const key1 = "{key}-1" + uuidv4(); const key2 = "{key}-2" + uuidv4(); + const key1Encoded = Buffer.from("{key}-1" + uuidv4()); + const key2Encoded = Buffer.from("{key}-2" + uuidv4()); const lpushArgs1 = ["2", "1"]; const lpushArgs2 = ["4", "3"]; // Initialize the tests expect(await client.lpush(key1, lpushArgs1)).toEqual(2); expect(await client.lpush(key2, lpushArgs2)).toEqual(2); + expect(await client.lpush(key1Encoded, lpushArgs1)).toEqual(2); + expect(await client.lpush(key2Encoded, lpushArgs2)).toEqual(2); // Move from LEFT to LEFT expect( @@ -2166,6 +2171,27 @@ export function runBaseTests(config: { expect(await client.lrange(key2, 0, -1)).toEqual(["1", "3"]); expect(await client.lrange(key1, 0, -1)).toEqual(["2", "4"]); + // Move from RIGHT to LEFT with encoded return value + expect( + await client.lmove( + key1, + key2, + ListDirection.RIGHT, + ListDirection.LEFT, + Decoder.Bytes, + ), + ).toEqual(Buffer.from("4")); + + // Move from RIGHT to LEFT with encoded list keys + expect( + await client.lmove( + key1Encoded, + key2Encoded, + ListDirection.RIGHT, + ListDirection.LEFT, + ), + ).toEqual("2"); + // Non-existing source key expect( await client.lmove( @@ -2212,12 +2238,16 @@ export function runBaseTests(config: { const key1 = "{key}-1" + uuidv4(); const key2 = "{key}-2" + uuidv4(); + const key1Encoded = Buffer.from("{key}-1" + uuidv4()); + const key2Encoded = Buffer.from("{key}-2" + uuidv4()); const lpushArgs1 = ["2", "1"]; const lpushArgs2 = ["4", "3"]; // Initialize the tests expect(await client.lpush(key1, lpushArgs1)).toEqual(2); expect(await client.lpush(key2, lpushArgs2)).toEqual(2); + expect(await client.lpush(key1Encoded, lpushArgs1)).toEqual(2); + expect(await client.lpush(key2Encoded, lpushArgs2)).toEqual(2); // Move from LEFT to LEFT with blocking expect( @@ -2281,6 +2311,29 @@ export function runBaseTests(config: { expect(await client.lrange(key2, 0, -1)).toEqual(["1", "3"]); expect(await client.lrange(key1, 0, -1)).toEqual(["2", "4"]); + // Move from RIGHT to LEFT with blocking and encoded return value + expect( + await client.blmove( + key1, + key2, + ListDirection.RIGHT, + ListDirection.LEFT, + 0.1, + Decoder.Bytes, + ), + ).toEqual(Buffer.from("4")); + + // Move from RIGHT to LEFT with encoded list keys + expect( + await client.blmove( + key1Encoded, + key2Encoded, + ListDirection.RIGHT, + ListDirection.LEFT, + 0.1, + ), + ).toEqual("2"); + // Non-existing source key with blocking expect( await client.blmove( @@ -5291,6 +5344,7 @@ export function runBaseTests(config: { async (protocol) => { await runTest(async (client: BaseClient) => { const listName = uuidv4(); + const encodedListName = Buffer.from(uuidv4()); const listKey1Value = uuidv4(); const listKey2Value = uuidv4(); expect( @@ -5299,10 +5353,25 @@ export function runBaseTests(config: { listKey2Value, ]), ).toEqual(2); + expect( + await client.lpush(encodedListName, [ + Buffer.from(listKey1Value), + Buffer.from(listKey2Value), + ]), + ).toEqual(2); expect(await client.lindex(listName, 0)).toEqual(listKey2Value); expect(await client.lindex(listName, 1)).toEqual(listKey1Value); expect(await client.lindex("notExsitingList", 1)).toEqual(null); expect(await client.lindex(listName, 3)).toEqual(null); + expect(await client.lindex(listName, 0, Decoder.Bytes)).toEqual( + Buffer.from(listKey2Value), + ); + expect(await client.lindex(listName, 1, Decoder.Bytes)).toEqual( + Buffer.from(listKey1Value), + ); + expect(await client.lindex(encodedListName, 0)).toEqual( + listKey2Value, + ); }, protocol); }, config.timeout, @@ -5313,6 +5382,8 @@ export function runBaseTests(config: { async (protocol) => { await runTest(async (client: BaseClient) => { const key1 = uuidv4(); + const key2 = uuidv4(); + const key2Encoded = Buffer.from(key2); const stringKey = uuidv4(); const nonExistingKey = uuidv4(); @@ -5361,6 +5432,35 @@ export function runBaseTests(config: { ), ).toEqual(0); + // key, pivot and element as buffers + expect(await client.lpush(key2, ["4", "3", "2", "1"])).toEqual( + 4, + ); + expect( + await client.linsert( + key2Encoded, + InsertPosition.Before, + Buffer.from("2"), + Buffer.from("1.5"), + ), + ).toEqual(5); + expect( + await client.linsert( + key2Encoded, + InsertPosition.After, + Buffer.from("3"), + Buffer.from("3.5"), + ), + ).toEqual(6); + expect(await client.lrange(key2Encoded, 0, -1)).toEqual([ + "1", + "1.5", + "2", + "3", + "3.5", + "4", + ]); + // key exists, but it is not a list expect(await client.set(stringKey, "value")).toEqual("OK"); await expect(