From f5c17764cc6ead6a835513fe580e1f419ff4417b Mon Sep 17 00:00:00 2001 From: Ashary Vermaysha Date: Tue, 4 Jun 2024 13:26:43 +0700 Subject: [PATCH 1/2] fix: dont add vary value when it is a wildcard --- src/main.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 05bcfc9..b1cd81f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -154,7 +154,13 @@ export const compression = ( const headerValueArray = Array.isArray(rawHeaderValue) ? rawHeaderValue : [rawHeaderValue] - if (!headerValueArray.some((h) => h.includes('accept-encoding'))) { + + // Add accept-encoding header if it doesn't exist + // and if vary not set to * + if ( + !headerValueArray.some((h) => h.includes('accept-encoding')) && + !headerValueArray.includes('*') + ) { headers.Vary = headerValueArray.concat('accept-encoding').join(', ') } } else { From aee4421d8418d24595bb2dc7803b1cb6f26e4090 Mon Sep 17 00:00:00 2001 From: Ashary Vermaysha Date: Tue, 4 Jun 2024 13:29:49 +0700 Subject: [PATCH 2/2] test: create unit tests to ensure the issue does not occur again --- tests/index.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/index.test.ts b/tests/index.test.ts index 222c26c..13e5ad6 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -31,6 +31,7 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('Content-Encoding')).toBeNull() + expect(res.headers.get('vary')).toBeNull() }) it('handle brotli compression', async () => { @@ -45,6 +46,7 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('Content-Encoding')).toBe('br') + expect(res.headers.get('vary')).toBe('accept-encoding') }) it('handle deflate compression', async () => { @@ -54,6 +56,7 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('Content-Encoding')).toBe('deflate') + expect(res.headers.get('vary')).toBe('accept-encoding') }) it('handle gzip compression', async () => { @@ -63,6 +66,7 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('Content-Encoding')).toBe('gzip') + expect(res.headers.get('vary')).toBe('accept-encoding') }) it('accept additional headers', async () => { @@ -77,6 +81,7 @@ describe('Compression With Elysia', () => { expect(res.headers.get('Content-Encoding')).toBe('deflate') expect(res.headers.get('x-powered-by')).toBe('Elysia') + expect(res.headers.get('vary')).toBe('accept-encoding') }) it('return correct plain/text', async () => { @@ -87,6 +92,7 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('Content-Type')).toBe('text/plain') + expect(res.headers.get('vary')).toBe('accept-encoding') }) it('return correct application/json', async () => { @@ -99,6 +105,7 @@ describe('Compression With Elysia', () => { expect(res.headers.get('Content-Type')).toBe( 'application/json;charset=utf-8', ) + expect(res.headers.get('vary')).toBe('accept-encoding') }) it('return correct image type', async () => { @@ -109,6 +116,7 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('Content-Type')).toBe('image/png') + expect(res.headers.get('vary')).toBeNull() }) it('must be redirected to /not-found', async () => { @@ -154,6 +162,7 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('Content-Encoding')).toBe('gzip') + expect(res.headers.get('vary')).toBe('accept-encoding') }) it('cors should be enable when threshold 1024', async () => { @@ -202,5 +211,6 @@ describe('Compression With Elysia', () => { const res = await app.handle(req()) expect(res.headers.get('access-control-allow-origin')).toBe('*') + expect(res.headers.get('vary')).toBe('*') }) })