Skip to content

Commit 71c41a2

Browse files
committed
fix(api-gateway): Allow messageId to be int in unsubscribe WS request (#10250)
1 parent 30a72fa commit 71c41a2

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

packages/cubejs-api-gateway/src/ws/local-subscription-store.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ interface LocalSubscriptionStoreOptions {
22
heartBeatInterval?: number;
33
}
44

5+
export type SubscriptionId = string | number;
6+
57
export type LocalSubscriptionStoreSubscription = {
68
message: any,
79
state: any,
810
timestamp: Date,
911
};
1012

1113
export type LocalSubscriptionStoreConnection = {
12-
subscriptions: Map<string, LocalSubscriptionStoreSubscription>,
14+
subscriptions: Map<SubscriptionId, LocalSubscriptionStoreSubscription>,
1315
authContext?: any,
1416
};
1517

@@ -35,7 +37,7 @@ export class LocalSubscriptionStore {
3537
});
3638
}
3739

38-
public async unsubscribe(connectionId: string, subscriptionId: string) {
40+
public async unsubscribe(connectionId: string, subscriptionId: SubscriptionId) {
3941
const connection = this.getConnectionOrCreate(connectionId);
4042
connection.subscriptions.delete(subscriptionId);
4143
}

packages/cubejs-api-gateway/src/ws/message-schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { z } from 'zod';
22

3-
const messageId = z.union([z.string().max(16), z.number()]);
3+
const messageId = z.union([z.string().max(16), z.int()]);
44
const requestId = z.string().max(64).optional();
55

66
export const authMessageSchema = z.object({
77
authorization: z.string(),
88
}).strict();
99

1010
export const unsubscribeMessageSchema = z.object({
11-
unsubscribe: z.string().max(16),
11+
unsubscribe: messageId,
1212
}).strict();
1313

1414
const queryParams = z.object({

packages/cubejs-api-gateway/test/ws/subscription-server.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ describe('SubscriptionServer', () => {
5959
expect(mockSubscriptionStore.unsubscribe).toHaveBeenCalledWith('conn-1', 'msg-1');
6060
});
6161

62+
it('should accept unsubscribe with numeric messageId', async () => {
63+
const { mockApiGateway, mockSubscriptionStore, mockSendMessage, mockContextAcceptor } = createMocks();
64+
const server = new SubscriptionServer(mockApiGateway, mockSendMessage, mockSubscriptionStore, mockContextAcceptor);
65+
66+
await server.processMessage('conn-1', JSON.stringify({ unsubscribe: 123 }));
67+
68+
expect(mockSubscriptionStore.unsubscribe).toHaveBeenCalledWith('conn-1', 123);
69+
});
70+
6271
it('should accept valid load message', async () => {
6372
const { mockApiGateway, mockSubscriptionStore, mockSendMessage, mockContextAcceptor, sentMessages } = createMocks();
6473
const server = new SubscriptionServer(mockApiGateway, mockSendMessage, mockSubscriptionStore, mockContextAcceptor);

0 commit comments

Comments
 (0)