chore: sanitise channel for client

This commit is contained in:
Jack Waller
2024-05-23 13:10:20 +10:00
parent 304e1e9bd0
commit a4312acf03

View File

@@ -35,9 +35,12 @@ console.log(`WS LISTENER PORT : ${WS_PORT}`);
const MAX_BUFFERED_AMOUNT = 300000; const MAX_BUFFERED_AMOUNT = 300000;
const CHANNEL_PREFIX = RedisClientPrefix.DLOB; const CHANNEL_PREFIX = RedisClientPrefix.DLOB;
const CHANNEL_PREFIX_HELIUS = RedisClientPrefix.DLOB_HELIUS;
const safeGetRawChannelFromMessage = (message: any): string => { const sanitiseChannelForClient = (channel: string): string => {
return message?.channel; return channel
.replace(CHANNEL_PREFIX, '')
.replace(CHANNEL_PREFIX_HELIUS, '');
}; };
const getRedisChannelFromMessage = (message: any): string => { const getRedisChannelFromMessage = (message: any): string => {
@@ -69,7 +72,7 @@ const getRedisChannelFromMessage = (message: any): string => {
case 'orderbook': case 'orderbook':
return `${CHANNEL_PREFIX}orderbook_${marketType}_${marketIndex}`; return `${CHANNEL_PREFIX}orderbook_${marketType}_${marketIndex}`;
case 'priorityfees': case 'priorityfees':
return `${CHANNEL_PREFIX}priorityFees_${marketType}_${marketIndex}`; return `${CHANNEL_PREFIX_HELIUS}priorityFees_${marketType}_${marketIndex}`;
case undefined: case undefined:
default: default:
throw new Error('Bad channel specified'); throw new Error('Bad channel specified');
@@ -105,7 +108,10 @@ async function main() {
ws.bufferedAmount < MAX_BUFFERED_AMOUNT ws.bufferedAmount < MAX_BUFFERED_AMOUNT
) )
ws.send( ws.send(
JSON.stringify({ channel: subscribedChannel, data: message }) JSON.stringify({
channel: sanitiseChannelForClient(subscribedChannel),
data: message,
})
); );
}); });
} }
@@ -135,7 +141,7 @@ async function main() {
try { try {
redisChannel = getRedisChannelFromMessage(parsedMessage); redisChannel = getRedisChannelFromMessage(parsedMessage);
} catch (error) { } catch (error) {
const requestChannel = safeGetRawChannelFromMessage(parsedMessage); const requestChannel = sanitiseChannelForClient(parsedMessage?.channel);
if (requestChannel) { if (requestChannel) {
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
@@ -208,7 +214,7 @@ async function main() {
try { try {
redisChannel = getRedisChannelFromMessage(parsedMessage); redisChannel = getRedisChannelFromMessage(parsedMessage);
} catch (error) { } catch (error) {
const requestChannel = safeGetRawChannelFromMessage(parsedMessage); const requestChannel = sanitiseChannelForClient(parsedMessage?.channel);
if (requestChannel) { if (requestChannel) {
console.log('Error unsubscribing from channel:', error.message); console.log('Error unsubscribing from channel:', error.message);
ws.send( ws.send(