1
1
import http from "node:http" ;
2
2
import { Socket } from "node:net" ;
3
+ import zlib from "node:zlib" ;
3
4
4
5
import { debug , error } from "../logger.js" ;
5
6
import type { ResponseStream } from "../types/aws-lambda.js" ;
@@ -68,9 +69,6 @@ export class StreamingServerResponse extends http.ServerResponse {
68
69
this . internalWrite ( data , isSse ) ;
69
70
70
71
cb ?.( ) ;
71
-
72
- console . log ( `${ n ++ } : ${ d } ` ) ;
73
-
74
72
return true ;
75
73
} ,
76
74
} ;
@@ -122,6 +120,7 @@ export class StreamingServerResponse extends http.ServerResponse {
122
120
this [ HEADERS ] = {
123
121
...this [ HEADERS ] ,
124
122
...this . _initialHeaders ,
123
+ "content-encoding" : "br" ,
125
124
} ;
126
125
127
126
debug ( "writeHead" , this [ HEADERS ] ) ;
@@ -139,20 +138,23 @@ export class StreamingServerResponse extends http.ServerResponse {
139
138
statusCode : statusCode as number ,
140
139
headers : this [ HEADERS ] ,
141
140
} ) ;
142
-
141
+ // Try to flush the buffer to the client to invoke
142
+ // the streaming. This does not work 100% of the time.
143
+ setImmediate ( ( ) => {
144
+ this . responseStream . write ( "\n\n" ) ;
145
+ this . responseStream . uncork ( ) ;
146
+ } ) ;
143
147
setImmediate ( ( ) => {
144
148
this . responseStream . write ( prelude ) ;
145
149
} ) ;
146
150
147
151
setImmediate ( ( ) => {
148
152
this . responseStream . write ( new Uint8Array ( 8 ) ) ;
149
- } ) ;
150
-
151
- // Try to flush the buffer to the client to invoke
152
- // the streaming. This does not work 100% of the time.
153
- process . nextTick ( ( ) => {
154
- this . responseStream . write ( "\n\n" ) ;
155
- this . responseStream . uncork ( ) ;
153
+ const br = zlib . createBrotliCompress ( {
154
+ flush : zlib . constants . BROTLI_OPERATION_FLUSH ,
155
+ } ) ;
156
+ br . pipe ( this . responseStream ) ;
157
+ this . responseStream = br as unknown as ResponseStream ;
156
158
} ) ;
157
159
158
160
debug ( "writeHead" , this [ HEADERS ] ) ;
0 commit comments