serve-favicon
Advanced tools
Comparing version 2.3.2 to 2.4.0
@@ -0,1 +1,15 @@ | ||
2.4.0 / 2017-02-19 | ||
================== | ||
* deps: etag@~1.8.0 | ||
- Use SHA1 instead of MD5 for ETag hashing | ||
- Works with FIPS 140-2 OpenSSL configuration | ||
* deps: fresh@0.4.0 | ||
- Fix false detection of `no-cache` request directive | ||
- perf: enable strict mode | ||
- perf: hoist regular expressions | ||
- perf: remove duplicate conditional | ||
- perf: remove unnecessary boolean coercions | ||
* perf: simplify initial argument checking | ||
2.3.2 / 2016-11-16 | ||
@@ -2,0 +16,0 @@ ================== |
164
index.js
@@ -9,3 +9,3 @@ /*! | ||
'use strict'; | ||
'use strict' | ||
@@ -17,9 +17,9 @@ /** | ||
var etag = require('etag'); | ||
var fresh = require('fresh'); | ||
var fs = require('fs'); | ||
var ms = require('ms'); | ||
var parseUrl = require('parseurl'); | ||
var path = require('path'); | ||
var resolve = path.resolve; | ||
var etag = require('etag') | ||
var fresh = require('fresh') | ||
var fs = require('fs') | ||
var ms = require('ms') | ||
var parseUrl = require('parseurl') | ||
var path = require('path') | ||
var resolve = path.resolve | ||
@@ -31,3 +31,3 @@ /** | ||
module.exports = favicon; | ||
module.exports = favicon | ||
@@ -39,3 +39,3 @@ /** | ||
var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year | ||
var ONE_YEAR_MS = 60 * 60 * 24 * 365 * 1000 // 1 year | ||
@@ -51,48 +51,46 @@ /** | ||
function favicon(path, options) { | ||
var opts = options || {}; | ||
function favicon (path, options) { | ||
var opts = options || {} | ||
var buf; | ||
var icon; // favicon cache | ||
var maxAge = calcMaxAge(opts.maxAge); | ||
var stat; | ||
var icon // favicon cache | ||
var maxAge = calcMaxAge(opts.maxAge) | ||
if (!path) throw new TypeError('path to favicon.ico is required'); | ||
if (!path) { | ||
throw new TypeError('path to favicon.ico is required') | ||
} | ||
if (Buffer.isBuffer(path)) { | ||
buf = new Buffer(path.length); | ||
path.copy(buf); | ||
icon = createIcon(buf, maxAge); | ||
icon = createIcon(copyBuffer(path), maxAge) | ||
} else if (typeof path === 'string') { | ||
path = resolve(path); | ||
stat = fs.statSync(path); | ||
if (stat.isDirectory()) throw createIsDirError(path); | ||
path = resolveSync(path) | ||
} else { | ||
throw new TypeError('path to favicon.ico must be string or buffer'); | ||
throw new TypeError('path to favicon.ico must be string or buffer') | ||
} | ||
return function favicon(req, res, next){ | ||
return function favicon (req, res, next) { | ||
if (parseUrl(req).pathname !== '/favicon.ico') { | ||
next(); | ||
return; | ||
next() | ||
return | ||
} | ||
if (req.method !== 'GET' && req.method !== 'HEAD') { | ||
res.statusCode = req.method === 'OPTIONS' ? 200 : 405; | ||
res.setHeader('Allow', 'GET, HEAD, OPTIONS'); | ||
res.setHeader('Content-Length', '0'); | ||
res.end(); | ||
return; | ||
res.statusCode = req.method === 'OPTIONS' ? 200 : 405 | ||
res.setHeader('Allow', 'GET, HEAD, OPTIONS') | ||
res.setHeader('Content-Length', '0') | ||
res.end() | ||
return | ||
} | ||
if (icon) return send(req, res, icon); | ||
if (icon) { | ||
send(req, res, icon) | ||
return | ||
} | ||
fs.readFile(path, function(err, buf){ | ||
if (err) return next(err); | ||
icon = createIcon(buf, maxAge); | ||
send(req, res, icon); | ||
}); | ||
}; | ||
}; | ||
fs.readFile(path, function (err, buf) { | ||
if (err) return next(err) | ||
icon = createIcon(buf, maxAge) | ||
send(req, res, icon) | ||
}) | ||
} | ||
} | ||
@@ -107,13 +105,26 @@ /** | ||
function calcMaxAge(val) { | ||
function calcMaxAge (val) { | ||
var num = typeof val === 'string' | ||
? ms(val) | ||
: val; | ||
: val | ||
return num != null | ||
? Math.min(Math.max(0, num), maxMaxAge) | ||
: maxMaxAge | ||
? Math.min(Math.max(0, num), ONE_YEAR_MS) | ||
: ONE_YEAR_MS | ||
} | ||
/** | ||
* Copy a given Buffer. | ||
* | ||
* @param {Buffer} buf | ||
* @private | ||
*/ | ||
function copyBuffer (buf) { | ||
var copy = new Buffer(buf.length) | ||
buf.copy(copy) | ||
return copy | ||
} | ||
/** | ||
* Create icon data from Buffer and max-age. | ||
@@ -127,3 +138,3 @@ * | ||
function createIcon(buf, maxAge) { | ||
function createIcon (buf, maxAge) { | ||
return { | ||
@@ -135,3 +146,3 @@ body: buf, | ||
} | ||
}; | ||
} | ||
} | ||
@@ -147,12 +158,30 @@ | ||
function createIsDirError(path) { | ||
var error = new Error('EISDIR, illegal operation on directory \'' + path + '\''); | ||
error.code = 'EISDIR'; | ||
error.errno = 28; | ||
error.path = path; | ||
error.syscall = 'open'; | ||
return error; | ||
function createIsDirError (path) { | ||
var error = new Error('EISDIR, illegal operation on directory \'' + path + '\'') | ||
error.code = 'EISDIR' | ||
error.errno = 28 | ||
error.path = path | ||
error.syscall = 'open' | ||
return error | ||
} | ||
/** | ||
* Resolve the path to icon. | ||
* | ||
* @param {string} iconPath | ||
* @private | ||
*/ | ||
function resolveSync (iconPath) { | ||
var path = resolve(iconPath) | ||
var stat = fs.statSync(path) | ||
if (stat.isDirectory()) { | ||
throw createIsDirError(path) | ||
} | ||
return path | ||
} | ||
/** | ||
* Send icon data in response to a request. | ||
@@ -166,22 +195,23 @@ * | ||
function send(req, res, icon) { | ||
var headers = icon.headers; | ||
function send (req, res, icon) { | ||
// Set headers | ||
var keys = Object.keys(headers); | ||
var headers = icon.headers | ||
var keys = Object.keys(headers) | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
res.setHeader(key, headers[key]); | ||
var key = keys[i] | ||
res.setHeader(key, headers[key]) | ||
} | ||
// Validate freshness | ||
if (fresh(req.headers, res._headers)) { | ||
res.statusCode = 304; | ||
res.end(); | ||
return; | ||
res.statusCode = 304 | ||
res.end() | ||
return | ||
} | ||
res.statusCode = 200; | ||
res.setHeader('Content-Length', icon.body.length); | ||
res.setHeader('Content-Type', 'image/x-icon'); | ||
res.end(icon.body); | ||
// Send icon | ||
res.statusCode = 200 | ||
res.setHeader('Content-Length', icon.body.length) | ||
res.setHeader('Content-Type', 'image/x-icon') | ||
res.end(icon.body) | ||
} |
{ | ||
"name": "serve-favicon", | ||
"description": "favicon serving middleware with caching", | ||
"version": "2.3.2", | ||
"version": "2.4.0", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
@@ -14,4 +14,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"etag": "~1.7.0", | ||
"fresh": "0.3.0", | ||
"etag": "~1.8.0", | ||
"fresh": "0.4.0", | ||
"ms": "0.7.2", | ||
@@ -21,5 +21,9 @@ "parseurl": "~1.3.1" | ||
"devDependencies": { | ||
"eslint": "3.15.0", | ||
"eslint-config-standard": "6.2.1", | ||
"eslint-plugin-markdown": "1.0.0-beta.3", | ||
"eslint-plugin-promise": "3.4.2", | ||
"eslint-plugin-standard": "2.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "2.5.3", | ||
"proxyquire": "1.4.0", | ||
"supertest": "1.1.0" | ||
@@ -36,2 +40,3 @@ }, | ||
"scripts": { | ||
"lint": "eslint --plugin markdown --ext js,md .", | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
@@ -38,0 +43,0 @@ "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/", |
@@ -68,11 +68,12 @@ # serve-favicon | ||
```javascript | ||
var express = require('express'); | ||
var favicon = require('serve-favicon'); | ||
var express = require('express') | ||
var favicon = require('serve-favicon') | ||
var path = require('path') | ||
var app = express(); | ||
app.use(favicon(__dirname + '/public/favicon.ico')); | ||
var app = express() | ||
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))) | ||
// Add your routes here, etc. | ||
app.listen(3000); | ||
app.listen(3000) | ||
``` | ||
@@ -83,11 +84,12 @@ | ||
```javascript | ||
var connect = require('connect'); | ||
var favicon = require('serve-favicon'); | ||
var connect = require('connect') | ||
var favicon = require('serve-favicon') | ||
var path = require('path') | ||
var app = connect(); | ||
app.use(favicon(__dirname + '/public/favicon.ico')); | ||
var app = connect() | ||
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))) | ||
// Add your middleware here, etc. | ||
app.listen(3000); | ||
app.listen(3000) | ||
``` | ||
@@ -101,22 +103,23 @@ | ||
```javascript | ||
var http = require('http'); | ||
var favicon = require('serve-favicon'); | ||
var finalhandler = require('finalhandler'); | ||
var http = require('http') | ||
var favicon = require('serve-favicon') | ||
var finalhandler = require('finalhandler') | ||
var path = require('path') | ||
var _favicon = favicon(__dirname + '/public/favicon.ico'); | ||
var _favicon = favicon(path.join(__dirname, 'public', 'favicon.ico')) | ||
var server = http.createServer(function onRequest(req, res) { | ||
var done = finalhandler(req, res); | ||
var server = http.createServer(function onRequest (req, res) { | ||
var done = finalhandler(req, res) | ||
_favicon(req, res, function onNext(err) { | ||
if (err) return done(err); | ||
_favicon(req, res, function onNext (err) { | ||
if (err) return done(err) | ||
// continue to process the request here, etc. | ||
res.statusCode = 404; | ||
res.end('oops'); | ||
}); | ||
}); | ||
res.statusCode = 404 | ||
res.end('oops') | ||
}) | ||
}) | ||
server.listen(3000); | ||
server.listen(3000) | ||
``` | ||
@@ -123,0 +126,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13368
174
140
8
+ Addedetag@1.8.1(transitive)
+ Addedfresh@0.4.0(transitive)
- Removedetag@1.7.0(transitive)
- Removedfresh@0.3.0(transitive)
Updatedetag@~1.8.0
Updatedfresh@0.4.0