diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 62562b7..0000000 --- a/.eslintignore +++ /dev/null @@ -1,2 +0,0 @@ -coverage -node_modules diff --git a/.eslintrc.yml b/.eslintrc.yml deleted file mode 100644 index cf3015f..0000000 --- a/.eslintrc.yml +++ /dev/null @@ -1,9 +0,0 @@ -root: true -extends: - - standard - - plugin:markdown/recommended -plugins: - - markdown -overrides: - - files: '**/*.md' - processor: 'markdown/markdown' diff --git a/index.js b/index.js index 1c1bb46..b10d858 100644 --- a/index.js +++ b/index.js @@ -6,26 +6,26 @@ * MIT Licensed */ -'use strict' +'use strict'; /** * Module dependencies. * @private */ -var encodeUrl = require('encodeurl') -var escapeHtml = require('escape-html') -var parseUrl = require('parseurl') -var resolve = require('path').resolve -var send = require('send') -var url = require('url') +const encodeUrl = require('encodeurl'); +const escapeHtml = require('escape-html'); +const parseUrl = require('parseurl'); +const resolve = require('path').resolve; +const send = require('send'); +const url = require('url'); /** * Module exports. * @public */ -module.exports = serveStatic +module.exports = serveStatic; /** * @param {string} root @@ -36,93 +36,93 @@ module.exports = serveStatic function serveStatic (root, options) { if (!root) { - throw new TypeError('root path required') + throw new TypeError('root path required'); } if (typeof root !== 'string') { - throw new TypeError('root path must be a string') + throw new TypeError('root path must be a string'); } // copy options object - var opts = Object.create(options || null) + const opts = Object.create(options || null); // fall-though - var fallthrough = opts.fallthrough !== false + const fallthrough = opts.fallthrough !== false; // default redirect - var redirect = opts.redirect !== false + const redirect = opts.redirect !== false; // headers listener - var setHeaders = opts.setHeaders + const setHeaders = opts.setHeaders; if (setHeaders && typeof setHeaders !== 'function') { - throw new TypeError('option setHeaders must be function') + throw new TypeError('option setHeaders must be function'); } // setup options for send - opts.maxage = opts.maxage || opts.maxAge || 0 - opts.root = resolve(root) + opts.maxage = opts.maxage || opts.maxAge || 0; + opts.root = resolve(root); // construct directory listener - var onDirectory = redirect + const onDirectory = redirect ? createRedirectDirectoryListener() - : createNotFoundDirectoryListener() + : createNotFoundDirectoryListener(); return function serveStatic (req, res, next) { if (req.method !== 'GET' && req.method !== 'HEAD') { if (fallthrough) { - return next() + return next(); } // method not allowed - res.statusCode = 405 - res.setHeader('Allow', 'GET, HEAD') - res.setHeader('Content-Length', '0') - res.end() - return + res.statusCode = 405; + res.setHeader('Allow', 'GET, HEAD'); + res.setHeader('Content-Length', '0'); + res.end(); + return; } - var forwardError = !fallthrough - var originalUrl = parseUrl.original(req) - var path = parseUrl(req).pathname + let forwardError = !fallthrough; + const originalUrl = parseUrl.original(req); + let path = parseUrl(req).pathname; // make sure redirect occurs at mount if (path === '/' && originalUrl.pathname.substr(-1) !== '/') { - path = '' + path = ''; } // create send stream - var stream = send(req, path, opts) + const stream = send(req, path, opts); // add directory handler - stream.on('directory', onDirectory) + stream.on('directory', onDirectory); // add headers listener if (setHeaders) { - stream.on('headers', setHeaders) + stream.on('headers', setHeaders); } // add file listener for fallthrough if (fallthrough) { stream.on('file', function onFile () { // once file is determined, always forward error - forwardError = true - }) + forwardError = true; + }); } // forward errors stream.on('error', function error (err) { if (forwardError || !(err.statusCode < 500)) { - next(err) - return + next(err); + return; } - next() - }) + next(); + }); // pipe - stream.pipe(res) - } + stream.pipe(res); + }; } /** @@ -130,15 +130,16 @@ function serveStatic (root, options) { * @private */ function collapseLeadingSlashes (str) { - for (var i = 0; i < str.length; i++) { + let i; + for (i = 0; i < str.length; i++) { if (str.charCodeAt(i) !== 0x2f /* / */) { - break + break; } } return i > 1 ? '/' + str.substr(i) - : str + : str; } /** @@ -159,7 +160,7 @@ function createHtmlDocument (title, body) { '
\n' + '' + body + '\n' + '\n' + - '