@@ -10,6 +10,15 @@ namespace ts.server {
1010 stack ?: string ;
1111 }
1212
13+ export interface CompressedData {
14+ __compressedDataTag : any ;
15+ length : number ;
16+ }
17+
18+ export interface ServerHost {
19+ writeCompressedData ( prefix : string , data : CompressedData , suffix : string ) : void ;
20+ }
21+
1322 export function generateSpaces ( n : number ) : string {
1423 if ( ! spaceCache [ n ] ) {
1524 let strBuilder = "" ;
@@ -183,6 +192,7 @@ namespace ts.server {
183192 cancellationToken : HostCancellationToken ,
184193 useSingleInferredProject : boolean ,
185194 private byteLength : ( buf : string , encoding ?: string ) => number ,
195+ private compress : ( s : string ) => CompressedData ,
186196 private hrtime : ( start ?: number [ ] ) => number [ ] ,
187197 private logger : Logger ) {
188198 this . projectService =
@@ -215,13 +225,27 @@ namespace ts.server {
215225 this . host . write ( line + this . host . newLine ) ;
216226 }
217227
218- public send ( msg : protocol . Message ) {
228+ private sendCompressedDataToClient ( prefix : string , data : CompressedData ) {
229+ this . host . writeCompressedData ( prefix , data , this . host . newLine ) ;
230+ }
231+
232+ public send ( msg : protocol . Message , canCompressResponse : boolean ) {
219233 const json = JSON . stringify ( msg ) ;
220234 if ( this . logger . isVerbose ( ) ) {
221235 this . logger . info ( msg . type + ": " + json ) ;
222236 }
223- this . sendLineToClient ( "Content-Length: " + ( 1 + this . byteLength ( json , "utf8" ) ) +
224- "\r\n\r\n" + json ) ;
237+ const len = this . byteLength ( json , "utf8" ) ;
238+ if ( len < 84000 || ! canCompressResponse ) {
239+ this . sendLineToClient ( "Content-Length: " + ( 1 + this . byteLength ( json , "utf8" ) ) + "\r\n\r\n" + json ) ;
240+ }
241+ else {
242+ // TODO: measure time
243+ const compressed = this . compress ( json ) ;
244+ if ( this . logger . isVerbose ( ) ) {
245+ this . logger . info ( `compressed message to ${ compressed . length } ` ) ;
246+ }
247+ this . sendCompressedDataToClient ( `Content-Length: ${ compressed . length + 1 } ` , compressed ) ;
248+ }
225249 }
226250
227251 public configFileDiagnosticEvent ( triggerFile : string , configFile : string , diagnostics : ts . Diagnostic [ ] ) {
@@ -236,7 +260,7 @@ namespace ts.server {
236260 diagnostics : bakedDiags
237261 }
238262 } ;
239- this . send ( ev ) ;
263+ this . send ( ev , /*canCompressResponse*/ false ) ;
240264 }
241265
242266 public event ( info : any , eventName : string ) {
@@ -246,10 +270,10 @@ namespace ts.server {
246270 event : eventName ,
247271 body : info ,
248272 } ;
249- this . send ( ev ) ;
273+ this . send ( ev , /*canCompressResponse*/ false ) ;
250274 }
251275
252- private response ( info : any , cmdName : string , reqSeq = 0 , errorMsg ?: string ) {
276+ private response ( info : any , cmdName : string , canCompressResponse : boolean , reqSeq = 0 , errorMsg ?: string ) {
253277 const res : protocol . Response = {
254278 seq : 0 ,
255279 type : "response" ,
@@ -263,11 +287,11 @@ namespace ts.server {
263287 else {
264288 res . message = errorMsg ;
265289 }
266- this . send ( res ) ;
290+ this . send ( res , canCompressResponse ) ;
267291 }
268292
269- public output ( body : any , commandName : string , requestSequence = 0 , errorMessage ?: string ) {
270- this . response ( body , commandName , requestSequence , errorMessage ) ;
293+ public output ( body : any , commandName : string , canCompressResponse : boolean , requestSequence = 0 , errorMessage ?: string ) {
294+ this . response ( body , commandName , canCompressResponse , requestSequence , errorMessage ) ;
271295 }
272296
273297 private getLocation ( position : number , scriptInfo : ScriptInfo ) : protocol . Location {
@@ -1030,7 +1054,7 @@ namespace ts.server {
10301054 this . changeSeq ++ ;
10311055 // make sure no changes happen before this one is finished
10321056 if ( project . reloadScript ( file ) ) {
1033- this . output ( undefined , CommandNames . Reload , reqSeq ) ;
1057+ this . output ( undefined , CommandNames . Reload , /*canCompressResponse*/ false , reqSeq ) ;
10341058 }
10351059 }
10361060 }
@@ -1407,7 +1431,7 @@ namespace ts.server {
14071431 } ,
14081432 [ CommandNames . Configure ] : ( request : protocol . ConfigureRequest ) => {
14091433 this . projectService . setHostConfiguration ( request . arguments ) ;
1410- this . output ( undefined , CommandNames . Configure , request . seq ) ;
1434+ this . output ( undefined , CommandNames . Configure , /*canCompressResponse*/ false , request . seq ) ;
14111435 return this . notRequired ( ) ;
14121436 } ,
14131437 [ CommandNames . Reload ] : ( request : protocol . ReloadRequest ) => {
@@ -1473,7 +1497,7 @@ namespace ts.server {
14731497 }
14741498 else {
14751499 this . projectService . log ( "Unrecognized JSON command: " + JSON . stringify ( request ) ) ;
1476- this . output ( undefined , CommandNames . Unknown , request . seq , "Unrecognized JSON command: " + request . command ) ;
1500+ this . output ( undefined , CommandNames . Unknown , /*canCompressResponse*/ false , request . seq , "Unrecognized JSON command: " + request . command ) ;
14771501 return { responseRequired : false } ;
14781502 }
14791503 }
@@ -1501,22 +1525,23 @@ namespace ts.server {
15011525 this . logger . msg ( leader + ": " + elapsedMs . toFixed ( 4 ) . toString ( ) , "Perf" ) ;
15021526 }
15031527 if ( response ) {
1504- this . output ( response , request . command , request . seq ) ;
1528+ this . output ( response , request . command , request . canCompressResponse , request . seq ) ;
15051529 }
15061530 else if ( responseRequired ) {
1507- this . output ( undefined , request . command , request . seq , "No content available." ) ;
1531+ this . output ( undefined , request . command , /*canCompressResponse*/ false , request . seq , "No content available." ) ;
15081532 }
15091533 }
15101534 catch ( err ) {
15111535 if ( err instanceof OperationCanceledException ) {
15121536 // Handle cancellation exceptions
1513- this . output ( { canceled : true } , request . command , request . seq ) ;
1537+ this . output ( { canceled : true } , request . command , /*canCompressResponse*/ false , request . seq ) ;
15141538 return ;
15151539 }
15161540 this . logError ( err , message ) ;
15171541 this . output (
15181542 undefined ,
15191543 request ? request . command : CommandNames . Unknown ,
1544+ /*canCompressResponse*/ false ,
15201545 request ? request . seq : 0 ,
15211546 "Error processing request. " + ( < StackTraceError > err ) . message + "\n" + ( < StackTraceError > err ) . stack ) ;
15221547 }
0 commit comments