fcs
2025-04-10 0163870d662dc5b7d69ceff977490ede594f7d82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
 
/**
 * Module dependencies.
 */
 
const resolve = require('path').resolve;
const fs = require('fs');
 
/**
 * Serve favicon.ico
 *
 * @param {String} path
 * @param {Object} [options]
 * @return {Function}
 * @api public
 */
 
module.exports = function (path, options){
  if (!path) {
    return (ctx, next) => {
      if ('/favicon.ico' != ctx.path) {
        return next();
      }
    };
  }
 
  path = resolve(path);
  options = options || {};
 
  let icon;
  const maxAge = options.maxAge == null
    ? 86400000
    : Math.min(Math.max(0, options.maxAge), 31556926000);
  const cacheControl = `public, max-age=${maxAge / 1000 | 0}`;
 
  return (ctx, next) => {
    if ('/favicon.ico' != ctx.path) {
      return next();
    }
 
    if ('GET' !== ctx.method && 'HEAD' !== ctx.method) {
      ctx.status = 'OPTIONS' == ctx.method ? 200 : 405;
      ctx.set('Allow', 'GET, HEAD, OPTIONS');
    } else {
      // lazily read the icon
      if (!icon) icon = fs.readFileSync(path);
      ctx.set('Cache-Control', cacheControl);
      ctx.type = 'image/x-icon';
      ctx.body = icon;
    }
  };
};