Skip to content
Myr

feat(serve-static): `rewriteRequestPath` option for Workers/Deno/Bun (#1065)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 55684f4 · Browse files · 12 files+90 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

12 files+90 added−11 removed

  • tests:+74 added−4 removed
  • other:+16 added−7 removed

12 files+90 added−11 removed

modified:deno_dist/adapter/deno/serve-static.ts+3 added-2 removed
added:runtime_tests/bun/.static/plain.txt+1 added
1Bun!!
added:runtime_tests/bun/static/plain.txt+1 added
1Bun!
modified:runtime_tests/bun/index.test.tsx+29 added-1 removed
73 unmodified lines
7475describe('Serve Static Middleware', () => {76 const app = new Hono()77 app.all('/favicon.ico', serveStatic({ path: './runtime_tests/bun/favicon.ico' }))78 app.all('/favicon-notfound.ico', serveStatic({ path: './test_bun/favicon-notfound.ico' }))78 app.all(79 '/favicon-notfound.ico',80 serveStatic({ path: './runtime_tests/bun/favicon-notfound.ico' })81 )82 app.use('/favicon-notfound.ico', async (c, next) => {83 await next()84 c.header('X-Custom', 'Bun')85 })86 app.get(87 '/static/*',88 serveStatic({89 root: './runtime_tests/bun/',90 })91 )92 app.get(93 '/dot-static/*',94 serveStatic({95 root: './runtime_tests/bun/',96 rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'),97 })98 )99100 it('Should return static file correctly', async () => {101 const res = await app.request(new Request('http://localhost/favicon.ico'))102 await res.arrayBuffer()
5 unmodified lines
108 const res = await app.request(new Request('http://localhost/favicon-notfound.ico'))109 expect(res.status).toBe(404)110 expect(res.headers.get('X-Custom')).toBe('Bun')111 })112113 it('Should return 200 response - /static/plain.txt', async () => {114 const res = await app.request(new Request('http://localhost/static/plain.txt'))115 expect(res.status).toBe(200)116 expect(await res.text()).toBe('Bun!')117 })118119 it('Should return 200 response - /dot-static/plain.txt', async () => {120 const res = await app.request(new Request('http://localhost/dot-static/plain.txt'))121 expect(res.status).toBe(200)122 expect(await res.text()).toBe('Bun!!')123 })124})125126// Bun support WebCrypto since v0.2.2127// So, JWT middleware works well.
71 unmodified lines
added:runtime_tests/deno/.static/plain.txt+1 added
1Deno!!
added:runtime_tests/deno/static/plain.txt+1 added
1Deno!
modified:runtime_tests/deno/hono.test.ts+4 added-3 removed
modified:runtime_tests/deno/middleware.test.tsx+25 added
65 unmodified lines
66 app.use('/favicon-notfound.ico', async (c, next) => {67 await next()68 c.header('X-Custom', 'Deno')69 })7071 app.get(72 '/static/*',73 serveStatic({74 root: './runtime_tests/deno',75 })76 )7778 app.get(79 '/dot-static/*',80 serveStatic({81 root: './runtime_tests/deno',82 rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'),83 })84 )8586 let res = await app.request('http://localhost/favicon.ico')87 assertEquals(res.status, 200)88 assertEquals(res.headers.get('Content-Type'), 'image/x-icon')8990 res = await app.request('http://localhost/favicon-notfound.ico')91 assertEquals(res.status, 404)92 assertMatch(res.headers.get('Content-Type') || '', /^text\/plain/)93 assertEquals(res.headers.get('X-Custom'), 'Deno')9495 res = await app.request('http://localhost/static/plain.txt')96 assertEquals(res.status, 200)97 assertEquals(await res.text(), 'Deno!')9899 res = await app.request('http://localhost/dot-static/plain.txt')100 assertEquals(res.status, 200)101 assertEquals(await res.text(), 'Deno!!')102})103104Deno.test('JWT Authentication middleware', async () => {105 const app = new Hono<{ Variables: { 'x-foo': string } }>()
25 unmodified lines
modified:src/adapter/bun/serve-static.ts+3 added-1 removed
10 unmodified lines
1112export type ServeStaticOptions = {13 root?: string14 path?: string15 rewriteRequestPath?: (path: string) => string16}1718const DEFAULT_DOCUMENT = 'index.html'19
5 unmodified lines
25 return26 }27 const url = new URL(c.req.url)2829 const filename = options.path ?? decodeURI(url.pathname)30 let path = getFilePath({29 filename: options.path ?? decodeURI(url.pathname),31 filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,32 root: options.root,33 defaultDocument: DEFAULT_DOCUMENT,34 })35 path = `./${path}`
18 unmodified lines
modified:src/adapter/cloudflare-workers/serve-static.test.ts+16 added
8 unmodified lines
9 'assets/static/hono.abcdef.html': '<h1>Hono!</h1>',10 'assets/static/top/index.abcdef.html': '<h1>Top</h1>',11 'static-no-root/plain.abcdef.txt': 'That is plain.txt',12 'assets/static/options/foo.abcdef.txt': 'With options',13 'assets/.static/plain.abcdef.txt': 'In the dot',14}15const manifest = JSON.stringify({16 'assets/static/plain.txt': 'assets/static/plain.abcdef.txt',17 'assets/static/hono.html': 'assets/static/hono.abcdef.html',18 'assets/static/top/index.html': 'assets/static/top/index.abcdef.html',19 'static-no-root/plain.txt': 'static-no-root/plain.abcdef.txt',20 'assets/.static/plain.txt': 'assets/.static/plain.abcdef.txt',21})2223Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest })24Object.assign(global, {
7 unmodified lines
32describe('ServeStatic Middleware', () => {33 const app = new Hono()34 app.use('/static/*', serveStatic({ root: './assets' }))35 app.use('/static-no-root/*', serveStatic())36 app.use(37 '/dot-static/*',38 serveStatic({39 root: './assets',40 rewriteRequestPath: (path) => path.replace(/^\/dot-static/, '/.static'),41 })42 )4344 it('Should return plain.txt', async () => {45 const res = await app.request('http://localhost/static/plain.txt')46 expect(res.status).toBe(200)
25 unmodified lines
72 expect(res.status).toBe(200)73 expect(await res.text()).toBe('<h1>Top</h1>')74 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')75 })7677 it('Should return plain.txt with a rewriteRequestPath option', async () => {78 const res = await app.request('http://localhost/dot-static/plain.txt')79 expect(res.status).toBe(200)80 expect(await res.text()).toBe('In the dot')81 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')82 })83})8485describe('With options', () => {86 const manifest = {
65 unmodified lines
modified:src/adapter/cloudflare-workers/serve-static.ts+3 added-2 removed
8 unmodified lines
9 root?: string10 path?: string11 manifest?: object | string12 namespace?: KVNamespace13 rewriteRequestPath?: (path: string) => string14}1516const DEFAULT_DOCUMENT = 'index.html'17
6 unmodified lines
24 return25 }2627 const url = new URL(c.req.url)2728 const filename = options.path ?? decodeURI(url.pathname)29 const path = getFilePath({29 filename: options.path ?? decodeURI(url.pathname),30 filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,31 root: options.root,32 defaultDocument: DEFAULT_DOCUMENT,33 })34
20 unmodified lines
modified:src/adapter/deno/serve-static.ts+3 added-2 removed
4 unmodified lines
56export type ServeStaticOptions = {7 root?: string8 path?: string9 rewriteRequestPath?: (path: string) => string10}1112const DEFAULT_DOCUMENT = 'index.html'13
5 unmodified lines
19 return20 }2122 const url = new URL(c.req.url)2223 const filename = options.path ?? decodeURI(url.pathname)24 let path = getFilePath({24 filename: options.path ?? decodeURI(url.pathname),25 filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,26 root: options.root,27 defaultDocument: DEFAULT_DOCUMENT,28 })29
24 unmodified lines