From 08814373e63f62b20fd1451afa7f7aa4a978eaa2 Mon Sep 17 00:00:00 2001 From: akvlad Date: Thu, 26 Sep 2024 14:34:36 +0300 Subject: [PATCH] fix gzip on accept-encoding --- lib/bun_wrapper.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/bun_wrapper.js b/lib/bun_wrapper.js index edc82a3a..2ce2eeeb 100644 --- a/lib/bun_wrapper.js +++ b/lib/bun_wrapper.js @@ -1,6 +1,7 @@ const { Transform } = require('stream') const log = require('./logger') const { EventEmitter } = require('events') +const zlib = require('zlib') class BodyStream extends Transform { _transform (chunk, encoding, callback) { @@ -121,6 +122,16 @@ const wrapper = (handler, parsers) => { headers['Content-Type'] = 'application/json' response = JSON.stringify(response) } + if (response && (ctx.headers.get('accept-encoding') || '').indexOf('gzip') !== -1) { + if (response.on) { + const _r = zlib.createGzip() + response.pipe(_r) + response = _r + } else { + response = Bun.gzipSync(response) + } + headers['Content-Encoding'] = 'gzip' + } return new Response(response, { status: status, headers: headers }) } return res