3 * Copyright(c) 2010 Sencha Inc.
4 * Copyright(c) 2011 TJ Holowaychuk
5 * Copyright(c) 2014 Douglas Christopher Wilson
10 * Module dependencies.
14 var etag = require('etag');
15 var fresh = require('fresh');
16 var fs = require('fs');
17 var ms = require('ms');
18 var parseUrl = require('parseurl');
19 var path = require('path');
20 var resolve = path.resolve;
27 var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
30 * Serves the favicon located by the given `path`.
33 * @param {String|Buffer} path
34 * @param {Object} options
35 * @return {Function} middleware
38 module.exports = function favicon(path, options){
39 options = options || {};
42 var icon; // favicon cache
43 var maxAge = calcMaxAge(options.maxAge);
46 if (!path) throw new TypeError('path to favicon.ico is required');
48 if (Buffer.isBuffer(path)) {
49 buf = new Buffer(path.length);
52 icon = createIcon(buf, maxAge);
53 } else if (typeof path === 'string') {
55 stat = fs.statSync(path);
56 if (stat.isDirectory()) throw createIsDirError(path);
58 throw new TypeError('path to favicon.ico must be string or buffer');
61 return function favicon(req, res, next){
62 if (parseUrl(req).pathname !== '/favicon.ico') {
67 if ('GET' !== req.method && 'HEAD' !== req.method) {
68 var status = 'OPTIONS' === req.method ? 200 : 405;
69 res.writeHead(status, {'Allow': 'GET, HEAD, OPTIONS'});
74 if (icon) return send(req, res, icon);
76 fs.readFile(path, function(err, buf){
77 if (err) return next(err);
78 icon = createIcon(buf, maxAge);
85 * Calculate the max-age from a configured value.
88 * @param {string|number} val
92 function calcMaxAge(val) {
93 var num = typeof val === 'string'
98 ? Math.min(Math.max(0, num), maxMaxAge)
103 * Create icon data from Buffer and max-age.
106 * @param {Buffer} buf
107 * @param {number} maxAge
111 function createIcon(buf, maxAge) {
115 'Cache-Control': 'public, max-age=' + ~~(maxAge / 1000),
122 * Create EISDIR error.
125 * @param {string} path
129 function createIsDirError(path) {
130 var error = new Error('EISDIR, illegal operation on directory \'' + path + '\'');
131 error.code = 'EISDIR';
134 error.syscall = 'open';
139 * Send icon data in response to a request.
142 * @param {IncomingMessage} req
143 * @param {OutgoingMessage} res
144 * @param {object} icon
147 function send(req, res, icon) {
148 var headers = icon.headers;
151 var keys = Object.keys(headers);
152 for (var i = 0; i < keys.length; i++) {
154 res.setHeader(key, headers[key]);
157 if (fresh(req.headers, res._headers)) {
158 res.statusCode = 304;
163 res.statusCode = 200;
164 res.setHeader('Content-Length', icon.body.length);
165 res.setHeader('Content-Type', 'image/x-icon');