Skip to content

Commit

Permalink
fix gzip on accept-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
akvlad committed Sep 26, 2024
1 parent ea19418 commit 0881437
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/bun_wrapper.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0881437

Please sign in to comment.