From 5a1f62a8fbf041305e8eadd9a248f7479e5ff0ef Mon Sep 17 00:00:00 2001 From: Asher Date: Wed, 10 Mar 2021 13:14:24 -0600 Subject: [PATCH] Support permessage-deflate web socket extension (#2846) --- lib/vscode/src/vs/server/node/server.ts | 5 +++-- src/node/routes/vscode.ts | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/vscode/src/vs/server/node/server.ts b/lib/vscode/src/vs/server/node/server.ts index 244c42495..52addd1e1 100644 --- a/lib/vscode/src/vs/server/node/server.ts +++ b/lib/vscode/src/vs/server/node/server.ts @@ -120,7 +120,7 @@ export class Vscode { }; } - public async handleWebSocket(socket: net.Socket, query: Query, _permessageDeflate: boolean): Promise { + public async handleWebSocket(socket: net.Socket, query: Query, permessageDeflate: boolean): Promise { if (!query.reconnectionToken) { throw new Error('Reconnection token is missing from query parameters'); } @@ -128,7 +128,8 @@ export class Vscode { reconnectionToken: query.reconnectionToken, reconnection: query.reconnection === 'true', skipWebSocketFrames: query.skipWebSocketFrames === 'true', - // TODO: permessageDeflate, + permessageDeflate, + recordInflateBytes: permessageDeflate, }); try { await this.connect(await protocol.handshake(), protocol); diff --git a/src/node/routes/vscode.ts b/src/node/routes/vscode.ts index 4f1d14e9a..f8cbbd302 100644 --- a/src/node/routes/vscode.ts +++ b/src/node/routes/vscode.ts @@ -199,16 +199,20 @@ wsRouter.ws("/", ensureAuthenticated, async (req) => { .createHash("sha1") .update(req.headers["sec-websocket-key"] + magic) .digest("base64") - req.ws.write( - [ - "HTTP/1.1 101 Switching Protocols", - "Upgrade: websocket", - "Connection: Upgrade", - `Sec-WebSocket-Accept: ${reply}`, - ].join("\r\n") + "\r\n\r\n", - ) - // TODO: Parse this header properly. Currently unused so haven't bothered. + + const responseHeaders = [ + "HTTP/1.1 101 Switching Protocols", + "Upgrade: websocket", + "Connection: Upgrade", + `Sec-WebSocket-Accept: ${reply}`, + ] + + // TODO: Parse this header properly. const extensions = req.headers["sec-websocket-extensions"] const permessageDeflate = extensions ? extensions.includes("permessage-deflate") : false + responseHeaders.push("Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=15") + + req.ws.write(responseHeaders.join("\r\n") + "\r\n\r\n") + await vscode.sendWebsocket(req.ws, req.query, permessageDeflate) })