Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.1KB

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