Skip to content

Commit

Permalink
elysia changed their onAfterHandle interface (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody authored Sep 28, 2023
1 parent 7d4cc1c commit 1a46d1a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Binary file modified bun.lockb
Binary file not shown.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elysia-compression",
"version": "0.0.3",
"version": "0.0.4",
"description": "Compression plugin for Elysia",
"author": {
"name": "Gusb3ll",
Expand Down Expand Up @@ -32,12 +32,12 @@
"release": "npm run build && npm publish --access public"
},
"devDependencies": {
"bun-types": "0.6.13",
"elysia": "0.5.20",
"eslint": "8.26.0",
"typescript": "5.1.6"
"bun-types": "1.0.3",
"elysia": "0.7.15",
"eslint": "8.50.0",
"typescript": "5.2.2"
},
"peerDependencies": {
"elysia": ">= 0.5.22"
"elysia": ">= 0.7.15"
}
}
}
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ export const compression =
({ type = 'gzip', options }: CompressionOptions = { type: 'gzip' }) =>
(app: Elysia) => {
if (type === 'gzip') {
return app.onAfterHandle(({ set }, res) => {
set.headers['Content-Encoding'] = 'gzip'
const compressed = gzipSync(toBuffer(res), options)

return new Response(compressed, set)
return app.onAfterHandle((context) => {
context.set.headers['Content-Encoding'] = 'gzip';
const compressed = gzipSync(toBuffer(context.response), options);
context.response = new Response(compressed, context as any);
})
} else if (type === 'deflate') {
return app.onAfterHandle(({ set }, res) => {
set.headers['Content-Encoding'] = 'deflate'
const compressed = deflateSync(toBuffer(res), options)
return app.onAfterHandle((context) => {
context.set.headers['Content-Encoding'] = 'deflate';
const compressed = deflateSync(toBuffer(context.response), options);
context.response = new Response(compressed, context as any);

return new Response(compressed, set)
})
} else {
throw new Error('Invalid compression type.')
Expand Down

0 comments on commit 1a46d1a

Please sign in to comment.