@@ -7,14 +7,15 @@ import { ClientClosedError, ClientOfflineError, DisconnectsClientError, WatchErr
7
7
import { URL } from 'node:url' ;
8
8
import { TcpSocketConnectOpts } from 'node:net' ;
9
9
import { PUBSUB_TYPE , PubSubType , PubSubListener , PubSubTypeListeners , ChannelListeners } from './pub-sub' ;
10
- import { Command , CommandSignature , TypeMapping , CommanderConfig , RedisFunction , RedisFunctions , RedisModules , RedisScript , RedisScripts , ReplyUnion , RespVersions , RedisArgument , ReplyWithTypeMapping , SimpleStringReply } from '../RESP/types' ;
10
+ import { Command , CommandSignature , TypeMapping , CommanderConfig , RedisFunction , RedisFunctions , RedisModules , RedisScript , RedisScripts , ReplyUnion , RespVersions , RedisArgument , ReplyWithTypeMapping , SimpleStringReply , TransformReply } from '../RESP/types' ;
11
11
import RedisClientMultiCommand , { RedisClientMultiCommandType } from './multi-command' ;
12
12
import { RedisMultiQueuedCommand } from '../multi-command' ;
13
13
import HELLO , { HelloOptions } from '../commands/HELLO' ;
14
14
import { ScanOptions , ScanCommonOptions } from '../commands/SCAN' ;
15
15
import { RedisLegacyClient , RedisLegacyClientType } from './legacy-mode' ;
16
16
import { RedisPoolOptions , RedisClientPool } from './pool' ;
17
17
import { RedisVariadicArgument , pushVariadicArguments } from '../commands/generic-transformers' ;
18
+ import { BasicCommandParser , CommandParser } from './parser' ;
18
19
19
20
export interface RedisClientOptions <
20
21
M extends RedisModules = RedisModules ,
@@ -151,52 +152,84 @@ export default class RedisClient<
151
152
> extends EventEmitter {
152
153
static #createCommand( command : Command , resp : RespVersions ) {
153
154
const transformReply = getTransformReply ( command , resp ) ;
155
+
154
156
return async function ( this : ProxyClient , ...args : Array < unknown > ) {
155
- const redisArgs = command . transformArguments ( ...args ) ,
156
- reply = await this . sendCommand ( redisArgs , this . _commandOptions ) ;
157
- return transformReply ?
158
- transformReply ( reply , redisArgs . preserve ) :
159
- reply ;
160
- } ;
157
+ if ( command . parseCommand ) {
158
+ const parser = this . _self . #newCommandParser( resp ) ;
159
+ command . parseCommand ( parser , ...args ) ;
160
+
161
+ return this . executeCommand ( undefined , parser , this . _commandOptions , transformReply ) ;
162
+ } else {
163
+ const redisArgs = command . transformArguments ( ...args ) ,
164
+ reply = await this . sendCommand ( redisArgs , this . _commandOptions ) ;
165
+ return transformReply ?
166
+ transformReply ( reply , redisArgs . preserve ) :
167
+ reply ;
168
+ } ;
169
+ }
161
170
}
162
171
163
172
static #createModuleCommand( command : Command , resp : RespVersions ) {
164
173
const transformReply = getTransformReply ( command , resp ) ;
174
+
165
175
return async function ( this : NamespaceProxyClient , ...args : Array < unknown > ) {
166
- const redisArgs = command . transformArguments ( ...args ) ,
167
- reply = await this . _self . sendCommand ( redisArgs , this . _self . _commandOptions ) ;
168
- return transformReply ?
169
- transformReply ( reply , redisArgs . preserve ) :
170
- reply ;
176
+ if ( command . parseCommand ) {
177
+ const parser = this . _self . _self . #newCommandParser( resp ) ;
178
+ command . parseCommand ( parser , ...args ) ;
179
+
180
+ return this . _self . executeCommand ( undefined , parser , this . _self . _commandOptions , transformReply ) ;
181
+ } else {
182
+ const redisArgs = command . transformArguments ( ...args ) ,
183
+ reply = await this . _self . sendCommand ( redisArgs , this . _self . _commandOptions ) ;
184
+ return transformReply ?
185
+ transformReply ( reply , redisArgs . preserve ) :
186
+ reply ;
187
+ }
171
188
} ;
172
189
}
173
190
174
191
static #createFunctionCommand( name : string , fn : RedisFunction , resp : RespVersions ) {
175
- const prefix = functionArgumentsPrefix ( name , fn ) ,
176
- transformReply = getTransformReply ( fn , resp ) ;
192
+ const prefix = functionArgumentsPrefix ( name , fn ) ;
193
+ const transformReply = getTransformReply ( fn , resp ) ;
194
+
177
195
return async function ( this : NamespaceProxyClient , ...args : Array < unknown > ) {
178
- const fnArgs = fn . transformArguments ( ...args ) ,
179
- reply = await this . _self . sendCommand (
180
- prefix . concat ( fnArgs ) ,
181
- this . _self . _commandOptions
182
- ) ;
183
- return transformReply ?
184
- transformReply ( reply , fnArgs . preserve ) :
185
- reply ;
196
+ if ( fn . parseCommand ) {
197
+ const parser = this . _self . _self . #newCommandParser( resp ) ;
198
+ fn . parseCommand ( parser , ...args ) ;
199
+
200
+ return this . _self . executeCommand ( prefix , parser , this . _self . _commandOptions , transformReply ) ;
201
+ } else {
202
+ const fnArgs = fn . transformArguments ( ...args ) ,
203
+ reply = await this . _self . sendCommand (
204
+ prefix . concat ( fnArgs ) ,
205
+ this . _self . _commandOptions
206
+ ) ;
207
+ return transformReply ?
208
+ transformReply ( reply , fnArgs . preserve ) :
209
+ reply ;
210
+ }
186
211
} ;
187
212
}
188
213
189
214
static #createScriptCommand( script : RedisScript , resp : RespVersions ) {
190
- const prefix = scriptArgumentsPrefix ( script ) ,
191
- transformReply = getTransformReply ( script , resp ) ;
215
+ const prefix = scriptArgumentsPrefix ( script ) ;
216
+ const transformReply = getTransformReply ( script , resp ) ;
217
+
192
218
return async function ( this : ProxyClient , ...args : Array < unknown > ) {
193
- const scriptArgs = script . transformArguments ( ...args ) ,
194
- redisArgs = prefix . concat ( scriptArgs ) ,
195
- reply = await this . executeScript ( script , redisArgs , this . _commandOptions ) ;
196
- return transformReply ?
197
- transformReply ( reply , scriptArgs . preserve ) :
198
- reply ;
199
- } ;
219
+ if ( script . parseCommand ) {
220
+ const parser = this . _self . #newCommandParser( resp ) ;
221
+ script . parseCommand ( parser , ...args ) ;
222
+
223
+ return this . executeCommand ( prefix , parser , this . _commandOptions , transformReply ) ;
224
+ } else {
225
+ const scriptArgs = script . transformArguments ( ...args ) ,
226
+ redisArgs = prefix . concat ( scriptArgs ) ,
227
+ reply = await this . executeScript ( script , redisArgs , this . _commandOptions ) ;
228
+ return transformReply ?
229
+ transformReply ( reply , scriptArgs . preserve ) :
230
+ reply ;
231
+ } ;
232
+ }
200
233
}
201
234
202
235
static factory <
@@ -309,6 +342,10 @@ export default class RedisClient<
309
342
this . _self . #dirtyWatch = msg ;
310
343
}
311
344
345
+ #newCommandParser( resp : RespVersions ) : CommandParser {
346
+ return new BasicCommandParser ( resp ) ;
347
+ }
348
+
312
349
constructor ( options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ) {
313
350
super ( ) ;
314
351
this . #options = this . #initiateOptions( options ) ;
@@ -573,6 +610,24 @@ export default class RedisClient<
573
610
return this as unknown as RedisClientType < M , F , S , RESP , TYPE_MAPPING > ;
574
611
}
575
612
613
+ async executeCommand < T = ReplyUnion > (
614
+ prefix : Array < string | Buffer > | undefined ,
615
+ parser : CommandParser ,
616
+ commandOptions : CommandOptions < TYPE_MAPPING > | undefined ,
617
+ transformReply : TransformReply | undefined
618
+ ) {
619
+ const redisArgs = prefix ? prefix . concat ( parser . redisArgs ) : parser . redisArgs ;
620
+ const fn = ( ) => { return this . sendCommand ( redisArgs , commandOptions ) } ;
621
+
622
+ const reply = await fn ( ) ;
623
+
624
+ if ( transformReply ) {
625
+ return transformReply ( reply , parser . preserve ) ;
626
+ }
627
+
628
+ return reply ;
629
+ }
630
+
576
631
sendCommand < T = ReplyUnion > (
577
632
args : Array < RedisArgument > ,
578
633
options ?: CommandOptions
0 commit comments