Skip to content
Myr

feat: serve-static middleware supports Module Worker mode (#250)

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 9cfba57 · Browse files · 11 files+254 added−160 removed
This commit

State

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

Diff stats

11 files+254 added−160 removed

  • tests:+117 added−100 removed
  • other:+137 added−60 removed

11 files+254 added−160 removed

deleted:src/middleware/serve-static/index.test.ts-100 removed
1import type { Context } from '../../context'2import type { Next } from '../../hono'3import { Hono } from '../../hono'4import { serveStatic } from '.'56// Mock7const store: { [key: string]: string } = {8 'assets/static/plain.abcdef.txt': 'This is plain.txt',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}13const manifest = JSON.stringify({14 'assets/static/plain.txt': 'assets/static/plain.abcdef.txt',15 'assets/static/hono.html': 'assets/static/hono.abcdef.html',16 'assets/static/top/index.html': 'assets/static/top/index.abcdef.html',17 'static-no-root/plain.txt': 'static-no-root/plain.abcdef.txt',18})1920Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest })21Object.assign(global, {22 __STATIC_CONTENT: {23 get: (path: string) => {24 return store[path]25 },26 },27})2829describe('ServeStatic Middleware', () => {30 const app = new Hono()31 app.use('/static/*', serveStatic({ root: './assets' }))32 app.use('/static-no-root/*', serveStatic())3334 it('Should return plain.txt', async () => {35 const res = await app.request('http://localhost/static/plain.txt')36 expect(res.status).toBe(200)37 expect(await res.text()).toBe('This is plain.txt')38 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')39 })4041 it('Should return hono.html', async () => {42 const res = await app.request('http://localhost/static/hono.html')43 expect(res.status).toBe(200)44 expect(await res.text()).toBe('<h1>Hono!</h1>')45 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')46 })4748 it('Should return 404 response', async () => {49 const res = await app.request('http://localhost/static/not-found.html')50 expect(res.status).toBe(404)51 })5253 it('Should return plan.txt', async () => {54 const res = await app.request('http://localhost/static-no-root/plain.txt')55 expect(res.status).toBe(200)56 expect(await res.text()).toBe('That is plain.txt')57 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')58 })5960 it('Should return index.html', async () => {61 const res = await app.request('http://localhost/static/top')62 expect(res.status).toBe(200)63 expect(await res.text()).toBe('<h1>Top</h1>')64 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')65 })66})6768describe('With middleware', () => {69 const app = new Hono()70 const md1 = async (c: Context, next: Next) => {71 await next()72 c.res.headers.append('x-foo', 'bar')73 }74 const md2 = async (c: Context, next: Next) => {75 await next()76 c.res.headers.append('x-foo2', 'bar2')77 }7879 app.use('/static/*', md1)80 app.use('/static/*', serveStatic({ root: './assets' }))81 app.use('/static/*', md2)82 app.get('/static/foo', (c) => {83 return c.text('bar')84 })8586 it('Should return plain.txt with correct headers', async () => {87 const res = await app.request('http://localhost/static/plain.txt')88 expect(res.status).toBe(200)89 expect(await res.text()).toBe('This is plain.txt')90 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')91 expect(res.headers.get('x-foo')).toBe('bar')92 expect(res.headers.get('x-foo2')).toBe('bar2')93 })9495 it('Should return 200 Response', async () => {96 const res = await app.request('http://localhost/static/foo')97 expect(res.status).toBe(200)98 expect(await res.text()).toBe('bar')99 })100})
modified:src/middleware/serve-static/index.ts+1 added-41 removed
added:src/middleware/serve-static/module.mts+15 added
modified:src/middleware/serve-static/README.md+15 added-3 removed
added:src/middleware/serve-static/serve-static.test.ts+117 added
1import type { Context } from '../../context'2import type { Next } from '../../hono'3import { Hono } from '../../hono'4import { serveStatic } from '.'56// Mock7const store: Record<string, string> = {8 'assets/static/plain.abcdef.txt': 'This is plain.txt',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}14const manifest = JSON.stringify({15 'assets/static/plain.txt': 'assets/static/plain.abcdef.txt',16 'assets/static/hono.html': 'assets/static/hono.abcdef.html',17 'assets/static/top/index.html': 'assets/static/top/index.abcdef.html',18 'static-no-root/plain.txt': 'static-no-root/plain.abcdef.txt',19})2021Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest })22Object.assign(global, {23 __STATIC_CONTENT: {24 get: (path: string) => {25 return store[path]26 },27 },28})2930describe('ServeStatic Middleware', () => {31 const app = new Hono()32 app.use('/static/*', serveStatic({ root: './assets' }))33 app.use('/static-no-root/*', serveStatic())3435 it('Should return plain.txt', async () => {36 const res = await app.request('http://localhost/static/plain.txt')37 expect(res.status).toBe(200)38 expect(await res.text()).toBe('This is plain.txt')39 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')40 })4142 it('Should return hono.html', async () => {43 const res = await app.request('http://localhost/static/hono.html')44 expect(res.status).toBe(200)45 expect(await res.text()).toBe('<h1>Hono!</h1>')46 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')47 })4849 it('Should return 404 response', async () => {50 const res = await app.request('http://localhost/static/not-found.html')51 expect(res.status).toBe(404)52 })5354 it('Should return plan.txt', async () => {55 const res = await app.request('http://localhost/static-no-root/plain.txt')56 expect(res.status).toBe(200)57 expect(await res.text()).toBe('That is plain.txt')58 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')59 })6061 it('Should return index.html', async () => {62 const res = await app.request('http://localhost/static/top')63 expect(res.status).toBe(200)64 expect(await res.text()).toBe('<h1>Top</h1>')65 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')66 })67})6869describe('With options', () => {70 const manifest = {71 'assets/static/options/foo.txt': 'assets/static/options/foo.abcdef.txt',72 }7374 const app = new Hono()75 app.use('/static/*', serveStatic({ root: './assets', manifest: manifest }))7677 it('Should return foo.txt', async () => {78 const res = await app.request('http://localhost/static/options/foo.txt')79 expect(res.status).toBe(200)80 expect(await res.text()).toBe('With options')81 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')82 })83})8485describe('With middleware', () => {86 const app = new Hono()87 const md1 = async (c: Context, next: Next) => {88 await next()89 c.res.headers.append('x-foo', 'bar')90 }91 const md2 = async (c: Context, next: Next) => {92 await next()93 c.res.headers.append('x-foo2', 'bar2')94 }9596 app.use('/static/*', md1)97 app.use('/static/*', serveStatic({ root: './assets' }))98 app.use('/static/*', md2)99 app.get('/static/foo', (c) => {100 return c.text('bar')101 })102103 it('Should return plain.txt with correct headers', async () => {104 const res = await app.request('http://localhost/static/plain.txt')105 expect(res.status).toBe(200)106 expect(await res.text()).toBe('This is plain.txt')107 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')108 expect(res.headers.get('x-foo')).toBe('bar')109 expect(res.headers.get('x-foo2')).toBe('bar2')110 })111112 it('Should return 200 Response', async () => {113 const res = await app.request('http://localhost/static/foo')114 expect(res.status).toBe(200)115 expect(await res.text()).toBe('bar')116 })117})
added:src/middleware/serve-static/serve-static.ts+46 added
modified:src/utils/cloudflare.ts+35 added-12 removed
modified:package.json+6 added-2 removed
9 unmodified lines
10 "scripts": {11 "test": "jest",12 "lint": "eslint --ext js,ts src .eslintrc.js",13 "lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",14 "build": "rimraf dist && tsc --project tsconfig.build.json",14 "build": "rimraf dist && tsc --project tsconfig.build.json && tsc --project tsconfig.build.esm.json",15 "watch": "tsc --project tsconfig.build.json -w",16 "prepublishOnly": "yarn build"17 },18 "exports": {
9 unmodified lines
28 "./mustache": "./dist/middleware/mustache/index.js",29 "./powered-by": "./dist/middleware/powered-by/index.js",30 "./pretty-json": "./dist/middleware/pretty-json/index.js",31 "./serve-static": "./dist/middleware/serve-static/index.js",32 "./serve-static.module": "./dist/middleware/serve-static/module.mjs",33 "./router/trie-router": "./dist/router/trie-router/index.js",34 "./router/reg-exp-router": "./dist/router/reg-exp-router/index.js",35 "./utils/jwt": "./dist/utils/jwt/index.js",36 "./utils/*": "./dist/utils/*.js"
33 unmodified lines
70 "pretty-json": [71 "./dist/middleware/pretty-json"72 ],73 "serve-static": [73 "./dist/middleware/serve-static"74 "./dist/middleware/serve-static/index.d.ts"75 ],76 "serve-static.module": [77 "./dist/middleware/serve-static/module.d.ts"78 ],79 "router/trie-router": [80 "./dist/router/trie-router/router.d.ts"81 ],82 "router/reg-exp-router": [
61 unmodified lines
added:tsconfig.build.esm.json+16 added
modified:tsconfig.build.json+1 added-1 removed
modified:tsconfig.json+2 added-1 removed
1 unmodified line
2 "compilerOptions": {3 "target": "es2017",4 "module": "commonjs",5 "declaration": true,6 "moduleResolution": "Node",7 "outDir": "./dist",8 "esModuleInterop": true,9 "forceConsistentCasingInFileNames": true,10 "strict": true,
6 unmodified lines
17 "@cloudflare/workers-types"18 ],19 },20 "include": [20 "src/**/*",21 "src/**/*.ts",22 "src/**/*.test.ts"23 ],24}