Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / serve-favicon / index.js
1 /*!
2  * serve-favicon
3  * Copyright(c) 2010 Sencha Inc.
4  * Copyright(c) 2011 TJ Holowaychuk
5  * Copyright(c) 2014 Douglas Christopher Wilson
6  * MIT Licensed
7  */
8
9 /**
10  * Module dependencies.
11  * @private
12  */
13
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;
21
22 /**
23  * Module variables.
24  * @private
25  */
26
27 var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
28
29 /**
30  * Serves the favicon located by the given `path`.
31  *
32  * @public
33  * @param {String|Buffer} path
34  * @param {Object} options
35  * @return {Function} middleware
36  */
37
38 module.exports = function favicon(path, options){
39   options = options || {};
40
41   var buf;
42   var icon; // favicon cache
43   var maxAge = calcMaxAge(options.maxAge);
44   var stat;
45
46   if (!path) throw new TypeError('path to favicon.ico is required');
47
48   if (Buffer.isBuffer(path)) {
49     buf = new Buffer(path.length);
50     path.copy(buf);
51
52     icon = createIcon(buf, maxAge);
53   } else if (typeof path === 'string') {
54     path = resolve(path);
55     stat = fs.statSync(path);
56     if (stat.isDirectory()) throw createIsDirError(path);
57   } else {
58     throw new TypeError('path to favicon.ico must be string or buffer');
59   }
60
61   return function favicon(req, res, next){
62     if (parseUrl(req).pathname !== '/favicon.ico') {
63       next();
64       return;
65     }
66
67     if ('GET' !== req.method && 'HEAD' !== req.method) {
68       var status = 'OPTIONS' === req.method ? 200 : 405;
69       res.writeHead(status, {'Allow': 'GET, HEAD, OPTIONS'});
70       res.end();
71       return;
72     }
73
74     if (icon) return send(req, res, icon);
75
76     fs.readFile(path, function(err, buf){
77       if (err) return next(err);
78       icon = createIcon(buf, maxAge);
79       send(req, res, icon);
80     });
81   };
82 };
83
84 /**
85  * Calculate the max-age from a configured value.
86  *
87  * @private
88  * @param {string|number} val
89  * @return {number}
90  */
91
92 function calcMaxAge(val) {
93   var num = typeof val === 'string'
94     ? ms(val)
95     : val;
96
97   return num != null
98     ? Math.min(Math.max(0, num), maxMaxAge)
99     : maxMaxAge
100 }
101
102 /**
103  * Create icon data from Buffer and max-age.
104  *
105  * @private
106  * @param {Buffer} buf
107  * @param {number} maxAge
108  * @return {object}
109  */
110
111 function createIcon(buf, maxAge) {
112   return {
113     body: buf,
114     headers: {
115       'Cache-Control': 'public, max-age=' + ~~(maxAge / 1000),
116       'ETag': etag(buf)
117     }
118   };
119 }
120
121 /**
122  * Create EISDIR error.
123  *
124  * @private
125  * @param {string} path
126  * @return {Error}
127  */
128
129 function createIsDirError(path) {
130   var error = new Error('EISDIR, illegal operation on directory \'' + path + '\'');
131   error.code = 'EISDIR';
132   error.errno = 28;
133   error.path = path;
134   error.syscall = 'open';
135   return error;
136 }
137
138 /**
139  * Send icon data in response to a request.
140  *
141  * @private
142  * @param {IncomingMessage} req
143  * @param {OutgoingMessage} res
144  * @param {object} icon
145  */
146
147 function send(req, res, icon) {
148   var headers = icon.headers;
149
150   // Set headers
151   var keys = Object.keys(headers);
152   for (var i = 0; i < keys.length; i++) {
153     var key = keys[i];
154     res.setHeader(key, headers[key]);
155   }
156
157   if (fresh(req.headers, res._headers)) {
158     res.statusCode = 304;
159     res.end();
160     return;
161   }
162
163   res.statusCode = 200;
164   res.setHeader('Content-Length', icon.body.length);
165   res.setHeader('Content-Type', 'image/x-icon');
166   res.end(icon.body);
167 }