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
Provenance
- Commit
21f4c19- Tree
ddd1654- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
55684f4
12 files changed+90 added−11 removed
modified:deno_dist/adapter/deno/serve-static.ts+3 added-2 removed
4 unmodified lines56789101112135 unmodified lines1920212222232424252627282924 unmodified lines4 unmodified lines
export type ServeStaticOptions = { root?: string path?: string rewriteRequestPath?: (path: string) => string}
const DEFAULT_DOCUMENT = 'index.html'
5 unmodified lines return }
const url = new URL(c.req.url)
const filename = options.path ?? decodeURI(url.pathname) let path = getFilePath({ filename: options.path ?? decodeURI(url.pathname), filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename, root: options.root, defaultDocument: DEFAULT_DOCUMENT, })
24 unmodified linesadded:runtime_tests/bun/.static/plain.txt+1 added
1+Bun!!added:runtime_tests/bun/static/plain.txt+1 added
1+Bun!modified:runtime_tests/bun/index.test.tsx+29 added-1 removed
73 unmodified lines74 75 describe('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+ )99 100 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 lines108 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 })112+113+ 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+ })118+119+ 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 })125 126 // Bun support WebCrypto since v0.2.2127 // So, JWT middleware works well.71 unmodified linesadded:runtime_tests/deno/.static/plain.txt+1 added
1+Deno!!added:runtime_tests/deno/static/plain.txt+1 added
1+Deno!modified:runtime_tests/deno/hono.test.ts+4 added-3 removed
11 unmodified lines1213141516161718192021222323242526272828293031323311 unmodified lines c.header('x-param', c.req.param('foo')) c.header('x-query', c.req.query('q') || '') return c.text('Hello Deno!') })
const res = await app.request('http://localhost/foo?q=bar') const res = await app.request('/foo?q=bar') assertEquals(res.status, 200) assertEquals(await res.text(), 'Hello Deno!') assertEquals(res.headers.get('x-param'), 'foo') assertEquals(res.headers.get('x-query'), 'bar')})
Deno.test('runtime', async () => {Deno.test('runtime', () => { const c = new Context(new Request('http://localhost/')) assertEquals(c.runtime, 'deno')})
Deno.test('environment variables', async () => {Deno.test('environment variables', () => { const c = new Context(new Request('http://localhost/')) const { NAME } = env<{ NAME: string }>(c) assertEquals(NAME, 'Deno')})modified:runtime_tests/deno/middleware.test.tsx+25 added
65 unmodified lines66 app.use('/favicon-notfound.ico', async (c, next) => {67 await next()68 c.header('X-Custom', 'Deno')69 })70+71+ app.get(72+ '/static/*',73+ serveStatic({74+ root: './runtime_tests/deno',75+ })76+ )77+78+ app.get(79+ '/dot-static/*',80+ serveStatic({81+ root: './runtime_tests/deno',82+ rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'),83+ })84+ )85+86 let res = await app.request('http://localhost/favicon.ico')87 assertEquals(res.status, 200)88 assertEquals(res.headers.get('Content-Type'), 'image/x-icon')89+90 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')94+95+ res = await app.request('http://localhost/static/plain.txt')96+ assertEquals(res.status, 200)97+ assertEquals(await res.text(), 'Deno!')98+99+ res = await app.request('http://localhost/dot-static/plain.txt')100+ assertEquals(res.status, 200)101+ assertEquals(await res.text(), 'Deno!!')102 })103 104 Deno.test('JWT Authentication middleware', async () => {105 const app = new Hono<{ Variables: { 'x-foo': string } }>()25 unmodified linesmodified:src/adapter/bun/serve-static.ts+3 added-1 removed
10 unmodified lines11 12 export type ServeStaticOptions = {13 root?: string14 path?: string15+ rewriteRequestPath?: (path: string) => string16 }17 18 const DEFAULT_DOCUMENT = 'index.html'19 5 unmodified lines25 return26 }27 const url = new URL(c.req.url)28 29+ 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 linesmodified:src/adapter/cloudflare-workers/serve-static.test.ts+16 added
8 unmodified lines9 '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 }15 const 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 })22 23 Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest })24 Object.assign(global, {7 unmodified lines32 describe('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+ )43 44 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 lines72 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 })76+77+ 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 })84 85 describe('With options', () => {86 const manifest = {65 unmodified linesmodified:src/adapter/cloudflare-workers/serve-static.ts+3 added-2 removed
8 unmodified lines9 root?: string10 path?: string11 manifest?: object | string12 namespace?: KVNamespace13+ rewriteRequestPath?: (path: string) => string14 }15 16 const DEFAULT_DOCUMENT = 'index.html'17 6 unmodified lines24 return25 }26 27 const url = new URL(c.req.url)27-28+ 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 linesmodified:src/adapter/deno/serve-static.ts+3 added-2 removed
4 unmodified lines5 6 export type ServeStaticOptions = {7 root?: string8 path?: string9+ rewriteRequestPath?: (path: string) => string10 }11 12 const DEFAULT_DOCUMENT = 'index.html'13 5 unmodified lines19 return20 }21 22 const url = new URL(c.req.url)22-23+ 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