From 083400b50a2983c05f81a41f38afd2c61350ed08 Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 4 May 2021 16:46:08 -0500 Subject: [PATCH] Add flag to enable permessage-deflate --- src/node/cli.ts | 9 +++++++++ src/node/entry.ts | 16 ++++++++++++++++ src/node/routes/vscode.ts | 14 +++++++++++--- test/unit/cli.test.ts | 5 +++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index 461a2e18..8278dfed 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -6,6 +6,11 @@ import * as path from "path" import { Args as VsArgs } from "../../typings/ipc" import { canConnect, generateCertificate, generatePassword, humanPath, paths } from "./util" +export enum Feature { + /** Web socket compression. */ + PermessageDeflate = "permessage-deflate", +} + export enum AuthType { Password = "password", None = "none", @@ -35,6 +40,7 @@ export interface Args extends VsArgs { "cert-key"?: string "disable-telemetry"?: boolean "disable-update-check"?: boolean + enable?: string[] help?: boolean host?: string json?: boolean @@ -128,6 +134,9 @@ const options: Options> = { "Disable update check. Without this flag, code-server checks every 6 hours against the latest github release and \n" + "then notifies you once every week that a new release is available.", }, + // --enable can be used to enable experimental features. These features + // provide no guarantees. + enable: { type: "string[]" }, help: { type: "boolean", short: "h", description: "Show this output." }, json: { type: "boolean" }, open: { type: "boolean", description: "Open in browser on startup. Does not work remotely." }, diff --git a/src/node/entry.ts b/src/node/entry.ts index b7b61b8f..12d74da3 100644 --- a/src/node/entry.ts +++ b/src/node/entry.ts @@ -8,6 +8,7 @@ import { createApp, ensureAddress } from "./app" import { AuthType, DefaultedArgs, + Feature, optionDescriptions, parse, readConfigFile, @@ -146,6 +147,21 @@ const main = async (args: DefaultedArgs): Promise => { } } + if (args.enable && args.enable.length > 0) { + logger.info("Enabling the following experimental features:") + args.enable.forEach((feature) => { + if (Object.values(Feature).includes(feature as Feature)) { + logger.info(` - "${feature}"`) + } else { + logger.error(` X "${feature}" (unknown feature)`) + } + }) + // TODO: Could be nice to add wrapping to the logger? + logger.info( + " The code-server project does not provide stability guarantees or commit to fixing bugs relating to these experimental features. When filing bug reports, please ensure that you can reproduce the bug with all experimental features turned off.", + ) + } + if (!args.socket && args.open) { // The web socket doesn't seem to work if browsing with 0.0.0.0. const openAddress = serverAddress.replace("://0.0.0.0", "://localhost") diff --git a/src/node/routes/vscode.ts b/src/node/routes/vscode.ts index 287216ec..40c63e92 100644 --- a/src/node/routes/vscode.ts +++ b/src/node/routes/vscode.ts @@ -7,6 +7,7 @@ import * as ipc from "../../../typings/ipc" import { Emitter } from "../../common/emitter" import { HttpCode, HttpError } from "../../common/http" import { getFirstString } from "../../common/util" +import { Feature } from "../cli" import { isDevMode, rootPath, version } from "../constants" import { authenticated, ensureAuthenticated, redirect, replaceTemplates } from "../http" import { getMediaMime, pathToFsPath } from "../util" @@ -209,14 +210,21 @@ wsRouter.ws("/", ensureAuthenticated, async (req) => { `Sec-WebSocket-Accept: ${reply}`, ] + // See if the browser reports it supports web socket compression. // TODO: Parse this header properly. const extensions = req.headers["sec-websocket-extensions"] - const permessageDeflate = extensions ? extensions.includes("permessage-deflate") : false - if (permessageDeflate) { + const isCompressionSupported = extensions ? extensions.includes("permessage-deflate") : false + + // TODO: For now we only use compression if the user enables it. + const isCompressionEnabled = !!req.args.enable?.includes(Feature.PermessageDeflate) + + const useCompression = isCompressionEnabled && isCompressionSupported + if (useCompression) { + // This response header tells the browser the server supports compression. 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) + await vscode.sendWebsocket(req.ws, req.query, useCompression) }) diff --git a/test/unit/cli.test.ts b/test/unit/cli.test.ts index 38d8dc7b..6d4fcafb 100644 --- a/test/unit/cli.test.ts +++ b/test/unit/cli.test.ts @@ -39,6 +39,10 @@ describe("parser", () => { it("should parse all available options", () => { expect( parse([ + "--enable", + "feature1", + "--enable", + "feature2", "--bind-addr=192.169.0.1:8080", "--auth", "none", @@ -82,6 +86,7 @@ describe("parser", () => { cert: { value: path.resolve("baz"), }, + enable: ["feature1", "feature2"], "extensions-dir": path.resolve("foo"), "extra-builtin-extensions-dir": [path.resolve("bazzle")], "extra-extensions-dir": [path.resolve("nozzle")],