// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import { DaemonFrameType, encodeDaemonEventFrame, encodeDaemonLogChunk } from '@rushstack/rush-daemon-protocol'; import type { DaemonControlMessage, DaemonRequestRejectionCode, IDaemonCommandResult, IDaemonEventEnvelope, IDaemonFrame, IDaemonPhasedRequestResult, IDaemonRequestQueuePositionMessage, IDaemonTerminalPolicyResult } from '@rushstack/rush-daemon-protocol'; import type { IDaemonRequestDispatchClient } from './DaemonRequestDispatcher'; import type { IInteractiveRequestSession } from './InteractiveRequestInputRouter'; export interface IDaemonWireRequestClientOptions { readonly abortSignal: AbortSignal; readonly getNextEventSequence: () => number; readonly interactiveSession: IInteractiveRequestSession; /** The `performance.now()` timestamp at which the control session read the request. */ readonly receivedTimeMs: number; readonly requestId: string; readonly sendControlAsync: (message: DaemonControlMessage) => Promise; /** Like `sendControlAsync`, but resolves only once the operating system holds the whole frame. */ readonly sendControlWrittenAsync: (message: DaemonControlMessage) => Promise; readonly sendFrameAsync: (frame: IDaemonFrame) => Promise; readonly sessionId: string; readonly supportsRequestAdmission: boolean; /** Whether the client accepts `requestStarted`; see {@link DaemonWireRequestClient.writeRequestStartedAsync}. */ readonly supportsRequestStarted?: boolean; /** Waits for the daemon's other connections that have not sent a request yet. Resolves at once if omitted. */ readonly waitForConnectingClientsAsync?: () => Promise; } /** Ordered wire destination for one request owned by a control session. @internal */ export class DaemonWireRequestClient implements IDaemonRequestDispatchClient { readonly #getNextEventSequence: () => number; readonly #requestId: string; readonly #sendControlAsync: (message: DaemonControlMessage) => Promise; readonly #sendControlWrittenAsync: (message: DaemonControlMessage) => Promise; readonly #sendFrameAsync: (frame: IDaemonFrame) => Promise; readonly #supportsRequestStarted: boolean; readonly #waitForConnectingClientsAsync: (() => Promise) | undefined; #startedSent: boolean = false; #requestStarted: boolean = false; #terminalOutcomeSent: boolean = false; public readonly abortSignal: AbortSignal; public readonly interactiveSession: IInteractiveRequestSession; public readonly receivedTimeMs: number; public readonly sessionId: string; public readonly supportsRequestAdmission: boolean; public constructor(options: IDaemonWireRequestClientOptions) { this.abortSignal = options.abortSignal; this.#getNextEventSequence = options.getNextEventSequence; this.interactiveSession = options.interactiveSession; this.receivedTimeMs = options.receivedTimeMs; this.#requestId = options.requestId; this.#sendControlAsync = options.sendControlAsync; this.#sendControlWrittenAsync = options.sendControlWrittenAsync; this.#sendFrameAsync = options.sendFrameAsync; this.sessionId = options.sessionId; this.supportsRequestAdmission = options.supportsRequestAdmission; this.#supportsRequestStarted = options.supportsRequestStarted === true; this.#waitForConnectingClientsAsync = options.waitForConnectingClientsAsync; } public getNextEventSequence(): number { return this.#getNextEventSequence(); } public async waitForConnectingClientsAsync(): Promise { await this.#waitForConnectingClientsAsync?.(); } public get terminalOutcomeSent(): boolean { return this.#terminalOutcomeSent; } /** Whether the request has left every queue, whether or not its client subscribed to `requestStarted`. */ public get requestStarted(): boolean { return this.#requestStarted; } public writeEventAsync(event: IDaemonEventEnvelope): Promise { return this.#sendFrameAsync({ kind: DaemonFrameType.event, payload: encodeDaemonEventFrame(event) }); } public writeLogChunkAsync( operationId: string, stream: 'stdout' | 'stderr', chunk: Uint8Array ): Promise { return this.#writeLogAsync(operationId, stream, chunk); } public writeTerminalChunkAsync(stream: 'stdout' | 'stderr', chunk: Uint8Array): Promise { return this.#writeLogAsync(this.#requestId, stream, chunk); } public writeQueuePositionAsync(message: IDaemonRequestQueuePositionMessage): Promise { return this.#sendControlAsync(message); } /** * Tells a client that subscribed with `supportsRequestStarted` that the request left every queue, once. Frames * are sent in order, so this precedes the request's output. For every client, it records that the request started, * so that a shutdown tells a request that never started that it was queued. * It resolves only once the operating system holds the notice, so that the client can read it even if the daemon * exits as soon as the request starts. */ public writeRequestStartedAsync(): Promise { this.#requestStarted = true; if (!this.#supportsRequestStarted || this.#startedSent) return Promise.resolve(); this.#startedSent = true; return this.#sendControlWrittenAsync({ kind: 'requestStarted', payload: { requestId: this.#requestId } }); } public writeResultAsync(result: IDaemonCommandResult | IDaemonPhasedRequestResult): Promise { this.#claimTerminalOutcome(); return this.#sendControlAsync({ kind: 'requestResult', payload: result }); } public writeTerminalPolicyAsync(result: IDaemonTerminalPolicyResult): Promise { this.#claimTerminalOutcome(); return this.#sendControlAsync({ kind: 'terminalPolicy', payload: result }); } public writeRejectionAsync(code: DaemonRequestRejectionCode, message: string): Promise { this.#claimTerminalOutcome(); return this.#sendControlAsync({ kind: 'requestRejected', payload: { code, message, requestId: this.#requestId } }); } #writeLogAsync(operationId: string, stream: 'stdout' | 'stderr', chunk: Uint8Array): Promise { return this.#sendFrameAsync({ kind: stream === 'stdout' ? DaemonFrameType.logStdout : DaemonFrameType.logStderr, payload: encodeDaemonLogChunk({ chunk, operationId }) }); } #claimTerminalOutcome(): void { if (this.#terminalOutcomeSent) { throw new Error(`Request "${this.#requestId}" already produced a terminal wire outcome.`); } this.#terminalOutcomeSent = true; } }