| 88 | */ |
| 89 | |
| 90 | export function favicon(path: string | Buffer, options?: FaviconOptions) { |
| 91 | let icon: FaviconBody // favicon cache |
| 92 | const maxAge = calcMaxAge(options?.maxAge) |
| 93 | |
| 94 | if (Buffer.isBuffer(path)) icon = createIcon(Buffer.from(path), maxAge) |
| 95 | else if (typeof path === 'string') path = resolveSync(path) |
| 96 | |
| 97 | return function favicon(req: Request, res: Response, next?: (err?: any) => void) { |
| 98 | if (getPathname(req.url) !== '/favicon.ico') return next?.() |
| 99 | |
| 100 | if (req.method !== 'GET' && req.method !== 'HEAD') { |
| 101 | res.statusCode = req.method === 'OPTIONS' ? 200 : 405 |
| 102 | res.setHeader('Allow', 'GET, HEAD, OPTIONS') |
| 103 | res.setHeader('Content-Length', '0') |
| 104 | |
| 105 | return res.end() |
| 106 | } |
| 107 | |
| 108 | if (icon) return send(req, res, icon) |
| 109 | |
| 110 | send(req, res, (icon = createIcon(readFileSync(path), maxAge))) |
| 111 | } |
| 112 | } |