@@ -23,6 +23,7 @@ export class StreamingServerResponse extends http.ServerResponse {
23
23
private _wroteHeader = false ;
24
24
private _hasWritten = false ;
25
25
private _initialHeaders : Record < string , string > = { } ;
26
+ private _cookies : string [ ] = [ ] ;
26
27
27
28
constructor ( {
28
29
method,
@@ -89,8 +90,16 @@ export class StreamingServerResponse extends http.ServerResponse {
89
90
return this [ HEADERS ] ;
90
91
}
91
92
92
- setHeader ( name : string , value : string | number | string [ ] ) : this {
93
- this [ HEADERS ] [ name . toLowerCase ( ) ] = convertHeader ( value ) ;
93
+ setHeader ( key : string , value : string | number | string [ ] ) : this {
94
+ key = key . toLowerCase ( ) ;
95
+ // There can be multiple set-cookie response headers
96
+ // They need to be returned as a special "cookies" array, eg:
97
+ // {statusCode: xxx, cookies: ['Cookie=Yum'], ...}
98
+ if ( key === "set-cookie" ) {
99
+ this . _cookies . push ( convertHeader ( value ) ) ;
100
+ } else {
101
+ this [ HEADERS ] [ key ] = convertHeader ( value ) ;
102
+ }
94
103
return this ;
95
104
}
96
105
@@ -116,17 +125,19 @@ export class StreamingServerResponse extends http.ServerResponse {
116
125
...this [ HEADERS ] ,
117
126
...parsedHeaders ,
118
127
} ;
128
+
119
129
this . fixHeaders ( this [ HEADERS ] ) ;
120
130
this [ HEADERS ] = {
121
131
...this [ HEADERS ] ,
122
132
...this . _initialHeaders ,
123
133
} ;
124
134
125
- console . log ( "~~thisheaders: " , this [ HEADERS ] ) ;
126
- if ( this [ HEADERS ] [ "accept-encoding" ] ?. includes ( "br" ) ) {
127
- console . log ( "~~setting encoding to br" ) ;
135
+ const acceptsBrEncoding =
136
+ this [ HEADERS ] [ "accept-encoding" ] ?. includes ( "br" ) ;
137
+ if ( acceptsBrEncoding ) {
128
138
this [ HEADERS ] [ "content-encoding" ] = "br" ;
129
139
}
140
+ delete this [ HEADERS ] [ "accept-encoding" ] ;
130
141
131
142
debug ( "writeHead" , this [ HEADERS ] ) ;
132
143
@@ -141,8 +152,10 @@ export class StreamingServerResponse extends http.ServerResponse {
141
152
) ;
142
153
const prelude = JSON . stringify ( {
143
154
statusCode : statusCode as number ,
155
+ cookies : this . _cookies ,
144
156
headers : this [ HEADERS ] ,
145
157
} ) ;
158
+
146
159
// Try to flush the buffer to the client to invoke
147
160
// the streaming. This does not work 100% of the time.
148
161
setImmediate ( ( ) => {
@@ -155,9 +168,10 @@ export class StreamingServerResponse extends http.ServerResponse {
155
168
156
169
setImmediate ( ( ) => {
157
170
this . responseStream . write ( new Uint8Array ( 8 ) ) ;
158
- console . log ( "~~accept-encoding:" , this [ HEADERS ] [ "accept-encoding" ] ) ;
159
171
160
- if ( this [ HEADERS ] [ "accept-encoding" ] ?. includes ( "br" ) ) {
172
+ // After headers are written, compress all writes
173
+ // using Brotli
174
+ if ( acceptsBrEncoding ) {
161
175
const br = zlib . createBrotliCompress ( {
162
176
flush : zlib . constants . BROTLI_OPERATION_FLUSH ,
163
177
} ) ;
0 commit comments