Skip to content

Commit

Permalink
Revert "fix: content-type (#5)"
Browse files Browse the repository at this point in the history
This reverts commit 7b22c90.
  • Loading branch information
Gusb3ll authored Oct 1, 2023
1 parent 7b22c90 commit ce1498c
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 143 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ Feature:
Bug fix:

- inherits response headers
Feature:
- add CommonJS support for using in Nodejs

Bug fix:
- inherits response headers
Binary file modified bun.lockb
Binary file not shown.
10 changes: 0 additions & 10 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@ import { Elysia } from 'elysia'
import { compression } from '../src/index'

const app = new Elysia()
<<<<<<< HEAD
.use(compression())
.all('/', () => ({ value: 'Hello, World!' }))
.all('/text', () => 'a')
.listen(4000)

console.log(
`Elysia is running at http://${app.server?.hostname}:${app.server?.port}`,
=======
.use(compression())
.post('/', ({ set }) => ({ value: 'Hello, World!' }))
.listen(3000)

console.log(
`Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
>>>>>>> 8837f0df944a1b7a5f6a109d18b2b0f3884821d3
)

export type App = typeof app
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elysia-compression",
"version": "0.0.6",
"version": "0.0.5",
"description": "Compression plugin for Elysia",
"author": {
"name": "Gusb3ll",
Expand Down
97 changes: 35 additions & 62 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Elysia, mapResponse } from 'elysia'
import type { Elysia } from 'elysia'
import { gzipSync, deflateSync, type ZlibCompressionOptions } from 'bun'
// import { brotliCompressSync, BrotliOptions } from 'zlib'

Expand Down Expand Up @@ -31,67 +31,40 @@ const toBuffer = (data: unknown, encoding: BufferEncoding) =>
encoding,
)

export const compression = (
{ type = 'gzip', options = {}, encoding = 'utf-8' }: CompressionOptions = {
type: 'gzip',
encoding: 'utf-8',
},
) => {
const app = new Elysia({
name: 'elysia-compression',
})

switch (type) {
case 'gzip':
return app.onAfterHandle(ctx => {
ctx.set.headers['Content-Encoding'] = 'gzip'

const compressed = gzipSync(
toBuffer(ctx.response, encoding),
options as ZlibCompressionOptions,
)

const response = mapResponse(ctx.response, {
status: 200,
headers: {},
export const compression =
(
{ type = 'gzip', options = {}, encoding = 'utf-8' }: CompressionOptions = {
type: 'gzip',
encoding: 'utf-8',
},
) =>
(app: Elysia) => {
switch (type) {
case 'gzip':
return app.onAfterHandle(ctx => {
ctx.set.headers['Content-Encoding'] = 'gzip'
const compressed = gzipSync(
toBuffer(ctx.response, encoding),
options as ZlibCompressionOptions,
)
ctx.response = new Response(compressed, ctx as any)
})

if (!response.headers.get('Content-Type'))
ctx.set.headers['content-type'] = 'text/plain'

ctx.response = response
})

case 'deflate':
return app.onAfterHandle(ctx => {
ctx.set.headers['Content-Encoding'] = 'deflate'

const compressed = deflateSync(
toBuffer(ctx.response, encoding),
options as ZlibCompressionOptions,
)

const response = mapResponse(ctx.response, {
status: 200,
headers: {},
case 'deflate':
return app.onAfterHandle(ctx => {
ctx.set.headers['Content-Encoding'] = 'deflate'
const compressed = deflateSync(
toBuffer(ctx.response, encoding),
options as ZlibCompressionOptions,
)
ctx.response = new Response(compressed, ctx as any)
})

if (!response.headers.get('Content-Type'))
ctx.set.headers['content-type'] = 'text/plain'

ctx.response = response
})

// case 'brotli':
// return app.onAfterHandle((ctx) => {
// ctx.set.headers['Content-Encoding'] = 'br';
// const compressed = brotliCompressSync(toBuffer(ctx.response, encoding), options as BrotliOptions);
// ctx.response = new Response(compressed, ctx as any);
// })

default:
throw new Error('Invalid compression type.')
// case 'brotli':
// return app.onAfterHandle((ctx) => {
// ctx.set.headers['Content-Encoding'] = 'br';
// const compressed = brotliCompressSync(toBuffer(ctx.response, encoding), options as BrotliOptions);
// ctx.response = new Response(compressed, ctx as any);
// })
default:
throw new Error('Invalid compression type.')
}
}

return app
}
65 changes: 0 additions & 65 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,69 +41,4 @@ describe('Compression', () => {
expect(res.headers.get('Content-Encoding')).toBe('deflate')
expect(res.headers.get('x-powered-by')).toBe('Elysia')
})

it('return correct plain/text', async () => {
const app = new Elysia()
.use(compression({ type: 'deflate' }))
.get('/', () => response)

const res = await app.handle(req())

expect(res.headers.get('Content-Type')).toBe('text/plain')
})

it('return correct application/json', async () => {
const app = new Elysia()
.use(compression({ type: 'deflate' }))
.get('/', () => ({ hello: 'world' }))

const res = await app.handle(req())

expect(res.headers.get('Content-Type')).toBe('application/json')
})

it('return correct application/json', async () => {
const app = new Elysia()
.use(compression({ type: 'deflate' }))
.get('/', () => ({ hello: 'world' }))

const res = await app.handle(req())

expect(res.headers.get('Content-Type')).toBe('application/json')
})

it('return correct image type', async () => {
const app = new Elysia()
.use(compression({ type: 'deflate' }))
.get('/', () => Bun.file('tests/mei.jpg'))

const res = await app.handle(req())

expect(res.headers.get('Content-Type')).toBe('image/jpeg')
})
it('handle gzip compression', async () => {
const app = new Elysia().use(compression()).get('/', () => response)
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBe('gzip')
})

it('handle deflate compression', async () => {
const app = new Elysia().use(compression({ 'type': 'deflate' })).get('/', () => response)
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBe('deflate')
})

it('accept additional headers', async () => {
const app = new Elysia().use(compression({ 'type': 'deflate' })).get('/', ({ set }) => {
set.headers['x-powered-by'] = 'Elysia'

return response
})
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBe('deflate')
expect(res.headers.get('x-powered-by')).toBe('Elysia')
})
})
Binary file removed tests/mei.jpg
Binary file not shown.
1 change: 1 addition & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"types": ["bun-types"],
"declaration": true,
"outDir": "./dist/cjs",
"removeComments": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"types": ["bun-types"],
"declaration": true,
"outDir": "./dist",
"removeComments": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down

0 comments on commit ce1498c

Please sign in to comment.