addons/isl-server/src/index.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {AppMode} from 'isl/src/types';
b69ab319import type {Logger} from './logger';
b69ab3110import type {ServerPlatform} from './serverPlatform';
b69ab3111
b69ab3112import type {Deferred} from 'shared/utils';
b69ab3113import {FileLogger} from './FileLogger';
b69ab3114import {Internal} from './Internal';
b69ab3115import ServerToClientAPI from './ServerToClientAPI';
b69ab3116import {makeServerSideTracker} from './analytics/serverSideTracker';
b69ab3117import {StdoutLogger} from './logger';
b69ab3118import {browserServerPlatform} from './serverPlatform';
b69ab3119
b69ab3120export interface ClientConnection {
b69ab3121 /**
b69ab3122 * Used to send a message from the server to the client.
b69ab3123 *
b69ab3124 * Designed to match
b69ab3125 * https://code.visualstudio.com/api/references/vscode-api#Webview.postMessage
b69ab3126 */
b69ab3127 postMessage(message: string): Promise<boolean>;
b69ab3128
b69ab3129 /**
b69ab3130 * Designed to match
b69ab3131 * https://code.visualstudio.com/api/references/vscode-api#Webview.onDidReceiveMessage
b69ab3132 */
b69ab3133 onDidReceiveMessage(handler: (event: Buffer, isBinary: boolean) => void | Promise<void>): {
b69ab3134 dispose(): void;
b69ab3135 };
b69ab3136
b69ab3137 /**
b69ab3138 * Which command to use to run `sl`
b69ab3139 */
b69ab3140 command?: string;
b69ab3141 /**
b69ab3142 * Platform-specific version string.
b69ab3143 * For `sl web`, this is the `sl` version.
b69ab3144 * For the VS Code extension, this is the extension version.
b69ab3145 */
b69ab3146 version: string;
b69ab3147 logFileLocation?: string;
b69ab3148 logger?: Logger;
b69ab3149 cwd: string;
b69ab3150
b69ab3151 platform?: ServerPlatform;
b69ab3152 appMode: AppMode;
b69ab3153
4bb999b54 /** When true, block all write operations (commit, amend, rebase, etc.) */
4bb999b55 readOnly?: boolean;
4bb999b56
b69ab3157 /**
b69ab3158 * A deferred promise that resolves when the client signals it's ready
b69ab3159 */
b69ab3160 readySignal?: Deferred<void>;
b69ab3161}
b69ab3162
b69ab3163export function onClientConnection(connection: ClientConnection): () => void {
b69ab3164 const logger =
b69ab3165 connection.logger ??
b69ab3166 (connection.logFileLocation ? new FileLogger(connection.logFileLocation) : new StdoutLogger());
b69ab3167 connection.logger = logger;
b69ab3168 const platform = connection?.platform ?? browserServerPlatform;
b69ab3169 const version = connection?.version ?? 'unknown';
b69ab3170 logger.log(`establish client connection for ${connection.cwd}`);
b69ab3171 logger.log(`platform '${platform.platformName}', version '${version}'`);
b69ab3172 void Internal.logInternalInfo?.(logger);
b69ab3173
b69ab3174 const tracker = makeServerSideTracker(logger, platform, version);
b69ab3175 tracker.track('ClientConnection', {extras: {cwd: connection.cwd, appMode: connection.appMode}});
b69ab3176
b69ab3177 // start listening to messages
b69ab3178 let api: ServerToClientAPI | null = new ServerToClientAPI(platform, connection, tracker, logger);
b69ab3179 api.setActiveRepoForCwd(connection.cwd);
b69ab3180
b69ab3181 return () => {
b69ab3182 api?.dispose();
b69ab3183 api = null;
b69ab3184 };
b69ab3185}