Skip to content

Commit 8e6d24b

Browse files
ctcpipwesleytodd
andcommitted
✨ move to neostandard / eslint-config-express
Co-authored-by: Wes Todd <[email protected]>
1 parent 1c317e0 commit 8e6d24b

File tree

8 files changed

+428
-443
lines changed

8 files changed

+428
-443
lines changed

.eslintignore

-2
This file was deleted.

.eslintrc.yml

-9
This file was deleted.

eslint.config.cjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const expressLintConfig = require('eslint-config-express');
2+
3+
module.exports = [
4+
...expressLintConfig,
5+
];

index.js

+63-62
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
* MIT Licensed
77
*/
88

9-
'use strict'
9+
'use strict';
1010

1111
/**
1212
* Module dependencies.
1313
* @private
1414
*/
1515

16-
var encodeUrl = require('encodeurl')
17-
var escapeHtml = require('escape-html')
18-
var parseUrl = require('parseurl')
19-
var resolve = require('path').resolve
20-
var send = require('send')
21-
var url = require('url')
16+
const encodeUrl = require('encodeurl');
17+
const escapeHtml = require('escape-html');
18+
const parseUrl = require('parseurl');
19+
const resolve = require('path').resolve;
20+
const send = require('send');
21+
const url = require('url');
2222

2323
/**
2424
* Module exports.
2525
* @public
2626
*/
2727

28-
module.exports = serveStatic
28+
module.exports = serveStatic;
2929

3030
/**
3131
* @param {string} root
@@ -36,109 +36,110 @@ module.exports = serveStatic
3636

3737
function serveStatic (root, options) {
3838
if (!root) {
39-
throw new TypeError('root path required')
39+
throw new TypeError('root path required');
4040
}
4141

4242
if (typeof root !== 'string') {
43-
throw new TypeError('root path must be a string')
43+
throw new TypeError('root path must be a string');
4444
}
4545

4646
// copy options object
47-
var opts = Object.create(options || null)
47+
const opts = Object.create(options || null);
4848

4949
// fall-though
50-
var fallthrough = opts.fallthrough !== false
50+
const fallthrough = opts.fallthrough !== false;
5151

5252
// default redirect
53-
var redirect = opts.redirect !== false
53+
const redirect = opts.redirect !== false;
5454

5555
// headers listener
56-
var setHeaders = opts.setHeaders
56+
const setHeaders = opts.setHeaders;
5757

5858
if (setHeaders && typeof setHeaders !== 'function') {
59-
throw new TypeError('option setHeaders must be function')
59+
throw new TypeError('option setHeaders must be function');
6060
}
6161

6262
// setup options for send
63-
opts.maxage = opts.maxage || opts.maxAge || 0
64-
opts.root = resolve(root)
63+
opts.maxage = opts.maxage || opts.maxAge || 0;
64+
opts.root = resolve(root);
6565

6666
// construct directory listener
67-
var onDirectory = redirect
67+
const onDirectory = redirect
6868
? createRedirectDirectoryListener()
69-
: createNotFoundDirectoryListener()
69+
: createNotFoundDirectoryListener();
7070

7171
return function serveStatic (req, res, next) {
7272
if (req.method !== 'GET' && req.method !== 'HEAD') {
7373
if (fallthrough) {
74-
return next()
74+
return next();
7575
}
7676

7777
// method not allowed
78-
res.statusCode = 405
79-
res.setHeader('Allow', 'GET, HEAD')
80-
res.setHeader('Content-Length', '0')
81-
res.end()
82-
return
78+
res.statusCode = 405;
79+
res.setHeader('Allow', 'GET, HEAD');
80+
res.setHeader('Content-Length', '0');
81+
res.end();
82+
return;
8383
}
8484

85-
var forwardError = !fallthrough
86-
var originalUrl = parseUrl.original(req)
87-
var path = parseUrl(req).pathname
85+
let forwardError = !fallthrough;
86+
const originalUrl = parseUrl.original(req);
87+
let path = parseUrl(req).pathname;
8888

8989
// make sure redirect occurs at mount
9090
if (path === '/' && originalUrl.pathname.substr(-1) !== '/') {
91-
path = ''
91+
path = '';
9292
}
9393

9494
// create send stream
95-
var stream = send(req, path, opts)
95+
const stream = send(req, path, opts);
9696

9797
// add directory handler
98-
stream.on('directory', onDirectory)
98+
stream.on('directory', onDirectory);
9999

100100
// add headers listener
101101
if (setHeaders) {
102-
stream.on('headers', setHeaders)
102+
stream.on('headers', setHeaders);
103103
}
104104

105105
// add file listener for fallthrough
106106
if (fallthrough) {
107107
stream.on('file', function onFile () {
108108
// once file is determined, always forward error
109-
forwardError = true
110-
})
109+
forwardError = true;
110+
});
111111
}
112112

113113
// forward errors
114114
stream.on('error', function error (err) {
115115
if (forwardError || !(err.statusCode < 500)) {
116-
next(err)
117-
return
116+
next(err);
117+
return;
118118
}
119119

120-
next()
121-
})
120+
next();
121+
});
122122

123123
// pipe
124-
stream.pipe(res)
125-
}
124+
stream.pipe(res);
125+
};
126126
}
127127

128128
/**
129129
* Collapse all leading slashes into a single slash
130130
* @private
131131
*/
132132
function collapseLeadingSlashes (str) {
133-
for (var i = 0; i < str.length; i++) {
133+
let i;
134+
for (i = 0; i < str.length; i++) {
134135
if (str.charCodeAt(i) !== 0x2f /* / */) {
135-
break
136+
break;
136137
}
137138
}
138139

139140
return i > 1
140141
? '/' + str.substr(i)
141-
: str
142+
: str;
142143
}
143144

144145
/**
@@ -159,7 +160,7 @@ function createHtmlDocument (title, body) {
159160
'<body>\n' +
160161
'<pre>' + body + '</pre>\n' +
161162
'</body>\n' +
162-
'</html>\n'
163+
'</html>\n';
163164
}
164165

165166
/**
@@ -169,8 +170,8 @@ function createHtmlDocument (title, body) {
169170

170171
function createNotFoundDirectoryListener () {
171172
return function notFound () {
172-
this.error(404)
173-
}
173+
this.error(404);
174+
};
174175
}
175176

176177
/**
@@ -181,29 +182,29 @@ function createNotFoundDirectoryListener () {
181182
function createRedirectDirectoryListener () {
182183
return function redirect (res) {
183184
if (this.hasTrailingSlash()) {
184-
this.error(404)
185-
return
185+
this.error(404);
186+
return;
186187
}
187188

188189
// get original URL
189-
var originalUrl = parseUrl.original(this.req)
190+
const originalUrl = parseUrl.original(this.req);
190191

191192
// append trailing slash
192-
originalUrl.path = null
193-
originalUrl.pathname = collapseLeadingSlashes(originalUrl.pathname + '/')
193+
originalUrl.path = null;
194+
originalUrl.pathname = collapseLeadingSlashes(originalUrl.pathname + '/');
194195

195196
// reformat the URL
196-
var loc = encodeUrl(url.format(originalUrl))
197-
var doc = createHtmlDocument('Redirecting', 'Redirecting to <a href="' + escapeHtml(loc) + '">' +
198-
escapeHtml(loc) + '</a>')
197+
const loc = encodeUrl(url.format(originalUrl));
198+
const doc = createHtmlDocument('Redirecting', 'Redirecting to <a href="' + escapeHtml(loc) + '">' +
199+
escapeHtml(loc) + '</a>');
199200

200201
// send redirect response
201-
res.statusCode = 301
202-
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
203-
res.setHeader('Content-Length', Buffer.byteLength(doc))
204-
res.setHeader('Content-Security-Policy', "default-src 'none'")
205-
res.setHeader('X-Content-Type-Options', 'nosniff')
206-
res.setHeader('Location', loc)
207-
res.end(doc)
208-
}
202+
res.statusCode = 301;
203+
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
204+
res.setHeader('Content-Length', Buffer.byteLength(doc));
205+
res.setHeader('Content-Security-Policy', "default-src 'none'");
206+
res.setHeader('X-Content-Type-Options', 'nosniff');
207+
res.setHeader('Location', loc);
208+
res.end(doc);
209+
};
209210
}

package.json

+2-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@
1212
"send": "^1.0.0"
1313
},
1414
"devDependencies": {
15-
"eslint": "7.32.0",
16-
"eslint-config-standard": "14.1.1",
17-
"eslint-plugin-import": "2.25.4",
18-
"eslint-plugin-markdown": "2.2.1",
19-
"eslint-plugin-node": "11.1.0",
20-
"eslint-plugin-promise": "5.2.0",
21-
"eslint-plugin-standard": "4.1.0",
15+
"eslint-config-express": "^0.1.0",
2216
"mocha": "^10.7.0",
2317
"nyc": "^17.0.0",
2418
"safe-buffer": "^5.2.1",
@@ -34,6 +28,7 @@
3428
},
3529
"scripts": {
3630
"lint": "eslint .",
31+
"lint:fix": "eslint . --fix",
3732
"test": "mocha --reporter spec --bail --check-leaks test/",
3833
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
3934
"test-cov": "nyc --reporter=html --reporter=text npm test",

scripts/version-history.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
'use strict'
1+
'use strict';
22

3-
var fs = require('fs')
4-
var path = require('path')
3+
const fs = require('fs');
4+
const path = require('path');
55

6-
var HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md')
7-
var MD_HEADER_REGEXP = /^====*$/
8-
var VERSION = process.env.npm_package_version
9-
var VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/
6+
const HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md');
7+
const MD_HEADER_REGEXP = /^====*$/;
8+
const VERSION = process.env.npm_package_version;
9+
const VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/;
1010

11-
var historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n')
11+
const historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n');
1212

1313
if (!MD_HEADER_REGEXP.test(historyFileLines[1])) {
14-
console.error('Missing header in HISTORY.md')
15-
process.exit(1)
14+
console.error('Missing header in HISTORY.md');
15+
process.exit(1);
1616
}
1717

1818
if (!VERSION_PLACEHOLDER_REGEXP.test(historyFileLines[0])) {
19-
console.error('Missing placegolder version in HISTORY.md')
20-
process.exit(1)
19+
console.error('Missing placegolder version in HISTORY.md');
20+
process.exit(1);
2121
}
2222

2323
if (historyFileLines[0].indexOf('x') !== -1) {
24-
var versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$')
24+
const versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$');
2525

2626
if (!versionCheckRegExp.test(VERSION)) {
27-
console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0])
28-
process.exit(1)
27+
console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0]);
28+
process.exit(1);
2929
}
3030
}
3131

32-
historyFileLines[0] = VERSION + ' / ' + getLocaleDate()
33-
historyFileLines[1] = repeat('=', historyFileLines[0].length)
32+
historyFileLines[0] = VERSION + ' / ' + getLocaleDate();
33+
historyFileLines[1] = repeat('=', historyFileLines[0].length);
3434

35-
fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n'))
35+
fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n'));
3636

3737
function getLocaleDate () {
38-
var now = new Date()
38+
const now = new Date();
3939

4040
return zeroPad(now.getFullYear(), 4) + '-' +
4141
zeroPad(now.getMonth() + 1, 2) + '-' +
42-
zeroPad(now.getDate(), 2)
42+
zeroPad(now.getDate(), 2);
4343
}
4444

4545
function repeat (str, length) {
46-
var out = ''
46+
let out = '';
4747

48-
for (var i = 0; i < length; i++) {
49-
out += str
48+
for (let i = 0; i < length; i++) {
49+
out += str;
5050
}
5151

52-
return out
52+
return out;
5353
}
5454

5555
function zeroPad (number, length) {
56-
var num = number.toString()
56+
let num = number.toString();
5757

5858
while (num.length < length) {
59-
num = '0' + num
59+
num = '0' + num;
6060
}
6161

62-
return num
62+
return num;
6363
}

test/.eslintrc

-5
This file was deleted.

0 commit comments

Comments
 (0)