From a3240bdfe81898b61d9b98b6ce36fea963180d71 Mon Sep 17 00:00:00 2001 From: Ivan Gabaldon Date: Tue, 3 Sep 2024 14:44:31 +0200 Subject: [PATCH 1/6] Initial update --- src/middleware/serve-static/index.test.ts | 43 +++++++++++++++++++ src/middleware/serve-static/index.ts | 52 ++++++++++++++++++++--- 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/middleware/serve-static/index.test.ts b/src/middleware/serve-static/index.test.ts index efae1aeb5..bfae51779 100644 --- a/src/middleware/serve-static/index.test.ts +++ b/src/middleware/serve-static/index.test.ts @@ -76,6 +76,49 @@ describe('Serve Static Middleware', () => { expect(await res.text()).toBe('404 Not Found') }) + it('Should return pre-compressed response - /static/hello.html', async () => { + const app = new Hono().use( + '*', + baseServeStatic({ + getContent, + precompressed: true, + }) + ) + + const res = await app.request('/static/hello.html', { + headers: { 'Accept-Encoding': 'gzip, deflate, br' }, + }) + + expect(res.status).toBe(200) + expect(res.headers.get('Content-Encoding')).toBe('br') + expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) + expect(await res.text()).toBe('Hello in static/hello.html.br') + }) + + it('Should not return pre-compressed response - /static/hello.html', async () => { + const app = new Hono().use( + '*', + baseServeStatic({ + getContent: vi.fn(async (path) => { + if (/\.(br|zst|gz)$/.test(path)) { + return null + } + return `Hello in ${path}` + }), + precompressed: true, + }) + ) + + const res = await app.request('/static/hello.html', { + headers: { 'Accept-Encoding': 'gzip, deflate, br' }, + }) + + expect(res.status).toBe(200) + expect(res.headers.get('Content-Encoding')).toBeNull() + expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) + expect(await res.text()).toBe('Hello in static/hello.html') + }) + it('Should return response object content as-is', async () => { const body = new ReadableStream() const response = new Response(body) diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 1e3e78569..467d2e12f 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -11,11 +11,18 @@ import { getMimeType } from '../../utils/mime' export type ServeStaticOptions = { root?: string path?: string + precompressed?: boolean mimes?: Record rewriteRequestPath?: (path: string) => string onNotFound?: (path: string, c: Context) => void | Promise } +const ENCODINGS = { + br: '.br', + zstd: '.zst', + gzip: '.gz', +} as const + const DEFAULT_DOCUMENT = 'index.html' const defaultPathResolve = (path: string) => path @@ -63,8 +70,40 @@ export const serveStatic = ( const getContent = options.getContent const pathResolve = options.pathResolve ?? defaultPathResolve + let mimeType: string | undefined + + if (options.precompressed) { + if (options.mimes) { + mimeType = getMimeType(path, options.mimes) ?? getMimeType(path) + } else { + mimeType = getMimeType(path) + } + + const acceptEncodings = + c.req + .header('Accept-Encoding') + ?.split(',') + .map((encoding) => encoding.trim()) + .filter((encoding): encoding is keyof typeof ENCODINGS => + Object.hasOwn(ENCODINGS, encoding) + ) + .sort((a, b) => Object.keys(ENCODINGS).indexOf(a) - Object.keys(ENCODINGS).indexOf(b)) || + [] + + for (const acceptEncoding of acceptEncodings) { + const extension = ENCODINGS[acceptEncoding] + + if (await getContent(path + extension, c)) { + path = pathResolve(path + extension) + c.header('Content-Encoding', acceptEncoding) + c.header('Vary', 'Accept-Encoding', { append: true }) + break + } + } + } else { + path = pathResolve(path) + } - path = pathResolve(path) let content = await getContent(path, c) if (!content) { @@ -90,11 +129,12 @@ export const serveStatic = ( } if (content) { - let mimeType: string | undefined - if (options.mimes) { - mimeType = getMimeType(path, options.mimes) ?? getMimeType(path) - } else { - mimeType = getMimeType(path) + if (!mimeType) { + if (options.mimes) { + mimeType = getMimeType(path, options.mimes) ?? getMimeType(path) + } else { + mimeType = getMimeType(path) + } } if (mimeType) { c.header('Content-Type', mimeType) From 670499cfafce33d345b95dd885973ec22de0f09e Mon Sep 17 00:00:00 2001 From: Ivan Gabaldon Date: Tue, 3 Sep 2024 23:39:20 +0200 Subject: [PATCH 2/6] typo --- src/middleware/serve-static/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 467d2e12f..0ce038f7d 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -54,7 +54,7 @@ export const serveStatic = ( root, }) if (path && (await options.isDir(path))) { - filename = filename + '/' + filename += '/' } } @@ -107,19 +107,19 @@ export const serveStatic = ( let content = await getContent(path, c) if (!content) { - let pathWithOutDefaultDocument = getFilePathWithoutDefaultDocument({ + let pathWithoutDefaultDocument = getFilePathWithoutDefaultDocument({ filename, root, }) - if (!pathWithOutDefaultDocument) { + if (!pathWithoutDefaultDocument) { return await next() } - pathWithOutDefaultDocument = pathResolve(pathWithOutDefaultDocument) + pathWithoutDefaultDocument = pathResolve(pathWithoutDefaultDocument) - if (pathWithOutDefaultDocument !== path) { - content = await getContent(pathWithOutDefaultDocument, c) + if (pathWithoutDefaultDocument !== path) { + content = await getContent(pathWithoutDefaultDocument, c) if (content) { - path = pathWithOutDefaultDocument + path = pathWithoutDefaultDocument } } } From 7fd0754509cd6ebdfc3b7c4ad7f955bba2811b94 Mon Sep 17 00:00:00 2001 From: Ivan Gabaldon Date: Wed, 4 Sep 2024 00:12:38 +0200 Subject: [PATCH 3/6] sort new logic --- src/middleware/serve-static/index.ts | 79 ++++++++++++---------------- 1 file changed, 34 insertions(+), 45 deletions(-) diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 0ce038f7d..2d5d67af4 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -70,42 +70,13 @@ export const serveStatic = ( const getContent = options.getContent const pathResolve = options.pathResolve ?? defaultPathResolve - let mimeType: string | undefined - - if (options.precompressed) { - if (options.mimes) { - mimeType = getMimeType(path, options.mimes) ?? getMimeType(path) - } else { - mimeType = getMimeType(path) - } - - const acceptEncodings = - c.req - .header('Accept-Encoding') - ?.split(',') - .map((encoding) => encoding.trim()) - .filter((encoding): encoding is keyof typeof ENCODINGS => - Object.hasOwn(ENCODINGS, encoding) - ) - .sort((a, b) => Object.keys(ENCODINGS).indexOf(a) - Object.keys(ENCODINGS).indexOf(b)) || - [] - - for (const acceptEncoding of acceptEncodings) { - const extension = ENCODINGS[acceptEncoding] + path = pathResolve(path) + let content = await getContent(path, c) - if (await getContent(path + extension, c)) { - path = pathResolve(path + extension) - c.header('Content-Encoding', acceptEncoding) - c.header('Vary', 'Accept-Encoding', { append: true }) - break - } - } - } else { - path = pathResolve(path) + if (content instanceof Response) { + return c.newResponse(content.body, content) } - let content = await getContent(path, c) - if (!content) { let pathWithoutDefaultDocument = getFilePathWithoutDefaultDocument({ filename, @@ -117,28 +88,46 @@ export const serveStatic = ( pathWithoutDefaultDocument = pathResolve(pathWithoutDefaultDocument) if (pathWithoutDefaultDocument !== path) { - content = await getContent(pathWithoutDefaultDocument, c) + content = (await getContent(pathWithoutDefaultDocument, c)) as Data | null if (content) { path = pathWithoutDefaultDocument } } } - if (content instanceof Response) { - return c.newResponse(content.body, content) + const mimeType = options.mimes + ? getMimeType(path, options.mimes) ?? getMimeType(path) + : getMimeType(path) + + if (mimeType) { + c.header('Content-Type', mimeType) } - if (content) { - if (!mimeType) { - if (options.mimes) { - mimeType = getMimeType(path, options.mimes) ?? getMimeType(path) - } else { - mimeType = getMimeType(path) + if (options.precompressed) { + const acceptEncodings = + c.req + .header('Accept-Encoding') + ?.split(',') + .map((encoding) => encoding.trim()) + .filter((encoding): encoding is keyof typeof ENCODINGS => + Object.hasOwn(ENCODINGS, encoding) + ) + .sort((a, b) => Object.keys(ENCODINGS).indexOf(a) - Object.keys(ENCODINGS).indexOf(b)) || + [] + + for (const encoding of acceptEncodings) { + const compressedContent = (await getContent(path + ENCODINGS[encoding], c)) as Data | null + + if (compressedContent) { + content = compressedContent + c.header('Content-Encoding', encoding) + c.header('Vary', 'Accept-Encoding', { append: true }) + break } } - if (mimeType) { - c.header('Content-Type', mimeType) - } + } + + if (content) { return c.body(content) } From d3bc0ccfda883ea6e259c8d1e551679da64389b3 Mon Sep 17 00:00:00 2001 From: Ivan Gabaldon Date: Wed, 4 Sep 2024 00:22:06 +0200 Subject: [PATCH 4/6] remove extra cast --- src/middleware/serve-static/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 2d5d67af4..537bcee2d 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -73,10 +73,6 @@ export const serveStatic = ( path = pathResolve(path) let content = await getContent(path, c) - if (content instanceof Response) { - return c.newResponse(content.body, content) - } - if (!content) { let pathWithoutDefaultDocument = getFilePathWithoutDefaultDocument({ filename, @@ -88,13 +84,17 @@ export const serveStatic = ( pathWithoutDefaultDocument = pathResolve(pathWithoutDefaultDocument) if (pathWithoutDefaultDocument !== path) { - content = (await getContent(pathWithoutDefaultDocument, c)) as Data | null + content = await getContent(pathWithoutDefaultDocument, c) if (content) { path = pathWithoutDefaultDocument } } } + if (content instanceof Response) { + return c.newResponse(content.body, content) + } + const mimeType = options.mimes ? getMimeType(path, options.mimes) ?? getMimeType(path) : getMimeType(path) From 50ab8952b35333e0d8f6334629518173d65aec91 Mon Sep 17 00:00:00 2001 From: Ivan Gabaldon Date: Sat, 7 Sep 2024 12:31:23 +0200 Subject: [PATCH 5/6] add more testing --- src/middleware/serve-static/index.test.ts | 64 +++++++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/src/middleware/serve-static/index.test.ts b/src/middleware/serve-static/index.test.ts index bfae51779..249e1301f 100644 --- a/src/middleware/serve-static/index.test.ts +++ b/src/middleware/serve-static/index.test.ts @@ -29,6 +29,7 @@ describe('Serve Static Middleware', () => { it('Should return 200 response - /static/hello.html', async () => { const res = await app.request('/static/hello.html') expect(res.status).toBe(200) + expect(res.headers.get('Content-Encoding')).toBeNull() expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) expect(await res.text()).toBe('Hello in ./static/hello.html') }) @@ -57,12 +58,15 @@ describe('Serve Static Middleware', () => { it('Should decode URI strings - /static/%E7%82%8E.txt', async () => { const res = await app.request('/static/%E7%82%8E.txt') expect(res.status).toBe(200) + expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/) expect(await res.text()).toBe('Hello in ./static/炎.txt') }) - it('Should return 404 response - /static/not-found', async () => { + it('Should return 404 response - /static/not-found.txt', async () => { const res = await app.request('/static/not-found.txt') expect(res.status).toBe(404) + expect(res.headers.get('Content-Encoding')).toBeNull() + expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/) expect(await res.text()).toBe('404 Not Found') expect(getContent).toBeCalledTimes(1) }) @@ -73,10 +77,31 @@ describe('Serve Static Middleware', () => { url: 'http://localhost/static/%2e%2e/static/hello.html', } as Request) expect(res.status).toBe(404) + expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/) expect(await res.text()).toBe('404 Not Found') }) - it('Should return pre-compressed response - /static/hello.html', async () => { + it('Should return a pre-compressed zstd response - /static/hello.html', async () => { + const app = new Hono().use( + '*', + baseServeStatic({ + getContent, + precompressed: true, + }) + ) + + const res = await app.request('/static/hello.html', { + headers: { 'Accept-Encoding': 'zstd' }, + }) + + expect(res.status).toBe(200) + expect(res.headers.get('Content-Encoding')).toBe('zstd') + expect(res.headers.get('Vary')).toBe('Accept-Encoding') + expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) + expect(await res.text()).toBe('Hello in static/hello.html.zst') + }) + + it('Should return a pre-compressed brotli response - /static/hello.html', async () => { const app = new Hono().use( '*', baseServeStatic({ @@ -86,35 +111,52 @@ describe('Serve Static Middleware', () => { ) const res = await app.request('/static/hello.html', { - headers: { 'Accept-Encoding': 'gzip, deflate, br' }, + headers: { 'Accept-Encoding': 'wompwomp, gzip, br, deflate, zstd' }, }) expect(res.status).toBe(200) expect(res.headers.get('Content-Encoding')).toBe('br') + expect(res.headers.get('Vary')).toBe('Accept-Encoding') expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) expect(await res.text()).toBe('Hello in static/hello.html.br') }) - it('Should not return pre-compressed response - /static/hello.html', async () => { + it('Should not return a pre-compressed response - /static/not-found.txt', async () => { const app = new Hono().use( '*', baseServeStatic({ - getContent: vi.fn(async (path) => { - if (/\.(br|zst|gz)$/.test(path)) { - return null - } - return `Hello in ${path}` - }), + getContent, + precompressed: true, + }) + ) + + const res = await app.request('/static/not-found.txt', { + headers: { 'Accept-Encoding': 'gzip, zstd, br' }, + }) + + expect(res.status).toBe(404) + expect(res.headers.get('Content-Encoding')).toBeNull() + expect(res.headers.get('Vary')).toBeNull() + expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/) + expect(await res.text()).toBe('404 Not Found') + }) + + it('Should not return a pre-compressed response - /static/hello.html', async () => { + const app = new Hono().use( + '*', + baseServeStatic({ + getContent, precompressed: true, }) ) const res = await app.request('/static/hello.html', { - headers: { 'Accept-Encoding': 'gzip, deflate, br' }, + headers: { 'Accept-Encoding': 'wompwomp, unknown' }, }) expect(res.status).toBe(200) expect(res.headers.get('Content-Encoding')).toBeNull() + expect(res.headers.get('Vary')).toBeNull() expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) expect(await res.text()).toBe('Hello in static/hello.html') }) From 2e04502b7dd793ee949e14ccc3319fddb70c0540 Mon Sep 17 00:00:00 2001 From: Ivan Gabaldon Date: Sat, 7 Sep 2024 12:32:11 +0200 Subject: [PATCH 6/6] fix content existence --- src/middleware/serve-static/index.ts | 45 ++++++++++++++-------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 537bcee2d..5e7d3af61 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -103,31 +103,32 @@ export const serveStatic = ( c.header('Content-Type', mimeType) } - if (options.precompressed) { - const acceptEncodings = - c.req - .header('Accept-Encoding') - ?.split(',') - .map((encoding) => encoding.trim()) - .filter((encoding): encoding is keyof typeof ENCODINGS => - Object.hasOwn(ENCODINGS, encoding) - ) - .sort((a, b) => Object.keys(ENCODINGS).indexOf(a) - Object.keys(ENCODINGS).indexOf(b)) || - [] - - for (const encoding of acceptEncodings) { - const compressedContent = (await getContent(path + ENCODINGS[encoding], c)) as Data | null - - if (compressedContent) { - content = compressedContent - c.header('Content-Encoding', encoding) - c.header('Vary', 'Accept-Encoding', { append: true }) - break + if (content) { + if (options.precompressed) { + const acceptEncodings = + c.req + .header('Accept-Encoding') + ?.split(',') + .map((encoding) => encoding.trim()) + .filter((encoding): encoding is keyof typeof ENCODINGS => + Object.hasOwn(ENCODINGS, encoding) + ) + .sort( + (a, b) => Object.keys(ENCODINGS).indexOf(a) - Object.keys(ENCODINGS).indexOf(b) + ) ?? [] + + for (const encoding of acceptEncodings) { + const compressedContent = (await getContent(path + ENCODINGS[encoding], c)) as Data | null + + if (compressedContent) { + content = compressedContent + c.header('Content-Encoding', encoding) + c.header('Vary', 'Accept-Encoding', { append: true }) + break + } } } - } - if (content) { return c.body(content) }