Skip to content

Commit f406dd8

Browse files
committed
chore: server: lint
1 parent 1ff9a52 commit f406dd8

File tree

6 files changed

+62
-62
lines changed

6 files changed

+62
-62
lines changed

server/.madrun.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {run} = require('madrun');
44

55
module.exports = {
66
'start': () => 'STATIC=../out node index.js',
7-
'lint': () => 'putout handlers *.js .madrun.js',
7+
'lint': () => 'putout .',
88
'fix:lint': () => run('lint', '--fix'),
99
};
10-

server/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const process = require('node:process');
4+
35
/*
46
if (!process.env.AUTH_TOKEN) {
57
console.error(
@@ -9,7 +11,6 @@ if (!process.env.AUTH_TOKEN) {
911
process.exit(1);
1012
}
1113
*/
12-
1314
module.exports = {
1415
AUTH_TOKEN: process.env.AUTH_TOKEN,
1516
SETTINGS_FORMAT: 2,

server/handlers/gist/loadGist.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
'use strict';
22

3-
const {AUTH_TOKEN} = require('../../constants');
43
const GitHub = require('github-api');
4+
const {AUTH_TOKEN} = require('../../constants');
55

66
module.exports = function loadGist(req, res, next) {
7-
const gh = new GitHub({token: AUTH_TOKEN});
7+
const gh = new GitHub({
8+
token: AUTH_TOKEN,
9+
});
810
const gist = gh.getGist(req.params.snippetid);
911
const latest = req.params.revisionid === 'latest';
10-
(latest ?
11-
gist.read() :
12-
gist.getRevision(req.params.revisionid)
13-
)
12+
13+
(latest ? gist.read() : gist.getRevision(req.params.revisionid))
1414
.then((response) => res.json(response.data))
1515
.catch(next);
1616
};

server/handlers/gist/saveAnonymousGist.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,43 @@
11
'use strict';
22

3+
const GitHub = require('github-api');
34
const {
45
AUTH_TOKEN,
56
SETTINGS_FORMAT,
67
} = require('../../constants');
7-
const GitHub = require('github-api');
88

9-
const gh = new GitHub({token: AUTH_TOKEN});
9+
const gh = new GitHub({
10+
token: AUTH_TOKEN,
11+
});
1012

1113
/**
1214
* Expects an array of the form [[filename, content], [filename, content], ...]
1315
*/
1416
function makeFiles(files) {
15-
return files.reduce(
16-
(obj, [filename, content]) => {
17-
obj[filename] = content ? {content} : content;
18-
return obj;
19-
},
20-
{},
21-
);
17+
return files.reduce((obj, [filename, content]) => {
18+
obj[filename] = content && {
19+
content,
20+
};
21+
return obj;
22+
}, {});
2223
}
2324

2425
function getDataFromBody(body, additionalData = {}) {
2526
const files = [
26-
[
27-
'astexplorer.json',
28-
JSON.stringify(
29-
{
30-
v: SETTINGS_FORMAT,
31-
parserID: body.parserID,
32-
toolID: body.toolID,
33-
settings: body.settings,
34-
versions: body.versions,
35-
...additionalData,
36-
},
37-
null,
38-
2,
39-
),
40-
],
27+
['astexplorer.json', JSON.stringify({
28+
v: SETTINGS_FORMAT,
29+
parserID: body.parserID,
30+
toolID: body.toolID,
31+
settings: body.settings,
32+
versions: body.versions,
33+
...additionalData,
34+
}, null, 2)],
4135
[body.filename, body.code],
4236
];
4337

4438
// null value indicates deletion
45-
if (body.transform || body.transform === null) {
39+
if (body.transform || body.transform === null)
4640
files.push(['transform.js', body.transform]);
47-
}
4841

4942
return {
5043
files: makeFiles(files),
@@ -53,26 +46,29 @@ function getDataFromBody(body, additionalData = {}) {
5346
};
5447
}
5548

56-
exports.create = (req, res, next) => {
57-
gh.getGist()
49+
module.exports.create = (req, res, next) => {
50+
gh
51+
.getGist()
5852
.create(getDataFromBody(req.body))
5953
.then((response) => res.json(response.data))
6054
.catch(next);
6155
};
6256

63-
exports.update = (req, res, next) => {
64-
gh.getGist(req.params.snippetid)
57+
module.exports.update = (req, res, next) => {
58+
gh
59+
.getGist(req.params.snippetid)
6560
.update(getDataFromBody(req.body))
6661
.then((response) => res.json(response.data))
6762
.catch(next);
6863
};
6964

70-
exports.fork = (req, res, next) => {
65+
module.exports.fork = (req, res, next) => {
7166
// We cannot really "fork" an "anonymous" snippet because a user (astexplorer)
7267
// cannot fork it's own gist.
7368
const data = getDataFromBody(req.body);
7469

75-
gh.getGist()
70+
gh
71+
.getGist()
7672
.create(data)
7773
.then((response) => res.json(response.data))
7874
.catch(next);

server/handlers/parse.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const process = require('node:process');
34
const express = require('express');
45
const snippets = prepareData(require(process.env.SNIPPET_FILE));
56
const snippetRevisions = prepareData(require(process.env.REVISION_FILE));
@@ -9,48 +10,47 @@ function notFound(req, res) {
910
res.sendStatus(404);
1011
}
1112

12-
module.exports = express.Router()
13-
// Load snippet
13+
module.exports = express
14+
.Router()
15+
// Load snippet
1416
.get('/:snippetid/:revisionid', load);
1517

1618
function prepareData(data) {
1719
// Array of objects -> Map index by object ID
1820
const m = new Map();
21+
1922
for (const obj of data)
2023
m.set(obj._id, obj);
24+
2125
return m;
2226
}
2327

2428
function load(req, res) {
2529
const snippet = snippets.get(req.params.snippetid);
2630
let revisionID = req.params.revisionid;
2731

28-
if (!snippet) {
32+
if (!snippet)
2933
return notFound(req, res);
30-
}
3134

32-
if (revisionID === 'latest') {
35+
if (revisionID === 'latest')
3336
revisionID = snippet.revisions.length - 1;
34-
}
3537

36-
if (+revisionID != revisionID || revisionID >= snippet.revisions.length) {
38+
if (revisionID !== +revisionID || revisionID >= snippet.revisions.length)
3739
return notFound(req, res);
38-
}
3940

4041
const revision = snippetRevisions.get(snippet.revisions[revisionID].objectId);
4142

42-
if (!revision) {
43+
if (!revision)
4344
return notFound(req, res);
44-
}
4545

4646
const copy = {
4747
revisionID: +revisionID,
4848
snippetID: snippet._id,
4949
...revision,
5050
};
51-
delete copy._id;
5251

53-
// The data will never change but we don't want to keep it in caches
52+
delete copy._id;
53+
// The data will never change but we don't want to keep it in caches
5454
// unnecessarily
5555
// res.append('Cache-Control', 'max-age=86400, public'); // 24h
5656
res.json(copy);

server/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22

3+
const {join} = require('node:path');
4+
const process = require('node:process');
35
const bodyParser = require('body-parser');
46
const express = require('express');
5-
const path = require('path');
7+
68
const cors = require('cors');
79

810
const app = express();
@@ -20,25 +22,27 @@ if (process.env.SNIPPET_FILE && process.env.REVISION_FILE) {
2022
console.log(process.env.STATIC);
2123

2224
if (process.env.STATIC)
23-
app.use(express.static(path.join(__dirname, process.env.STATIC)));
25+
app.use(express.static(join(__dirname, process.env.STATIC)));
2426

2527
// `next` is needed here to mark this as an error handler
2628
// eslint-disable-next-line no-unused-vars
27-
app.use((err, req, res, next) => {
29+
app.use((err, req, res) => {
2830
console.error(new Date().toLocaleString(), err);
2931

3032
if (err.response) {
31-
res.status(err.response.status).send(err.response.statusText);
33+
res
34+
.status(err.response.status)
35+
.send(err.response.statusText);
3236
return;
3337
}
34-
res.status(500).send('Something went wrong');
38+
39+
res
40+
.status(500)
41+
.send('Something went wrong');
3542
});
3643

37-
const {
38-
PORT = 8080,
39-
} = process.env;
44+
const {PORT = 8080} = process.env;
4045

4146
app.listen(PORT, '0.0.0.0', () => {
4247
console.log(`Server listening on port ${PORT}!`);
4348
});
44-

0 commit comments

Comments
 (0)