Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/server/_namespaces/ts.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "../moduleSpecifierCache";
export * from "../packageJsonCache";
export * from "../session";
export * from "../scriptVersionCache";
export * from "../typingInstallerAdapter";
250 changes: 250 additions & 0 deletions src/server/typingInstallerAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
import {
ApplyCodeActionCommandResult,
assertType,
createQueue,
Debug,
JsTyping,
MapLike,
server,
SortedReadonlyArray,
TypeAcquisition,
} from "./_namespaces/ts";
import {
ActionInvalidate,
ActionPackageInstalled,
ActionSet,
ActionWatchTypingLocations,
BeginInstallTypes,
createInstallTypingsRequest,
DiscoverTypings,
EndInstallTypes,
Event,
EventBeginInstallTypes,
EventEndInstallTypes,
EventInitializationFailed,
EventTypesRegistry,
InitializationFailedResponse,
InstallPackageOptionsWithProject,
InstallPackageRequest,
InvalidateCachedTypings,
ITypingsInstaller,
Logger,
LogLevel,
PackageInstalledResponse,
Project,
ProjectService,
protocol,
ServerHost,
SetTypings,
stringifyIndented,
TypesRegistryResponse,
TypingInstallerRequestUnion,
} from "./_namespaces/ts.server";

/** @internal */
export interface TypingsInstallerWorkerProcess {
send<T extends TypingInstallerRequestUnion>(rq: T): void;
}

/** @internal */
export abstract class TypingsInstallerAdapter implements ITypingsInstaller {
protected installer!: TypingsInstallerWorkerProcess;
private projectService!: ProjectService;
protected activeRequestCount = 0;
private requestQueue = createQueue<DiscoverTypings>();
private requestMap = new Map<string, DiscoverTypings>(); // Maps project name to newest requestQueue entry for that project
/** 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;

// 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 requestDelayMillis = 100;
private packageInstalledPromise: {
resolve(value: ApplyCodeActionCommandResult): void;
reject(reason: unknown): void;
} | undefined;

constructor(
protected readonly telemetryEnabled: boolean,
protected readonly logger: Logger,
protected readonly host: ServerHost,
readonly globalTypingsCacheLocation: string,
protected event: Event,
private readonly maxActiveRequestCount: number,
) {
}

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) {
this.requestedRegistry = true;
this.installer.send({ kind: "typesRegistry" });
}
return !!this.typesRegistryCache?.has(name);
}

installPackage(options: InstallPackageOptionsWithProject): Promise<ApplyCodeActionCommandResult> {
this.installer.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;
this.installer = this.createInstallerProcess();
}

onProjectClosed(p: Project): void {
this.installer.send({ projectName: p.getProjectName(), kind: "closeProject" });
}

enqueueInstallTypingsRequest(project: Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>): void {
const request = createInstallTypingsRequest(project, typeAcquisition, unresolvedImports);
if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`TIAdapter:: Scheduling throttled operation:${stringifyIndented(request)}`);
}

if (this.activeRequestCount < this.maxActiveRequestCount) {
this.scheduleRequest(request);
}
else {
if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`TIAdapter:: Deferring request for: ${request.projectName}`);
}
this.requestQueue.enqueue(request);
this.requestMap.set(request.projectName, request);
}
}

handleMessage(response: TypesRegistryResponse | PackageInstalledResponse | SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse | server.WatchTypingLocations) {
if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`TIAdapter:: 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("TIAdapter:: Received too many responses");
}

while (!this.requestQueue.isEmpty()) {
const queuedRequest = this.requestQueue.dequeue();
if (this.requestMap.get(queuedRequest.projectName) === queuedRequest) {
this.requestMap.delete(queuedRequest.projectName);
this.scheduleRequest(queuedRequest);
break;
}

if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`TIAdapter:: Skipping defunct request for: ${queuedRequest.projectName}`);
}
}

this.projectService.updateTypingsForProject(response);
this.event(response, "setTypings");

break;
}
case ActionWatchTypingLocations:
this.projectService.watchTypingLocations(response);
break;
default:
assertType<never>(response);
}
}

scheduleRequest(request: DiscoverTypings) {
if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`TIAdapter:: Scheduling request for: ${request.projectName}`);
}
this.activeRequestCount++;
this.host.setTimeout(
() => {
if (this.logger.hasLevel(LogLevel.verbose)) {
this.logger.info(`TIAdapter:: Sending request:${stringifyIndented(request)}`);
}
this.installer.send(request);
},
TypingsInstallerAdapter.requestDelayMillis,
`${request.projectName}::${request.kind}`,
);
}

protected abstract createInstallerProcess(): TypingsInstallerWorkerProcess;
}
5 changes: 3 additions & 2 deletions src/server/typingsCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ export class TypingsCache {
}

onProjectClosed(project: Project) {
this.perProjectCache.delete(project.getProjectName());
this.installer.onProjectClosed(project);
if (this.perProjectCache.delete(project.getProjectName())) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for unnecessary queueing of closeProject request

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eg tests in unittests\tsserver\autoImportProvider would fail by showing creating TI when the project is closed that does not enable TI

this.installer.onProjectClosed(project);
}
}
}
6 changes: 3 additions & 3 deletions src/testRunner/unittests/helpers/tsserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "./solutionBuilder";
import {
customTypesMap,
TestTypingsInstaller,
TestTypingsInstallerAdapter,
TestTypingsInstallerOptions,
} from "./typingsInstaller";
import {
Expand Down Expand Up @@ -103,13 +103,13 @@ export class TestSession extends ts.server.Session {
private seq = 0;
public override host!: TestSessionAndServiceHost;
public override logger!: LoggerWithInMemoryLogs;
public override readonly typingsInstaller!: TestTypingsInstaller;
public override readonly typingsInstaller!: TestTypingsInstallerAdapter;
public serverCancellationToken: TestServerCancellationToken;

constructor(optsOrHost: TestSessionConstructorOptions) {
const opts = getTestSessionPartialOptionsAndHost(optsOrHost);
opts.logger = opts.logger || createLoggerWithInMemoryLogs(opts.host);
const typingsInstaller = !opts.disableAutomaticTypingAcquisition ? new TestTypingsInstaller(opts) : undefined;
const typingsInstaller = !opts.disableAutomaticTypingAcquisition ? new TestTypingsInstallerAdapter(opts) : undefined;
const cancellationToken = opts.useCancellationToken ?
new TestServerCancellationToken(
opts.logger,
Expand Down
Loading