UNPKG

4.16 kBJavaScriptView Raw
1/*!
2 * serve-favicon
3 * Copyright(c) 2010 Sencha Inc.
4 * Copyright(c) 2011 TJ Holowaychuk
5 * Copyright(c) 2014-2017 Douglas Christopher Wilson
6 * MIT Licensed
7 */
8
9'use strict'
10
11/**
12 * Module dependencies.
13 * @private
14 */
15
16var Buffer = require('safe-buffer').Buffer
17var etag = require('etag')
18var fresh = require('fresh')
19var fs = require('fs')
20var ms = require('ms')
21var parseUrl = require('parseurl')
22var path = require('path')
23var resolve = path.resolve
24
25/**
26 * Module exports.
27 * @public
28 */
29
30module.exports = favicon
31
32/**
33 * Module variables.
34 * @private
35 */
36
37var ONE_YEAR_MS = 60 * 60 * 24 * 365 * 1000 // 1 year
38
39/**
40 * Serves the favicon located by the given `path`.
41 *
42 * @public
43 * @param {String|Buffer} path
44 * @param {Object} [options]
45 * @return {Function} middleware
46 */
47
48function favicon (path, options) {
49 var opts = options || {}
50
51 var icon // favicon cache
52 var maxAge = calcMaxAge(opts.maxAge)
53
54 if (!path) {
55 throw new TypeError('path to favicon.ico is required')
56 }
57
58 if (Buffer.isBuffer(path)) {
59 icon = createIcon(Buffer.from(path), maxAge)
60 } else if (typeof path === 'string') {
61 path = resolveSync(path)
62 } else {
63 throw new TypeError('path to favicon.ico must be string or buffer')
64 }
65
66 return function favicon (req, res, next) {
67 if (getPathname(req) !== '/favicon.ico') {
68 next()
69 return
70 }
71
72 if (req.method !== 'GET' && req.method !== 'HEAD') {
73 res.statusCode = req.method === 'OPTIONS' ? 200 : 405
74 res.setHeader('Allow', 'GET, HEAD, OPTIONS')
75 res.setHeader('Content-Length', '0')
76 res.end()
77 return
78 }
79
80 if (icon) {
81 send(req, res, icon)
82 return
83 }
84
85 fs.readFile(path, function (err, buf) {
86 if (err) return next(err)
87 icon = createIcon(buf, maxAge)
88 send(req, res, icon)
89 })
90 }
91}
92
93/**
94 * Calculate the max-age from a configured value.
95 *
96 * @private
97 * @param {string|number} val
98 * @return {number}
99 */
100
101function calcMaxAge (val) {
102 var num = typeof val === 'string'
103 ? ms(val)
104 : val
105
106 return num != null
107 ? Math.min(Math.max(0, num), ONE_YEAR_MS)
108 : ONE_YEAR_MS
109}
110
111/**
112 * Create icon data from Buffer and max-age.
113 *
114 * @private
115 * @param {Buffer} buf
116 * @param {number} maxAge
117 * @return {object}
118 */
119
120function createIcon (buf, maxAge) {
121 return {
122 body: buf,
123 headers: {
124 'Cache-Control': 'public, max-age=' + Math.floor(maxAge / 1000),
125 'ETag': etag(buf)
126 }
127 }
128}
129
130/**
131 * Create EISDIR error.
132 *
133 * @private
134 * @param {string} path
135 * @return {Error}
136 */
137
138function createIsDirError (path) {
139 var error = new Error('EISDIR, illegal operation on directory \'' + path + '\'')
140 error.code = 'EISDIR'
141 error.errno = 28
142 error.path = path
143 error.syscall = 'open'
144 return error
145}
146
147/**
148 * Get the request pathname.
149 *
150 * @param {object} req
151 * @return {string}
152 */
153
154function getPathname (req) {
155 try {
156 return parseUrl(req).pathname
157 } catch (e) {
158 return undefined
159 }
160}
161
162/**
163 * Determine if the cached representation is fresh.
164 *
165 * @param {object} req
166 * @param {object} res
167 * @return {boolean}
168 * @private
169 */
170
171function isFresh (req, res) {
172 return fresh(req.headers, {
173 'etag': res.getHeader('ETag'),
174 'last-modified': res.getHeader('Last-Modified')
175 })
176}
177
178/**
179 * Resolve the path to icon.
180 *
181 * @param {string} iconPath
182 * @private
183 */
184
185function resolveSync (iconPath) {
186 var path = resolve(iconPath)
187 var stat = fs.statSync(path)
188
189 if (stat.isDirectory()) {
190 throw createIsDirError(path)
191 }
192
193 return path
194}
195
196/**
197 * Send icon data in response to a request.
198 *
199 * @private
200 * @param {IncomingMessage} req
201 * @param {OutgoingMessage} res
202 * @param {object} icon
203 */
204
205function send (req, res, icon) {
206 // Set headers
207 var headers = icon.headers
208 var keys = Object.keys(headers)
209 for (var i = 0; i < keys.length; i++) {
210 var key = keys[i]
211 res.setHeader(key, headers[key])
212 }
213
214 // Validate freshness
215 if (isFresh(req, res)) {
216 res.statusCode = 304
217 res.end()
218 return
219 }
220
221 // Send icon
222 res.statusCode = 200
223 res.setHeader('Content-Length', icon.body.length)
224 res.setHeader('Content-Type', 'image/x-icon')
225 res.end(icon.body)
226}