Skip to content

Commit c26d272

Browse files
committed
abstracted some code
1 parent 9a1b9d1 commit c26d272

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

server/index.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,37 @@ wsServer = new ws({
2727
httpServer: server
2828
});
2929

30+
function getShellFilePath(moduleName) {
31+
return __dirname + '/modules/shell_files/' + moduleName + '.sh';
32+
}
33+
34+
function shellPathAndModuleNameAreValid(shellFilePath, moduleName) {
35+
36+
var moduleInvalidName = moduleName.indexOf('.') > -1;
37+
var moduleNameEmpty = !moduleName;
38+
var moduleNotFound = !fs.existsSync(shellFilePath);
39+
var isValid = true;
40+
41+
if (moduleInvalidName || moduleNameEmpty || moduleNotFound) {
42+
isValid = false;
43+
}
44+
45+
return isValid;
46+
}
47+
3048
wsServer.on('request', function(request) {
3149

3250
var wsClient = request.accept('linux-dash', request.origin);
3351

3452
wsClient.on('message', function(wsReq) {
3553

36-
var moduleName = wsReq.utf8Data;
37-
var shellFile = __dirname + '/modules/shell_files/' + moduleName + '.sh';
38-
var moduleInvalidName = moduleName.indexOf('.') > -1;
39-
var moduleNameEmpty = !moduleName;
40-
var moduleNotFound = !fs.existsSync(shellFile);
54+
var moduleName = wsReq.utf8Data;
55+
var shellFile = getShellFilePath(moduleName);
4156

42-
if (moduleInvalidName || moduleNameEmpty || moduleNotFound) {
57+
if (!shellPathAndModuleNameAreValid(shellFile, moduleName)) {
4358
res.sendStatus(406);
44-
return;
45-
}
59+
return;
60+
}
4661

4762
var command = spawn(shellFile, [ wsReq.color || '' ]);
4863
var output = [];
@@ -71,15 +86,12 @@ wsServer.on('request', function(request) {
7186

7287
app.get('/server/', function (req, res) {
7388

74-
var shellFile = __dirname + '/modules/shell_files/' + req.query.module + '.sh';
89+
var shellFile = getShellFilePath(req.query.module);
7590

76-
if (req.query.module.indexOf('.') > -1
77-
|| !req.query.module
78-
|| !fs.existsSync(shellFile))
79-
{
80-
res.sendStatus(406);
81-
return;
82-
}
91+
if (!shellPathAndModuleNameAreValid(shellFile, req.query.module)) {
92+
res.sendStatus(406);
93+
return;
94+
}
8395

8496
var command = spawn(shellFile, [ req.query.color || '' ]);
8597
var output = [];

0 commit comments

Comments
 (0)