Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #68 not working with static plugin #130

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"devDependencies": {
"@elysiajs/cors": "^1.1.1",
"@elysiajs/static": "^1.1.1",
"@elysiajs/stream": "^1.1.0",
"@eslint/js": "^9.15.0",
"@types/bun": "latest",
Expand Down
14 changes: 5 additions & 9 deletions src/compression-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ export const CompressionStream = (
let handler: Transform

const zlibOptions: zlib.ZlibOptions = {
...{
level: 6,
},
level: 6,
...options?.zlibOptions,
}
const brotliOptions: zlib.BrotliOptions = {
...{
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]:
zlib.constants.BROTLI_DEFAULT_QUALITY,
},
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]:
zlib.constants.BROTLI_DEFAULT_QUALITY,
},
...options?.brotliOptions,
}
Expand Down
14 changes: 5 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@
options?: CompressionOptions & LifeCycleOptions & CacheOptions,
) => {
const zlibOptions: ZlibOptions = {
...{
level: 6,
},
level: 6,
...options?.zlibOptions,
}
const brotliOptions: BrotliOptions = {
...{
params: {
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_GENERIC,
[constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_DEFAULT_QUALITY,
},
params: {
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_GENERIC,
[constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_DEFAULT_QUALITY,
},
...options?.brotliOptions,
}
Expand Down Expand Up @@ -138,7 +134,7 @@

contentType = resContentType ? resContentType : 'text/plain'

const buffer = await res.arrayBuffer()
const buffer = await res.clone().arrayBuffer()

Check failure on line 137 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L137

Unsafe assignment of an error typed value.

Check failure on line 137 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L137

Unsafe call of an `error` type typed value.

Check failure on line 137 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L137

Unsafe member access .clone on an `error` typed value.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix. The other changes are tests and minor cleanup of issues found by my IDE.

// Disable compression when buffer size is less than threshold
if (buffer.byteLength < threshold) {
return
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type CompressionOptions = {
* Defaults to `false`
* @default false
*/
compressStream: boolean
compressStream?: boolean
}

export type LifeCycleOptions = {
Expand Down
File renamed without changes
32 changes: 29 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, expect, it } from 'bun:test'
import zlib from 'node:zlib'
import Elysia from 'elysia'
import staticPlugin from '@elysiajs/static'
import { Stream } from '@elysiajs/stream'
import { cors } from '@elysiajs/cors'

import { req, responseShort, jsonResponse } from './setup'
import { req, responseShort, imageResponse, jsonResponse } from './setup'
import compression from '../src'

describe(`elysia-compress`, () => {
Expand Down Expand Up @@ -97,15 +98,40 @@ describe(`elysia-compress`, () => {
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('return correct image type', async () => {
it('return correct image', async () => {
const app = new Elysia()
.use(compression({ encodings: ['gzip'], threshold: 1 }))
.get('/', () => Bun.file('tests/waifu.png'))
.get('/', () => imageResponse)

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

expect(res.headers.get('Content-Type')).toBe('image/png')
expect(res.headers.get('vary')).toBeNull()

const actualBody = await res.arrayBuffer()
const expectedBody = await imageResponse.arrayBuffer()
expect(actualBody).toEqual(expectedBody)
})

it('return correct image from static plugin', async () => {
const app = new Elysia()
.use(staticPlugin({ assets: 'tests/images', prefix: '' }))
.use(compression())

await app.modules

const res = await app.handle(
new Request('http://localhost/waifu.png', {
headers: { 'accept-encoding': 'br, deflate, gzip, zstd' },
}),
)

expect(res.headers.get('Content-Type')).toBe('image/png')
expect(res.headers.get('vary')).toBeNull()

const actualBody = await res.arrayBuffer()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix, the error here is:

132 |     const actualBody = await res.arrayBuffer()
                                       ^
error: Body already used
 code: "ERR_BODY_ALREADY_USED"

const expectedBody = await imageResponse.arrayBuffer()
expect(actualBody).toEqual(expectedBody)
})

it('must be redirected to /not-found', async () => {
Expand Down
29 changes: 15 additions & 14 deletions tests/node/cjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions tests/node/esm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deseru
export const responseLong = responseShort.repeat(100)

export const jsonResponse = Bun.file('./tests/data.json')

export const imageResponse = Bun.file('./tests/images/waifu.png')