Skip to content
Myr

feat: add `serve-static` middlware for deno (#339)

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

* feat: add `serve-static` middlware for deno * make `getFilePath` into utils * ignore deno ts files

authored by Yusuke Wada · committed by GitHub · parent 6030c05 · Browse files · 15 files+185 added−73 removed
This commit

State

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

Diff stats

15 files+185 added−73 removed

  • tests:+20 added−17 removed
  • other:+165 added−56 removed

15 files+185 added−73 removed

added:deno_dist/deno/serve-static.ts+43 added
added:deno_dist/utils/filepath.ts+31 added
modified:deno_dist/mod.ts+1 added
modified:deno_dist/README.md+2 added-9 removed
761 unmodified lines
762763```tsx764/** @jsx jsx */765import { serve } from 'https://deno.land/std@0.146.0/http/server.ts'766import { Hono, logger, poweredBy, basicAuth, jsx } from 'https://deno.land/x/hono/mod.ts'766import { Hono, logger, poweredBy, serveStatic, jsx } from 'https://deno.land/x/hono/mod.ts'767768const app = new Hono()769770app.use('*', logger(), poweredBy())771app.get(772 '/auth/*',773 basicAuth({774 username: 'deno',775 password: 'iscool',776 })777)771772app.get('/favicon.ico', serveStatic({ path: './public/favicon.ico' }))773app.get('/', (c) => {774 return c.html(<h1>Hello Deno!</h1>)775})782app.get('/auth/abc', (c) => c.text('You are authorized'))776777serve(app.fire())778```779
33 unmodified lines
added:src/deno/serve-static.ts+43 added
modified:src/middleware/mustache/mustache.ts+4 added-3 removed
modified:src/middleware/serve-static/serve-static.ts+3 added-2 removed
1import type { Context, Env } from '../../context'2import type { Handler, Next } from '../../hono'3import { getContentFromKVAsset, getKVFilePath } from '../../utils/cloudflare'3import { getContentFromKVAsset } from '../../utils/cloudflare'4import { getFilePath } from '../../utils/filepath'5import { getMimeType } from '../../utils/mime'67export type ServeStaticOptions = {8 root?: string
13 unmodified lines
22 }2324 const url = new URL(c.req.url)2525 const path = getKVFilePath({26 const path = getFilePath({27 filename: options.path ?? url.pathname,28 root: options.root,29 defaultDocument: DEFAULT_DOCUMENT,30 })
19 unmodified lines
modified:src/utils/cloudflare.test.ts+1 added-17 removed
1import { getContentFromKVAsset, getKVFilePath } from './cloudflare'1import { getContentFromKVAsset } from './cloudflare'23// Mock4const store: { [key: string]: string } = {5 'index.abcdef.html': 'This is index',
23 unmodified lines
29 content = await getContentFromKVAsset('assets/static/plain.txt')30 expect(content).not.toBeUndefined()31 expect(content).toBe('Asset text')32 })3334 it('getKVFilePath', async () => {35 expect(getKVFilePath({ filename: 'foo' })).toBe('foo/index.html')36 expect(getKVFilePath({ filename: 'foo.txt' })).toBe('foo.txt')3738 expect(getKVFilePath({ filename: 'foo', root: 'bar' })).toBe('bar/foo/index.html')39 expect(getKVFilePath({ filename: 'foo.txt', root: 'bar' })).toBe('bar/foo.txt')4041 expect(getKVFilePath({ filename: 'foo', defaultDocument: 'index.txt' })).toBe('foo/index.txt')42 expect(getKVFilePath({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe(43 'bar/foo/index.txt'44 )4546 expect(getKVFilePath({ filename: './foo' })).toBe('foo/index.html')47 expect(getKVFilePath({ filename: 'foo', root: './bar' })).toBe('bar/foo/index.html')48 })33})
modified:src/utils/cloudflare.ts-32 removed
43 unmodified lines
44 content = content as ArrayBuffer45 }46 return content47}4849type FilePathOptions = {50 filename: string51 root?: string52 defaultDocument?: string53}5455export const getKVFilePath = (options: FilePathOptions): string => {56 let filename = options.filename57 let root = options.root || ''58 const defaultDocument = options.defaultDocument || 'index.html'5960 if (filename.endsWith('/')) {61 // /top/ => /top/index.html62 filename = filename.concat(defaultDocument)63 } else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {64 // /top => /top/index.html65 filename = filename.concat('/' + defaultDocument)66 }6768 // /foo.html => foo.html69 filename = filename.replace(/^\.?\//, '')7071 // assets/ => assets72 root = root.replace(/\/$/, '')7374 // ./assets/foo.html => assets/foo.html75 let path = root ? root + '/' + filename : filename76 path = path.replace(/^\.?\//, '')7778 return path79}
added:src/utils/filepath.test.ts+19 added
1import { getFilePath } from './filepath'23describe('getFilePath', () => {4 it('Should return file path correctly', async () => {5 expect(getFilePath({ filename: 'foo' })).toBe('foo/index.html')6 expect(getFilePath({ filename: 'foo.txt' })).toBe('foo.txt')78 expect(getFilePath({ filename: 'foo', root: 'bar' })).toBe('bar/foo/index.html')9 expect(getFilePath({ filename: 'foo.txt', root: 'bar' })).toBe('bar/foo.txt')1011 expect(getFilePath({ filename: 'foo', defaultDocument: 'index.txt' })).toBe('foo/index.txt')12 expect(getFilePath({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe(13 'bar/foo/index.txt'14 )1516 expect(getFilePath({ filename: './foo' })).toBe('foo/index.html')17 expect(getFilePath({ filename: 'foo', root: './bar' })).toBe('bar/foo/index.html')18 })19})
added:src/utils/filepath.ts+31 added
1type FilePathOptions = {2 filename: string3 root?: string4 defaultDocument?: string5}67export const getFilePath = (options: FilePathOptions): string => {8 let filename = options.filename9 let root = options.root || ''10 const defaultDocument = options.defaultDocument || 'index.html'1112 if (filename.endsWith('/')) {13 // /top/ => /top/index.html14 filename = filename.concat(defaultDocument)15 } else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {16 // /top => /top/index.html17 filename = filename.concat('/' + defaultDocument)18 }1920 // /foo.html => foo.html21 filename = filename.replace(/^\.?\//, '')2223 // assets/ => assets24 root = root.replace(/\/$/, '')2526 // ./assets/foo.html => assets/foo.html27 let path = root ? root + '/' + filename : filename28 path = path.replace(/^\.?\//, '')2930 return path31}
modified:src/mod.ts+1 added
48 unmodified lines
49export { jwt } from './middleware/jwt'50export { logger } from './middleware/logger'51export { poweredBy } from './middleware/powered-by'52export { prettyJSON } from './middleware/pretty-json'53export { serveStatic } from './deno/serve-static'
modified:README.md+2 added-9 removed
761 unmodified lines
762763```tsx764/** @jsx jsx */765import { serve } from 'https://deno.land/std@0.146.0/http/server.ts'766import { Hono, logger, poweredBy, basicAuth, jsx } from 'https://deno.land/x/hono/mod.ts'766import { Hono, logger, poweredBy, serveStatic, jsx } from 'https://deno.land/x/hono/mod.ts'767768const app = new Hono()769770app.use('*', logger(), poweredBy())771app.get(772 '/auth/*',773 basicAuth({774 username: 'deno',775 password: 'iscool',776 })777)771772app.get('/favicon.ico', serveStatic({ path: './public/favicon.ico' }))773app.get('/', (c) => {774 return c.html(<h1>Hello Deno!</h1>)775})782app.get('/auth/abc', (c) => c.text('You are authorized'))776777serve(app.fire())778```779
33 unmodified lines
modified:tsconfig.build.esm.json+3 added-1 removed
9 unmodified lines
10 "src/**/*.mts"11 ],12 "exclude": [13 "src/**/*.ts",14 "src/**/*.test.ts"14 "src/**/*.test.ts",15 "src/mod.ts",16 "src/deno/**/*.ts",17 ]18}
modified:tsconfig.build.json+1 added
3 unmodified lines
4 "src/**/*.ts"5 ],6 "exclude": [7 "src/mod.ts",8 "src/deno/**/*.ts",9 "src/**/*.test.ts"10 ]11}