2.7 KB86 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {AppMode} from 'isl/src/types';
9import type {Logger} from './logger';
10import type {ServerPlatform} from './serverPlatform';
11
12import type {Deferred} from 'shared/utils';
13import {FileLogger} from './FileLogger';
14import {Internal} from './Internal';
15import ServerToClientAPI from './ServerToClientAPI';
16import {makeServerSideTracker} from './analytics/serverSideTracker';
17import {StdoutLogger} from './logger';
18import {browserServerPlatform} from './serverPlatform';
19
20export interface ClientConnection {
21 /**
22 * Used to send a message from the server to the client.
23 *
24 * Designed to match
25 * https://code.visualstudio.com/api/references/vscode-api#Webview.postMessage
26 */
27 postMessage(message: string): Promise<boolean>;
28
29 /**
30 * Designed to match
31 * https://code.visualstudio.com/api/references/vscode-api#Webview.onDidReceiveMessage
32 */
33 onDidReceiveMessage(handler: (event: Buffer, isBinary: boolean) => void | Promise<void>): {
34 dispose(): void;
35 };
36
37 /**
38 * Which command to use to run `sl`
39 */
40 command?: string;
41 /**
42 * Platform-specific version string.
43 * For `sl web`, this is the `sl` version.
44 * For the VS Code extension, this is the extension version.
45 */
46 version: string;
47 logFileLocation?: string;
48 logger?: Logger;
49 cwd: string;
50
51 platform?: ServerPlatform;
52 appMode: AppMode;
53
54 /** When true, block all write operations (commit, amend, rebase, etc.) */
55 readOnly?: boolean;
56
57 /**
58 * A deferred promise that resolves when the client signals it's ready
59 */
60 readySignal?: Deferred<void>;
61}
62
63export function onClientConnection(connection: ClientConnection): () => void {
64 const logger =
65 connection.logger ??
66 (connection.logFileLocation ? new FileLogger(connection.logFileLocation) : new StdoutLogger());
67 connection.logger = logger;
68 const platform = connection?.platform ?? browserServerPlatform;
69 const version = connection?.version ?? 'unknown';
70 logger.log(`establish client connection for ${connection.cwd}`);
71 logger.log(`platform '${platform.platformName}', version '${version}'`);
72 void Internal.logInternalInfo?.(logger);
73
74 const tracker = makeServerSideTracker(logger, platform, version);
75 tracker.track('ClientConnection', {extras: {cwd: connection.cwd, appMode: connection.appMode}});
76
77 // start listening to messages
78 let api: ServerToClientAPI | null = new ServerToClientAPI(platform, connection, tracker, logger);
79 api.setActiveRepoForCwd(connection.cwd);
80
81 return () => {
82 api?.dispose();
83 api = null;
84 };
85}
86