Skip to content

Commit 3ba8565

Browse files
committed
Use node ipc for TS Server
For microsoft#46417 This lets us use Node's built-in ipc for passing messages to/from the typescript server instead of using stdio
1 parent 605788d commit 3ba8565

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/tsserver/nodeServer.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,24 @@ namespace ts.server {
715715
this.constructed = true;
716716
}
717717

718+
public send(msg: protocol.Message) {
719+
if (useNodeIpc) {
720+
if (msg.type === "event" && !this.canUseEvents) {
721+
if (this.logger.hasLevel(LogLevel.verbose)) {
722+
this.logger.info(`Session does not support events: ignored event: ${JSON.stringify(msg)}`);
723+
}
724+
return;
725+
}
726+
// TODO: Do we need any perf stuff here?
727+
// const msgText = formatMessage(msg, this.logger, this.byteLength, this.host.newLine);
728+
// perfLogger.logEvent(`Response message size: ${msgText.length}`);
729+
process.send!(msg);
730+
}
731+
else {
732+
super.send(msg);
733+
}
734+
}
735+
718736
event<T extends object>(body: T, eventName: string): void {
719737
Debug.assert(!!this.constructed, "Should only call `IOSession.prototype.event` on an initialized IOSession");
720738

@@ -748,14 +766,21 @@ namespace ts.server {
748766
}
749767

750768
listen() {
751-
rl.on("line", (input: string) => {
752-
const message = input.trim();
753-
this.onMessage(message);
754-
});
769+
if (useNodeIpc) {
770+
process.on('message', (e: any) => {
771+
this.onMessage(JSON.stringify(e)); // TODO: should not convert to back to a string just to parse again :)
772+
});
773+
}
774+
else {
775+
rl.on("line", (input: string) => {
776+
const message = input.trim();
777+
this.onMessage(message);
778+
});
755779

756-
rl.on("close", () => {
757-
this.exit();
758-
});
780+
rl.on("close", () => {
781+
this.exit();
782+
});
783+
}
759784
}
760785
}
761786

@@ -765,6 +790,7 @@ namespace ts.server {
765790
const npmLocation = findArgument(Arguments.NpmLocation);
766791
const validateDefaultNpmLocation = hasArgument(Arguments.ValidateDefaultNpmLocation);
767792
const disableAutomaticTypingAcquisition = hasArgument("--disableAutomaticTypingAcquisition");
793+
const useNodeIpc = hasArgument('--useNodeIpc');
768794
const telemetryEnabled = hasArgument(Arguments.EnableTelemetry);
769795
const commandLineTraceDir = findArgument("--traceDirectory");
770796
const traceDir = commandLineTraceDir

0 commit comments

Comments
 (0)