@@ -11,6 +11,9 @@ const color = require('colorful');
1111const WebSocketServer = require ( 'ws' ) . Server ;
1212const tls = require ( 'tls' ) ;
1313const crypto = require ( 'crypto' ) ;
14+ const stream = require ( 'stream' ) ;
15+ const brotli = require ( 'brotli' ) ;
16+ const zlib = require ( 'zlib' ) ;
1417
1518const createSecureContext = tls . createSecureContext || crypto . createSecureContext ;
1619
@@ -209,11 +212,6 @@ KoaServer.prototype.constructRouter = function () {
209212 this . response . set ( 'Allow' , 'GET, HEAD, POST, OPTIONS' ) ;
210213 } ) ;
211214
212- // router.connect('/test/connect', function *(next) {
213- // printLog('requesting connect /test/connect');
214- // this.response.body = 'connect_established_body';
215- // });
216-
217215 router . get ( '/test/should_not_replace_option' , this . logRequest , function * ( next ) {
218216 this . response . body = 'the_option_that_not_be_replaced' ;
219217 } ) ;
@@ -249,6 +247,23 @@ KoaServer.prototype.constructRouter = function () {
249247 this . response . body = buf ;
250248 } ) ;
251249
250+ router . get ( '/test/brotli' , this . logRequest , function * ( next ) {
251+ this . status = 200 ;
252+ this . response . set ( 'Content-Encoding' , 'br' ) ;
253+ this . response . set ( 'Content-Type' , 'application/json' ) ;
254+ const buf = new Buffer ( '{"type":"brotli","message":"This is a brotli encoding response, but it need to be a long string or the brotli module\'s compress result will be null"}' ) ;
255+ this . response . body = Buffer . from ( brotli . compress ( buf ) ) ;
256+ } ) ;
257+
258+ router . get ( '/test/gzip' , this . logRequest , function * ( next ) {
259+ this . status = 200 ;
260+ this . response . set ( 'Content-Encoding' , 'gzip' ) ;
261+ this . response . set ( 'Content-Type' , 'application/json' ) ;
262+ const bufStream = new stream . PassThrough ( ) ;
263+ bufStream . end ( new Buffer ( '{"type":"gzip","message":"This is a gzip encoding response"}' ) ) ;
264+ this . response . body = bufStream . pipe ( zlib . createGzip ( ) ) ;
265+ } ) ;
266+
252267 return router ;
253268} ;
254269
0 commit comments