Browse Source

tidy

master
Alan Faubert 2 years ago
parent
commit
09af1f6a7d
  1. 10
      .eslintrc.yaml
  2. 3
      .prettierrc.yaml
  3. 46
      sfs.js

10
.eslintrc.yaml

@ -1,10 +0,0 @@
env:
es6: true
node: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 2018
sourceType: module
rules:
no-console: off
prefer-const: error

3
.prettierrc.yaml

@ -1,3 +0,0 @@
useTabs: true
singleQuote: true
trailingComma: all

46
sfs.js

@ -9,11 +9,8 @@ const util = require('util');
const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
const sendError = (res, code, message, headers) => {
res.writeHead(code, {
'content-type': 'text/html; charset=utf-8',
...headers,
});
const send_error = (res, code, message) => {
res.writeHead(code, { 'content-type': 'text/html; charset=utf-8' });
res.end(`<!doctype html>
<html>
<head>
@ -26,7 +23,7 @@ const sendError = (res, code, message, headers) => {
};
const prefixes = ' KMGT';
const humanizeSize = size => {
const humanize_size = size => {
let t = 0;
while (size >= 1024) {
size /= 1024;
@ -47,14 +44,14 @@ http
.createServer(async (req, res) => {
const path = '.' + decodeURIComponent(req.url);
if (!path.startsWith('./') || path.includes('/..') || path.includes('\\')) {
sendError(res, 403, 'Forbidden');
send_error(res, 403, 'Forbidden');
return;
}
let stats;
try {
stats = await stat(path);
} catch (e) {
sendError(res, 404, 'Not Found');
send_error(res, 404, 'Not Found');
return;
}
if (stats.isFile()) {
@ -85,7 +82,7 @@ http
});
}
fs.createReadStream(path, range)
.on('error', () => sendError(res, 500, 'Internal Server Error'))
.on('error', () => send_error(res, 500, 'Internal Server Error'))
.pipe(res);
return;
}
@ -95,15 +92,11 @@ http
res.end();
return;
}
const dir = (await Promise.all(
(await readdir(path)).map(async file => [
file,
await stat(path + file),
file.replace(/\d+/g, _ => '1'.repeat(_.length - 1) + '0' + _),
]),
)).sort(
(a, b) => +a[1].isFile() - +b[1].isFile() || a[2].localeCompare(b[2]),
);
const dir = (await Promise.all((await readdir(path)).map(async file => [
file,
await stat(path + file),
file.replace(/\d+/g, _ => '1'.repeat(_.length - 1) + '0' + _),
]))).sort((a, b) => +a[1].isFile() - +b[1].isFile() || a[2].localeCompare(b[2]));
if (path !== './') {
dir.unshift(['..', { isFile: () => false, isDirectory: () => true }]);
}
@ -115,21 +108,18 @@ http
</head>
<body>
<h1>${req.headers.host + decodeURIComponent(req.url)}</h1>
<ul>${dir
.map(
([file, stats]) => `
<li><a href="${encodeURIComponent(file) +
(stats.isDirectory() ? '/' : '')}">${file.replace(/&/g, '&amp;')}</a>${
stats.isFile() ? ` (${humanizeSize(stats.size)})` : ''
}</li>`,
)
.join('')}
<ul>${dir.map(([file, stats]) => `
<li>
<a href="${encodeURIComponent(file)}${stats.isDirectory() ? '/' : ''}">
${file.replace(/&/g, '&amp;')}
</a>${stats.isFile() ? ` (${humanize_size(stats.size)})` : ''}
</li>`).join('')}
</ul>
</body>
</html>`);
return;
}
sendError(res, 500, 'Internal Server Error');
send_error(res, 500, 'Internal Server Error');
})
.listen(port, host);

Loading…
Cancel
Save