MCPcopy
hub / github.com/tinyhttp/tinyhttp / favicon

Function favicon

packages/favicon/src/index.ts:90–112  ·  view source on GitHub ↗
(path: string | Buffer, options?: FaviconOptions)

Source from the content-addressed store, hash-verified

88 */
89
90export 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}

Callers 2

createServerFunction · 0.90
favicon.test.tsFile · 0.90

Calls 5

getPathnameFunction · 0.90
calcMaxAgeFunction · 0.85
createIconFunction · 0.85
resolveSyncFunction · 0.85
sendFunction · 0.70

Tested by 1

createServerFunction · 0.72