@@ -132,6 +132,18 @@ namespace ts.server {
132132 export const CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects" ;
133133 }
134134
135+ export function formatMessage < T extends protocol . Message > ( msg : T , logger : server . Logger , byteLength : ( s : string , encoding : string ) => number , newLine : string ) : string {
136+ const verboseLogging = logger . hasLevel ( LogLevel . verbose ) ;
137+
138+ const json = JSON . stringify ( msg ) ;
139+ if ( verboseLogging ) {
140+ logger . info ( msg . type + ": " + json ) ;
141+ }
142+
143+ const len = byteLength ( json , "utf8" ) ;
144+ return `Content-Length: ${ 1 + len } \r\n\r\n${ json } ${ newLine } ` ;
145+ }
146+
135147 export class Session {
136148 private readonly gcTimer : GcTimer ;
137149 protected projectService : ProjectService ;
@@ -145,8 +157,6 @@ namespace ts.server {
145157 useSingleInferredProject : boolean ,
146158 protected readonly typingsInstaller : ITypingsInstaller ,
147159 private byteLength : ( buf : string , encoding ?: string ) => number ,
148- private maxUncompressedMessageSize : number ,
149- private compress : ( s : string ) => CompressedData ,
150160 private hrtime : ( start ?: number [ ] ) => number [ ] ,
151161 protected logger : Logger ) {
152162 this . projectService =
@@ -175,27 +185,8 @@ namespace ts.server {
175185 this . logger . msg ( msg , Msg . Err ) ;
176186 }
177187
178- public send ( msg : protocol . Message , canCompressResponse : boolean ) {
179- const verboseLogging = this . logger . hasLevel ( LogLevel . verbose ) ;
180-
181- const json = JSON . stringify ( msg ) ;
182- if ( verboseLogging ) {
183- this . logger . info ( msg . type + ": " + json ) ;
184- }
185-
186- const len = this . byteLength ( json , "utf8" ) ;
187- if ( len < this . maxUncompressedMessageSize || ! canCompressResponse ) {
188- this . host . write ( `Content-Length: ${ 1 + this . byteLength ( json , "utf8" ) } \r\n\r\n${ json } ${ this . host . newLine } ` ) ;
189- }
190- else {
191- const start = verboseLogging && this . hrtime ( ) ;
192- const compressed = this . compress ( json ) ;
193- if ( verboseLogging ) {
194- const elapsed = this . hrtime ( start ) ;
195- this . logger . info ( `compressed message ${ json . length } to ${ compressed . length } in ${ hrTimeToMilliseconds ( elapsed ) } ms using ${ compressed . compressionKind } ` ) ;
196- }
197- this . host . writeCompressedData ( `Content-Length: ${ compressed . length + 1 } ${ compressed . compressionKind } \r\n\r\n` , compressed , this . host . newLine ) ;
198- }
188+ public send ( msg : protocol . Message ) {
189+ this . host . write ( formatMessage ( msg , this . logger , this . byteLength , this . host . newLine ) ) ;
199190 }
200191
201192 public configFileDiagnosticEvent ( triggerFile : string , configFile : string , diagnostics : ts . Diagnostic [ ] ) {
@@ -210,7 +201,7 @@ namespace ts.server {
210201 diagnostics : bakedDiags
211202 }
212203 } ;
213- this . send ( ev , /*canCompressResponse*/ false ) ;
204+ this . send ( ev ) ;
214205 }
215206
216207 public event ( info : any , eventName : string ) {
@@ -220,10 +211,10 @@ namespace ts.server {
220211 event : eventName ,
221212 body : info ,
222213 } ;
223- this . send ( ev , /*canCompressResponse*/ false ) ;
214+ this . send ( ev ) ;
224215 }
225216
226- public output ( info : any , cmdName : string , canCompressResponse : boolean , reqSeq = 0 , errorMsg ?: string ) {
217+ public output ( info : any , cmdName : string , reqSeq = 0 , errorMsg ?: string ) {
227218 const res : protocol . Response = {
228219 seq : 0 ,
229220 type : "response" ,
@@ -237,7 +228,7 @@ namespace ts.server {
237228 else {
238229 res . message = errorMsg ;
239230 }
240- this . send ( res , canCompressResponse ) ;
231+ this . send ( res ) ;
241232 }
242233
243234 private getLocation ( position : number , scriptInfo : ScriptInfo ) : protocol . Location {
@@ -1002,7 +993,7 @@ namespace ts.server {
1002993 this . changeSeq ++ ;
1003994 // make sure no changes happen before this one is finished
1004995 if ( project . reloadScript ( file ) ) {
1005- this . output ( undefined , CommandNames . Reload , /*canCompressResponse*/ false , reqSeq ) ;
996+ this . output ( undefined , CommandNames . Reload , reqSeq ) ;
1006997 }
1007998 }
1008999 }
@@ -1376,7 +1367,7 @@ namespace ts.server {
13761367 } ,
13771368 [ CommandNames . Configure ] : ( request : protocol . ConfigureRequest ) => {
13781369 this . projectService . setHostConfiguration ( request . arguments ) ;
1379- this . output ( undefined , CommandNames . Configure , /*canCompressResponse*/ false , request . seq ) ;
1370+ this . output ( undefined , CommandNames . Configure , request . seq ) ;
13801371 return this . notRequired ( ) ;
13811372 } ,
13821373 [ CommandNames . Reload ] : ( request : protocol . ReloadRequest ) => {
@@ -1446,7 +1437,7 @@ namespace ts.server {
14461437 }
14471438 else {
14481439 this . logger . msg ( `Unrecognized JSON command: ${ JSON . stringify ( request ) } ` , Msg . Err ) ;
1449- this . output ( undefined , CommandNames . Unknown , /*canCompressResponse*/ false , request . seq , `Unrecognized JSON command: ${ request . command } ` ) ;
1440+ this . output ( undefined , CommandNames . Unknown , request . seq , `Unrecognized JSON command: ${ request . command } ` ) ;
14501441 return { responseRequired : false } ;
14511442 }
14521443 }
@@ -1477,23 +1468,22 @@ namespace ts.server {
14771468 }
14781469
14791470 if ( response ) {
1480- this . output ( response , request . command , request . canCompressResponse , request . seq ) ;
1471+ this . output ( response , request . command , request . seq ) ;
14811472 }
14821473 else if ( responseRequired ) {
1483- this . output ( undefined , request . command , /*canCompressResponse*/ false , request . seq , "No content available." ) ;
1474+ this . output ( undefined , request . command , request . seq , "No content available." ) ;
14841475 }
14851476 }
14861477 catch ( err ) {
14871478 if ( err instanceof OperationCanceledException ) {
14881479 // Handle cancellation exceptions
1489- this . output ( { canceled : true } , request . command , /*canCompressResponse*/ false , request . seq ) ;
1480+ this . output ( { canceled : true } , request . command , request . seq ) ;
14901481 return ;
14911482 }
14921483 this . logError ( err , message ) ;
14931484 this . output (
14941485 undefined ,
14951486 request ? request . command : CommandNames . Unknown ,
1496- /*canCompressResponse*/ false ,
14971487 request ? request . seq : 0 ,
14981488 "Error processing request. " + ( < StackTraceError > err ) . message + "\n" + ( < StackTraceError > err ) . stack ) ;
14991489 }
0 commit comments