diff --git a/bun.lockb b/bun.lockb index 03f8dd7..02bfb04 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 846afc9..3b2fe0f 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "fmt": "prettier --write ." }, "dependencies": { - "compressible": "2.0.18" + "compressible": "2.0.18", + "zlib": "^1.0.5" }, "devDependencies": { "@elysiajs/stream": "0.7.2", diff --git a/src/index.ts b/src/index.ts index 4f38183..1521e6c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ import compressible from 'compressible' import { Elysia, mapResponse } from 'elysia' -import { gzipSync, deflateSync, type ZlibCompressionOptions } from 'bun' +import { gzipSync, deflateSync, type ZlibCompressionOptions} from 'bun' +import { brotliCompressSync, BrotliOptions } from 'zlib' import { CompressionStream } from './stream' import { isReadableStream } from './utils' -// import { brotliCompressSync, BrotliOptions } from 'zlib' export type CompressionOptions = { /** @@ -11,13 +11,13 @@ export type CompressionOptions = { * * Algorithm to use for compression. */ - type: 'gzip' | 'deflate' // | 'brotli' + type: 'gzip' | 'deflate' | 'brotli' /** * @param {Object} * * Options for the compression algorithm. */ - options?: ZlibCompressionOptions // | BrotliOptions + options?: ZlibCompressionOptions | BrotliOptions /** * @default `utf-8` * @@ -52,8 +52,8 @@ export const compression = ( name: 'elysia-compression', }) - if (!['gzip', 'deflate'].includes(type)) { - throw new Error('Invalid compression type. Use gzip or deflate.') + if (!['gzip', 'deflate', 'brotli'].includes(type)) { + throw new Error('Invalid compression type. Use gzip, deflate, or brotli.') } return app.onAfterHandle(ctx => { @@ -81,10 +81,16 @@ export const compression = ( toBuffer(ctx.response, encoding), options as ZlibCompressionOptions, ) - : deflateSync( - toBuffer(ctx.response, encoding), - options as ZlibCompressionOptions, - ) + : type === 'deflate' + ? deflateSync( + toBuffer(ctx.response, encoding), + options as ZlibCompressionOptions, + ) + : brotliCompressSync( + toBuffer(ctx.response, encoding), + options as BrotliOptions, + ) + ctx.response = new Response(compressedBody, { headers: res.headers, })