1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict'
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var Buffer = require('safe-buffer').Buffer
|
17 | var etag = require('etag')
|
18 | var fresh = require('fresh')
|
19 | var fs = require('fs')
|
20 | var ms = require('ms')
|
21 | var parseUrl = require('parseurl')
|
22 | var path = require('path')
|
23 | var resolve = path.resolve
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | module.exports = favicon
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var ONE_YEAR_MS = 60 * 60 * 24 * 365 * 1000
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function favicon (path, options) {
|
49 | var opts = options || {}
|
50 |
|
51 | var icon
|
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 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function 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 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | function 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 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function 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 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | function getPathname (req) {
|
155 | try {
|
156 | return parseUrl(req).pathname
|
157 | } catch (e) {
|
158 | return undefined
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | function 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 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | function 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 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | function send (req, res, icon) {
|
206 |
|
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 |
|
215 | if (isFresh(req, res)) {
|
216 | res.statusCode = 304
|
217 | res.end()
|
218 | return
|
219 | }
|
220 |
|
221 |
|
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 | }
|