mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-18 05:43:11 -05:00
Pull out parts of TI Adapter so we can test that more correctly instead of having to copy things (#56387)
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import * as protocol from "../server/protocol";
|
||||
import * as ts from "./_namespaces/ts";
|
||||
import {
|
||||
ApplyCodeActionCommandResult,
|
||||
assertType,
|
||||
CharacterCodes,
|
||||
combinePaths,
|
||||
createQueue,
|
||||
@@ -12,7 +10,6 @@ import {
|
||||
FileWatcher,
|
||||
getDirectoryPath,
|
||||
getRootLength,
|
||||
JsTyping,
|
||||
LanguageServiceMode,
|
||||
MapLike,
|
||||
noop,
|
||||
@@ -20,60 +17,36 @@ import {
|
||||
normalizePath,
|
||||
normalizeSlashes,
|
||||
perfLogger,
|
||||
SortedReadonlyArray,
|
||||
startTracing,
|
||||
stripQuotes,
|
||||
sys,
|
||||
toFileNameLowerCase,
|
||||
tracing,
|
||||
TypeAcquisition,
|
||||
validateLocaleAndSetLanguage,
|
||||
versionMajorMinor,
|
||||
WatchOptions,
|
||||
} from "./_namespaces/ts";
|
||||
import * as server from "./_namespaces/ts.server";
|
||||
import {
|
||||
ActionInvalidate,
|
||||
ActionPackageInstalled,
|
||||
ActionSet,
|
||||
ActionWatchTypingLocations,
|
||||
Arguments,
|
||||
BeginInstallTypes,
|
||||
createInstallTypingsRequest,
|
||||
EndInstallTypes,
|
||||
EventBeginInstallTypes,
|
||||
EventEndInstallTypes,
|
||||
EventInitializationFailed,
|
||||
EventTypesRegistry,
|
||||
Event,
|
||||
findArgument,
|
||||
formatMessage,
|
||||
getLogLevel,
|
||||
hasArgument,
|
||||
indent,
|
||||
InitializationFailedResponse,
|
||||
InstallPackageOptionsWithProject,
|
||||
InstallPackageRequest,
|
||||
InvalidateCachedTypings,
|
||||
ITypingsInstaller,
|
||||
Logger,
|
||||
LogLevel,
|
||||
Msg,
|
||||
nowString,
|
||||
nullCancellationToken,
|
||||
nullTypingsInstaller,
|
||||
PackageInstalledResponse,
|
||||
Project,
|
||||
ProjectService,
|
||||
ServerCancellationToken,
|
||||
ServerHost,
|
||||
Session,
|
||||
SetTypings,
|
||||
StartInput,
|
||||
StartSessionOptions,
|
||||
stringifyIndented,
|
||||
toEvent,
|
||||
TypesRegistryResponse,
|
||||
TypingInstallerRequestUnion,
|
||||
TypingsInstallerAdapter,
|
||||
} from "./_namespaces/ts.server";
|
||||
|
||||
interface LogOptions {
|
||||
@@ -520,69 +493,37 @@ function startNodeSession(options: StartSessionOptions, logger: Logger, cancella
|
||||
terminal: false,
|
||||
});
|
||||
|
||||
interface QueuedOperation {
|
||||
operationId: string;
|
||||
operation: () => void;
|
||||
}
|
||||
|
||||
class NodeTypingsInstaller implements ITypingsInstaller {
|
||||
private installer!: NodeChildProcess;
|
||||
private projectService!: ProjectService;
|
||||
private activeRequestCount = 0;
|
||||
private requestQueue = createQueue<QueuedOperation>();
|
||||
private requestMap = new Map<string, QueuedOperation>(); // Maps operation ID to newest requestQueue entry with that ID
|
||||
/** We will lazily request the types registry on the first call to `isKnownTypesPackageName` and store it in `typesRegistryCache`. */
|
||||
private requestedRegistry = false;
|
||||
private typesRegistryCache: Map<string, MapLike<string>> | undefined;
|
||||
|
||||
class NodeTypingsInstallerAdapter extends TypingsInstallerAdapter {
|
||||
protected override installer!: NodeChildProcess;
|
||||
// This number is essentially arbitrary. Processing more than one typings request
|
||||
// at a time makes sense, but having too many in the pipe results in a hang
|
||||
// (see https://github.com/nodejs/node/issues/7657).
|
||||
// It would be preferable to base our limit on the amount of space left in the
|
||||
// buffer, but we have yet to find a way to retrieve that value.
|
||||
private static readonly maxActiveRequestCount = 10;
|
||||
private static readonly requestDelayMillis = 100;
|
||||
private packageInstalledPromise: { resolve(value: ApplyCodeActionCommandResult): void; reject(reason: unknown): void; } | undefined;
|
||||
|
||||
constructor(
|
||||
private readonly telemetryEnabled: boolean,
|
||||
private readonly logger: Logger,
|
||||
private readonly host: ServerHost,
|
||||
readonly globalTypingsCacheLocation: string,
|
||||
telemetryEnabled: boolean,
|
||||
logger: Logger,
|
||||
host: ServerHost,
|
||||
globalTypingsCacheLocation: string,
|
||||
readonly typingSafeListLocation: string,
|
||||
readonly typesMapLocation: string,
|
||||
private readonly npmLocation: string | undefined,
|
||||
private readonly validateDefaultNpmLocation: boolean,
|
||||
private event: server.Event,
|
||||
event: Event,
|
||||
) {
|
||||
super(
|
||||
telemetryEnabled,
|
||||
logger,
|
||||
host,
|
||||
globalTypingsCacheLocation,
|
||||
event,
|
||||
NodeTypingsInstallerAdapter.maxActiveRequestCount,
|
||||
);
|
||||
}
|
||||
|
||||
isKnownTypesPackageName(name: string): boolean {
|
||||
// We want to avoid looking this up in the registry as that is expensive. So first check that it's actually an NPM package.
|
||||
const validationResult = JsTyping.validatePackageName(name);
|
||||
if (validationResult !== JsTyping.NameValidationResult.Ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.requestedRegistry) {
|
||||
return !!this.typesRegistryCache && this.typesRegistryCache.has(name);
|
||||
}
|
||||
|
||||
this.requestedRegistry = true;
|
||||
this.send({ kind: "typesRegistry" });
|
||||
return false;
|
||||
}
|
||||
|
||||
installPackage(options: InstallPackageOptionsWithProject): Promise<ApplyCodeActionCommandResult> {
|
||||
this.send<InstallPackageRequest>({ kind: "installPackage", ...options });
|
||||
Debug.assert(this.packageInstalledPromise === undefined);
|
||||
return new Promise<ApplyCodeActionCommandResult>((resolve, reject) => {
|
||||
this.packageInstalledPromise = { resolve, reject };
|
||||
});
|
||||
}
|
||||
|
||||
attach(projectService: ProjectService) {
|
||||
this.projectService = projectService;
|
||||
createInstallerProcess() {
|
||||
if (this.logger.hasLevel(LogLevel.requestTime)) {
|
||||
this.logger.info("Binding...");
|
||||
}
|
||||
@@ -634,155 +575,7 @@ function startNodeSession(options: StartSessionOptions, logger: Logger, cancella
|
||||
process.on("exit", () => {
|
||||
this.installer.kill();
|
||||
});
|
||||
}
|
||||
|
||||
onProjectClosed(p: Project): void {
|
||||
this.send({ projectName: p.getProjectName(), kind: "closeProject" });
|
||||
}
|
||||
|
||||
private send<T extends TypingInstallerRequestUnion>(rq: T): void {
|
||||
this.installer.send(rq);
|
||||
}
|
||||
|
||||
enqueueInstallTypingsRequest(project: Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>): void {
|
||||
const request = createInstallTypingsRequest(project, typeAcquisition, unresolvedImports);
|
||||
if (this.logger.hasLevel(LogLevel.verbose)) {
|
||||
if (this.logger.hasLevel(LogLevel.verbose)) {
|
||||
this.logger.info(`Scheduling throttled operation:${stringifyIndented(request)}`);
|
||||
}
|
||||
}
|
||||
|
||||
const operationId = project.getProjectName();
|
||||
const operation = () => {
|
||||
if (this.logger.hasLevel(LogLevel.verbose)) {
|
||||
this.logger.info(`Sending request:${stringifyIndented(request)}`);
|
||||
}
|
||||
this.send(request);
|
||||
};
|
||||
const queuedRequest: QueuedOperation = { operationId, operation };
|
||||
|
||||
if (this.activeRequestCount < NodeTypingsInstaller.maxActiveRequestCount) {
|
||||
this.scheduleRequest(queuedRequest);
|
||||
}
|
||||
else {
|
||||
if (this.logger.hasLevel(LogLevel.verbose)) {
|
||||
this.logger.info(`Deferring request for: ${operationId}`);
|
||||
}
|
||||
this.requestQueue.enqueue(queuedRequest);
|
||||
this.requestMap.set(operationId, queuedRequest);
|
||||
}
|
||||
}
|
||||
|
||||
private handleMessage(response: TypesRegistryResponse | PackageInstalledResponse | SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse | server.WatchTypingLocations) {
|
||||
if (this.logger.hasLevel(LogLevel.verbose)) {
|
||||
this.logger.info(`Received response:${stringifyIndented(response)}`);
|
||||
}
|
||||
|
||||
switch (response.kind) {
|
||||
case EventTypesRegistry:
|
||||
this.typesRegistryCache = new Map(Object.entries(response.typesRegistry));
|
||||
break;
|
||||
case ActionPackageInstalled: {
|
||||
const { success, message } = response;
|
||||
if (success) {
|
||||
this.packageInstalledPromise!.resolve({ successMessage: message });
|
||||
}
|
||||
else {
|
||||
this.packageInstalledPromise!.reject(message);
|
||||
}
|
||||
this.packageInstalledPromise = undefined;
|
||||
|
||||
this.projectService.updateTypingsForProject(response);
|
||||
|
||||
// The behavior is the same as for setTypings, so send the same event.
|
||||
this.event(response, "setTypings");
|
||||
break;
|
||||
}
|
||||
case EventInitializationFailed: {
|
||||
const body: protocol.TypesInstallerInitializationFailedEventBody = {
|
||||
message: response.message,
|
||||
};
|
||||
const eventName: protocol.TypesInstallerInitializationFailedEventName = "typesInstallerInitializationFailed";
|
||||
this.event(body, eventName);
|
||||
break;
|
||||
}
|
||||
case EventBeginInstallTypes: {
|
||||
const body: protocol.BeginInstallTypesEventBody = {
|
||||
eventId: response.eventId,
|
||||
packages: response.packagesToInstall,
|
||||
};
|
||||
const eventName: protocol.BeginInstallTypesEventName = "beginInstallTypes";
|
||||
this.event(body, eventName);
|
||||
break;
|
||||
}
|
||||
case EventEndInstallTypes: {
|
||||
if (this.telemetryEnabled) {
|
||||
const body: protocol.TypingsInstalledTelemetryEventBody = {
|
||||
telemetryEventName: "typingsInstalled",
|
||||
payload: {
|
||||
installedPackages: response.packagesToInstall.join(","),
|
||||
installSuccess: response.installSuccess,
|
||||
typingsInstallerVersion: response.typingsInstallerVersion,
|
||||
},
|
||||
};
|
||||
const eventName: protocol.TelemetryEventName = "telemetry";
|
||||
this.event(body, eventName);
|
||||
}
|
||||
|
||||
const body: protocol.EndInstallTypesEventBody = {
|
||||
eventId: response.eventId,
|
||||
packages: response.packagesToInstall,
|
||||
success: response.installSuccess,
|
||||
};
|
||||
const eventName: protocol.EndInstallTypesEventName = "endInstallTypes";
|
||||
this.event(body, eventName);
|
||||
break;
|
||||
}
|
||||
case ActionInvalidate: {
|
||||
this.projectService.updateTypingsForProject(response);
|
||||
break;
|
||||
}
|
||||
case ActionSet: {
|
||||
if (this.activeRequestCount > 0) {
|
||||
this.activeRequestCount--;
|
||||
}
|
||||
else {
|
||||
Debug.fail("Received too many responses");
|
||||
}
|
||||
|
||||
while (!this.requestQueue.isEmpty()) {
|
||||
const queuedRequest = this.requestQueue.dequeue();
|
||||
if (this.requestMap.get(queuedRequest.operationId) === queuedRequest) {
|
||||
this.requestMap.delete(queuedRequest.operationId);
|
||||
this.scheduleRequest(queuedRequest);
|
||||
break;
|
||||
}
|
||||
|
||||
if (this.logger.hasLevel(LogLevel.verbose)) {
|
||||
this.logger.info(`Skipping defunct request for: ${queuedRequest.operationId}`);
|
||||
}
|
||||
}
|
||||
|
||||
this.projectService.updateTypingsForProject(response);
|
||||
|
||||
this.event(response, "setTypings");
|
||||
|
||||
break;
|
||||
}
|
||||
case ActionWatchTypingLocations:
|
||||
this.projectService.watchTypingLocations(response);
|
||||
break;
|
||||
default:
|
||||
assertType<never>(response);
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleRequest(request: QueuedOperation) {
|
||||
if (this.logger.hasLevel(LogLevel.verbose)) {
|
||||
this.logger.info(`Scheduling request for: ${request.operationId}`);
|
||||
}
|
||||
this.activeRequestCount++;
|
||||
this.host.setTimeout(request.operation, NodeTypingsInstaller.requestDelayMillis);
|
||||
return this.installer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -802,13 +595,13 @@ function startNodeSession(options: StartSessionOptions, logger: Logger, cancella
|
||||
|
||||
const typingsInstaller = disableAutomaticTypingAcquisition
|
||||
? undefined
|
||||
: new NodeTypingsInstaller(telemetryEnabled, logger, host, getGlobalTypingsCacheLocation(), typingSafeListLocation, typesMapLocation, npmLocation, validateDefaultNpmLocation, event);
|
||||
: new NodeTypingsInstallerAdapter(telemetryEnabled, logger, host, getGlobalTypingsCacheLocation(), typingSafeListLocation, typesMapLocation, npmLocation, validateDefaultNpmLocation, event);
|
||||
|
||||
super({
|
||||
host,
|
||||
cancellationToken,
|
||||
...options,
|
||||
typingsInstaller: typingsInstaller || nullTypingsInstaller,
|
||||
typingsInstaller,
|
||||
byteLength: Buffer.byteLength,
|
||||
hrtime: process.hrtime,
|
||||
logger,
|
||||
|
||||
Reference in New Issue
Block a user