-
Notifications
You must be signed in to change notification settings - Fork 52
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
serveStatic w/ compress middleware issue #104
Comments
I can confirm, that there is an issue with the response content-type header, as soon as compression is enabled. Minimal example: import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import { compress } from 'hono/compress'
const app = new Hono()
app.use('*', compress())
app.get('/', (c) =>
c.json({
some: 'value',
}),
)
serve({
fetch: app.fetch,
port: 3000,
}) response: Tried it with node 20.8 and 20.10 The issue seems not to be present when with Deno. I had a quick look into compress middleware and I didn't recognize something, that might be the root cause. From quick look into the code, I guess the code, the issue relies on this code here: Line 24 in e2648a4
and here: Line 41 in e2648a4
The mapping from the current headers to the final headers is messed up in compression with: I guess the headers simply have different types, but I'm too unfamiliar with hono code base, to provide a propper fix atm. |
@luwes @sebastianwessel Thank you for raising the issue. Hi @usualoma, in the fowllowing case, test will fail. Can you see it? describe('content-type', () => {
const app = new Hono()
app.use('*', async (c, next) => {
await next()
c.res = new Response('', c.res)
c.res.headers // If this is set, test fails
})
app.get('/json', async (c) => {
return c.json({})
})
it('Should return 200 response - GET /json', async () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/json')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/application\/json/)
})
}) |
It appears to be a bug in the Response object. It can be fixed. Please wait a moment. |
Thanks! |
Thanks to @usualoma , this issue is fixed in latest version v1.3.2. Give it a try! |
I did a quick test, and it seems to work as expected. |
Thank you for the fix! I tested it out but not all statics are compressed it seems. Using app.use('*', serveStatic({ root: dir }));
app.use('*', compress());
|
It can be reproduced by this minimal code: app.get('/', () => {
const stream = new ReadableStream({
start(controller) {
const encoder = new TextEncoder()
controller.enqueue(encoder.encode('hello'))
controller.close()
},
})
const originalResponse = new Response(stream)
const compStream = new CompressionStream('gzip')
const res = new Response(originalResponse.body!.pipeThrough(compStream), originalResponse)
res.headers.set('Content-Encoding', 'gzip')
return res
}) @usualoma When you have time, please take a look. |
In my testing the compress middleware made a
serveStatic
HTML page return plain texttext/plain;charset=UTF-8
.https://hono.dev/middleware/builtin/compress
The text was updated successfully, but these errors were encountered: