Skip to content

Commit

Permalink
lint: use standard style
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 20, 2017
1 parent 0f44452 commit 92e0a99
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 177 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cache:
before_install:
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
Expand All @@ -25,5 +26,6 @@ script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-ci"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cache:
install:
- ps: Install-Product node $env:nodejs_version
- if "%nodejs_version%" equ "0.8" npm rm --save-dev istanbul
- npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard
- if exist node_modules npm prune
- if exist node_modules npm rebuild
- npm install
Expand Down
122 changes: 64 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@
* MIT Licensed
*/

'use strict';
'use strict'

/**
* Module dependencies.
* @private
*/

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

/**
* Module exports.
* @public
*/

module.exports = favicon;
module.exports = favicon

/**
* Module variables.
* @private
*/

var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
var ONE_YEAR_MS = 60 * 60 * 24 * 365 * 1000 // 1 year

/**
* Serves the favicon located by the given `path`.
Expand All @@ -44,45 +44,50 @@ var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
* @return {Function} middleware
*/

function favicon(path, options) {
var opts = options || {};
function favicon (path, options) {
var opts = options || {}

var icon; // favicon cache
var maxAge = calcMaxAge(opts.maxAge);
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)) {
icon = createIcon(copyBuffer(path), maxAge)
} else if (typeof path === 'string') {
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)
})
}
}

/**
* Calculate the max-age from a configured value.
Expand All @@ -92,14 +97,14 @@ function favicon(path, options) {
* @return {number}
*/

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
}

/**
Expand All @@ -124,14 +129,14 @@ function copyBuffer (buf) {
* @return {object}
*/

function createIcon(buf, maxAge) {
function createIcon (buf, maxAge) {
return {
body: buf,
headers: {
'Cache-Control': 'public, max-age=' + Math.floor(maxAge / 1000),
'ETag': etag(buf)
}
};
}
}

/**
Expand All @@ -142,13 +147,13 @@ function createIcon(buf, maxAge) {
* @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;
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
}

/**
Expand Down Expand Up @@ -178,24 +183,25 @@ function resolveSync (iconPath) {
* @param {object} icon
*/

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)
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"parseurl": "~1.3.1"
},
"devDependencies": {
"eslint": "3.15.0",
"eslint-config-standard": "6.2.1",
"eslint-plugin-promise": "3.4.2",
"eslint-plugin-standard": "2.0.1",
"istanbul": "0.4.5",
"mocha": "2.5.3",
"supertest": "1.1.0"
Expand All @@ -30,6 +34,7 @@
"node": ">= 0.8.0"
},
"scripts": {
"lint": "eslint .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/"
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}

0 comments on commit 92e0a99

Please sign in to comment.