File tree 2 files changed +15
-4
lines changed
packages/client/lib/client
2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ const RESP2_PUSH_TYPE_MAPPING = {
42
42
[ RESP_TYPES . SIMPLE_STRING ] : Buffer
43
43
} ;
44
44
45
+ export const pushHandlerError = 'Cannot override built in push message handler' ;
46
+
45
47
export default class RedisCommandsQueue {
46
48
readonly #respVersion;
47
49
readonly #maxLength;
@@ -79,7 +81,7 @@ export default class RedisCommandsQueue {
79
81
80
82
const s = new Set < string > ( ) ;
81
83
this . #builtInSet = s ;
82
- for ( const str in this . #pushHandlers. keys ) {
84
+ for ( const str of this . #pushHandlers. keys ( ) ) {
83
85
s . add ( str ) ;
84
86
}
85
87
@@ -118,15 +120,15 @@ export default class RedisCommandsQueue {
118
120
119
121
addPushHandler ( messageType : string , handler : ( pushMsg : Array < any > ) => unknown ) {
120
122
if ( this . #builtInSet. has ( messageType ) ) {
121
- throw new Error ( "Cannot override built in push message handler" ) ;
123
+ throw new Error ( pushHandlerError ) ;
122
124
}
123
125
124
126
this . #pushHandlers. set ( messageType , handler ) ;
125
127
}
126
128
127
129
removePushHandler ( messageType : string ) {
128
130
if ( this . #builtInSet. has ( messageType ) ) {
129
- throw new Error ( "Cannot override built in push message handler" ) ;
131
+ throw new Error ( pushHandlerError ) ;
130
132
}
131
133
132
134
this . #pushHandlers. delete ( messageType ) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ import { RESP_TYPES } from '../RESP/decoder';
10
10
import { BlobStringReply , NumberReply } from '../RESP/types' ;
11
11
import { SortedSetMember } from '../commands/generic-transformers' ;
12
12
import { createClient } from '../..' ;
13
+ import { COMMANDS , PUBSUB_TYPE } from './pub-sub' ;
14
+ import { pushHandlerError } from './commands-queue' ;
13
15
14
16
export const SQUARE_SCRIPT = defineScript ( {
15
17
SCRIPT :
@@ -771,7 +773,14 @@ describe('Client', () => {
771
773
} , GLOBAL . SERVERS . OPEN ) ;
772
774
} ) ;
773
775
774
- describe ( 'Push Handlers' , ( ) => {
776
+ describe . only ( 'Push Handlers' , ( ) => {
777
+ testUtils . testWithClient ( 'prevent overriding a built in handler' , async client => {
778
+ assert . throws ( ( ) => { client . addPushHandler ( COMMANDS [ PUBSUB_TYPE . CHANNELS ] . message . toString ( ) , ( push : Array < any > ) => { } ) } , new Error ( pushHandlerError ) ) ;
779
+ assert . throws ( ( ) => { client . removePushHandler ( COMMANDS [ PUBSUB_TYPE . CHANNELS ] . message . toString ( ) ) } , new Error ( pushHandlerError ) ) ;
780
+ } , {
781
+ ...GLOBAL . SERVERS . OPEN
782
+ } ) ;
783
+
775
784
testUtils . testWithClient ( 'RESP2: add/remove invalidate handler, and validate its called' , async client => {
776
785
const key = 'x'
777
786
You can’t perform that action at this time.
0 commit comments