src/vs/platform/agentHost/node/agentHostUpgradeChannel.ts
89 LOC · 52 covered · 37 uncovered · 3 ranges · 171 concepts · 2 introducers · 77 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
protocolServerHandler.ts ×78
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/**
* Environment variable populated by the VS Code CLI when it spawns the
* agent host server. Its value is the path of a unix-domain socket
* (POSIX) or named pipe (Windows) on which the CLI is serving its HTTP
* management API. Presence of the variable also serves as the "I was
* spawned by a managing CLI" marker that decides whether the server
* advertises an in-band upgrade method to clients.
*/
export const VSCODE_AGENT_HOST_MANAGEMENT_SOCKET_ENV = 'VSCODE_AGENT_HOST_MANAGEMENT_SOCKET';
/**
* Status payload returned by the CLI's `POST /upgrade` endpoint. Sent
* back verbatim to the agent host client so the UI can surface it.
*/
export interface IUpgradeRequestResponse {
readonly ok: boolean;
/** Whether the running server is older than the latest known release. */
readonly upgradeNeeded?: boolean;
/** Whether the CLI committed to performing the upgrade (true => kill+respawn was scheduled). */
readonly upgradeStarted?: boolean;
/** Commit hash of the currently running server, or `null` if none. */
readonly runningCommit?: string | null;
/** Commit hash of the latest known release at the time of the call. */
readonly latestCommit?: string;
/** Milliseconds the client should wait before reconnecting (only set when upgrade started). */
readonly restartDelayMs?: number;
/** Human-readable error message when `ok` is false. */
readonly error?: string;
}
/**
* Returns the management socket path advertised by the hosting CLI, or
* `undefined` when the current process was not spawned by one.
*/
export function getAgentHostManagementSocketPath(): string | undefined {
const value = process.env[VSCODE_AGENT_HOST_MANAGEMENT_SOCKET_ENV];
agentHostUpgradeChannel.ts ×1
return value && value.length > 0 ? value : undefined;
}
/**
* Ask the hosting CLI to check for an update and (if needed) restart this
* server. Sends `POST /upgrade` to the management socket and returns the
* CLI's parsed JSON response.
*
* Rejects when no management socket is advertised, when the connection
* fails, on non-2xx responses, or when the response body cannot be parsed.
*/
export async function requestAgentHostUpgrade(socketPath = getAgentHostManagementSocketPath()): Promise<IUpgradeRequestResponse> {
const http = await import('http');
if (!socketPath) {
return Promise.reject(new Error(`Cannot request upgrade: ${VSCODE_AGENT_HOST_MANAGEMENT_SOCKET_ENV} is not set.`));
}
return new Promise<IUpgradeRequestResponse>((resolve, reject) => {
const req = http.request({
socketPath,
method: 'POST',
path: '/upgrade',
headers: { 'content-length': '0' },
}, (res) => {
const chunks: Buffer[] = [];
res.on('data', (chunk: Buffer) => chunks.push(chunk));
res.on('end', () => {
const body = Buffer.concat(chunks).toString('utf8');
const status = res.statusCode ?? 0;
let parsed: IUpgradeRequestResponse | undefined;
try {
parsed = body ? JSON.parse(body) as IUpgradeRequestResponse : undefined;
} catch {
// fall through to error reporting below
}
if (status >= 200 && status < 300 && parsed && parsed.ok !== false) {
resolve(parsed);
} else {
const reason = parsed?.error || body || `HTTP ${status}`;
reject(new Error(`Agent host upgrade request failed: ${reason}`));
}
});
res.on('error', reject);
});
req.once('error', reject);
req.end();
});
}