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