filepath.ts
This file
Path
src/utils/filepath.ts
51 lines · 1.2 KiB · TypeScript
Last changed by
fix(serve-static): support extensionless files and refactor (#2260)Yusuke Wadabefore adoptionf8371ab
History of this fileTake it
The walk stopped after 200 commits: lines older than 2f9a07d point at it.
perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 1–16why, one step back1type FilePathOptions = {2 filename: string3 root?: string4 defaultDocument?: string5}67export const getFilePath = (options: FilePathOptions): string | undefined => {8 let filename = options.filename9 const defaultDocument = options.defaultDocument || 'index.html'1011 if (filename.endsWith('/')) {12 // /top/ => /top/index.html13 filename = filename.concat(defaultDocument)14 } else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {15 // /top => /top/index.html16 filename = filename.concat('/' + defaultDocument)fix(serve-static): support extensionless files and refactor (#2260)Yusuke Wadabefore adoptionf8371ab
lines 17–34why, one step back17 }1819 const path = getFilePathWithoutDefaultDocument({20 root: options.root,21 filename,22 })2324 return path25}2627export const getFilePathWithoutDefaultDocument = (28 options: Omit<FilePathOptions, 'defaultDocument'>29) => {30 let root = options.root || ''31 let filename = options.filename3233 if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) {34 returnperf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 35–37why, one step back35 }3637 // /foo.html => foo.htmlfix(utils/filepath): filepath supports Windows (#1642)Yusuke Wadabefore adoption48dc87d
lines 38–41why, one step back38 filename = filename.replace(/^\.?[\/\\]/, '')3940 // foo\bar.txt => foo/bar.txt41 filename = filename.replace(/\\/, '/')perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 42–51why, one step back4243 // assets/ => assets44 root = root.replace(/\/$/, '')4546 // ./assets/foo.html => assets/foo.html47 let path = root ? root + '/' + filename : filename48 path = path.replace(/^\.?\//, '')4950 return path51}