Skip to content

Commit

Permalink
feat(client): block messages that are too long in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
st-vi committed Dec 12, 2023
1 parent 2641d9f commit c74d4bd
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions WebSocketLanguageServer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { createConnection, createServerProcess, forward } from 'vscode-ws-jsonrp
import { Message, InitializeRequest, InitializeParams } from 'vscode-languageserver';
import config from './config.js';

const MAX_MESSAGE_SIZE = 11000;

const launchLanguageServer = (socket: IWebSocket) => {
const serverName: string = 'UVLS';
const ls = config.languageServerBinary;
Expand All @@ -22,20 +24,31 @@ const launchLanguageServer = (socket: IWebSocket) => {
const writer = new WebSocketMessageWriter(socket);
const socketConnection = createConnection(reader, writer, () => socket.dispose());
if (serverConnection) {

forward(socketConnection, serverConnection, message => {
if(JSON.stringify(message).length > MAX_MESSAGE_SIZE){
console.log(`blocked message larger than ${MAX_MESSAGE_SIZE}`);
message = {jsonrpc: "2.0"};
socketConnection.dispose();
return message;
}
if (Message.isRequest(message)) {
console.log(`${serverName} Server received:`);
console.log(message);
logObjectRecursively(message);
if (message.method === InitializeRequest.type.method) {
const initializeParams = message.params as InitializeParams;
initializeParams.processId = process.pid;
}
}
if(Message.isNotification(message)){
logObjectRecursively(message);
}
if (Message.isResponse(message)) {
console.log(`${serverName} Server sent:`);
logObjectRecursively(message);
logObjectRecursively(JSON.stringify(message));
}
return message;

});
}
};
Expand Down

0 comments on commit c74d4bd

Please sign in to comment.