diff --git a/packages/client/lib/cluster/commands.ts b/packages/client/lib/cluster/commands.ts index 4b2aba6a7ae..b8893d9710e 100644 --- a/packages/client/lib/cluster/commands.ts +++ b/packages/client/lib/cluster/commands.ts @@ -1,6 +1,7 @@ import * as APPEND from '../commands/APPEND'; import * as BITCOUNT from '../commands/BITCOUNT'; +import * as BITFIELD_RO from '../commands/BITFIELD_RO'; import * as BITFIELD from '../commands/BITFIELD'; import * as BITOP from '../commands/BITOP'; import * as BITPOS from '../commands/BITPOS'; @@ -181,6 +182,8 @@ export default { append: APPEND, BITCOUNT, bitCount: BITCOUNT, + BITFIELD_RO, + bitFieldRo: BITFIELD_RO, BITFIELD, bitField: BITFIELD, BITOP, diff --git a/packages/client/lib/commands/BITFIELD.spec.ts b/packages/client/lib/commands/BITFIELD.spec.ts index 93a5cb08a63..aaf0f93e501 100644 --- a/packages/client/lib/commands/BITFIELD.spec.ts +++ b/packages/client/lib/commands/BITFIELD.spec.ts @@ -10,14 +10,14 @@ describe('BITFIELD', () => { behavior: 'WRAP' }, { operation: 'GET', - type: 'i8', + encoding: 'i8', offset: 0 }, { operation: 'OVERFLOW', behavior: 'SAT' }, { operation: 'SET', - type: 'i16', + encoding: 'i16', offset: 1, value: 0 }, { @@ -25,7 +25,7 @@ describe('BITFIELD', () => { behavior: 'FAIL' }, { operation: 'INCRBY', - type: 'i32', + encoding: 'i32', offset: 2, increment: 1 }]), @@ -35,8 +35,12 @@ describe('BITFIELD', () => { testUtils.testWithClient('client.bitField', async client => { assert.deepEqual( - await client.bitField('key', []), - [] + await client.bitField('key', [{ + operation: 'GET', + encoding: 'i8', + offset: 0 + }]), + [0] ); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/client/lib/commands/BITFIELD.ts b/packages/client/lib/commands/BITFIELD.ts index 5be12cc391e..6a477b89f01 100644 --- a/packages/client/lib/commands/BITFIELD.ts +++ b/packages/client/lib/commands/BITFIELD.ts @@ -1,26 +1,24 @@ export const FIRST_KEY_INDEX = 1; -export const IS_READ_ONLY = true; +export type BitFieldEncoding = `${'i' | 'u'}${number}`; -type BitFieldType = string; // TODO 'i[1-64]' | 'u[1-63]' - -interface BitFieldOperation { +export interface BitFieldOperation { operation: S; } -interface BitFieldGetOperation extends BitFieldOperation<'GET'> { - type: BitFieldType; +export interface BitFieldGetOperation extends BitFieldOperation<'GET'> { + encoding: BitFieldEncoding; offset: number | string; } interface BitFieldSetOperation extends BitFieldOperation<'SET'> { - type: BitFieldType; + encoding: BitFieldEncoding; offset: number | string; value: number; } interface BitFieldIncrByOperation extends BitFieldOperation<'INCRBY'> { - type: BitFieldType; + encoding: BitFieldEncoding; offset: number | string; increment: number; } @@ -44,7 +42,7 @@ export function transformArguments(key: string, operations: BitFieldOperations): case 'GET': args.push( 'GET', - options.type, + options.encoding, options.offset.toString() ); break; @@ -52,7 +50,7 @@ export function transformArguments(key: string, operations: BitFieldOperations): case 'SET': args.push( 'SET', - options.type, + options.encoding, options.offset.toString(), options.value.toString() ); @@ -61,7 +59,7 @@ export function transformArguments(key: string, operations: BitFieldOperations): case 'INCRBY': args.push( 'INCRBY', - options.type, + options.encoding, options.offset.toString(), options.increment.toString() ); diff --git a/packages/client/lib/commands/BITFIELD_RO.spec.ts b/packages/client/lib/commands/BITFIELD_RO.spec.ts new file mode 100644 index 00000000000..98399d5f235 --- /dev/null +++ b/packages/client/lib/commands/BITFIELD_RO.spec.ts @@ -0,0 +1,27 @@ +import { strict as assert } from 'assert'; +import testUtils, { GLOBAL } from '../test-utils'; +import { transformArguments } from './BITFIELD_RO'; + +describe('BITFIELD RO', () => { + testUtils.isVersionGreaterThanHook([6, 2]); + + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key', [{ + encoding: 'i8', + offset: 0 + }]), + ['BITFIELD_RO', 'key', 'GET', 'i8', '0'] + ); + }); + + testUtils.testWithClient('client.bitFieldRo', async client => { + assert.deepEqual( + await client.bitFieldRo('key', [{ + encoding: 'i8', + offset: 0 + }]), + [0] + ); + }, GLOBAL.SERVERS.OPEN); +}); diff --git a/packages/client/lib/commands/BITFIELD_RO.ts b/packages/client/lib/commands/BITFIELD_RO.ts new file mode 100644 index 00000000000..efd4eac188e --- /dev/null +++ b/packages/client/lib/commands/BITFIELD_RO.ts @@ -0,0 +1,26 @@ +import { BitFieldGetOperation } from './BITFIELD'; + +export const FIRST_KEY_INDEX = 1; + +export const IS_READ_ONLY = true; + +type BitFieldRoOperations = Array< + Omit & + Partial> +>; + +export function transformArguments(key: string, operations: BitFieldRoOperations): Array { + const args = ['BITFIELD_RO', key]; + + for (const operation of operations) { + args.push( + 'GET', + operation.encoding, + operation.offset.toString() + ); + } + + return args; +} + +export declare function transformReply(): Array;