Skip to content

Commit eb10194

Browse files
committed
add test (/ fix code) to prevent overriding built in push handlers
1 parent 3340d49 commit eb10194

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

packages/client/lib/client/commands-queue.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const RESP2_PUSH_TYPE_MAPPING = {
4242
[RESP_TYPES.SIMPLE_STRING]: Buffer
4343
};
4444

45+
export const pushHandlerError = 'Cannot override built in push message handler';
46+
4547
export default class RedisCommandsQueue {
4648
readonly #respVersion;
4749
readonly #maxLength;
@@ -79,7 +81,7 @@ export default class RedisCommandsQueue {
7981

8082
const s = new Set<string>();
8183
this.#builtInSet = s;
82-
for (const str in this.#pushHandlers.keys) {
84+
for (const str of this.#pushHandlers.keys()) {
8385
s.add(str);
8486
}
8587

@@ -118,15 +120,15 @@ export default class RedisCommandsQueue {
118120

119121
addPushHandler(messageType: string, handler: (pushMsg: Array<any>) => unknown) {
120122
if (this.#builtInSet.has(messageType)) {
121-
throw new Error("Cannot override built in push message handler");
123+
throw new Error(pushHandlerError);
122124
}
123125

124126
this.#pushHandlers.set(messageType, handler);
125127
}
126128

127129
removePushHandler(messageType: string) {
128130
if (this.#builtInSet.has(messageType)) {
129-
throw new Error("Cannot override built in push message handler");
131+
throw new Error(pushHandlerError);
130132
}
131133

132134
this.#pushHandlers.delete(messageType);

packages/client/lib/client/index.spec.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { RESP_TYPES } from '../RESP/decoder';
1010
import { BlobStringReply, NumberReply } from '../RESP/types';
1111
import { SortedSetMember } from '../commands/generic-transformers';
1212
import { createClient } from '../..';
13+
import { COMMANDS, PUBSUB_TYPE } from './pub-sub';
14+
import { pushHandlerError } from './commands-queue';
1315

1416
export const SQUARE_SCRIPT = defineScript({
1517
SCRIPT:
@@ -771,7 +773,14 @@ describe('Client', () => {
771773
}, GLOBAL.SERVERS.OPEN);
772774
});
773775

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+
775784
testUtils.testWithClient('RESP2: add/remove invalidate handler, and validate its called', async client => {
776785
const key = 'x'
777786

0 commit comments

Comments
 (0)