From 2a320f6e32fe70e21b91eb756a7da4bdb976b7c8 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Wed, 1 Jul 2026 19:34:00 +0900 Subject: [PATCH] fix(serve-static): serve precompressed files for application/octet-stream --- src/serve-static.ts | 7 ++++++- test/assets/static-with-precompressed/hello.bin | 1 + test/assets/static-with-precompressed/hello.bin.br | 1 + test/serve-static.test.ts | 12 ++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/assets/static-with-precompressed/hello.bin create mode 100644 test/assets/static-with-precompressed/hello.bin.br diff --git a/src/serve-static.ts b/src/serve-static.ts index 9daa4c86..3e1b053c 100644 --- a/src/serve-static.ts +++ b/src/serve-static.ts @@ -108,7 +108,12 @@ export const serveStatic = ( const mimeType = getMimeType(path) c.header('Content-Type', mimeType || 'application/octet-stream') - if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) { + if ( + options.precompressed && + (!mimeType || + mimeType === 'application/octet-stream' || + COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType)) + ) { const acceptEncodingSet = new Set( c.req .header('Accept-Encoding') diff --git a/test/assets/static-with-precompressed/hello.bin b/test/assets/static-with-precompressed/hello.bin new file mode 100644 index 00000000..895426d4 --- /dev/null +++ b/test/assets/static-with-precompressed/hello.bin @@ -0,0 +1 @@ +Hello Not Compressed \ No newline at end of file diff --git a/test/assets/static-with-precompressed/hello.bin.br b/test/assets/static-with-precompressed/hello.bin.br new file mode 100644 index 00000000..2584ce17 --- /dev/null +++ b/test/assets/static-with-precompressed/hello.bin.br @@ -0,0 +1 @@ +Hello br Compressed \ No newline at end of file diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index 0e09d277..39fad9af 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -277,6 +277,18 @@ describe('Serve Static Middleware', () => { expect(res.text).toBe('Hello Not Compressed') }) + it('Should return a pre-compressed response for an octet-stream file - /static-with-precompressed/hello.bin', async () => { + const res = await request(server) + .get('/static-with-precompressed/hello.bin') + .set('Accept-Encoding', 'gzip, br, zstd') + expect(res.status).toBe(200) + expect(res.headers['content-type']).toBe('application/octet-stream') + expect(res.headers['content-length']).toBe('19') + expect(res.headers['content-encoding']).toBe('br') + expect(res.headers['vary']).toBe('Accept-Encoding') + expect(res.body.toString()).toBe('Hello br Compressed') + }) + describe('Absolute path', () => { const rootPaths = [ path.join(__dirname, 'assets'),